OPC_TreeBuilders.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains code for tree builders.
  11. * \file OPC_TreeBuilders.cpp
  12. * \author Pierre Terdiman
  13. * \date March, 20, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * A builder for AABB-trees of vertices.
  19. *
  20. * \class AABBTreeOfVerticesBuilder
  21. * \author Pierre Terdiman
  22. * \version 1.3
  23. * \date March, 20, 2001
  24. */
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. /**
  28. * A builder for AABB-trees of AABBs.
  29. *
  30. * \class AABBTreeOfAABBsBuilder
  31. * \author Pierre Terdiman
  32. * \version 1.3
  33. * \date March, 20, 2001
  34. */
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. /**
  38. * A builder for AABB-trees of triangles.
  39. *
  40. * \class AABBTreeOfTrianglesBuilder
  41. * \author Pierre Terdiman
  42. * \version 1.3
  43. * \date March, 20, 2001
  44. */
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. #include "Opcode.h"
  48. using namespace Opcode;
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /**
  51. * Computes the AABB of a set of primitives.
  52. * \param primitives [in] list of indices of primitives
  53. * \param nb_prims [in] number of indices
  54. * \param global_box [out] global AABB enclosing the set of input primitives
  55. * \return true if success
  56. */
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. bool AABBTreeOfAABBsBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
  59. {
  60. // Checkings
  61. if(!primitives || !nb_prims) return false;
  62. // Initialize global box
  63. global_box = mAABBArray[primitives[0]];
  64. // Loop through boxes
  65. for(udword i=1;i<nb_prims;i++)
  66. {
  67. // Update global box
  68. global_box.Add(mAABBArray[primitives[i]]);
  69. }
  70. return true;
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. /**
  74. * Computes the splitting value along a given axis for a given primitive.
  75. * \param index [in] index of the primitive to split
  76. * \param axis [in] axis index (0,1,2)
  77. * \return splitting value
  78. */
  79. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. float AABBTreeOfAABBsBuilder::GetSplittingValue(udword index, udword axis) const
  81. {
  82. // For an AABB, the splitting value is the middle of the given axis,
  83. // i.e. the corresponding component of the center point
  84. return mAABBArray[index].GetCenter(axis);
  85. }
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. /**
  88. * Computes the AABB of a set of primitives.
  89. * \param primitives [in] list of indices of primitives
  90. * \param nb_prims [in] number of indices
  91. * \param global_box [out] global AABB enclosing the set of input primitives
  92. * \return true if success
  93. */
  94. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. bool AABBTreeOfTrianglesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
  96. {
  97. // Checkings
  98. if(!primitives || !nb_prims) return false;
  99. // Initialize global box
  100. Point Min(MAX_FLOAT, MAX_FLOAT, MAX_FLOAT);
  101. Point Max(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
  102. // Loop through triangles
  103. VertexPointers VP;
  104. while(nb_prims--)
  105. {
  106. // Get current triangle-vertices
  107. mIMesh->GetTriangle(VP, *primitives++);
  108. // Update global box
  109. Min.Min(*VP.Vertex[0]).Min(*VP.Vertex[1]).Min(*VP.Vertex[2]);
  110. Max.Max(*VP.Vertex[0]).Max(*VP.Vertex[1]).Max(*VP.Vertex[2]);
  111. }
  112. global_box.SetMinMax(Min, Max);
  113. return true;
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. /**
  117. * Computes the splitting value along a given axis for a given primitive.
  118. * \param index [in] index of the primitive to split
  119. * \param axis [in] axis index (0,1,2)
  120. * \return splitting value
  121. */
  122. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123. float AABBTreeOfTrianglesBuilder::GetSplittingValue(udword index, udword axis) const
  124. {
  125. /* // Compute center of triangle
  126. Point Center;
  127. mTriList[index].Center(mVerts, Center);
  128. // Return value
  129. return Center[axis];*/
  130. // Compute correct component from center of triangle
  131. // return (mVerts[mTriList[index].mVRef[0]][axis]
  132. // +mVerts[mTriList[index].mVRef[1]][axis]
  133. // +mVerts[mTriList[index].mVRef[2]][axis])*INV3;
  134. VertexPointers VP;
  135. mIMesh->GetTriangle(VP, index);
  136. // Compute correct component from center of triangle
  137. return ((*VP.Vertex[0])[axis]
  138. +(*VP.Vertex[1])[axis]
  139. +(*VP.Vertex[2])[axis])*INV3;
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. /**
  143. * Computes the splitting value along a given axis for a given node.
  144. * \param primitives [in] list of indices of primitives
  145. * \param nb_prims [in] number of indices
  146. * \param global_box [in] global AABB enclosing the set of input primitives
  147. * \param axis [in] axis index (0,1,2)
  148. * \return splitting value
  149. */
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. float AABBTreeOfTrianglesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  152. {
  153. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  154. {
  155. // Loop through triangles
  156. float SplitValue = 0.0f;
  157. VertexPointers VP;
  158. for(udword i=0;i<nb_prims;i++)
  159. {
  160. // Get current triangle-vertices
  161. mIMesh->GetTriangle(VP, primitives[i]);
  162. // Update split value
  163. SplitValue += (*VP.Vertex[0])[axis];
  164. SplitValue += (*VP.Vertex[1])[axis];
  165. SplitValue += (*VP.Vertex[2])[axis];
  166. }
  167. return SplitValue / float(nb_prims*3);
  168. }
  169. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  170. }
  171. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  172. /**
  173. * Computes the AABB of a set of primitives.
  174. * \param primitives [in] list of indices of primitives
  175. * \param nb_prims [in] number of indices
  176. * \param global_box [out] global AABB enclosing the set of input primitives
  177. * \return true if success
  178. */
  179. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  180. bool AABBTreeOfVerticesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
  181. {
  182. // Checkings
  183. if(!primitives || !nb_prims) return false;
  184. // Initialize global box
  185. global_box.SetEmpty();
  186. // Loop through vertices
  187. for(udword i=0;i<nb_prims;i++)
  188. {
  189. // Update global box
  190. global_box.Extend(mVertexArray[primitives[i]]);
  191. }
  192. return true;
  193. }
  194. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  195. /**
  196. * Computes the splitting value along a given axis for a given primitive.
  197. * \param index [in] index of the primitive to split
  198. * \param axis [in] axis index (0,1,2)
  199. * \return splitting value
  200. */
  201. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  202. float AABBTreeOfVerticesBuilder::GetSplittingValue(udword index, udword axis) const
  203. {
  204. // For a vertex, the splitting value is simply the vertex coordinate.
  205. return mVertexArray[index][axis];
  206. }
  207. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  208. /**
  209. * Computes the splitting value along a given axis for a given node.
  210. * \param primitives [in] list of indices of primitives
  211. * \param nb_prims [in] number of indices
  212. * \param global_box [in] global AABB enclosing the set of input primitives
  213. * \param axis [in] axis index (0,1,2)
  214. * \return splitting value
  215. */
  216. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  217. float AABBTreeOfVerticesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  218. {
  219. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  220. {
  221. // Loop through vertices
  222. float SplitValue = 0.0f;
  223. for(udword i=0;i<nb_prims;i++)
  224. {
  225. // Update split value
  226. SplitValue += mVertexArray[primitives[i]][axis];
  227. }
  228. return SplitValue / float(nb_prims);
  229. }
  230. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  231. }