IceTriangle.cpp
Go to the documentation of this file.
00001 
00002 
00008 
00009 
00011 // Precompiled Header
00012 #include "Stdafx.h"
00013 
00014 using namespace IceMaths;
00015 
00017 
00025 
00026 
00027 static sdword VPlaneSideEps(const Point& v, const Plane& plane, float epsilon)
00028 {
00029         // Compute distance from current vertex to the plane
00030         float Dist = plane.Distance(v);
00031         // Compute side:
00032         // 1    = the vertex is on the positive side of the plane
00033         // -1   = the vertex is on the negative side of the plane
00034         // 0    = the vertex is on the plane (within epsilon)
00035         return Dist > epsilon ? 1 : Dist < -epsilon ? -1 : 0;
00036 }
00037 
00039 
00042 
00043 void Triangle::Flip()
00044 {
00045         Point Tmp = mVerts[1];
00046         mVerts[1] = mVerts[2];
00047         mVerts[2] = Tmp;
00048 }
00049 
00051 
00055 
00056 float Triangle::Area() const
00057 {
00058         const Point& p0 = mVerts[0];
00059         const Point& p1 = mVerts[1];
00060         const Point& p2 = mVerts[2];
00061         return ((p0 - p1)^(p0 - p2)).Magnitude() * 0.5f;
00062 }
00063 
00065 
00069 
00070 float Triangle::Perimeter()     const
00071 {
00072         const Point& p0 = mVerts[0];
00073         const Point& p1 = mVerts[1];
00074         const Point& p2 = mVerts[2];
00075         return          p0.Distance(p1)
00076                         +       p0.Distance(p2)
00077                         +       p1.Distance(p2);
00078 }
00079 
00081 
00085 
00086 float Triangle::Compacity() const
00087 {
00088         float P = Perimeter();
00089         if(P==0.0f)     return 0.0f;
00090         return (4.0f*PI*Area()/(P*P));
00091 }
00092 
00094 
00098 
00099 void Triangle::Normal(Point& normal) const
00100 {
00101         const Point& p0 = mVerts[0];
00102         const Point& p1 = mVerts[1];
00103         const Point& p2 = mVerts[2];
00104         normal = ((p0 - p1)^(p0 - p2)).Normalize();
00105 }
00106 
00108 
00112 
00113 void Triangle::DenormalizedNormal(Point& normal) const
00114 {
00115         const Point& p0 = mVerts[0];
00116         const Point& p1 = mVerts[1];
00117         const Point& p2 = mVerts[2];
00118         normal = ((p0 - p1)^(p0 - p2));
00119 }
00120 
00122 
00126 
00127 void Triangle::Center(Point& center) const
00128 {
00129         const Point& p0 = mVerts[0];
00130         const Point& p1 = mVerts[1];
00131         const Point& p2 = mVerts[2];
00132         center = (p0 + p1 + p2)*INV3;
00133 }
00134 
00135 PartVal Triangle::TestAgainstPlane(const Plane& plane, float epsilon) const
00136 {
00137         bool Pos = false, Neg = false;
00138 
00139         // Loop through all vertices
00140         for(udword i=0;i<3;i++)
00141         {
00142                 // Compute side:
00143                 sdword Side = VPlaneSideEps(mVerts[i], plane, epsilon);
00144 
00145                                 if (Side < 0)   Neg = true;
00146                 else    if (Side > 0)   Pos = true;
00147         }
00148 
00149                         if (!Pos && !Neg)       return TRI_ON_PLANE;
00150         else    if (Pos && Neg)         return TRI_INTERSECT;
00151         else    if (Pos && !Neg)        return TRI_PLUS_SPACE;
00152         else    if (!Pos && Neg)        return TRI_MINUS_SPACE;
00153 
00154         // What?!
00155         return TRI_FORCEDWORD;
00156 }
00157 
00159 
00163 
00164 /*
00165 void Triangle::ComputeMoment(Moment& m)
00166 {
00167         // Compute the area of the triangle
00168         m.mArea = Area();
00169 
00170         // Compute the centroid
00171         Center(m.mCentroid);
00172 
00173         // Second-order components. Handle zero-area faces.
00174         Point& p = mVerts[0];
00175         Point& q = mVerts[1];
00176         Point& r = mVerts[2];
00177         if(m.mArea==0.0f)
00178         {
00179                 // This triangle has zero area. The second order components would be eliminated with the usual formula, so, for the 
00180                 // sake of robustness we use an alternative form. These are the centroid and second-order components of the triangle's vertices.
00181                 m.mCovariance.m[0][0] = (p.x*p.x + q.x*q.x + r.x*r.x);
00182                 m.mCovariance.m[0][1] = (p.x*p.y + q.x*q.y + r.x*r.y);
00183                 m.mCovariance.m[0][2] = (p.x*p.z + q.x*q.z + r.x*r.z);
00184                 m.mCovariance.m[1][1] = (p.y*p.y + q.y*q.y + r.y*r.y);
00185                 m.mCovariance.m[1][2] = (p.y*p.z + q.y*q.z + r.y*r.z);
00186                 m.mCovariance.m[2][2] = (p.z*p.z + q.z*q.z + r.z*r.z);      
00187                 m.mCovariance.m[2][1] = m.mCovariance.m[1][2];
00188                 m.mCovariance.m[1][0] = m.mCovariance.m[0][1];
00189                 m.mCovariance.m[2][0] = m.mCovariance.m[0][2];
00190         }
00191         else
00192         {
00193                 const float OneOverTwelve = 1.0f / 12.0f;
00194                 m.mCovariance.m[0][0] = m.mArea * (9.0f * m.mCentroid.x*m.mCentroid.x + p.x*p.x + q.x*q.x + r.x*r.x) * OneOverTwelve;
00195                 m.mCovariance.m[0][1] = m.mArea * (9.0f * m.mCentroid.x*m.mCentroid.y + p.x*p.y + q.x*q.y + r.x*r.y) * OneOverTwelve;
00196                 m.mCovariance.m[1][1] = m.mArea * (9.0f * m.mCentroid.y*m.mCentroid.y + p.y*p.y + q.y*q.y + r.y*r.y) * OneOverTwelve;
00197                 m.mCovariance.m[0][2] = m.mArea * (9.0f * m.mCentroid.x*m.mCentroid.z + p.x*p.z + q.x*q.z + r.x*r.z) * OneOverTwelve;
00198                 m.mCovariance.m[1][2] = m.mArea * (9.0f * m.mCentroid.y*m.mCentroid.z + p.y*p.z + q.y*q.z + r.y*r.z) * OneOverTwelve;
00199                 m.mCovariance.m[2][2] = m.mArea * (9.0f * m.mCentroid.z*m.mCentroid.z + p.z*p.z + q.z*q.z + r.z*r.z) * OneOverTwelve;
00200                 m.mCovariance.m[2][1] = m.mCovariance.m[1][2];
00201                 m.mCovariance.m[1][0] = m.mCovariance.m[0][1];
00202                 m.mCovariance.m[2][0] = m.mCovariance.m[0][2];
00203         }
00204 }
00205 */
00206 
00208 
00212 
00213 float Triangle::MinEdgeLength() const
00214 {
00215         float Min = MAX_FLOAT;
00216         float Length01 = mVerts[0].Distance(mVerts[1]);
00217         float Length02 = mVerts[0].Distance(mVerts[2]);
00218         float Length12 = mVerts[1].Distance(mVerts[2]);
00219         if(Length01 < Min)      Min = Length01;
00220         if(Length02 < Min)      Min = Length02;
00221         if(Length12 < Min)      Min = Length12;
00222         return Min;
00223 }
00224 
00226 
00230 
00231 float Triangle::MaxEdgeLength() const
00232 {
00233         float Max = MIN_FLOAT;
00234         float Length01 = mVerts[0].Distance(mVerts[1]);
00235         float Length02 = mVerts[0].Distance(mVerts[2]);
00236         float Length12 = mVerts[1].Distance(mVerts[2]);
00237         if(Length01 > Max)      Max = Length01;
00238         if(Length02 > Max)      Max = Length02;
00239         if(Length12 > Max)      Max = Length12;
00240         return Max;
00241 }
00242 
00244 
00250 
00251 void Triangle::ComputePoint(float u, float v, Point& pt, udword* nearvtx)       const
00252 {
00253         // Compute point coordinates
00254         pt = (1.0f - u - v)*mVerts[0] + u*mVerts[1] + v*mVerts[2];
00255 
00256         // Compute nearest vertex if needed
00257         if(nearvtx)
00258         {
00259                 // Compute distance vector
00260                 Point d(mVerts[0].SquareDistance(pt),   // Distance^2 from vertex 0 to point on the face
00261                                 mVerts[1].SquareDistance(pt),   // Distance^2 from vertex 1 to point on the face
00262                                 mVerts[2].SquareDistance(pt));  // Distance^2 from vertex 2 to point on the face
00263 
00264                 // Get smallest distance
00265                 *nearvtx = d.SmallestAxis();
00266         }
00267 }
00268 
00269 void Triangle::Inflate(float fat_coeff, bool constant_border)
00270 {
00271         // Compute triangle center
00272         Point TriangleCenter;
00273         Center(TriangleCenter);
00274 
00275         // Don't normalize?
00276         // Normalize => add a constant border, regardless of triangle size
00277         // Don't => add more to big triangles
00278         for(udword i=0;i<3;i++)
00279         {
00280                 Point v = mVerts[i] - TriangleCenter;
00281 
00282                 if(constant_border)     v.Normalize();
00283 
00284                 mVerts[i] += v * fat_coeff;
00285         }
00286 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17