| [1963] | 1 | /* | 
|---|
|  | 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
|  | 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
|  | 4 |  | 
|---|
|  | 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
|  | 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
|  | 7 | Permission is granted to anyone to use this software for any purpose, | 
|---|
|  | 8 | including commercial applications, and to alter it and redistribute it freely, | 
|---|
|  | 9 | subject to the following restrictions: | 
|---|
|  | 10 |  | 
|---|
|  | 11 | 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. | 
|---|
|  | 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
|  | 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
|  | 14 | */ | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "btTriangleMesh.h" | 
|---|
|  | 17 |  | 
|---|
|  | 18 |  | 
|---|
|  | 19 |  | 
|---|
|  | 20 | btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices) | 
|---|
|  | 21 | :m_use32bitIndices(use32bitIndices), | 
|---|
|  | 22 | m_use4componentVertices(use4componentVertices), | 
|---|
|  | 23 | m_weldingThreshold(0.0) | 
|---|
|  | 24 | { | 
|---|
|  | 25 | btIndexedMesh meshIndex; | 
|---|
|  | 26 | meshIndex.m_numTriangles = 0; | 
|---|
|  | 27 | meshIndex.m_numVertices = 0; | 
|---|
|  | 28 | meshIndex.m_indexType = PHY_INTEGER; | 
|---|
|  | 29 | meshIndex.m_triangleIndexBase = 0; | 
|---|
|  | 30 | meshIndex.m_triangleIndexStride = 3*sizeof(int); | 
|---|
|  | 31 | meshIndex.m_vertexBase = 0; | 
|---|
|  | 32 | meshIndex.m_vertexStride = sizeof(btVector3); | 
|---|
|  | 33 | m_indexedMeshes.push_back(meshIndex); | 
|---|
|  | 34 |  | 
|---|
|  | 35 | if (m_use32bitIndices) | 
|---|
|  | 36 | { | 
|---|
|  | 37 | m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size()/3; | 
|---|
| [2882] | 38 | m_indexedMeshes[0].m_triangleIndexBase = 0; | 
|---|
| [1963] | 39 | m_indexedMeshes[0].m_indexType = PHY_INTEGER; | 
|---|
|  | 40 | m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(int); | 
|---|
|  | 41 | } else | 
|---|
|  | 42 | { | 
|---|
|  | 43 | m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size()/3; | 
|---|
| [2882] | 44 | m_indexedMeshes[0].m_triangleIndexBase = 0; | 
|---|
| [1963] | 45 | m_indexedMeshes[0].m_indexType = PHY_SHORT; | 
|---|
|  | 46 | m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(short int); | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | if (m_use4componentVertices) | 
|---|
|  | 50 | { | 
|---|
|  | 51 | m_indexedMeshes[0].m_numVertices = m_4componentVertices.size(); | 
|---|
| [2882] | 52 | m_indexedMeshes[0].m_vertexBase = 0; | 
|---|
| [1963] | 53 | m_indexedMeshes[0].m_vertexStride = sizeof(btVector3); | 
|---|
|  | 54 | } else | 
|---|
|  | 55 | { | 
|---|
|  | 56 | m_indexedMeshes[0].m_numVertices = m_3componentVertices.size()/3; | 
|---|
| [2882] | 57 | m_indexedMeshes[0].m_vertexBase = 0; | 
|---|
| [1963] | 58 | m_indexedMeshes[0].m_vertexStride = 3*sizeof(btScalar); | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 |  | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 | void    btTriangleMesh::addIndex(int index) | 
|---|
|  | 65 | { | 
|---|
|  | 66 | if (m_use32bitIndices) | 
|---|
|  | 67 | { | 
|---|
|  | 68 | m_32bitIndices.push_back(index); | 
|---|
|  | 69 | m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0]; | 
|---|
|  | 70 | } else | 
|---|
|  | 71 | { | 
|---|
|  | 72 | m_16bitIndices.push_back(index); | 
|---|
|  | 73 | m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0]; | 
|---|
|  | 74 | } | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
| [2430] | 77 |  | 
|---|
|  | 78 | int     btTriangleMesh::findOrAddVertex(const btVector3& vertex, bool removeDuplicateVertices) | 
|---|
| [1963] | 79 | { | 
|---|
|  | 80 | //return index of new/existing vertex | 
|---|
| [2430] | 81 | ///@todo: could use acceleration structure for this | 
|---|
| [1963] | 82 | if (m_use4componentVertices) | 
|---|
|  | 83 | { | 
|---|
| [2430] | 84 | if (removeDuplicateVertices) | 
|---|
| [1963] | 85 | { | 
|---|
| [2430] | 86 | for (int i=0;i< m_4componentVertices.size();i++) | 
|---|
|  | 87 | { | 
|---|
|  | 88 | if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold) | 
|---|
|  | 89 | { | 
|---|
|  | 90 | return i; | 
|---|
|  | 91 | } | 
|---|
| [1963] | 92 | } | 
|---|
|  | 93 | } | 
|---|
|  | 94 | m_indexedMeshes[0].m_numVertices++; | 
|---|
|  | 95 | m_4componentVertices.push_back(vertex); | 
|---|
|  | 96 | m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0]; | 
|---|
|  | 97 |  | 
|---|
|  | 98 | return m_4componentVertices.size()-1; | 
|---|
|  | 99 |  | 
|---|
|  | 100 | } else | 
|---|
|  | 101 | { | 
|---|
|  | 102 |  | 
|---|
| [2430] | 103 | if (removeDuplicateVertices) | 
|---|
| [1963] | 104 | { | 
|---|
| [2430] | 105 | for (int i=0;i< m_3componentVertices.size();i+=3) | 
|---|
| [1963] | 106 | { | 
|---|
| [2430] | 107 | btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]); | 
|---|
|  | 108 | if ((vtx-vertex).length2() <= m_weldingThreshold) | 
|---|
|  | 109 | { | 
|---|
|  | 110 | return i/3; | 
|---|
|  | 111 | } | 
|---|
| [1963] | 112 | } | 
|---|
| [2430] | 113 | } | 
|---|
| [2882] | 114 | m_3componentVertices.push_back((float)vertex.getX()); | 
|---|
|  | 115 | m_3componentVertices.push_back((float)vertex.getY()); | 
|---|
|  | 116 | m_3componentVertices.push_back((float)vertex.getZ()); | 
|---|
| [1963] | 117 | m_indexedMeshes[0].m_numVertices++; | 
|---|
|  | 118 | m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0]; | 
|---|
|  | 119 | return (m_3componentVertices.size()/3)-1; | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [2430] | 124 | void    btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2,bool removeDuplicateVertices) | 
|---|
| [1963] | 125 | { | 
|---|
|  | 126 | m_indexedMeshes[0].m_numTriangles++; | 
|---|
| [2430] | 127 | addIndex(findOrAddVertex(vertex0,removeDuplicateVertices)); | 
|---|
|  | 128 | addIndex(findOrAddVertex(vertex1,removeDuplicateVertices)); | 
|---|
|  | 129 | addIndex(findOrAddVertex(vertex2,removeDuplicateVertices)); | 
|---|
| [1963] | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | int btTriangleMesh::getNumTriangles() const | 
|---|
|  | 133 | { | 
|---|
|  | 134 | if (m_use32bitIndices) | 
|---|
|  | 135 | { | 
|---|
|  | 136 | return m_32bitIndices.size() / 3; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | return m_16bitIndices.size() / 3; | 
|---|
|  | 139 | } | 
|---|