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
OPC_TreeBuilders.cpp
Go to the documentation of this file.
1 /*
3  * OPCODE - Optimized Collision Detection
4  * Copyright (C) 2001 Pierre Terdiman
5  * Homepage: http://www.codercorner.com/Opcode.htm
6  */
8 
10 
16 
19 
27 
30 
38 
41 
49 
52 // Precompiled Header
53 #include "Stdafx.h"
54 
55 
56 using namespace Opcode;
57 
59 
66 bool AABBTreeOfAABBsBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
68 {
69  // Checkings
70  if(!primitives || !nb_prims) return false;
71 
72  // Initialize global box
73  global_box = mAABBArray[primitives[0]];
74 
75  // Loop through boxes
76  for(udword i=1;i<nb_prims;i++)
77  {
78  // Update global box
79  global_box.Add(mAABBArray[primitives[i]]);
80  }
81  return true;
82 }
83 
85 
93 {
94  // For an AABB, the splitting value is the middle of the given axis,
95  // i.e. the corresponding component of the center point
96  return mAABBArray[index].GetCenter(axis);
97 }
98 
100 
107 bool AABBTreeOfTrianglesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
109 {
110  // Checkings
111  if(!primitives || !nb_prims) return false;
112 
113  // Initialize global box
116 
117  // Loop through triangles
118  VertexPointers VP;
119  while(nb_prims--)
120  {
121  // Get current triangle-vertices
122  mIMesh->GetTriangle(VP, *primitives++);
123  // Update global box
124  Min.Min(*VP.Vertex[0]).Min(*VP.Vertex[1]).Min(*VP.Vertex[2]);
125  Max.Max(*VP.Vertex[0]).Max(*VP.Vertex[1]).Max(*VP.Vertex[2]);
126  }
127  global_box.SetMinMax(Min, Max);
128  return true;
129 }
130 
132 
140 {
141 /* // Compute center of triangle
142  Point Center;
143  mTriList[index].Center(mVerts, Center);
144  // Return value
145  return Center[axis];*/
146 
147  // Compute correct component from center of triangle
148 // return (mVerts[mTriList[index].mVRef[0]][axis]
149 // +mVerts[mTriList[index].mVRef[1]][axis]
150 // +mVerts[mTriList[index].mVRef[2]][axis])*INV3;
151 
152  VertexPointers VP;
153  mIMesh->GetTriangle(VP, index);
154 
155  // Compute correct component from center of triangle
156  return ((*VP.Vertex[0])[axis]
157  +(*VP.Vertex[1])[axis]
158  +(*VP.Vertex[2])[axis])*INV3;
159 }
160 
162 
170 float AABBTreeOfTrianglesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
172 {
174  {
175  // Loop through triangles
176  float SplitValue = 0.0f;
177  VertexPointers VP;
178  for(udword i=0;i<nb_prims;i++)
179  {
180  // Get current triangle-vertices
181  mIMesh->GetTriangle(VP, primitives[i]);
182  // Update split value
183  SplitValue += (*VP.Vertex[0])[axis];
184  SplitValue += (*VP.Vertex[1])[axis];
185  SplitValue += (*VP.Vertex[2])[axis];
186  }
187  return SplitValue / float(nb_prims*3);
188  }
189  else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
190 }
191 
193 
200 bool AABBTreeOfVerticesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
202 {
203  // Checkings
204  if(!primitives || !nb_prims) return false;
205 
206  // Initialize global box
207  global_box.SetEmpty();
208 
209  // Loop through vertices
210  for(udword i=0;i<nb_prims;i++)
211  {
212  // Update global box
213  global_box.Extend(mVertexArray[primitives[i]]);
214  }
215  return true;
216 }
217 
219 
227 {
228  // For a vertex, the splitting value is simply the vertex coordinate.
229  return mVertexArray[index][axis];
230 }
231 
233 
241 float AABBTreeOfVerticesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
243 {
245  {
246  // Loop through vertices
247  float SplitValue = 0.0f;
248  for(udword i=0;i<nb_prims;i++)
249  {
250  // Update split value
251  SplitValue += mVertexArray[primitives[i]][axis];
252  }
253  return SplitValue / float(nb_prims);
254  }
255  else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
256 }
257