OPC_TreeBuilders.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // Precompiled Header
  48. #include "Stdafx.h"
  49. using namespace Opcode;
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. /**
  52. * Computes the AABB of a set of primitives.
  53. * \param primitives [in] list of indices of primitives
  54. * \param nb_prims [in] number of indices
  55. * \param global_box [out] global AABB enclosing the set of input primitives
  56. * \return true if success
  57. */
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. bool AABBTreeOfAABBsBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  60. {
  61. // Checkings
  62. if(!primitives || !nb_prims) return false;
  63. // Initialize global box
  64. global_box = mAABBArray[primitives[0]];
  65. // Loop through boxes
  66. for(udword i=1;i<nb_prims;i++)
  67. {
  68. // Update global box
  69. global_box.Add(mAABBArray[primitives[i]]);
  70. }
  71. return true;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /**
  75. * Computes the splitting value along a given axis for a given primitive.
  76. * \param index [in] index of the primitive to split
  77. * \param axis [in] axis index (0,1,2)
  78. * \return splitting value
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. float AABBTreeOfAABBsBuilder::GetSplittingValue(udword index, udword axis) const
  82. {
  83. // For an AABB, the splitting value is the middle of the given axis,
  84. // i.e. the corresponding component of the center point
  85. return mAABBArray[index].GetCenter(axis);
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. /**
  89. * Computes the AABB of a set of primitives.
  90. * \param primitives [in] list of indices of primitives
  91. * \param nb_prims [in] number of indices
  92. * \param global_box [out] global AABB enclosing the set of input primitives
  93. * \return true if success
  94. */
  95. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  96. bool AABBTreeOfTrianglesBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  97. {
  98. // Checkings
  99. if(!primitives || !nb_prims) return false;
  100. // Initialize global box
  101. Point Min(MAX_FLOAT, MAX_FLOAT, MAX_FLOAT);
  102. Point Max(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
  103. // Loop through triangles
  104. VertexPointers VP;
  105. ConversionArea VC;
  106. while(nb_prims--)
  107. {
  108. // Get current triangle-vertices
  109. mIMesh->GetTriangle(VP, *primitives++, VC);
  110. // Update global box
  111. Min.Min(*VP.Vertex[0]).Min(*VP.Vertex[1]).Min(*VP.Vertex[2]);
  112. Max.Max(*VP.Vertex[0]).Max(*VP.Vertex[1]).Max(*VP.Vertex[2]);
  113. }
  114. global_box.SetMinMax(Min, Max);
  115. return true;
  116. }
  117. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. /**
  119. * Computes the splitting value along a given axis for a given primitive.
  120. * \param index [in] index of the primitive to split
  121. * \param axis [in] axis index (0,1,2)
  122. * \return splitting value
  123. */
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. float AABBTreeOfTrianglesBuilder::GetSplittingValue(udword index, udword axis) const
  126. {
  127. /* // Compute center of triangle
  128. Point Center;
  129. mTriList[index].Center(mVerts, Center);
  130. // Return value
  131. return Center[axis];*/
  132. // Compute correct component from center of triangle
  133. // return (mVerts[mTriList[index].mVRef[0]][axis]
  134. // +mVerts[mTriList[index].mVRef[1]][axis]
  135. // +mVerts[mTriList[index].mVRef[2]][axis])*INV3;
  136. VertexPointers VP;
  137. ConversionArea VC;
  138. mIMesh->GetTriangle(VP, index, VC);
  139. // Compute correct component from center of triangle
  140. return ((*VP.Vertex[0])[axis]
  141. +(*VP.Vertex[1])[axis]
  142. +(*VP.Vertex[2])[axis])*INV3;
  143. }
  144. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  145. /**
  146. * Computes the splitting value along a given axis for a given node.
  147. * \param primitives [in] list of indices of primitives
  148. * \param nb_prims [in] number of indices
  149. * \param global_box [in] global AABB enclosing the set of input primitives
  150. * \param axis [in] axis index (0,1,2)
  151. * \return splitting value
  152. */
  153. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. float AABBTreeOfTrianglesBuilder::GetSplittingValue(const dTriIndex* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  155. {
  156. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  157. {
  158. // Loop through triangles
  159. float SplitValue = 0.0f;
  160. VertexPointers VP;
  161. ConversionArea VC;
  162. for(udword i=0;i<nb_prims;i++)
  163. {
  164. // Get current triangle-vertices
  165. mIMesh->GetTriangle(VP, primitives[i], VC);
  166. // Update split value
  167. SplitValue += (*VP.Vertex[0])[axis];
  168. SplitValue += (*VP.Vertex[1])[axis];
  169. SplitValue += (*VP.Vertex[2])[axis];
  170. }
  171. return SplitValue / float(nb_prims*3);
  172. }
  173. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  176. /**
  177. * Computes the AABB of a set of primitives.
  178. * \param primitives [in] list of indices of primitives
  179. * \param nb_prims [in] number of indices
  180. * \param global_box [out] global AABB enclosing the set of input primitives
  181. * \return true if success
  182. */
  183. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  184. bool AABBTreeOfVerticesBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  185. {
  186. // Checkings
  187. if(!primitives || !nb_prims) return false;
  188. // Initialize global box
  189. global_box.SetEmpty();
  190. // Loop through vertices
  191. for(udword i=0;i<nb_prims;i++)
  192. {
  193. // Update global box
  194. global_box.Extend(mVertexArray[primitives[i]]);
  195. }
  196. return true;
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. /**
  200. * Computes the splitting value along a given axis for a given primitive.
  201. * \param index [in] index of the primitive to split
  202. * \param axis [in] axis index (0,1,2)
  203. * \return splitting value
  204. */
  205. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  206. float AABBTreeOfVerticesBuilder::GetSplittingValue(udword index, udword axis) const
  207. {
  208. // For a vertex, the splitting value is simply the vertex coordinate.
  209. return mVertexArray[index][axis];
  210. }
  211. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. /**
  213. * Computes the splitting value along a given axis for a given node.
  214. * \param primitives [in] list of indices of primitives
  215. * \param nb_prims [in] number of indices
  216. * \param global_box [in] global AABB enclosing the set of input primitives
  217. * \param axis [in] axis index (0,1,2)
  218. * \return splitting value
  219. */
  220. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. float AABBTreeOfVerticesBuilder::GetSplittingValue(const dTriIndex* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  222. {
  223. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  224. {
  225. // Loop through vertices
  226. float SplitValue = 0.0f;
  227. for(udword i=0;i<nb_prims;i++)
  228. {
  229. // Update split value
  230. SplitValue += mVertexArray[primitives[i]][axis];
  231. }
  232. return SplitValue / float(nb_prims);
  233. }
  234. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  235. }