corrected InterpolationParameters funtion in order to evaluate correctly barycentric coordinates even for points wich falls outside the triangle.

This commit is contained in:
Nico Pietroni 2010-12-12 23:59:55 +00:00
parent 387c62414a
commit 542bfc8a91
1 changed files with 25 additions and 33 deletions

View File

@ -77,47 +77,39 @@ public:
/** evaluate barycentric coordinates /** evaluate barycentric coordinates
@param bq Point on the face @param bq Point on the face
@param a barycentric value for V(0) @param L0 barycentric value for V(0)
@param b barycentric value for V(1) @param L1 barycentric value for V(1)
@param c barycentric value for V(2) @param L2 barycentric value for V(2)
@return true se bq appartain to the face, false otherwise @return true se bq appartain to the face, false otherwise
from http://en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)
L1=((y2-y3)(x-x3)+(x3-x2)(y-y3))/((y2-y3)(x1-x3)+(x3-x2)(y1-y3))
L2=((y3-y1)(x-x3)+(x1-x3)(y-y3))/((y3-y1)(x2-x3)+(x1-x3)(y2-y3))
L3=1-L1-L2
*/ */
bool InterpolationParameters(const CoordType & bq, ScalarType &a, ScalarType &b, ScalarType &c ) const bool InterpolationParameters(const CoordType & bq, ScalarType &L1,
ScalarType &L2, ScalarType &L3 ) const
{ {
const ScalarType EPSILON = ScalarType(0.0001f); const ScalarType EPSILON = ScalarType(0.0001f);
ScalarType AreaGlobal=(P(1) - P(0)) ^ (P(2) - P(0)); ScalarType x1=P(0).X();
ScalarType Area0=((P(2) - P(1)) ^ (bq - P(1))); ScalarType x2=P(1).X();
ScalarType Area1=((P(0) - P(2)) ^ (bq - P(2))); ScalarType x3=P(2).X();
ScalarType Area2=((P(1) - P(0)) ^ (bq - P(0)));
//ScalarType AreaGlobal=Area0+Area1+Area2;
/*if ((Area0>(AreaGlobal+EPSILON))||(Area1>(AreaGlobal+EPSILON))||(Area2>(AreaGlobal+EPSILON)))
return false;*/
a=Area0/AreaGlobal;
b=Area1/AreaGlobal;
c=Area2/AreaGlobal;
///test inside/outside ScalarType y1=P(0).Y();
if(((a>(ScalarType)1+EPSILON)||(b>(ScalarType)1+EPSILON)||(c>(ScalarType)1+EPSILON))|| ScalarType y2=P(1).Y();
((a<-EPSILON)||(b<-EPSILON)||(c<-EPSILON))) ScalarType y3=P(2).Y();
return false;
///approximation errors
if(a>1)
a=(ScalarType)1;
if(b>1)
b=(ScalarType)1;
if(c>1)
c=(ScalarType)1;
if(a<0)
a=(ScalarType)0;
if(b<0)
b=(ScalarType)0;
if(c<0)
c=(ScalarType)0;
ScalarType x=bq.X();
ScalarType y=bq.Y();
return true; L1=((y2-y3)*(x-x3)+(x3-x2)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3));
L2=((y3-y1)*(x-x3)+(x1-x3)*(y-y3))/((y3-y1)*(x2-x3)+(x1-x3)*(y2-y3));
L3=1-L1-L2;
bool inside=true;
inside&=(L1>=0-EPSILON)&&(L1<=1+EPSILON);
inside&=(L2>=0-EPSILON)&&(L2<=1+EPSILON);
inside&=(L3>=0-EPSILON)&&(L3<=1+EPSILON);
return inside;
} }
///return the distance to the point q and neighors point p ///return the distance to the point q and neighors point p