b3StridingMeshInterface.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "b3StridingMeshInterface.h"
  14. b3StridingMeshInterface::~b3StridingMeshInterface()
  15. {
  16. }
  17. void b3StridingMeshInterface::InternalProcessAllTriangles(b3InternalTriangleIndexCallback* callback, const b3Vector3& aabbMin, const b3Vector3& aabbMax) const
  18. {
  19. (void)aabbMin;
  20. (void)aabbMax;
  21. int numtotalphysicsverts = 0;
  22. int part, graphicssubparts = getNumSubParts();
  23. const unsigned char* vertexbase;
  24. const unsigned char* indexbase;
  25. int indexstride;
  26. PHY_ScalarType type;
  27. PHY_ScalarType gfxindextype;
  28. int stride, numverts, numtriangles;
  29. int gfxindex;
  30. b3Vector3 triangle[3];
  31. b3Vector3 meshScaling = getScaling();
  32. ///if the number of parts is big, the performance might drop due to the innerloop switch on indextype
  33. for (part = 0; part < graphicssubparts; part++)
  34. {
  35. getLockedReadOnlyVertexIndexBase(&vertexbase, numverts, type, stride, &indexbase, indexstride, numtriangles, gfxindextype, part);
  36. numtotalphysicsverts += numtriangles * 3; //upper bound
  37. ///unlike that developers want to pass in double-precision meshes in single-precision Bullet build
  38. ///so disable this feature by default
  39. ///see patch http://code.google.com/p/bullet/issues/detail?id=213
  40. switch (type)
  41. {
  42. case PHY_FLOAT:
  43. {
  44. float* graphicsbase;
  45. switch (gfxindextype)
  46. {
  47. case PHY_INTEGER:
  48. {
  49. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  50. {
  51. unsigned int* tri_indices = (unsigned int*)(indexbase + gfxindex * indexstride);
  52. graphicsbase = (float*)(vertexbase + tri_indices[0] * stride);
  53. triangle[0].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  54. graphicsbase = (float*)(vertexbase + tri_indices[1] * stride);
  55. triangle[1].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  56. graphicsbase = (float*)(vertexbase + tri_indices[2] * stride);
  57. triangle[2].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  58. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  59. }
  60. break;
  61. }
  62. case PHY_SHORT:
  63. {
  64. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  65. {
  66. unsigned short int* tri_indices = (unsigned short int*)(indexbase + gfxindex * indexstride);
  67. graphicsbase = (float*)(vertexbase + tri_indices[0] * stride);
  68. triangle[0].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  69. graphicsbase = (float*)(vertexbase + tri_indices[1] * stride);
  70. triangle[1].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  71. graphicsbase = (float*)(vertexbase + tri_indices[2] * stride);
  72. triangle[2].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  73. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  74. }
  75. break;
  76. }
  77. case PHY_UCHAR:
  78. {
  79. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  80. {
  81. unsigned char* tri_indices = (unsigned char*)(indexbase + gfxindex * indexstride);
  82. graphicsbase = (float*)(vertexbase + tri_indices[0] * stride);
  83. triangle[0].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  84. graphicsbase = (float*)(vertexbase + tri_indices[1] * stride);
  85. triangle[1].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  86. graphicsbase = (float*)(vertexbase + tri_indices[2] * stride);
  87. triangle[2].setValue(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
  88. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  89. }
  90. break;
  91. }
  92. default:
  93. b3Assert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
  94. }
  95. break;
  96. }
  97. case PHY_DOUBLE:
  98. {
  99. double* graphicsbase;
  100. switch (gfxindextype)
  101. {
  102. case PHY_INTEGER:
  103. {
  104. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  105. {
  106. unsigned int* tri_indices = (unsigned int*)(indexbase + gfxindex * indexstride);
  107. graphicsbase = (double*)(vertexbase + tri_indices[0] * stride);
  108. triangle[0].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  109. graphicsbase = (double*)(vertexbase + tri_indices[1] * stride);
  110. triangle[1].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  111. graphicsbase = (double*)(vertexbase + tri_indices[2] * stride);
  112. triangle[2].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  113. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  114. }
  115. break;
  116. }
  117. case PHY_SHORT:
  118. {
  119. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  120. {
  121. unsigned short int* tri_indices = (unsigned short int*)(indexbase + gfxindex * indexstride);
  122. graphicsbase = (double*)(vertexbase + tri_indices[0] * stride);
  123. triangle[0].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  124. graphicsbase = (double*)(vertexbase + tri_indices[1] * stride);
  125. triangle[1].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  126. graphicsbase = (double*)(vertexbase + tri_indices[2] * stride);
  127. triangle[2].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  128. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  129. }
  130. break;
  131. }
  132. case PHY_UCHAR:
  133. {
  134. for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
  135. {
  136. unsigned char* tri_indices = (unsigned char*)(indexbase + gfxindex * indexstride);
  137. graphicsbase = (double*)(vertexbase + tri_indices[0] * stride);
  138. triangle[0].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  139. graphicsbase = (double*)(vertexbase + tri_indices[1] * stride);
  140. triangle[1].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  141. graphicsbase = (double*)(vertexbase + tri_indices[2] * stride);
  142. triangle[2].setValue((b3Scalar)graphicsbase[0] * meshScaling.getX(), (b3Scalar)graphicsbase[1] * meshScaling.getY(), (b3Scalar)graphicsbase[2] * meshScaling.getZ());
  143. callback->internalProcessTriangleIndex(triangle, part, gfxindex);
  144. }
  145. break;
  146. }
  147. default:
  148. b3Assert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
  149. }
  150. break;
  151. }
  152. default:
  153. b3Assert((type == PHY_FLOAT) || (type == PHY_DOUBLE));
  154. }
  155. unLockReadOnlyVertexBase(part);
  156. }
  157. }
  158. void b3StridingMeshInterface::calculateAabbBruteForce(b3Vector3& aabbMin, b3Vector3& aabbMax)
  159. {
  160. struct AabbCalculationCallback : public b3InternalTriangleIndexCallback
  161. {
  162. b3Vector3 m_aabbMin;
  163. b3Vector3 m_aabbMax;
  164. AabbCalculationCallback()
  165. {
  166. m_aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  167. m_aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  168. }
  169. virtual void internalProcessTriangleIndex(b3Vector3* triangle, int partId, int triangleIndex)
  170. {
  171. (void)partId;
  172. (void)triangleIndex;
  173. m_aabbMin.setMin(triangle[0]);
  174. m_aabbMax.setMax(triangle[0]);
  175. m_aabbMin.setMin(triangle[1]);
  176. m_aabbMax.setMax(triangle[1]);
  177. m_aabbMin.setMin(triangle[2]);
  178. m_aabbMax.setMax(triangle[2]);
  179. }
  180. };
  181. //first calculate the total aabb for all triangles
  182. AabbCalculationCallback aabbCallback;
  183. aabbMin.setValue(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  184. aabbMax.setValue(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  185. InternalProcessAllTriangles(&aabbCallback, aabbMin, aabbMax);
  186. aabbMin = aabbCallback.m_aabbMin;
  187. aabbMax = aabbCallback.m_aabbMax;
  188. }