Bullet Collision Detection & Physics Library: btConvexPointCloudShape.cpp Source File

Bullet Collision Detection & Physics Library

btConvexPointCloudShape.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 #include "btConvexPointCloudShape.h"
17 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
18
19 #include "LinearMath/btQuaternion.h"
20
21 void btConvexPointCloudShape::setLocalScaling(const btVector3& scaling)
22 {
23 m_localScaling = scaling;
24 recalcLocalAabb();
25 }
26
27 #ifndef __SPU__
28 btVector3 btConvexPointCloudShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
29 {
30 btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
31 btScalar maxDot = btScalar(-BT_LARGE_FLOAT);
32
33 btVector3 vec = vec0;
34 btScalar lenSqr = vec.length2();
35 if (lenSqr < btScalar(0.0001))
36 {
37 vec.setValue(1,0,0);
38 } else
39 {
40 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
41 vec *= rlen;
42 }
43
44 if( m_numPoints > 0 )
45 {
46 // Here we take advantage of dot(a*b, c) = dot( a, b*c) to do less work. Note this transformation is true mathematically, not numerically.
47 // btVector3 scaled = vec * m_localScaling;
48 int index = (int) vec.maxDot( &m_unscaledPoints[0], m_numPoints, maxDot); //FIXME: may violate encapsulation of m_unscaledPoints
49 return getScaledPoint(index);
50 }
51
52 return supVec;
53 }
54
55 void btConvexPointCloudShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
56 {
57 for( int j = 0; j < numVectors; j++ )
58 {
59 const btVector3& vec = vectors[j] * m_localScaling; // dot( a*c, b) = dot(a, b*c)
60 btScalar maxDot;
61 int index = (int) vec.maxDot( &m_unscaledPoints[0], m_numPoints, maxDot);
62 supportVerticesOut[j][3] = btScalar(-BT_LARGE_FLOAT);
63 if( 0 <= index )
64 {
65 //WARNING: don't swap next lines, the w component would get overwritten!
66 supportVerticesOut[j] = getScaledPoint(index);
67 supportVerticesOut[j][3] = maxDot;
68 }
69 }
70
71 }
72
73
74
75 btVector3 btConvexPointCloudShape::localGetSupportingVertex(const btVector3& vec)const
76 {
77 btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
78
79 if ( getMargin()!=btScalar(0.) )
80 {
81 btVector3 vecnorm = vec;
82 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
83 {
84 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
85 }
86 vecnorm.normalize();
87 supVertex+= getMargin() * vecnorm;
88 }
89 return supVertex;
90 }
91
92
93 #endif
94
95
96
97
98
99
100 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
101 //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
102 int btConvexPointCloudShape::getNumVertices() const
103 {
104 return m_numPoints;
105 }
106
107 int btConvexPointCloudShape::getNumEdges() const
108 {
109 return 0;
110 }
111
112 void btConvexPointCloudShape::getEdge(int i,btVector3& pa,btVector3& pb) const
113 {
114 btAssert (0);
115 }
116
117 void btConvexPointCloudShape::getVertex(int i,btVector3& vtx) const
118 {
119 vtx = m_unscaledPoints[i]*m_localScaling;
120 }
121
122 int btConvexPointCloudShape::getNumPlanes() const
123 {
124 return 0;
125 }
126
127 void btConvexPointCloudShape::getPlane(btVector3& ,btVector3& ,int ) const
128 {
129
130 btAssert(0);
131 }
132
133 //not yet
134 bool btConvexPointCloudShape::isInside(const btVector3& ,btScalar ) const
135 {
136 btAssert(0);
137 return false;
138 }
139
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:521
btConvexPointCloudShape::getScaledPoint
btVector3 getScaledPoint(int index) const
Definition: btConvexPointCloudShape.h:76
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:294
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
btConvexPointCloudShape::getNumEdges
virtual int getNumEdges() const
Definition: btConvexPointCloudShape.cpp:107
btConvexPointCloudShape::getEdge
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btConvexPointCloudShape.cpp:112
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:257
btSqrt
btScalar btSqrt(btScalar y)
Definition: btScalar.h:444
btAssert
#define btAssert(x)
Definition: btScalar.h:131
btConvexPointCloudShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
in case we receive negative scaling
Definition: btConvexPointCloudShape.cpp:21
btConvexInternalShape::m_localScaling
btVector3 m_localScaling
Definition: btConvexInternalShape.h:35
btConvexPointCloudShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btConvexPointCloudShape.cpp:55
btConvexPointCloudShape::getNumPlanes
virtual int getNumPlanes() const
Definition: btConvexPointCloudShape.cpp:122
btCollisionMargin.h
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
btVector3::maxDot
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:1013
btConvexPointCloudShape::getVertex
virtual void getVertex(int i, btVector3 &vtx) const
Definition: btConvexPointCloudShape.cpp:117
btConvexPointCloudShape::getPlane
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btConvexPointCloudShape.cpp:127
btQuaternion.h
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition: btConvexInternalShape.h:113
btConvexPointCloudShape::isInside
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btConvexPointCloudShape.cpp:134
btConvexPointCloudShape.h
btConvexPointCloudShape::getNumVertices
virtual int getNumVertices() const
Definition: btConvexPointCloudShape.cpp:102
btConvexPointCloudShape::m_unscaledPoints
btVector3 * m_unscaledPoints
Definition: btConvexPointCloudShape.h:26
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btConvexPointCloudShape::m_numPoints
int m_numPoints
Definition: btConvexPointCloudShape.h:27
btConvexPointCloudShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btConvexPointCloudShape.cpp:75
btConvexPointCloudShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition: btConvexPointCloudShape.cpp:28
btPolyhedralConvexAabbCachingShape::recalcLocalAabb
void recalcLocalAabb()
Definition: btPolyhedralConvexShape.cpp:450
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292