00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCG_TRIANGLE3
00025 #define __VCG_TRIANGLE3
00026
00027 #include <vcg/space/box3.h>
00028 #include <vcg/space/point2.h>
00029 #include <vcg/space/point3.h>
00030 #include <vcg/space/plane3.h>
00031 #include <vcg/space/segment3.h>
00032 #include <vcg/space/triangle2.h>
00033
00034 namespace vcg {
00035
00042 template <class ScalarTriangleType> class Triangle3
00043 {
00044 public:
00045 typedef ScalarTriangleType ScalarType;
00046 typedef Point3< ScalarType > CoordType;
00048 typedef Box3<ScalarType> BoxType;
00049
00050
00051
00052
00053
00054 Triangle3(){}
00055 Triangle3(const CoordType & c0,const CoordType & c1,const CoordType & c2){_v[0]=c0;_v[1]=c1;_v[2]=c2;}
00056 protected:
00058 Point3<ScalarType> _v[3];
00059 public:
00060
00062 inline CoordType & P( const int j ) { return _v[j];}
00063 inline CoordType & P0( const int j ) { return _v[j];}
00064 inline CoordType & P1( const int j ) { return _v[(j+1)%3];}
00065 inline CoordType & P2( const int j ) { return _v[(j+2)%3];}
00066 inline const CoordType & P( const int j ) const { return _v[j];}
00067 inline const CoordType & cP( const int j ) const { return _v[j];}
00068 inline const CoordType & P0( const int j ) const { return _v[j];}
00069 inline const CoordType & P1( const int j ) const { return _v[(j+1)%3];}
00070 inline const CoordType & P2( const int j ) const { return _v[(j+2)%3];}
00071 inline const CoordType & cP0( const int j ) const { return _v[j];}
00072 inline const CoordType & cP1( const int j ) const { return _v[(j+1)%3];}
00073 inline const CoordType & cP2( const int j ) const { return _v[(j+2)%3];}
00074
00075 inline int VN() const { return 3;}
00076 };
00077
00078
00079
00081 template<class TriangleType>
00082 typename TriangleType::CoordType TriangleNormal(const TriangleType &t)
00083 {
00084 return (( t.cP(1) - t.cP(0)) ^ (t.cP(2) - t.cP(0)));
00085 }
00087 template<class TriangleType>
00088 typename TriangleType::CoordType NormalizedTriangleNormal(const TriangleType &t)
00089 {
00090 return (( t.cP(1) - t.cP(0)) ^ (t.cP(2) - t.cP(0))).Normalize();
00091 }
00092 template<class Point3Type>
00093 Point3Type Normal( Point3Type const &p0, Point3Type const & p1, Point3Type const & p2)
00094 {
00095 return (( p1 - p0) ^ (p2 - p0));
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 template<class TriangleType, class ScalarType>
00113 bool InterpolationParameters(const TriangleType t, const int Axis, const Point3<ScalarType> & P, Point3<ScalarType> & L)
00114 {
00115 typedef Point2<ScalarType> P2;
00116 if(Axis==0) return InterpolationParameters2( P2(t.cP(0)[1],t.cP(0)[2]), P2(t.cP(1)[1],t.cP(1)[2]), P2(t.cP(2)[1],t.cP(2)[2]), P2(P[1],P[2]), L);
00117 if(Axis==1) return InterpolationParameters2( P2(t.cP(0)[0],t.cP(0)[2]), P2(t.cP(1)[0],t.cP(1)[2]), P2(t.cP(2)[0],t.cP(2)[2]), P2(P[0],P[2]), L);
00118 if(Axis==2) return InterpolationParameters2( P2(t.cP(0)[0],t.cP(0)[1]), P2(t.cP(1)[0],t.cP(1)[1]), P2(t.cP(2)[0],t.cP(2)[1]), P2(P[0],P[1]), L);
00119 return false;
00120 }
00122 template<class TriangleType, class ScalarType>
00123 bool InterpolationParameters(const TriangleType t, const Point3<ScalarType> & N, const Point3<ScalarType> & P, Point3<ScalarType> & L)
00124 {
00125 if(fabs(N[0])>fabs(N[1]))
00126 {
00127 if(fabs(N[0])>fabs(N[2]))
00128 return InterpolationParameters(t,0,P,L);
00129 else
00130 return InterpolationParameters(t,2,P,L);
00131 }
00132 else
00133 {
00134 if(fabs(N[1])>fabs(N[2]))
00135 return InterpolationParameters(t,1,P,L);
00136 else
00137 return InterpolationParameters(t,2,P,L);
00138 }
00139 }
00140
00141
00142 template<class ScalarType>
00143 bool InterpolationParameters2(const Point2<ScalarType> &V1,
00144 const Point2<ScalarType> &V2,
00145 const Point2<ScalarType> &V3,
00146 const Point2<ScalarType> &P, Point3<ScalarType> &L)
00147 {
00148 vcg::Triangle2<ScalarType> t2=vcg::Triangle2<ScalarType>(V1,V2,V3);
00149 return (t2.InterpolationParameters(P,L.X(),L.Y(),L.Z() ));
00150 }
00151
00153 template<class TriangleType, class ScalarType>
00154 bool InterpolationParameters(const TriangleType t, const Point3<ScalarType> & P, Point3<ScalarType> & L)
00155 {
00156 vcg::Point3<ScalarType> N=vcg::TriangleNormal<TriangleType>(t);
00157 return (InterpolationParameters<TriangleType,ScalarType>(t,N,P,L));
00158 }
00159
00160
00161
00162
00167 template<class P3ScalarType>
00168 P3ScalarType Quality( Point3<P3ScalarType> const &p0, Point3<P3ScalarType> const & p1, Point3<P3ScalarType> const & p2)
00169 {
00170 Point3<P3ScalarType> d10=p1-p0;
00171 Point3<P3ScalarType> d20=p2-p0;
00172 Point3<P3ScalarType> d12=p1-p2;
00173 Point3<P3ScalarType> x = d10^d20;
00174
00175 P3ScalarType a = Norm( x );
00176 if(a==0) return 0;
00177 P3ScalarType b = SquaredNorm( d10 );
00178 if(b==0) return 0;
00179 P3ScalarType t = b;
00180 t = SquaredNorm( d20 ); if ( b<t ) b = t;
00181 t = SquaredNorm( d12 ); if ( b<t ) b = t;
00182 return a/b;
00183 }
00184
00185
00187 template<class TriangleType>
00188 typename TriangleType::ScalarType QualityFace(const TriangleType &t)
00189 {
00190 return Quality(t.cP(0), t.cP(1), t.cP(2));
00191 }
00196 template<class P3ScalarType>
00197 P3ScalarType QualityRadii(Point3<P3ScalarType> const &p0,
00198 Point3<P3ScalarType> const &p1,
00199 Point3<P3ScalarType> const &p2) {
00200
00201 P3ScalarType a=(p1-p0).Norm();
00202 P3ScalarType b=(p2-p0).Norm();
00203 P3ScalarType c=(p1-p2).Norm();
00204
00205 P3ScalarType sum = (a + b + c)*0.5;
00206 P3ScalarType area2 = sum*(a+b-sum)*(a+c-sum)*(b+c-sum);
00207 if(area2 <= 0) return 0;
00208
00209
00210 return (8*area2)/(a*b*c*sum);
00211 }
00212
00217 template<class P3ScalarType>
00218 P3ScalarType QualityMeanRatio(Point3<P3ScalarType> const &p0,
00219 Point3<P3ScalarType> const &p1,
00220 Point3<P3ScalarType> const &p2) {
00221
00222 P3ScalarType a=(p1-p0).Norm();
00223 P3ScalarType b=(p2-p0).Norm();
00224 P3ScalarType c=(p1-p2).Norm();
00225 P3ScalarType sum = (a + b + c)*0.5;
00226 P3ScalarType area2 = sum*(a+b-sum)*(a+c-sum)*(b+c-sum);
00227 if(area2 <= 0) return 0;
00228 return (4.0*sqrt(3.0)*sqrt(area2))/(a*a + b*b + c*c);
00229 }
00230
00231
00232
00234
00235
00236
00237
00238
00239
00240 template<class TriangleType>
00241 typename TriangleType::ScalarType DoubleArea(const TriangleType &t)
00242 {
00243 return Norm( (t.cP(1) - t.cP(0)) ^ (t.cP(2) - t.cP(0)) );
00244 }
00245
00246 template<class TriangleType>
00247 typename TriangleType::ScalarType CosWedge(const TriangleType &t, int k)
00248 {
00249 typename TriangleType::CoordType
00250 e0 = t.cP((k+1)%3) - t.cP(k),
00251 e1 = t.cP((k+2)%3) - t.cP(k);
00252 return (e0*e1)/(e0.Norm()*e1.Norm());
00253 }
00254
00255 template<class TriangleType>
00256 Point3<typename TriangleType::ScalarType> Barycenter(const TriangleType &t)
00257 {
00258 return ((t.cP(0)+t.cP(1)+t.cP(2))/(typename TriangleType::ScalarType) 3.0);
00259 }
00260
00261 template<class TriangleType>
00262 typename TriangleType::ScalarType Perimeter(const TriangleType &t)
00263 {
00264 return Distance(t.cP(0),t.cP(1))+
00265 Distance(t.cP(1),t.cP(2))+
00266 Distance(t.cP(2),t.cP(0));
00267 }
00268
00269 template<class TriangleType>
00270 Point3<typename TriangleType::ScalarType> Circumcenter(const TriangleType &t)
00271 {
00272 typename TriangleType::ScalarType a2 = (t.cP(1) - t.cP(2)).SquaredNorm();
00273 typename TriangleType::ScalarType b2 = (t.cP(2) - t.cP(0)).SquaredNorm();
00274 typename TriangleType::ScalarType c2 = (t.cP(0) - t.cP(1)).SquaredNorm();
00275 Point3<typename TriangleType::ScalarType>c = t.cP(0)*a2*(-a2 + b2 + c2) +
00276 t.cP(1)*b2*( a2 - b2 + c2) +
00277 t.cP(2)*c2*( a2 + b2 - c2);
00278 c /= 2*(a2*b2 + a2*c2 + b2*c2) - a2*a2 - b2*b2 - c2*c2;
00279 return c;
00280 }
00281
00282
00283 }
00284
00285
00286 #endif
00287