Core 1.0
Public Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
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. More...
 
 ~Triangle ()
 Empty.
 
Vector operator[] (int) const
 Return the i-th vertex. More...
 
Vector Vertex (const double &, const double &) const
 Compute a point in the triangle, given uv-coordinates. More...
 
bool Intersect (const Ray &, double &) const
 Compute the intersection between a ray and a triangle. More...
 
bool Intersect (const Ray &, double &, double &, double &) const
 Compute the intersection between a ray and a triangle. More...
 
bool Intersect (const Sphere &) const
 Check if a sphere intersects or contains a triangle. More...
 
void Translate (const Vector &)
 Translates a triangle by a given vector. More...
 
void Rotate (const Matrix &)
 Rotates a triangle. More...
 
void Scale (const Vector &)
 Scale a triangle. More...
 
void Transform (const Frame &)
 Transforms a triangle given a frame transformation. More...
 
Triangle Transformed (const FrameScaled &) const
 Transforms a triangle given a frame transformation. More...
 
void Shrink (const double &)
 Shrinks a triangle. More...
 
Vector Normal () const
 Compute the normal vector of the triangle. More...
 
Vector AreaNormal () const
 Compute the normal vector of the triangle, and scale the normal using its area. More...
 
Vector Center () const
 Compute the barycenter of the triangle.
 
double R (const Vector &) const
 Computes the squared distance between a point and a triangle. More...
 
Vector Normal (const Vector &) const
 Computes the normal vector to a triangle. More...
 
Sphere Inscribed () const
 Compute the sphere inscribed in the triangle. More...
 
Sphere Circumscribed () const
 Compute the sphere circumscribing the triangle. More...
 
double Area () const
 Compute the area of the triangle.
 
double Aspect () const
 Computes the aspect ratio of the triangle. More...
 
Box GetBox () const
 Computes the axis aligned box enclosing the triangle.
 
int Intersect (const Triangle &, int &, Vector &, Vector &) const
 Check if two triangles intersect. More...
 
int Intersect (const Plane &, Segment &) const
 Compute the intersection between a triangle and a plane. More...
 
int Intersect (const double &, Segment &) const
 Compute the intersection between a triangle and a horizontal plane. More...
 
Vector BarycentricCoordinates (const Vector &) const
 Compute the barycentric coordinates of an argument vector with respect to the triangle. More...
 
void Subdivide (int, QVector< Vector > &, QVector< int > &) const
 Create an n-adic subdivision of a triangle. More...
 
double InscribedRadius () const
 Compute the radius of the circle inscribed in the triangle. More...
 
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. More...
 

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 &, const Triangle &)
 Overloaded. More...
 

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.

IIt 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();
Base minimum storage triangle class.
Definition: triangle.h:17
double InscribedRadius() const
Compute the radius of the circle inscribed in the triangle.
Definition: triangle.cpp:258
double CircumscribedRadius() const
Compute the radius of the circumscribed circle of the triangle.
Definition: triangle.cpp:271

◆ BarycentricCoordinates()

Vector Triangle::BarycentricCoordinates ( const Vector p) const

Compute the barycentric coordinates of an argument vector with respect to the triangle.

If the point lies within the triangle, the coordinates are positive and form a partition of the unity. This no longer holds if the point is outside of the triangle.

Note that this function is not the most efficient, as it relies on the evaluation of the area formed by the triangles pab, pbc, and pca.

Parameters
pPoint.

◆ 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.

Some algebra will show that it is half the ratio of the half perimeter and the surface of the triangle.

◆ 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.

After Tomas Moller 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.

◆ 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.

After Tomas Moller 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.
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.

If a triangle vertex lies exactly on the sphere, then no intersection occurs. Apart from this limit case, 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]

int Triangle::Intersect ( const Triangle U,
int &  coplanar,
Vector isectpt1,
Vector isectpt2 
) const

Check if two triangles intersect.

This function analyses all cases : edge intersecting coplanar triangles, fully inside or outside triangles, face intersecting triangles in three dimensionnal space.

Parameters
UTriangle.
isectpt1,isectpt2end vertices of segment if intersection occurs.
coplanarCoplanar flag.

After T. Moller, A Fast Triangle-Triangle Intersection Test, Journal of graphics tools, 2(2):25-30, 1997.

◆ 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[]()

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.

◆ 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.

Friends And Related Function Documentation

◆ operator<<

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

Overloaded.

Parameters
tTriangle.
sStream.