Go to the documentation of this file.00001
00002
00008
00009
00011
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
00030 float Dist = plane.Distance(v);
00031
00032
00033
00034
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
00140 for(udword i=0;i<3;i++)
00141 {
00142
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
00155 return TRI_FORCEDWORD;
00156 }
00157
00159
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
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
00254 pt = (1.0f - u - v)*mVerts[0] + u*mVerts[1] + v*mVerts[2];
00255
00256
00257 if(nearvtx)
00258 {
00259
00260 Point d(mVerts[0].SquareDistance(pt),
00261 mVerts[1].SquareDistance(pt),
00262 mVerts[2].SquareDistance(pt));
00263
00264
00265 *nearvtx = d.SmallestAxis();
00266 }
00267 }
00268
00269 void Triangle::Inflate(float fat_coeff, bool constant_border)
00270 {
00271
00272 Point TriangleCenter;
00273 Center(TriangleCenter);
00274
00275
00276
00277
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 }