Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Opcode::IndexedTriangle Class Reference

#include <Opcode.h>

Public Member Functions

inline_ IndexedTriangle ()
 Constructor. More...
 
inline_ IndexedTriangle (udword r0, udword r1, udword r2)
 Constructor. More...
 
inline_ IndexedTriangle (const IndexedTriangle &triangle)
 Copy constructor. More...
 
inline_ ~IndexedTriangle ()
 Destructor. More...
 
void Flip ()
 
float Area (const Point *verts) const
 
float Perimeter (const Point *verts) const
 
float Compacity (const Point *verts) const
 
void Normal (const Point *verts, Point &normal) const
 
void DenormalizedNormal (const Point *verts, Point &normal) const
 
void Center (const Point *verts, Point &center) const
 
void CenteredNormal (const Point *verts, Point &normal) const
 
void RandomPoint (const Point *verts, Point &random) const
 
bool IsVisible (const Point *verts, const Point &source) const
 
bool BackfaceCulling (const Point *verts, const Point &source) const
 
float ComputeOcclusionPotential (const Point *verts, const Point &view) const
 
bool ReplaceVertex (udword oldref, udword newref)
 
bool IsDegenerate () const
 
bool HasVertex (udword ref) const
 
bool HasVertex (udword ref, udword *index) const
 
ubyte FindEdge (udword vref0, udword vref1) const
 
udword OppositeVertex (udword vref0, udword vref1) const
 
inline_ udword OppositeVertex (ubyte edgenb) const
 
void GetVRefs (ubyte edgenb, udword &vref0, udword &vref1, udword &vref2) const
 
float MinEdgeLength (const Point *verts) const
 
float MaxEdgeLength (const Point *verts) const
 
void ComputePoint (const Point *verts, float u, float v, Point &pt, udword *nearvtx=null) const
 
float Angle (const IndexedTriangle &tri, const Point *verts) const
 
inline_ Plane PlaneEquation (const Point *verts) const
 
bool Equal (const IndexedTriangle &tri) const
 

Public Attributes

udword mVRef [3]
 Vertex-references. More...
 

Detailed Description

Definition at line 17 of file Opcode.h.

Constructor & Destructor Documentation

inline_ Opcode::IndexedTriangle::IndexedTriangle ( )
inline

Constructor.

Definition at line 21 of file Opcode.h.

39 {
inline_ Opcode::IndexedTriangle::IndexedTriangle ( udword  r0,
udword  r1,
udword  r2 
)
inline

Constructor.

Definition at line 23 of file Opcode.h.

39 {
inline_ Opcode::IndexedTriangle::IndexedTriangle ( const IndexedTriangle triangle)
inline

Copy constructor.

Definition at line 25 of file Opcode.h.

39  {
inline_ Opcode::IndexedTriangle::~IndexedTriangle ( )
inline

Destructor.

Definition at line 32 of file Opcode.h.

39 {

Member Function Documentation

float IndexedTriangle::Angle ( const IndexedTriangle tri,
const Point verts 
) const

Computes the angle between two triangles.

Parameters
tri[in] the other triangle
verts[in] the list of indexed vertices
Returns
the angle in radians

Definition at line 517 of file IceIndexedTriangle.cpp.

518 {
519  // Checkings
520  if(!verts) return 0.0f;
521 
522  // Compute face normals
523  Point n0, n1;
524  Normal(verts, n0);
525  tri.Normal(verts, n1);
526 
527  // Compute angle
528  float dp = n0|n1;
529  if(dp>1.0f) return 0.0f;
530  if(dp<-1.0f) return PI;
531  return acosf(dp);
532 
533 // return ::Angle(n0,n1);
534 }
float IndexedTriangle::Area ( const Point verts) const

Computes the triangle area.

Parameters
verts[in] the list of indexed vertices
Returns
the area

Definition at line 45 of file IceIndexedTriangle.cpp.

46 {
47  if(!verts) return 0.0f;
48  const Point& p0 = verts[0];
49  const Point& p1 = verts[1];
50  const Point& p2 = verts[2];
51  return ((p0-p1)^(p0-p2)).Magnitude() * 0.5f;
52 }
bool IndexedTriangle::BackfaceCulling ( const Point verts,
const Point source 
) const

Computes backface culling.

Parameters
verts[in] the list of indexed vertices
source[in] source point (in local space) from which culling must be computed
Returns
true if the triangle is visible from the source point

Definition at line 218 of file IceIndexedTriangle.cpp.

219 {
220  // Checkings
221  if(!verts) return false;
222 
223  const Point& p0 = verts[mVRef[0]];
224  const Point& p1 = verts[mVRef[1]];
225  const Point& p2 = verts[mVRef[2]];
226 
227  // Compute base
228 // Point Base = (p0 + p1 + p2)*INV3;
229 
230  // Compute denormalized normal
231  Point Normal = (p2 - p1)^(p0 - p1);
232 
233  // Backface culling
234 // return (Normal | (source - Base)) >= 0.0f;
235  return (Normal | (source - p0)) >= 0.0f;
236 
237 // Same as: (but a bit faster)
238 // Plane PL(verts[mVRef[0]], verts[mVRef[1]], verts[mVRef[2]]);
239 // return PL.Distance(source)>0.0f;
240 }
void IndexedTriangle::Center ( const Point verts,
Point center 
) const

Computes the triangle center.

Parameters
verts[in] the list of indexed vertices
center[out] the computed center

Definition at line 128 of file IceIndexedTriangle.cpp.

129 {
130  if(!verts) return;
131 
132  const Point& p0 = verts[mVRef[0]];
133  const Point& p1 = verts[mVRef[1]];
134  const Point& p2 = verts[mVRef[2]];
135  center = (p0+p1+p2)*INV3;
136 }
void IndexedTriangle::CenteredNormal ( const Point verts,
Point normal 
) const

Computes the centered normal

Parameters
verts[in] the list of indexed vertices
normal[out] the computed centered normal

Definition at line 145 of file IceIndexedTriangle.cpp.

146 {
147  if(!verts) return;
148 
149  const Point& p0 = verts[mVRef[0]];
150  const Point& p1 = verts[mVRef[1]];
151  const Point& p2 = verts[mVRef[2]];
152  Point Center = (p0+p1+p2)*INV3;
153  normal = Center + ((p2-p1)^(p0-p1)).Normalize();
154 }
float IndexedTriangle::Compacity ( const Point verts) const

Computes the triangle compacity.

Parameters
verts[in] the list of indexed vertices
Returns
the compacity

Definition at line 79 of file IceIndexedTriangle.cpp.

80 {
81  if(!verts) return 0.0f;
82  float P = Perimeter(verts);
83  if(P==0.0f) return 0.0f;
84  return (4.0f*PI*Area(verts)/(P*P));
85 }
float IndexedTriangle::ComputeOcclusionPotential ( const Point verts,
const Point view 
) const

Computes the occlusion potential of the triangle.

Parameters
verts[in] the list of indexed vertices
source[in] source point (in local space) from which occlusion potential must be computed
Returns
the occlusion potential

Definition at line 250 of file IceIndexedTriangle.cpp.

251 {
252  if(!verts) return 0.0f;
253  // Occlusion potential: -(A * (N|V) / d^2)
254  // A = polygon area
255  // N = polygon normal
256  // V = view vector
257  // d = distance viewpoint-center of polygon
258 
259  float A = Area(verts);
260  Point N; Normal(verts, N);
261  Point C; Center(verts, C);
262  float d = view.Distance(C);
263  return -(A*(N|view))/(d*d);
264 }
void IndexedTriangle::ComputePoint ( const Point verts,
float  u,
float  v,
Point pt,
udword nearvtx = null 
) const

Computes a point on the triangle according to the stabbing information.

Parameters
verts[in] the list of indexed vertices
u,v[in] point's barycentric coordinates
pt[out] point on triangle
nearvtx[out] index of nearest vertex

Definition at line 451 of file IceIndexedTriangle.cpp.

452 {
453  // Checkings
454  if(!verts) return;
455 
456  // Get face in local or global space
457  const Point& p0 = verts[mVRef[0]];
458  const Point& p1 = verts[mVRef[1]];
459  const Point& p2 = verts[mVRef[2]];
460 
461  // Compute point coordinates
462  pt = (1.0f - u - v)*p0 + u*p1 + v*p2;
463 
464  // Compute nearest vertex if needed
465  if(nearvtx)
466  {
467  // Compute distance vector
468  Point d(p0.SquareDistance(pt), // Distance^2 from vertex 0 to point on the face
469  p1.SquareDistance(pt), // Distance^2 from vertex 1 to point on the face
470  p2.SquareDistance(pt)); // Distance^2 from vertex 2 to point on the face
471 
472  // Get smallest distance
473  *nearvtx = mVRef[d.SmallestAxis()];
474  }
475 }
void IndexedTriangle::DenormalizedNormal ( const Point verts,
Point normal 
) const

Computes the triangle denormalized normal.

Parameters
verts[in] the list of indexed vertices
normal[out] the computed normal

Definition at line 111 of file IceIndexedTriangle.cpp.

112 {
113  if(!verts) return;
114 
115  const Point& p0 = verts[mVRef[0]];
116  const Point& p1 = verts[mVRef[1]];
117  const Point& p2 = verts[mVRef[2]];
118  normal = ((p2-p1)^(p0-p1));
119 }
bool IndexedTriangle::Equal ( const IndexedTriangle tri) const

Checks a triangle is the same as another one.

Parameters
tri[in] the other triangle
Returns
true if same triangle

Definition at line 543 of file IceIndexedTriangle.cpp.

544 {
545  // Test all vertex references
546  return (HasVertex(tri.mVRef[0]) &&
547  HasVertex(tri.mVRef[1]) &&
548  HasVertex(tri.mVRef[2]));
549 }
ubyte IndexedTriangle::FindEdge ( udword  vref0,
udword  vref1 
) const

Finds an edge in a tri, given two vertex references.

Parameters
vref0[in] the edge's first vertex reference
vref1[in] the edge's second vertex reference
Returns
the edge number between 0 and 2, or 0xff if input refs are wrong.

Definition at line 335 of file IceIndexedTriangle.cpp.

336 {
337  if(mVRef[0]==vref0 && mVRef[1]==vref1) return 0;
338  else if(mVRef[0]==vref1 && mVRef[1]==vref0) return 0;
339  else if(mVRef[0]==vref0 && mVRef[2]==vref1) return 1;
340  else if(mVRef[0]==vref1 && mVRef[2]==vref0) return 1;
341  else if(mVRef[1]==vref0 && mVRef[2]==vref1) return 2;
342  else if(mVRef[1]==vref1 && mVRef[2]==vref0) return 2;
343  return 0xff;
344 }
void IndexedTriangle::Flip ( )

Flips the winding order.

Definition at line 33 of file IceIndexedTriangle.cpp.

34 {
35  Swap(mVRef[1], mVRef[2]);
36 }
void IndexedTriangle::GetVRefs ( ubyte  edgenb,
udword vref0,
udword vref1,
udword vref2 
) const

Gets the three sorted vertex references according to an edge number. edgenb = 0 => edge 0-1, returns references 0, 1, 2 edgenb = 1 => edge 0-2, returns references 0, 2, 1 edgenb = 2 => edge 1-2, returns references 1, 2, 0

Parameters
edgenb[in] the edge number, 0, 1 or 2
vref0[out] the returned first vertex reference
vref1[out] the returned second vertex reference
vref2[out] the returned third vertex reference

Definition at line 378 of file IceIndexedTriangle.cpp.

379 {
380  if(edgenb==0)
381  {
382  vref0 = mVRef[0];
383  vref1 = mVRef[1];
384  vref2 = mVRef[2];
385  }
386  else if(edgenb==1)
387  {
388  vref0 = mVRef[0];
389  vref1 = mVRef[2];
390  vref2 = mVRef[1];
391  }
392  else if(edgenb==2)
393  {
394  vref0 = mVRef[1];
395  vref1 = mVRef[2];
396  vref2 = mVRef[0];
397  }
398 }
bool IndexedTriangle::HasVertex ( udword  ref) const

Checks whether the input vertex reference belongs to the triangle or not.

Parameters
ref[in] the vertex reference to look for
Returns
true if the triangle contains the vertex reference

Definition at line 303 of file IceIndexedTriangle.cpp.

304 {
305  if(mVRef[0]==ref) return true;
306  if(mVRef[1]==ref) return true;
307  if(mVRef[2]==ref) return true;
308  return false;
309 }
bool IndexedTriangle::HasVertex ( udword  ref,
udword index 
) const

Checks whether the input vertex reference belongs to the triangle or not.

Parameters
ref[in] the vertex reference to look for
index[out] the corresponding index in the triangle
Returns
true if the triangle contains the vertex reference

Definition at line 319 of file IceIndexedTriangle.cpp.

320 {
321  if(mVRef[0]==ref) { *index = 0; return true; }
322  if(mVRef[1]==ref) { *index = 1; return true; }
323  if(mVRef[2]==ref) { *index = 2; return true; }
324  return false;
325 }
bool IndexedTriangle::IsDegenerate ( ) const

Checks whether the triangle is degenerate or not. A degenerate triangle has two common vertex references. This is a zero-area triangle.

Returns
true if the triangle is degenerate

Definition at line 288 of file IceIndexedTriangle.cpp.

289 {
290  if(mVRef[0]==mVRef[1]) return true;
291  if(mVRef[1]==mVRef[2]) return true;
292  if(mVRef[2]==mVRef[0]) return true;
293  return false;
294 }
bool IndexedTriangle::IsVisible ( const Point verts,
const Point source 
) const

Computes backface culling.

Parameters
verts[in] the list of indexed vertices
source[in] source point (in local space) from which culling must be computed
Returns
true if the triangle is visible from the source point

Definition at line 190 of file IceIndexedTriangle.cpp.

191 {
192  // Checkings
193  if(!verts) return false;
194 
195  const Point& p0 = verts[mVRef[0]];
196  const Point& p1 = verts[mVRef[1]];
197  const Point& p2 = verts[mVRef[2]];
198 
199  // Compute denormalized normal
200  Point Normal = (p2 - p1)^(p0 - p1);
201 
202  // Backface culling
203  return (Normal | source) >= 0.0f;
204 
205 // Same as:
206 // Plane PL(verts[mVRef[0]], verts[mVRef[1]], verts[mVRef[2]]);
207 // return PL.Distance(source) > PL.d;
208 }
float IndexedTriangle::MaxEdgeLength ( const Point verts) const

Computes the triangle's largest edge length.

Parameters
verts[in] the list of indexed vertices
Returns
the largest edge length

Definition at line 428 of file IceIndexedTriangle.cpp.

429 {
430  if(!verts) return 0.0f;
431 
432  float Max = MIN_FLOAT;
433  float Length01 = verts[0].Distance(verts[1]);
434  float Length02 = verts[0].Distance(verts[2]);
435  float Length12 = verts[1].Distance(verts[2]);
436  if(Length01 > Max) Max = Length01;
437  if(Length02 > Max) Max = Length02;
438  if(Length12 > Max) Max = Length12;
439  return Max;
440 }
float IndexedTriangle::MinEdgeLength ( const Point verts) const

Computes the triangle's smallest edge length.

Parameters
verts[in] the list of indexed vertices
Returns
the smallest edge length

Definition at line 407 of file IceIndexedTriangle.cpp.

408 {
409  if(!verts) return 0.0f;
410 
411  float Min = MAX_FLOAT;
412  float Length01 = verts[0].Distance(verts[1]);
413  float Length02 = verts[0].Distance(verts[2]);
414  float Length12 = verts[1].Distance(verts[2]);
415  if(Length01 < Min) Min = Length01;
416  if(Length02 < Min) Min = Length02;
417  if(Length12 < Min) Min = Length12;
418  return Min;
419 }
void IndexedTriangle::Normal ( const Point verts,
Point normal 
) const

Computes the triangle normal.

Parameters
verts[in] the list of indexed vertices
normal[out] the computed normal

Definition at line 94 of file IceIndexedTriangle.cpp.

95 {
96  if(!verts) return;
97 
98  const Point& p0 = verts[mVRef[0]];
99  const Point& p1 = verts[mVRef[1]];
100  const Point& p2 = verts[mVRef[2]];
101  normal = ((p2-p1)^(p0-p1)).Normalize();
102 }
udword IndexedTriangle::OppositeVertex ( udword  vref0,
udword  vref1 
) const

Gets the last reference given the first two.

Parameters
vref0[in] the first vertex reference
vref1[in] the second vertex reference
Returns
the last reference, or INVALID_ID if input refs are wrong.

Definition at line 354 of file IceIndexedTriangle.cpp.

355 {
356  if(mVRef[0]==vref0 && mVRef[1]==vref1) return mVRef[2];
357  else if(mVRef[0]==vref1 && mVRef[1]==vref0) return mVRef[2];
358  else if(mVRef[0]==vref0 && mVRef[2]==vref1) return mVRef[1];
359  else if(mVRef[0]==vref1 && mVRef[2]==vref0) return mVRef[1];
360  else if(mVRef[1]==vref0 && mVRef[2]==vref1) return mVRef[0];
361  else if(mVRef[1]==vref1 && mVRef[2]==vref0) return mVRef[0];
362  return INVALID_ID;
363 }
inline_ udword Opcode::IndexedTriangle::OppositeVertex ( ubyte  edgenb) const
inline

Definition at line 55 of file Opcode.h.

float IndexedTriangle::Perimeter ( const Point verts) const

Computes the triangle perimeter.

Parameters
verts[in] the list of indexed vertices
Returns
the perimeter

Definition at line 61 of file IceIndexedTriangle.cpp.

62 {
63  if(!verts) return 0.0f;
64  const Point& p0 = verts[0];
65  const Point& p1 = verts[1];
66  const Point& p2 = verts[2];
67  return p0.Distance(p1)
68  + p0.Distance(p2)
69  + p1.Distance(p2);
70 }
inline_ Plane Opcode::IndexedTriangle::PlaneEquation ( const Point verts) const
inline

Definition at line 61 of file Opcode.h.

void IndexedTriangle::RandomPoint ( const Point verts,
Point random 
) const

Computes a random point within the triangle.

Parameters
verts[in] the list of indexed vertices
normal[out] the computed centered normal

Definition at line 163 of file IceIndexedTriangle.cpp.

164 {
165  if(!verts) return;
166 
167  // Random barycentric coords
168  float Alpha = UnitRandomFloat();
169  float Beta = UnitRandomFloat();
170  float Gamma = UnitRandomFloat();
171  float OneOverTotal = 1.0f / (Alpha + Beta + Gamma);
172  Alpha *= OneOverTotal;
173  Beta *= OneOverTotal;
174  Gamma *= OneOverTotal;
175 
176  const Point& p0 = verts[mVRef[0]];
177  const Point& p1 = verts[mVRef[1]];
178  const Point& p2 = verts[mVRef[2]];
179  random = Alpha*p0 + Beta*p1 + Gamma*p2;
180 }
bool IndexedTriangle::ReplaceVertex ( udword  oldref,
udword  newref 
)

Replaces a vertex reference with another one.

Parameters
oldref[in] the vertex reference to replace
newref[in] the new vertex reference
Returns
true if success, else false if the input vertex reference doesn't belong to the triangle

Definition at line 274 of file IceIndexedTriangle.cpp.

275 {
276  if(mVRef[0]==oldref) { mVRef[0] = newref; return true; }
277  else if(mVRef[1]==oldref) { mVRef[1] = newref; return true; }
278  else if(mVRef[2]==oldref) { mVRef[2] = newref; return true; }
279  return false;
280 }

Member Data Documentation

udword Opcode::IndexedTriangle::mVRef[3]

Vertex-references.

Definition at line 34 of file Opcode.h.


The documentation for this class was generated from the following files: