mesh_conv::MC_triangle Class Reference

Container class for a triangle. More...

#include <MC_triangle.hpp>

Inheritance diagram for mesh_conv::MC_triangle:
Inheritance graph
[legend]
Collaboration diagram for mesh_conv::MC_triangle:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MC_triangle ()
 empty constructor
 MC_triangle (const MC_v3d &v0, const MC_v3d &v1, const MC_v3d &v2)
 constructor from a triangle
 MC_triangle (const MC_v3d_vector &vec)
 copy constructor
 MC_triangle (const std::vector< MC_v3d > &vec)
 direct constructor from a vector of MC_v3d
 MC_triangle (const MC_triangle &p)
 copy constructor
 MC_triangle (const MC_polygon &p)
 constructor from polygon (is 3 edge)
MC_v3d normal () const
 get the normal of the triangle.
MC_v3d barycenter () const
 get the barycenter of the polygon
MC_v3d closest_point (const MC_v3d &x, int *type=0) const
 get shortest position on the triangle between the triangle and the point
bool is_inside (const MC_v3d &_x) const
 check if a vertex (in plane) is inside the triangle by calculating its barycentric coordinates
std::pair< MC_double_vector, bool > barycentric_coordinates (const MC_v3d &x) const
 get barycentric coordinates for vertex in this plane
double area () const
 get the area of the triangle
MC_matrix inertia () const
 get the inertia matrix (3x3) of a triangle
MC_v3d segment_intersection (const MC_segment &s, int *type=0) const
 segment_intersection
MC_segment segment (const int &edge_number) const
 get the segment of the given edge

Static Public Member Functions

static std::vector< MC_triangletriangulate (const MC_polygon &p)
 triangulate polygon (do fan starting at p[0])
static MC_v3d pn_triangle (const MC_v3d_vector &X, const MC_v3d_vector &N, const MC_double_vector &bar_coord)
 PN triangle.

Detailed Description

Container class for a triangle.

Internal structure is a vector of MC_v3d

Definition at line 39 of file MC_triangle.hpp.


Constructor & Destructor Documentation

mesh_conv::MC_triangle::MC_triangle (  ) 

empty constructor

Definition at line 30 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::resize().

Referenced by triangulate().

00030 {resize(3);}

Here is the call graph for this function:

Here is the caller graph for this function:

mesh_conv::MC_triangle::MC_triangle ( const MC_v3d v0,
const MC_v3d v1,
const MC_v3d v2 
)

constructor from a triangle

Definition at line 31 of file MC_triangle.cpp.

00032             :MC_v3d_vector(v0,v1,v2)
00033     {}

mesh_conv::MC_triangle::MC_triangle ( const MC_v3d_vector vec  ) 

copy constructor

Definition at line 34 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::resize(), mesh_conv::MC_v3d_vector::size(), and mesh_conv::MC_v3d_vector::v.

00035             :MC_v3d_vector()
00036     {
00037         if(vec.size()!=3)
00038         {std::cout<<"Error in MC_triangle::MC_triangle("<<vec<<"), size should be 3"<<std::endl;exit(-1);}
00039         resize(3);
00040         v[0]=vec[0];v[1]=vec[1];v[2]=vec[2];
00041     }

Here is the call graph for this function:

mesh_conv::MC_triangle::MC_triangle ( const std::vector< MC_v3d > &  vec  ) 

direct constructor from a vector of MC_v3d

Definition at line 42 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::resize(), and mesh_conv::MC_v3d_vector::v.

00043             :MC_v3d_vector()
00044     {
00045         if(vec.size()!=3)
00046         {std::cout<<"Error in MC_triangle::MC_triangle(std::vector <MC_v3d>), size should be 3 and not "<<vec.size()<<std::endl;exit(-1);}
00047         resize(3);
00048         v[0]=vec[0];v[1]=vec[1];v[2]=vec[2];
00049     }

Here is the call graph for this function:

mesh_conv::MC_triangle::MC_triangle ( const MC_triangle p  ) 

copy constructor

Definition at line 50 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::v.

00051             :MC_v3d_vector(p)
00052     {v=p.v;}

mesh_conv::MC_triangle::MC_triangle ( const MC_polygon p  ) 

constructor from polygon (is 3 edge)

Definition at line 141 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::resize(), mesh_conv::MC_v3d_vector::size(), and mesh_conv::MC_v3d_vector::v.

00142     {
00143         if(p.size()!=3)
00144         {std::cout<<"Error in MC_triangle::MC_triangle("<<p<<"), size must be 3 and not "<<p.size()<<std::endl;exit(-1);}
00145         resize(3);
00146         v[0]=p[0];v[1]=p[1];v[2]=p[2];
00147     }

Here is the call graph for this function:


Member Function Documentation

double mesh_conv::MC_triangle::area (  )  const

get the area of the triangle

Definition at line 136 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::v.

Referenced by barycentric_coordinates().

00137     {
00138         return MC_v3d::area(v[1]-v[0],v[2]-v[0]);
00139     }

Here is the caller graph for this function:

MC_v3d mesh_conv::MC_triangle::barycenter (  )  const

get the barycenter of the polygon

Reimplemented from mesh_conv::MC_v3d_vector.

Definition at line 27 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::v.

00028     {return 1.0/3.0*(v[0]+v[1]+v[2]);}

std::pair< MC_double_vector, bool > mesh_conv::MC_triangle::barycentric_coordinates ( const MC_v3d x  )  const

get barycentric coordinates for vertex in this plane

Returns:
double_vector (alpha,beta,gamma) if x is in the plane,
true/false if x is in the plane

Definition at line 110 of file MC_triangle.cpp.

References area(), normal(), and mesh_conv::MC_v3d_vector::v.

Referenced by is_inside().

00111     {
00112         // check if _x is in the plane
00113         double epsilon=0.0001;
00114         double L = (x-v[0]).dot(normal());
00115         if(L>epsilon)//not in the plane
00116             return std::pair <MC_double_vector,bool> (MC_double_vector(-1,-1,-1),false);
00117 
00118         // calculate the barycentric coordinates
00119         double area_1=MC_v3d::area(v[1]-v[0],x-v[0]);
00120         double area_2=MC_v3d::area(v[2]-v[1],x-v[1]);
00121         double area_3=MC_v3d::area(v[0]-v[2],x-v[2]);
00122         double area_tot = area();
00123 
00124         MC_double_vector b(area_1/area_tot,area_2/area_tot,area_3/area_tot);
00125         return std::pair <MC_double_vector,bool> (b,true);
00126     }

Here is the call graph for this function:

Here is the caller graph for this function:

MC_v3d mesh_conv::MC_triangle::closest_point ( const MC_v3d x,
int *  type = 0 
) const

get shortest position on the triangle between the triangle and the point

Returns:
the closest point

type the type of point as pointer (default=null)

type = 0: interior point in the polygon

type = 1: edge point

type = 2: vertex point

A vertex is inside the polygon (in the plane) if it lies on one of its triangulation

Definition at line 54 of file MC_triangle.cpp.

References mesh_conv::MC_segment::closest_point(), is_inside(), mesh_conv::MC_v3d_vector::norm(), normal(), segment(), and mesh_conv::MC_v3d_vector::v.

00055     {
00056 
00057         double epsilon=0.00001;
00058 
00059         // First project and look if the projection is inside the triangle (barycentric coordinates)
00060         MC_v3d n=normal();
00061         MC_v3d proj = x-((x-v[0]).dot(n))*n;
00062 
00063         // if the proj is inside the triangle, this is the closest point
00064         if(is_inside(proj)==1)
00065         {
00066             if(type!=0)
00067                 *type=0;
00068             return proj;
00069         }
00070 
00071 
00072         // if not, take the closest line
00073         if(type!=0)
00074             *type=1;
00075         MC_segment s0=segment(0),s1=segment(1),s2=segment(2);
00076         MC_v3d y0=s0.closest_point(x),y1=s1.closest_point(x),y2=s2.closest_point(x);
00077         double dist_1=(y0-x).norm(),dist_2=(y1-x).norm(),dist_3=(y2-x).norm();
00078         if(dist_1<=dist_2 && dist_1<=dist_3)
00079         {
00080             //check if its a vertex point
00081             for(int k=0;k<3;k++){if((y0-v[k]).norm()<epsilon) *type=2;}
00082             return y0;
00083         }
00084         else if(dist_2<=dist_1 && dist_2<=dist_3)
00085         {
00086             for(int k=0;k<3;k++){if((y1-v[k]).norm()<epsilon) *type=2;}
00087             return y1;
00088         }
00089         else
00090         {
00091             for(int k=0;k<3;k++){if((y2-v[k]).norm()<epsilon) *type=2;}
00092             return y2;
00093         }
00094 
00095     }

Here is the call graph for this function:

MC_matrix mesh_conv::MC_triangle::inertia (  )  const

get the inertia matrix (3x3) of a triangle

see http://en.wikipedia.org/wiki/Inertia_tensor_of_triangle J=tr(C)*I-C, where C=int(x*x^t)

Definition at line 204 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::norm(), mesh_conv::MC_matrix::trace(), and mesh_conv::MC_matrix::transposed().

00205     {
00206         MC_v3d x0=(*this)[0];
00207         MC_v3d x1=(*this)[1];
00208         MC_v3d x2=(*this)[2];
00209 
00210         double a=((x1-x0).cross(x2-x0)).norm();
00211 
00212         MC_matrix S(3,3);
00213         S(0,0)=2;S(0,1)=1;S(0,2)=1;
00214         S(1,0)=1;S(1,1)=2;S(1,2)=1;
00215         S(2,0)=1;S(2,1)=1;S(2,2)=2;
00216         S*=1.0/24.0;
00217 
00218         MC_matrix V(x0,x1,x2);
00219 
00220         MC_matrix C=a*V*S*V.transposed();
00221         MC_matrix J=C.trace()*MC_matrix(3)-C;
00222 
00223         return J;
00224     }

Here is the call graph for this function:

bool mesh_conv::MC_triangle::is_inside ( const MC_v3d _x  )  const

check if a vertex (in plane) is inside the triangle by calculating its barycentric coordinates

Returns:
true if inside, false outside

Definition at line 97 of file MC_triangle.cpp.

References barycentric_coordinates().

Referenced by closest_point(), and segment_intersection().

00098     {
00099         std::pair <MC_double_vector,bool> bar=barycentric_coordinates(_x);
00100         if(bar.second==false) //not in plane
00101             return false;
00102         double epsilon=0.0001;
00103         if(bar.first[0]>1.0 || bar.first[1]>1.0 || bar.first[2]>1.0 || (bar.first[0]+bar.first[1]+bar.first[2]-1.0)>epsilon) // outside the polygon
00104             return false;
00105 
00106         // in the polygon
00107         return true;
00108     }

Here is the call graph for this function:

Here is the caller graph for this function:

MC_v3d mesh_conv::MC_triangle::normal (  )  const

get the normal of the triangle.

Returns:
the normal as a R3 vector.

Definition at line 12 of file MC_triangle.cpp.

References mesh_conv::MC_v3d::cross(), mesh_conv::MC_v3d::norm(), and mesh_conv::MC_v3d_vector::v.

Referenced by barycentric_coordinates(), closest_point(), and segment_intersection().

00013     {
00014 
00015         MC_v3d v0=v[1]-v[0];
00016         MC_v3d v1=v[2]-v[0];
00017 
00018         MC_v3d n=v0.cross(v1);
00019         double nn=n.norm();
00020         double epsilon=0.00000001;
00021         if(nn<epsilon)
00022         {std::cout<<"Warning in MC_triangle::normal(), from triangle "<<*this<<", normal is "<<n<<" of norm()="<<nn<<std::endl;return MC_v3d(0,0,1);}
00023         n/=nn;
00024         return n;
00025     }

Here is the call graph for this function:

Here is the caller graph for this function:

MC_v3d mesh_conv::MC_triangle::pn_triangle ( const MC_v3d_vector X,
const MC_v3d_vector N,
const MC_double_vector bar_coord 
) [static]

PN triangle.

[Curved PN triangles, A. Vlachos]

Definition at line 175 of file MC_triangle.cpp.

References mesh_conv::MC_double_vector::dot(), and mesh_conv::MC_v3d_vector::zeros().

00176     {
00177         MC_double_vector w=MC_double_vector::zeros(9);
00178         for(int i=0;i<3;++i)
00179             for(int j=0;j<3;++j)
00180                 w[3*i+j]=(X[j]-X[i]).dot(N[i]);
00181 
00182         MC_v3d b300=X[0];
00183         MC_v3d b030=X[1];
00184         MC_v3d b003=X[2];
00185 
00186         MC_v3d b210=(2*X[0]+X[1]-w[0*3+1]*N[0])/3.0;
00187         MC_v3d b120=(2*X[1]+X[0]-w[1*3+0]*N[1])/3.0;
00188         MC_v3d b021=(2*X[1]+X[2]-w[1*3+2]*N[1])/3.0;
00189         MC_v3d b012=(2*X[2]+X[1]-w[2*3+1]*N[2])/3.0;
00190         MC_v3d b102=(2*X[2]+X[0]-w[2*3+0]*N[2])/3.0;
00191         MC_v3d b201=(2*X[0]+X[2]-w[0*3+2]*N[0])/3.0;
00192         MC_v3d E=(b210+b120+b021+b012+b102+b201)/6.0;
00193         MC_v3d V=(X[0]+X[1]+X[2])/3.0;
00194         MC_v3d b111=E+(E-V)/2.0;
00195 
00196         MC_v3d y=b300*x[0]*x[0]*x[0]+b030*x[1]*x[1]*x[1]+b003*x[2]*x[2]*x[2]+
00197                  b210*3.0*x[0]*x[0]*x[1]+b120*3.0*x[0]*x[1]*x[1]+b201*3.0*x[0]*x[0]*x[2]+
00198                  b021*3.0*x[1]*x[1]*x[2]+b102*3.0*x[0]*x[2]*x[2]+b012*3.0*x[1]*x[2]*x[2]+
00199                  b111*6.0*x[0]*x[1]*x[2];
00200 
00201         return y;
00202     }

Here is the call graph for this function:

MC_segment mesh_conv::MC_triangle::segment ( const int &  edge_number  )  const

get the segment of the given edge

Definition at line 128 of file MC_triangle.cpp.

References mesh_conv::MC_v3d_vector::v.

Referenced by closest_point().

00129     {
00130         if(edge_number<0 || edge_number>2)
00131         {std::cout<<"MC_triangle::segment("<<edge_number<<"),_k_edge has to be between 0 and 2"<<std::endl;exit(-1);}
00132 
00133         return MC_segment(v[edge_number],v[(edge_number+1)%3]);
00134     }

Here is the caller graph for this function:

MC_v3d mesh_conv::MC_triangle::segment_intersection ( const MC_segment s,
int *  type = 0 
) const

segment_intersection

Parameters:
s the segment to intersect
type the type of intersection (default 0)
Returns:
the point of intersection if it exist (else defaut value)

_ no intersections (type 0)

_ triangle intersection on one interior point (type 1)

_ triangle intersection on a vertex of the Segment (type 2)

_ triangle is partly or totally inside the segment (type 3)

Definition at line 157 of file MC_triangle.cpp.

References is_inside(), normal(), mesh_conv::MC_segment::plane_intersection(), and mesh_conv::MC_v3d_vector::v.

00158     {
00159         int temp_type=0;
00160         MC_v3d i=s.plane_intersection(normal(),v[0],&temp_type);
00161 
00162         if(type!=0)
00163             *type=temp_type;
00164 
00165         if(temp_type==0 || temp_type==3)
00166             return i;
00167 
00168         if(is_inside(i)==false)
00169             if(type!=0)
00170                 *type=0;
00171 
00172         return i;
00173     }

Here is the call graph for this function:

std::vector< MC_triangle > mesh_conv::MC_triangle::triangulate ( const MC_polygon p  )  [static]

triangulate polygon (do fan starting at p[0])

Definition at line 148 of file MC_triangle.cpp.

References MC_triangle(), and mesh_conv::MC_v3d_vector::size().

Referenced by mesh_conv::MC_polygon::closest_point(), mesh_conv::MC_mesh_index_vector::inertia(), mesh_conv::MC_polygon::is_inside(), and mesh_conv::MC_polygon::segment_intersection().

00149     {
00150         int N=p.size();
00151         std::vector <MC_triangle> vtri(N-2);
00152         for(int k_triangle=0;k_triangle<N-2;k_triangle++)
00153             vtri[k_triangle]=MC_triangle(p[0],p[k_triangle+1],p[k_triangle+2]);
00154         return vtri;
00155     }

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files:

Generated on Sun Apr 18 20:24:48 2010 by  doxygen 1.6.1