Blender  V3.3
btConvexHullShape.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
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 #if defined(_WIN32) || defined(__i386__)
17 #define BT_USE_SSE_IN_API
18 #endif
19 
20 #include "btConvexHullShape.h"
22 
25 #include "btConvexPolyhedron.h"
27 
29 {
30  m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE;
31  m_unscaledPoints.resize(numPoints);
32 
33  unsigned char* pointsAddress = (unsigned char*)points;
34 
35  for (int i = 0; i < numPoints; i++)
36  {
37  btScalar* point = (btScalar*)pointsAddress;
38  m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]);
39  pointsAddress += stride;
40  }
41 
43 }
44 
46 {
47  m_localScaling = scaling;
49 }
50 
52 {
53  m_unscaledPoints.push_back(point);
56 }
57 
59 {
60  btVector3 supVec(btScalar(0.), btScalar(0.), btScalar(0.));
62 
63  // Here we take advantage of dot(a, b*c) = dot(a*b, c). Note: This is true mathematically, but not numerically.
64  if (0 < m_unscaledPoints.size())
65  {
67  int index = (int)scaled.maxDot(&m_unscaledPoints[0], m_unscaledPoints.size(), maxDot); // FIXME: may violate encapsulation of m_unscaledPoints
68  return m_unscaledPoints[index] * m_localScaling;
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  {
81  supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
82  }
83  }
84 
85  for (int j = 0; j < numVectors; j++)
86  {
87  btVector3 vec = vectors[j] * m_localScaling; // dot(a*b,c) = dot(a,b*c)
88  if (0 < m_unscaledPoints.size())
89  {
90  int i = (int)vec.maxDot(&m_unscaledPoints[0], m_unscaledPoints.size(), newDot);
91  supportVerticesOut[j] = getScaledPoint(i);
92  supportVerticesOut[j][3] = newDot;
93  }
94  else
95  supportVerticesOut[j][3] = -BT_LARGE_FLOAT;
96  }
97 }
98 
100 {
102 
103  if (getMargin() != btScalar(0.))
104  {
105  btVector3 vecnorm = vec;
106  if (vecnorm.length2() < (SIMD_EPSILON * SIMD_EPSILON))
107  {
108  vecnorm.setValue(btScalar(-1.), btScalar(-1.), btScalar(-1.));
109  }
110  vecnorm.normalize();
111  supVertex += getMargin() * vecnorm;
112  }
113  return supVertex;
114 }
115 
117 {
119  conv.compute(&m_unscaledPoints[0].getX(), sizeof(btVector3), m_unscaledPoints.size(), 0.f, 0.f);
120  int numVerts = conv.vertices.size();
121  m_unscaledPoints.resize(0);
122  for (int i = 0; i < numVerts; i++)
123  {
124  m_unscaledPoints.push_back(conv.vertices[i]);
125  }
126 }
127 
128 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
129 //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
131 {
132  return m_unscaledPoints.size();
133 }
134 
136 {
137  return m_unscaledPoints.size();
138 }
139 
140 void btConvexHullShape::getEdge(int i, btVector3& pa, btVector3& pb) const
141 {
142  int index0 = i % m_unscaledPoints.size();
143  int index1 = (i + 1) % m_unscaledPoints.size();
144  pa = getScaledPoint(index0);
145  pb = getScaledPoint(index1);
146 }
147 
148 void btConvexHullShape::getVertex(int i, btVector3& vtx) const
149 {
150  vtx = getScaledPoint(i);
151 }
152 
154 {
155  return 0;
156 }
157 
159 {
160  btAssert(0);
161 }
162 
163 //not yet
165 {
166  btAssert(0);
167  return false;
168 }
169 
171 const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const
172 {
173  //int szc = sizeof(btConvexHullShapeData);
174  btConvexHullShapeData* shapeData = (btConvexHullShapeData*)dataBuffer;
176 
177  int numElem = m_unscaledPoints.size();
178  shapeData->m_numUnscaledPoints = numElem;
179 #ifdef BT_USE_DOUBLE_PRECISION
180  shapeData->m_unscaledPointsFloatPtr = 0;
181  shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]) : 0;
182 #else
183  shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]) : 0;
184  shapeData->m_unscaledPointsDoublePtr = 0;
185 #endif
186 
187  if (numElem)
188  {
189  int sz = sizeof(btVector3Data);
190  // int sz2 = sizeof(btVector3DoubleData);
191  // int sz3 = sizeof(btVector3FloatData);
192  btChunk* chunk = serializer->allocate(sz, numElem);
193  btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr;
194  for (int i = 0; i < numElem; i++, memPtr++)
195  {
196  m_unscaledPoints[i].serialize(*memPtr);
197  }
198  serializer->finalizeChunk(chunk, btVector3DataName, BT_ARRAY_CODE, (void*)&m_unscaledPoints[0]);
199  }
200 
201  // Fill padding with zeros to appease msan.
202  memset(shapeData->m_padding3, 0, sizeof(shapeData->m_padding3));
203 
204  return "btConvexHullShapeData";
205 }
206 
207 void btConvexHullShape::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin, btVector3& witnesPtMax) const
208 {
209 #if 1
210  minProj = FLT_MAX;
211  maxProj = -FLT_MAX;
212 
213  int numVerts = m_unscaledPoints.size();
214  for (int i = 0; i < numVerts; i++)
215  {
216  btVector3 vtx = m_unscaledPoints[i] * m_localScaling;
217  btVector3 pt = trans * vtx;
218  btScalar dp = pt.dot(dir);
219  if (dp < minProj)
220  {
221  minProj = dp;
222  witnesPtMin = pt;
223  }
224  if (dp > maxProj)
225  {
226  maxProj = dp;
227  witnesPtMax = pt;
228  }
229  }
230 #else
231  btVector3 localAxis = dir * trans.getBasis();
232  witnesPtMin = trans(localGetSupportingVertex(localAxis));
233  witnesPtMax = trans(localGetSupportingVertex(-localAxis));
234 
235  minProj = witnesPtMin.dot(dir);
236  maxProj = witnesPtMax.dot(dir);
237 #endif
238 
239  if (minProj > maxProj)
240  {
241  btSwap(minProj, maxProj);
242  btSwap(witnesPtMin, witnesPtMax);
243  }
244 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei stride
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
virtual void setLocalScaling(const btVector3 &scaling)
in case we receive negative scaling
Definition: btBox2dShape.h:120
SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
btConvexShape Interface
Definition: btBox2dShape.h:62
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btBox2dShape.h:218
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btBox2dShape.h:284
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btBox2dShape.h:51
virtual int getNumVertices() const
Definition: btBox2dShape.h:140
virtual void getVertex(int i, btVector3 &vtx) const
Definition: btBox2dShape.h:179
virtual int getNumPlanes() const
Definition: btBox2dShape.h:169
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btBox2dShape.h:155
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btBox2dShape.h:71
virtual int getNumEdges() const
Definition: btBox2dShape.h:174
@ CONVEX_HULL_SHAPE_PROXYTYPE
virtual btScalar getMargin() const =0
virtual void recalculateLocalAabb()
btVector3 m_localScaling
btConvexHullShape(const btScalar *points=0, int numPoints=0, int stride=sizeof(btVector3))
SIMD_FORCE_INLINE btVector3 getScaledPoint(int i) const
void optimizeConvexHull()
void addPoint(const btVector3 &point, bool recalculateLocalAabb=true)
btMatrix3x3 scaled(const btVector3 &s) const
Create a scaled copy of the matrix.
Definition: btMatrix3x3.h:622
SIMD_FORCE_INLINE const btScalar & getX() const
Return the x value.
Definition: btQuadWord.h:99
virtual bool serialize(void *o_alignedDataBuffer, unsigned i_dataBufferSize, bool i_swapEndian) const
Data buffer MUST be 16 byte aligned.
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define BT_LARGE_FLOAT
Definition: btScalar.h:316
SIMD_FORCE_INLINE void btSwap(T &a, T &b)
Definition: btScalar.h:643
#define SIMD_EPSILON
Definition: btScalar.h:543
#define btAssert(x)
Definition: btScalar.h:295
#define BT_ARRAY_CODE
Definition: btSerializer.h:118
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
void recalcLocalAabb()
#define btVector3DataName
Definition: btVector3.h:28
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
SIMD_FORCE_INLINE long maxDot(const btVector3 *array, long array_count, btScalar &dotOut) const
returns index of maximum dot product between this and vectors in array[]
Definition: btVector3.h:998
#define btVector3Data
Definition: btVector3.h:27
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
void * m_oldPtr
Definition: btSerializer.h:52
btAlignedObjectArray< btVector3 > vertices
The btPolyhedralConvexAabbCachingShape adds aabb caching to the btPolyhedralConvexShape.
virtual btChunk * allocate(size_t size, int numElements)=0
virtual void * getUniquePointer(void *oldPtr)=0
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)=0
vec_base< T, Size > project(const vec_base< T, Size > &p, const vec_base< T, Size > &v_proj)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
btVector3DoubleData * m_unscaledPointsDoublePtr
btVector3FloatData * m_unscaledPointsFloatPtr
btConvexInternalShapeData m_convexInternalShapeData