[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
[7983] | 3 | Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org |
---|
[1963] | 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 | */ |
---|
[7983] | 15 | |
---|
[1963] | 16 | #include "btConvexHullShape.h" |
---|
| 17 | #include "BulletCollision/CollisionShapes/btCollisionMargin.h" |
---|
| 18 | |
---|
| 19 | #include "LinearMath/btQuaternion.h" |
---|
[7983] | 20 | #include "LinearMath/btSerializer.h" |
---|
[1963] | 21 | |
---|
[7983] | 22 | btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape () |
---|
[1963] | 23 | { |
---|
| 24 | m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE; |
---|
[2430] | 25 | m_unscaledPoints.resize(numPoints); |
---|
[1963] | 26 | |
---|
[7983] | 27 | unsigned char* pointsAddress = (unsigned char*)points; |
---|
[1963] | 28 | |
---|
| 29 | for (int i=0;i<numPoints;i++) |
---|
| 30 | { |
---|
[7983] | 31 | btScalar* point = (btScalar*)pointsAddress; |
---|
| 32 | m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]); |
---|
| 33 | pointsAddress += stride; |
---|
[1963] | 34 | } |
---|
| 35 | |
---|
| 36 | recalcLocalAabb(); |
---|
| 37 | |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | void btConvexHullShape::setLocalScaling(const btVector3& scaling) |
---|
| 43 | { |
---|
| 44 | m_localScaling = scaling; |
---|
| 45 | recalcLocalAabb(); |
---|
| 46 | } |
---|
| 47 | |
---|
[2430] | 48 | void btConvexHullShape::addPoint(const btVector3& point) |
---|
[1963] | 49 | { |
---|
[2430] | 50 | m_unscaledPoints.push_back(point); |
---|
[1963] | 51 | recalcLocalAabb(); |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[7983] | 55 | btVector3 btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const |
---|
[1963] | 56 | { |
---|
| 57 | btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
[7983] | 58 | btScalar newDot,maxDot = btScalar(-BT_LARGE_FLOAT); |
---|
[1963] | 59 | |
---|
[2430] | 60 | for (int i=0;i<m_unscaledPoints.size();i++) |
---|
[1963] | 61 | { |
---|
[2430] | 62 | btVector3 vtx = m_unscaledPoints[i] * m_localScaling; |
---|
[1963] | 63 | |
---|
| 64 | newDot = vec.dot(vtx); |
---|
| 65 | if (newDot > maxDot) |
---|
| 66 | { |
---|
| 67 | maxDot = newDot; |
---|
| 68 | supVec = vtx; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | return supVec; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
| 75 | { |
---|
| 76 | btScalar newDot; |
---|
| 77 | //use 'w' component of supportVerticesOut? |
---|
| 78 | { |
---|
| 79 | for (int i=0;i<numVectors;i++) |
---|
| 80 | { |
---|
[7983] | 81 | supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT); |
---|
[1963] | 82 | } |
---|
| 83 | } |
---|
[2430] | 84 | for (int i=0;i<m_unscaledPoints.size();i++) |
---|
[1963] | 85 | { |
---|
[2430] | 86 | btVector3 vtx = getScaledPoint(i); |
---|
[1963] | 87 | |
---|
| 88 | for (int j=0;j<numVectors;j++) |
---|
| 89 | { |
---|
| 90 | const btVector3& vec = vectors[j]; |
---|
| 91 | |
---|
| 92 | newDot = vec.dot(vtx); |
---|
| 93 | if (newDot > supportVerticesOut[j][3]) |
---|
| 94 | { |
---|
| 95 | //WARNING: don't swap next lines, the w component would get overwritten! |
---|
| 96 | supportVerticesOut[j] = vtx; |
---|
| 97 | supportVerticesOut[j][3] = newDot; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | btVector3 btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const |
---|
| 109 | { |
---|
| 110 | btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec); |
---|
| 111 | |
---|
| 112 | if ( getMargin()!=btScalar(0.) ) |
---|
| 113 | { |
---|
| 114 | btVector3 vecnorm = vec; |
---|
| 115 | if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON)) |
---|
| 116 | { |
---|
| 117 | vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.)); |
---|
| 118 | } |
---|
| 119 | vecnorm.normalize(); |
---|
| 120 | supVertex+= getMargin() * vecnorm; |
---|
| 121 | } |
---|
| 122 | return supVertex; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection |
---|
| 134 | //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo |
---|
| 135 | int btConvexHullShape::getNumVertices() const |
---|
| 136 | { |
---|
[2430] | 137 | return m_unscaledPoints.size(); |
---|
[1963] | 138 | } |
---|
| 139 | |
---|
| 140 | int btConvexHullShape::getNumEdges() const |
---|
| 141 | { |
---|
[2430] | 142 | return m_unscaledPoints.size(); |
---|
[1963] | 143 | } |
---|
| 144 | |
---|
[2430] | 145 | void btConvexHullShape::getEdge(int i,btVector3& pa,btVector3& pb) const |
---|
[1963] | 146 | { |
---|
| 147 | |
---|
[2430] | 148 | int index0 = i%m_unscaledPoints.size(); |
---|
| 149 | int index1 = (i+1)%m_unscaledPoints.size(); |
---|
| 150 | pa = getScaledPoint(index0); |
---|
| 151 | pb = getScaledPoint(index1); |
---|
[1963] | 152 | } |
---|
| 153 | |
---|
[2430] | 154 | void btConvexHullShape::getVertex(int i,btVector3& vtx) const |
---|
[1963] | 155 | { |
---|
[2430] | 156 | vtx = getScaledPoint(i); |
---|
[1963] | 157 | } |
---|
| 158 | |
---|
| 159 | int btConvexHullShape::getNumPlanes() const |
---|
| 160 | { |
---|
| 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
[2430] | 164 | void btConvexHullShape::getPlane(btVector3& ,btVector3& ,int ) const |
---|
[1963] | 165 | { |
---|
| 166 | |
---|
| 167 | btAssert(0); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | //not yet |
---|
[2430] | 171 | bool btConvexHullShape::isInside(const btVector3& ,btScalar ) const |
---|
[1963] | 172 | { |
---|
[2882] | 173 | btAssert(0); |
---|
[1963] | 174 | return false; |
---|
| 175 | } |
---|
| 176 | |
---|
[7983] | 177 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
| 178 | const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const |
---|
| 179 | { |
---|
| 180 | //int szc = sizeof(btConvexHullShapeData); |
---|
| 181 | btConvexHullShapeData* shapeData = (btConvexHullShapeData*) dataBuffer; |
---|
| 182 | btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer); |
---|
| 183 | |
---|
| 184 | int numElem = m_unscaledPoints.size(); |
---|
| 185 | shapeData->m_numUnscaledPoints = numElem; |
---|
| 186 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
| 187 | shapeData->m_unscaledPointsFloatPtr = 0; |
---|
| 188 | shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0; |
---|
| 189 | #else |
---|
| 190 | shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0; |
---|
| 191 | shapeData->m_unscaledPointsDoublePtr = 0; |
---|
| 192 | #endif |
---|
| 193 | |
---|
| 194 | if (numElem) |
---|
| 195 | { |
---|
| 196 | int sz = sizeof(btVector3Data); |
---|
| 197 | // int sz2 = sizeof(btVector3DoubleData); |
---|
| 198 | // int sz3 = sizeof(btVector3FloatData); |
---|
| 199 | btChunk* chunk = serializer->allocate(sz,numElem); |
---|
| 200 | btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr; |
---|
| 201 | for (int i=0;i<numElem;i++,memPtr++) |
---|
| 202 | { |
---|
| 203 | m_unscaledPoints[i].serialize(*memPtr); |
---|
| 204 | } |
---|
| 205 | serializer->finalizeChunk(chunk,btVector3DataName,BT_ARRAY_CODE,(void*)&m_unscaledPoints[0]); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | return "btConvexHullShapeData"; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|