btConvexHullShape.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "btConvexHullShape.h"
00017 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00018
00019 #include "LinearMath/btQuaternion.h"
00020 #include "LinearMath/btSerializer.h"
00021
00022 btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape ()
00023 {
00024 m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE;
00025 m_unscaledPoints.resize(numPoints);
00026
00027 unsigned char* pointsAddress = (unsigned char*)points;
00028
00029 for (int i=0;i<numPoints;i++)
00030 {
00031 btScalar* point = (btScalar*)pointsAddress;
00032 m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]);
00033 pointsAddress += stride;
00034 }
00035
00036 recalcLocalAabb();
00037
00038 }
00039
00040
00041
00042 void btConvexHullShape::setLocalScaling(const btVector3& scaling)
00043 {
00044 m_localScaling = scaling;
00045 recalcLocalAabb();
00046 }
00047
00048 void btConvexHullShape::addPoint(const btVector3& point)
00049 {
00050 m_unscaledPoints.push_back(point);
00051 recalcLocalAabb();
00052
00053 }
00054
00055 btVector3 btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00056 {
00057 btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
00058 btScalar newDot,maxDot = btScalar(-BT_LARGE_FLOAT);
00059
00060 btVector3 vec = vec0;
00061 btScalar lenSqr = vec.length2();
00062 if (lenSqr < btScalar(0.0001))
00063 {
00064 vec.setValue(1,0,0);
00065 } else
00066 {
00067 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00068 vec *= rlen;
00069 }
00070
00071
00072 for (int i=0;i<m_unscaledPoints.size();i++)
00073 {
00074 btVector3 vtx = m_unscaledPoints[i] * m_localScaling;
00075
00076 newDot = vec.dot(vtx);
00077 if (newDot > maxDot)
00078 {
00079 maxDot = newDot;
00080 supVec = vtx;
00081 }
00082 }
00083 return supVec;
00084 }
00085
00086 void btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00087 {
00088 btScalar newDot;
00089
00090 {
00091 for (int i=0;i<numVectors;i++)
00092 {
00093 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
00094 }
00095 }
00096 for (int i=0;i<m_unscaledPoints.size();i++)
00097 {
00098 btVector3 vtx = getScaledPoint(i);
00099
00100 for (int j=0;j<numVectors;j++)
00101 {
00102 const btVector3& vec = vectors[j];
00103
00104 newDot = vec.dot(vtx);
00105 if (newDot > supportVerticesOut[j][3])
00106 {
00107
00108 supportVerticesOut[j] = vtx;
00109 supportVerticesOut[j][3] = newDot;
00110 }
00111 }
00112 }
00113
00114
00115
00116 }
00117
00118
00119
00120 btVector3 btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const
00121 {
00122 btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
00123
00124 if ( getMargin()!=btScalar(0.) )
00125 {
00126 btVector3 vecnorm = vec;
00127 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
00128 {
00129 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
00130 }
00131 vecnorm.normalize();
00132 supVertex+= getMargin() * vecnorm;
00133 }
00134 return supVertex;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 int btConvexHullShape::getNumVertices() const
00148 {
00149 return m_unscaledPoints.size();
00150 }
00151
00152 int btConvexHullShape::getNumEdges() const
00153 {
00154 return m_unscaledPoints.size();
00155 }
00156
00157 void btConvexHullShape::getEdge(int i,btVector3& pa,btVector3& pb) const
00158 {
00159
00160 int index0 = i%m_unscaledPoints.size();
00161 int index1 = (i+1)%m_unscaledPoints.size();
00162 pa = getScaledPoint(index0);
00163 pb = getScaledPoint(index1);
00164 }
00165
00166 void btConvexHullShape::getVertex(int i,btVector3& vtx) const
00167 {
00168 vtx = getScaledPoint(i);
00169 }
00170
00171 int btConvexHullShape::getNumPlanes() const
00172 {
00173 return 0;
00174 }
00175
00176 void btConvexHullShape::getPlane(btVector3& ,btVector3& ,int ) const
00177 {
00178
00179 btAssert(0);
00180 }
00181
00182
00183 bool btConvexHullShape::isInside(const btVector3& ,btScalar ) const
00184 {
00185 btAssert(0);
00186 return false;
00187 }
00188
00190 const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const
00191 {
00192
00193 btConvexHullShapeData* shapeData = (btConvexHullShapeData*) dataBuffer;
00194 btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
00195
00196 int numElem = m_unscaledPoints.size();
00197 shapeData->m_numUnscaledPoints = numElem;
00198 #ifdef BT_USE_DOUBLE_PRECISION
00199 shapeData->m_unscaledPointsFloatPtr = 0;
00200 shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)&m_unscaledPoints[0]: 0;
00201 #else
00202 shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)&m_unscaledPoints[0]: 0;
00203 shapeData->m_unscaledPointsDoublePtr = 0;
00204 #endif
00205
00206 if (numElem)
00207 {
00208 int sz = sizeof(btVector3Data);
00209
00210
00211 btChunk* chunk = serializer->allocate(sz,numElem);
00212 btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr;
00213 for (int i=0;i<numElem;i++,memPtr++)
00214 {
00215 m_unscaledPoints[i].serialize(*memPtr);
00216 }
00217 serializer->finalizeChunk(chunk,btVector3DataName,BT_ARRAY_CODE,(void*)&m_unscaledPoints[0]);
00218 }
00219
00220 return "btConvexHullShapeData";
00221 }
00222
00223