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/triangle3.h> 00031 #include <vcg/space/point2.h> 00032 #include <vcg/space/segment2.h> 00033 #include <float.h> 00034 00035 namespace vcg { 00036 00043 template <class SCALAR_TYPE> class Triangle2 00044 { 00045 public: 00046 typedef SCALAR_TYPE ScalarType; 00047 typedef Point2< ScalarType > CoordType; 00048 typedef Triangle2<ScalarType> TriangleType; 00049 00050 protected: 00052 Point2<ScalarType> _v[3]; 00053 public: 00054 00055 Triangle2() 00056 {} 00057 00058 Triangle2(const CoordType &p0,const CoordType &p1,const CoordType &p2) 00059 { 00060 P(0)=p0; 00061 P(1)=p1; 00062 P(2)=p2; 00063 } 00064 00066 inline CoordType & P( const int j ) { return _v[j];} 00067 inline CoordType & P0( const int j ) { return _v[j];} 00068 inline CoordType & P1( const int j ) { return _v[(j+1)%3];} 00069 inline CoordType & P2( const int j ) { return _v[(j+2)%3];} 00070 inline const CoordType & P( const int j ) const { return _v[j];} 00071 inline const CoordType & P0( const int j ) const { return _v[j];} 00072 inline const CoordType & P1( const int j ) const { return _v[(j+1)%3];} 00073 inline const CoordType & P2( const int j ) const { return _v[(j+2)%3];} 00074 inline const CoordType & cP0( const int j ) const { return _v[j];} 00075 inline const CoordType & cP1( const int j ) const { return _v[(j+1)%3];} 00076 inline const CoordType & cP2( const int j ) const { return _v[(j+2)%3];} 00077 00085 bool InterpolationParameters(const CoordType & bq, ScalarType &a, ScalarType &b, ScalarType &c ) const 00086 { 00087 const ScalarType EPSILON = ScalarType(0.0001f); 00088 00089 ScalarType AreaGlobal=(P(1) - P(0)) ^ (P(2) - P(0)); 00090 ScalarType Area0=((P(2) - P(1)) ^ (bq - P(1))); 00091 ScalarType Area1=((P(0) - P(2)) ^ (bq - P(2))); 00092 ScalarType Area2=((P(1) - P(0)) ^ (bq - P(0))); 00093 //ScalarType AreaGlobal=Area0+Area1+Area2; 00094 /*if ((Area0>(AreaGlobal+EPSILON))||(Area1>(AreaGlobal+EPSILON))||(Area2>(AreaGlobal+EPSILON))) 00095 return false;*/ 00096 a=Area0/AreaGlobal; 00097 b=Area1/AreaGlobal; 00098 c=Area2/AreaGlobal; 00099 00101 if(((a>(ScalarType)1+EPSILON)||(b>(ScalarType)1+EPSILON)||(c>(ScalarType)1+EPSILON))|| 00102 ((a<-EPSILON)||(b<-EPSILON)||(c<-EPSILON))) 00103 return false; 00104 00106 if(a>1) 00107 a=(ScalarType)1; 00108 if(b>1) 00109 b=(ScalarType)1; 00110 if(c>1) 00111 c=(ScalarType)1; 00112 if(a<0) 00113 a=(ScalarType)0; 00114 if(b<0) 00115 b=(ScalarType)0; 00116 if(c<0) 00117 c=(ScalarType)0; 00118 00119 00120 return true; 00121 } 00122 00124 void PointDistance(const CoordType & q, 00125 ScalarType & dist, 00126 CoordType & p ) const 00127 { 00128 dist=FLT_MAX; 00130 for (int i=0;i<3;i++) 00131 { 00132 vcg::Segment2<float> s=vcg::Segment2<float>(P(i),P((i+1)%3)); 00133 CoordType clos=ClosestPoint<ScalarType>(s,q); 00134 ScalarType dis_test=(clos-q).Norm(); 00135 if (dis_test<dist) 00136 { 00137 dist=dis_test; 00138 p=clos; 00139 } 00140 } 00141 } 00142 00143 }; //end Class 00144 00145 00146 } // end namespace 00147 #endif 00148