| 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 | #include "btConvexHullShape.h" | 
|---|
| 16 | #include "BulletCollision/CollisionShapes/btCollisionMargin.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include "LinearMath/btQuaternion.h" | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexShape () | 
|---|
| 23 | { | 
|---|
| 24 | m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE; | 
|---|
| 25 | m_unscaledPoints.resize(numPoints); | 
|---|
| 26 |  | 
|---|
| 27 | unsigned char* pointsBaseAddress = (unsigned char*)points; | 
|---|
| 28 |  | 
|---|
| 29 | for (int i=0;i<numPoints;i++) | 
|---|
| 30 | { | 
|---|
| 31 | btVector3* point = (btVector3*)(pointsBaseAddress + i*stride); | 
|---|
| 32 | m_unscaledPoints[i] = point[0]; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | recalcLocalAabb(); | 
|---|
| 36 |  | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | void btConvexHullShape::setLocalScaling(const btVector3& scaling) | 
|---|
| 42 | { | 
|---|
| 43 | m_localScaling = scaling; | 
|---|
| 44 | recalcLocalAabb(); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | void btConvexHullShape::addPoint(const btVector3& point) | 
|---|
| 48 | { | 
|---|
| 49 | m_unscaledPoints.push_back(point); | 
|---|
| 50 | recalcLocalAabb(); | 
|---|
| 51 |  | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | btVector3       btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const | 
|---|
| 55 | { | 
|---|
| 56 | btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.)); | 
|---|
| 57 | btScalar newDot,maxDot = btScalar(-1e30); | 
|---|
| 58 |  | 
|---|
| 59 | btVector3 vec = vec0; | 
|---|
| 60 | btScalar lenSqr = vec.length2(); | 
|---|
| 61 | if (lenSqr < btScalar(0.0001)) | 
|---|
| 62 | { | 
|---|
| 63 | vec.setValue(1,0,0); | 
|---|
| 64 | } else | 
|---|
| 65 | { | 
|---|
| 66 | btScalar rlen = btScalar(1.) / btSqrt(lenSqr ); | 
|---|
| 67 | vec *= rlen; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | for (int i=0;i<m_unscaledPoints.size();i++) | 
|---|
| 72 | { | 
|---|
| 73 | btVector3 vtx = m_unscaledPoints[i] * m_localScaling; | 
|---|
| 74 |  | 
|---|
| 75 | newDot = vec.dot(vtx); | 
|---|
| 76 | if (newDot > maxDot) | 
|---|
| 77 | { | 
|---|
| 78 | maxDot = newDot; | 
|---|
| 79 | supVec = vtx; | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | return supVec; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void    btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const | 
|---|
| 86 | { | 
|---|
| 87 | btScalar newDot; | 
|---|
| 88 | //use 'w' component of supportVerticesOut? | 
|---|
| 89 | { | 
|---|
| 90 | for (int i=0;i<numVectors;i++) | 
|---|
| 91 | { | 
|---|
| 92 | supportVerticesOut[i][3] = btScalar(-1e30); | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | for (int i=0;i<m_unscaledPoints.size();i++) | 
|---|
| 96 | { | 
|---|
| 97 | btVector3 vtx = getScaledPoint(i); | 
|---|
| 98 |  | 
|---|
| 99 | for (int j=0;j<numVectors;j++) | 
|---|
| 100 | { | 
|---|
| 101 | const btVector3& vec = vectors[j]; | 
|---|
| 102 |  | 
|---|
| 103 | newDot = vec.dot(vtx); | 
|---|
| 104 | if (newDot > supportVerticesOut[j][3]) | 
|---|
| 105 | { | 
|---|
| 106 | //WARNING: don't swap next lines, the w component would get overwritten! | 
|---|
| 107 | supportVerticesOut[j] = vtx; | 
|---|
| 108 | supportVerticesOut[j][3] = newDot; | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | btVector3       btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const | 
|---|
| 120 | { | 
|---|
| 121 | btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec); | 
|---|
| 122 |  | 
|---|
| 123 | if ( getMargin()!=btScalar(0.) ) | 
|---|
| 124 | { | 
|---|
| 125 | btVector3 vecnorm = vec; | 
|---|
| 126 | if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON)) | 
|---|
| 127 | { | 
|---|
| 128 | vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.)); | 
|---|
| 129 | } | 
|---|
| 130 | vecnorm.normalize(); | 
|---|
| 131 | supVertex+= getMargin() * vecnorm; | 
|---|
| 132 | } | 
|---|
| 133 | return supVertex; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection | 
|---|
| 145 | //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo | 
|---|
| 146 | int     btConvexHullShape::getNumVertices() const | 
|---|
| 147 | { | 
|---|
| 148 | return m_unscaledPoints.size(); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | int btConvexHullShape::getNumEdges() const | 
|---|
| 152 | { | 
|---|
| 153 | return m_unscaledPoints.size(); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | void btConvexHullShape::getEdge(int i,btVector3& pa,btVector3& pb) const | 
|---|
| 157 | { | 
|---|
| 158 |  | 
|---|
| 159 | int index0 = i%m_unscaledPoints.size(); | 
|---|
| 160 | int index1 = (i+1)%m_unscaledPoints.size(); | 
|---|
| 161 | pa = getScaledPoint(index0); | 
|---|
| 162 | pb = getScaledPoint(index1); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void btConvexHullShape::getVertex(int i,btVector3& vtx) const | 
|---|
| 166 | { | 
|---|
| 167 | vtx = getScaledPoint(i); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | int     btConvexHullShape::getNumPlanes() const | 
|---|
| 171 | { | 
|---|
| 172 | return 0; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | void btConvexHullShape::getPlane(btVector3& ,btVector3& ,int ) const | 
|---|
| 176 | { | 
|---|
| 177 |  | 
|---|
| 178 | btAssert(0); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | //not yet | 
|---|
| 182 | bool btConvexHullShape::isInside(const btVector3& ,btScalar ) const | 
|---|
| 183 | { | 
|---|
| 184 | assert(0); | 
|---|
| 185 | return false; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|