Core 1.0
Triangle Class Reference

Base minimum storage triangle class. More...

#include <triangle.h>

Public Member Functions

 Triangle ()
 Empty.
 Triangle (const Vector &, const Vector &, const Vector &)
 Create a triangle.
 ~Triangle ()
 Empty.
Vector operator[] (int) const
 Return the i-th vertex.
Vector & operator[] (int)
 Return a reference to the i-th vertex.
Vector Vertex (const double &, const double &) const
 Compute a point in the triangle, given uv-coordinates.
bool Intersect (const Ray &, double &) const
 Compute the intersection between a ray and a triangle.
bool Intersect (const Ray &, double &, double &, double &) const
 Compute the intersection between a ray and a triangle.
bool Intersect (const Sphere &) const
 Check if a sphere intersects or contains a triangle.
bool Intersect (const Triangle &) const
 Check if two triangles intersect.
void Translate (const Vector &)
 Translates a triangle by a given vector.
void Rotate (const Matrix &)
 Rotates a triangle.
void Scale (const Vector &)
 Scale a triangle.
void Transform (const Frame &)
 Transforms a triangle given a frame transformation.
Triangle Transformed (const FrameScaled &) const
 Transforms a triangle given a frame transformation.
void Shrink (const double &)
 Shrinks a triangle.
Vector Normal () const
 Compute the normal vector of the triangle.
Vector AreaNormal () const
 Compute the normal vector of the triangle, and scale the normal using its area.
Vector Center () const
 Compute the barycenter of the triangle.
double R (const Vector &) const
 Computes the squared distance between a point and a triangle.
Vector Normal (const Vector &) const
 Computes the normal vector to a triangle.
Sphere Inscribed () const
 Compute the sphere inscribed in the triangle.
Sphere Circumscribed () const
 Compute the sphere circumscribing the triangle.
Plane GetPlane () const
 Compute the plane of the triangle.
double Area () const
 Compute the area of the triangle.
double Length () const
 Compute the perimeter of the triangle.
double Aspect () const
 Computes the aspect ratio of the triangle.
Box GetBox () const
 Computes the axis aligned box enclosing the triangle.
int Intersect (const Plane &, Segment &) const
 Compute the intersection between a triangle and a plane.
int Intersect (const double &, Segment &) const
 Compute the intersection between a triangle and a horizontal plane.
Vector BarycentricCoordinates (const Vector &) const
 Compute the barycentric coordinates of the projection of the point onto the plane of the triangle.
void Subdivide (int, QVector< Vector > &, QVector< int > &) const
 Create an n-adic subdivision of a triangle.
double InscribedRadius () const
 Compute the radius of the circle inscribed in the triangle.
double CircumscribedRadius () const
 Compute the radius of the circumscribed circle of the triangle.
Vector RandomInside (Random &=Random::R239) const
 Generate a random vector inside the triangle.

Protected Member Functions

Vector2 SignedHeight (const Vector &) const
 Compute the signed distance to the triangle in its plane, and the distance to the plane.

Protected Attributes

Vector p [3]
 Array of vertices.

Static Protected Attributes

static constexpr const double epsilon = 1.0e-7
 Internal epsilon constant.

Friends

std::ostream & operator<< (std::ostream &s, const Triangle &t)
 Overloaded.

Detailed Description

Base minimum storage triangle class.

The data-structure of the triangle does not include the normalized normal vector.

Constructor & Destructor Documentation

◆ Triangle()

Triangle::Triangle ( const Vector & a,
const Vector & b,
const Vector & c )
inlineexplicit

Create a triangle.

Parameters
a,b,cVertices of the triangle.

Member Function Documentation

◆ AreaNormal()

Vector Triangle::AreaNormal ( ) const

Compute the normal vector of the triangle, and scale the normal using its area.

This function is less expensive than Triangle::Normal() as it does not require normalizing the cross product of the edge vectors.

It is useful for computing the vertex normals of a triangle mesh by averaging the face normals of the triangles and weighting the influence of the different triangles with their area.

See also
Triangle::Normal()

◆ Aspect()

double Triangle::Aspect ( ) const

Computes the aspect ratio of the triangle.

It is defined as the radius of the inscribed circle divided by the radius of the circumscribing circle. This function is faster than:

double a=2.0*t.InscribedRadius()/t.CircumscribedRadius();
double InscribedRadius() const
Compute the radius of the circle inscribed in the triangle.
Definition triangle.cpp:259
Triangle()
Empty.
Definition triangle.h:24
double CircumscribedRadius() const
Compute the radius of the circumscribed circle of the triangle.
Definition triangle.cpp:272

◆ BarycentricCoordinates()

Vector Triangle::BarycentricCoordinates ( const Vector & p) const

Compute the barycentric coordinates of the projection of the point onto the plane of the triangle.

Parameters
pPoint.
See also
Triangle2::BarycentricCoordinates

◆ Circumscribed()

Sphere Triangle::Circumscribed ( ) const

Compute the sphere circumscribing the triangle.

This function directly relies on the constructor of the class Sphere.

See also
Sphere::Sphere(const Vector&, const Vector&, const Vector&);

◆ Inscribed()

Sphere Triangle::Inscribed ( ) const

Compute the sphere inscribed in the triangle.

The center of the sphere is the center of (A,a), (B,b) and (C,c) where A, B and C are the vertices of the triangle and coefficients a, b and c represent the length of their facing edge.

◆ InscribedRadius()

double Triangle::InscribedRadius ( ) const

Compute the radius of the circle inscribed in the triangle.

It is equal to the area divided by the half perimeter.

◆ Intersect() [1/6]

int Triangle::Intersect ( const double & z,
Segment & segment ) const

Compute the intersection between a triangle and a horizontal plane.

Parameters
zHeight of horizontal plane.
segmentReturned segment if intersection occurs.
Returns
The number of intersections, denoted as n. If n=1 then only one vertex of the triangle is one the plane, whereas if n=3 then the triangle is on the plane.
See also
int Triangle::Intersect(const double&,Segment&)

◆ Intersect() [2/6]

int Triangle::Intersect ( const Plane & plane,
Segment & segment ) const

Compute the intersection between a triangle and a plane.

Parameters
planeThe plane.
segmentReturned segment if intersection occurs.
Returns
The number of intersections, denoted as n. If n=1 then only one vertex of the triangle is one the plane, whereas if n=3 then the triangle is on the plane.
See also
int Triangle::Intersect(const double&,Segment&)

◆ Intersect() [3/6]

bool Triangle::Intersect ( const Ray & ray,
double & t ) const

Compute the intersection between a ray and a triangle.

Uses the Möller-Trumbore algorithm, after Tomas Möller and Ben Trumbore, Fast, minimum storage ray-triangle intersection, Journal of graphics tools, 2(1):21-28, 1997.

Parameters
rayThe ray (direction should be of unit length).
tIntersection depth, intersections behind the origin of the ray (negative depth) are rejected.

◆ Intersect() [4/6]

bool Triangle::Intersect ( const Ray & ray,
double & t,
double & u,
double & v ) const

Compute the intersection between a ray and a triangle.

Return the barycentric coordinates of the intersection if within the triangle.

Uses the Möller-Trumbore algorithm, after Tomas Möller and Ben Trumbore, Fast, minimum storage ray-triangle intersection, Journal of graphics tools, 2(1):21-28, 1997.

Parameters
rayThe ray (direction should be of unit length).
tIntersection depth, intersections behind the origin of the ray (negative depth) are rejected.
u,vParametric coordinates of the intersection depth in the triangle.

◆ Intersect() [5/6]

bool Triangle::Intersect ( const Sphere & sphere) const

Check if a sphere intersects or contains a triangle.

It does not attempt to compute the set of intersection.

A triangle vertex lying exactly on the sphere is considered inside, whereas a triangle whose vertices are all outside the sphere intersects it only if its distance to the center is strictly smaller than the radius. The algorithm proceeds as follows.
1. All three triangle vertices are contained in the sphere. The sphere completely contains the triangle.
2. At least one vertex is inside the sphere and at least one vertex is outside the sphere. The sphere and triangle intersect.
3. All three vertices are outside the sphere. The sphere and triangle intersect when the distance from sphere center to triangle is less than the sphere radius.

Parameters
sphereThe sphere.

◆ Intersect() [6/6]

bool Triangle::Intersect ( const Triangle & other) const

Check if two triangles intersect.

The triangles are considered as closed sets: they intersect if they have at least one point in common, including a vertex or an edge. The function handles all the cases: triangles crossing in three dimensional space, triangles touching each other, coplanar triangles whose edges intersect, and coplanar triangles such that one is inside the other one. Vertices closer to the plane of the other triangle than a small fraction of the size of the triangles are considered as lying in the plane, and triangles which are closer to each other than this tolerance are considered as touching, so that the result is robust for contacts.

Triangles of null area are not supported: the function returns false if one of the triangles is degenerate.

After Tomas Moller, A Fast Triangle-Triangle Intersection Test, Journal of Graphics Tools, 2(2):25-30, 1997.

Parameters
otherThe other triangle.
Returns
True if the triangles intersect.

◆ Normal() [1/2]

Vector Triangle::Normal ( ) const

Compute the normal vector of the triangle.

This function is expensive as it requires normalizing the cross product of the edge vectors.

See also
TriangleEdge

◆ Normal() [2/2]

Vector Triangle::Normal ( const Vector & q) const

Computes the normal vector to a triangle.

Parameters
qPoint.

◆ operator[]() [1/2]

Vector & Triangle::operator[] ( int i)
inline

Return a reference to the i-th vertex.

Parameters
iIndex.

◆ operator[]() [2/2]

Vector Triangle::operator[] ( int i) const
inline

Return the i-th vertex.

Parameters
iIndex.

◆ R()

double Triangle::R ( const Vector & q) const

Computes the squared distance between a point and a triangle.

Parameters
qPoint.

◆ RandomInside()

Vector Triangle::RandomInside ( Random & random = Random::R239) const

Generate a random vector inside the triangle.

After R. Osada, T. Funkhouser, B. Chazelle, D. Dobkin. Shape Distributions, ACM Transactions on Graphics, 21(4), 807-832, 2002.

Parameters
randomRandom number generator.

◆ Rotate()

void Triangle::Rotate ( const Matrix & r)

Rotates a triangle.

Parameters
rRotation matrix.

◆ Scale()

void Triangle::Scale ( const Vector & u)

Scale a triangle.

Parameters
uScaling vector.

◆ Shrink()

void Triangle::Shrink ( const double & e)

Shrinks a triangle.

This function erodes a triangle by moving its edges by a given distance.

Parameters
eErosion radius.

◆ SignedHeight()

Vector2 Triangle::SignedHeight ( const Vector & q) const
protected

Compute the signed distance to the triangle in its plane, and the distance to the plane.

Parameters
qPoint.
Returns
A vector, containing the signed distance to the contour in the plane of the triangle, and the signed distance to its plane.

◆ Subdivide()

void Triangle::Subdivide ( int n,
QVector< Vector > & vertex,
QVector< int > & index ) const

Create an n-adic subdivision of a triangle.

Parameters
nSubdivision level.
vertexArray of points.
indexArray of indexes defining the triangles.

◆ Transform()

void Triangle::Transform ( const Frame & t)

Transforms a triangle given a frame transformation.

Parameters
tTransformation.

◆ Transformed()

Triangle Triangle::Transformed ( const FrameScaled & t) const

Transforms a triangle given a frame transformation.

Parameters
tTransformation.

◆ Translate()

void Triangle::Translate ( const Vector & u)

Translates a triangle by a given vector.

Parameters
uTranslation vector.

◆ Vertex()

Vector Triangle::Vertex ( const double & u,
const double & v ) const

Compute a point in the triangle, given uv-coordinates.

Parameters
u,vCoordinates.

◆ operator<<

std::ostream & operator<< ( std::ostream & s,
const Triangle & t )
friend

Overloaded.

Parameters
tTriangle.
sStream.