00001 /**************************************************************************** 00002 * VCGLib o o * 00003 * Visual and Computer Graphics Library o o * 00004 * _ O _ * 00005 * Copyright(C) 2004 \/)\/ * 00006 * Visual Computing Lab /\/| * 00007 * ISTI - Italian National Research Council | * 00008 * \ * 00009 * All rights reserved. * 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This program is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * 00020 * for more details. * 00021 * * 00022 ****************************************************************************/ 00023 /**************************************************************************** 00024 History 00025 00026 ****************************************************************************/ 00027 00028 #ifndef __VCG_TRIANGLE2 00029 #define __VCG_TRIANGLE2 00030 #include <vcg/space/point2.h> 00031 #include <vcg/space/segment2.h> 00032 #include <float.h> 00033 00034 namespace vcg { 00035 00042 template <class SCALAR_TYPE> class Triangle2 00043 { 00044 public: 00045 typedef SCALAR_TYPE ScalarType; 00046 typedef Point2< ScalarType > CoordType; 00047 typedef Triangle2<ScalarType> TriangleType; 00048 00049 protected: 00051 Point2<ScalarType> _v[3]; 00052 public: 00053 00054 Triangle2() 00055 {} 00056 00057 Triangle2(const CoordType &p0,const CoordType &p1,const CoordType &p2) 00058 { 00059 P(0)=p0; 00060 P(1)=p1; 00061 P(2)=p2; 00062 } 00063 00065 inline CoordType & P( const int j ) { return _v[j];} 00066 inline CoordType & P0( const int j ) { return _v[j];} 00067 inline CoordType & P1( const int j ) { return _v[(j+1)%3];} 00068 inline CoordType & P2( const int j ) { return _v[(j+2)%3];} 00069 inline const CoordType & P( const int j ) const { return _v[j];} 00070 inline const CoordType & P0( const int j ) const { return _v[j];} 00071 inline const CoordType & P1( const int j ) const { return _v[(j+1)%3];} 00072 inline const CoordType & P2( const int j ) const { return _v[(j+2)%3];} 00073 inline const CoordType & cP0( const int j ) const { return _v[j];} 00074 inline const CoordType & cP1( const int j ) const { return _v[(j+1)%3];} 00075 inline const CoordType & cP2( const int j ) const { return _v[(j+2)%3];} 00076 00088 bool InterpolationParameters(const CoordType & bq, ScalarType &L1, 00089 ScalarType &L2, ScalarType &L3 ) const 00090 { 00091 const ScalarType EPSILON = ScalarType(0.0001f); 00092 00093 ScalarType x1=P(0).X(); 00094 ScalarType x2=P(1).X(); 00095 ScalarType x3=P(2).X(); 00096 00097 ScalarType y1=P(0).Y(); 00098 ScalarType y2=P(1).Y(); 00099 ScalarType y3=P(2).Y(); 00100 00101 ScalarType x=bq.X(); 00102 ScalarType y=bq.Y(); 00103 00104 L1=((y2-y3)*(x-x3)+(x3-x2)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3)); 00105 L2=((y3-y1)*(x-x3)+(x1-x3)*(y-y3))/((y3-y1)*(x2-x3)+(x1-x3)*(y2-y3)); 00106 L3=1-L1-L2; 00107 if(math::IsNAN(L1) || math::IsNAN(L2) || math::IsNAN(L3)) L1=L2=L3=(ScalarType)(1.0/3.0); 00108 bool inside=true; 00109 inside&=(L1>=0-EPSILON)&&(L1<=1+EPSILON); 00110 inside&=(L2>=0-EPSILON)&&(L2<=1+EPSILON); 00111 inside&=(L3>=0-EPSILON)&&(L3<=1+EPSILON); 00112 return inside; 00113 } 00114 00116 void PointDistance(const CoordType & q, 00117 ScalarType & dist, 00118 CoordType & p ) const 00119 { 00120 dist=FLT_MAX; 00122 for (int i=0;i<3;i++) 00123 { 00124 vcg::Segment2<ScalarType> s=vcg::Segment2<ScalarType>(P(i),P((i+1)%3)); 00125 CoordType clos=ClosestPoint<ScalarType>(s,q); 00126 ScalarType dis_test=(clos-q).Norm(); 00127 if (dis_test<dist) 00128 { 00129 dist=dis_test; 00130 p=clos; 00131 } 00132 } 00133 } 00134 00136 bool IsCCW() 00137 { 00138 ScalarType Area=(P(1)-P(0))^(P(2)-P(0)); 00139 return (Area>0); 00140 } 00141 00142 }; //end Class 00143 00144 00145 } // end namespace 00146 #endif 00147