1 /*
2 ***CHRONO***
3 Bullet Continuous Collision Detection and Physics Library
4 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it freely,
10 subject to the following restrictions:
11
12 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.
13 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
14 3. This notice may not be removed or altered from any source distribution.
15 */
16
17 //#define NOMINMAX
18 #include <algorithm>
19 #include "btCEtriangleShape.h"
20 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
21 #include "LinearMath/btQuaternion.h"
22 #include "chrono/collision/ChCollisionModelBullet.h"
23 #include <stdio.h>
24
25 using namespace chrono;
26
btCEtriangleShape(ChVector<> * mp1,ChVector<> * mp2,ChVector<> * mp3,ChVector<> * me1,ChVector<> * me2,ChVector<> * me3,bool mowns_vertex_1,bool mowns_vertex_2,bool mowns_vertex_3,bool mowns_edge_1,bool mowns_edge_2,bool mowns_edge_3,double msphereswept_rad)27 btCEtriangleShape::btCEtriangleShape(ChVector<>* mp1,
28 ChVector<>* mp2,
29 ChVector<>* mp3,
30 ChVector<>* me1,
31 ChVector<>* me2,
32 ChVector<>* me3,
33 bool mowns_vertex_1,
34 bool mowns_vertex_2,
35 bool mowns_vertex_3,
36 bool mowns_edge_1,
37 bool mowns_edge_2,
38 bool mowns_edge_3,
39 double msphereswept_rad)
40 {
41 p1 = mp1;
42 p2 = mp2;
43 p3 = mp3;
44 e1 = me1;
45 e2 = me2;
46 e3 = me3;
47 owns_vertex_1 = mowns_vertex_1;
48 owns_vertex_2 = mowns_vertex_2;
49 owns_vertex_3 = mowns_vertex_3;
50 owns_edge_1 = mowns_edge_1;
51 owns_edge_2 = mowns_edge_2;
52 owns_edge_3 = mowns_edge_3;
53 sphereswept_rad = msphereswept_rad;
54
55 m_shapeType = CE_TRIANGLE_SHAPE_PROXYTYPE;
56 }
57
58
59
localGetSupportingVertexWithoutMargin(const btVector3 & vec0) const60 btVector3 btCEtriangleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
61 {
62 btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
63 btScalar newDot,maxDot = btScalar(-BT_LARGE_FLOAT);
64 btVector3 vtx;
65 vtx = btVector3((btScalar)this->p1->x(),(btScalar)this->p1->y(),(btScalar)this->p1->z());
66 newDot = vec0.dot(vtx);
67 if (newDot > maxDot){
68 maxDot = newDot;
69 supVec = vtx;
70 }
71 vtx = btVector3((btScalar)this->p2->x(),(btScalar)this->p2->y(),(btScalar)this->p2->z());
72 newDot = vec0.dot(vtx);
73 if (newDot > maxDot){
74 maxDot = newDot;
75 supVec = vtx;
76 }
77 vtx = btVector3((btScalar)this->p3->x(),(btScalar)this->p3->y(),(btScalar)this->p3->z());
78 newDot = vec0.dot(vtx);
79 if (newDot > maxDot){
80 maxDot = newDot;
81 supVec = vtx;
82 }
83
84 return supVec; //+ vec0.normalized()*this->sphereswept_rad; //***TODO*** add the sphereswept_rad layer (but gives seldom jittering.. why?)
85 }
86
batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 * vectors,btVector3 * supportVerticesOut,int numVectors) const87 void btCEtriangleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
88 {
89 printf("NOT SUPPORTED!! \n");
90 }
91
92
calculateLocalInertia(btScalar mass,btVector3 & inertia) const93 void btCEtriangleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
94 {
95 //***TO DO***
96 //as an approximation, take the inertia of an average radius sphere
97
98 btTransform ident;
99 ident.setIdentity();
100
101 btVector3 halfExtents;
102 double radius= ChMax((*p2-*p1).Length(), (*p3-*p1).Length());
103 halfExtents.setValue((btScalar)(radius),
104 (btScalar)(radius),
105 (btScalar)(radius));
106
107 btScalar margin = CONVEX_DISTANCE_MARGIN;
108
109 btScalar lx=btScalar(2.)*(halfExtents[0]+margin);
110 btScalar ly=btScalar(2.)*(halfExtents[1]+margin);
111 btScalar lz=btScalar(2.)*(halfExtents[2]+margin);
112 const btScalar x2 = lx*lx;
113 const btScalar y2 = ly*ly;
114 const btScalar z2 = lz*lz;
115 const btScalar scaledmass = mass * btScalar(.08333333);
116
117 inertia[0] = scaledmass * (y2+z2);
118 inertia[1] = scaledmass * (x2+z2);
119 inertia[2] = scaledmass * (x2+y2);
120 }
121
122
getAabb(const btTransform & t,btVector3 & aabbMin,btVector3 & aabbMax) const123 void btCEtriangleShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
124 {
125
126 btVector3 p1_w = t.getOrigin()+ t.getBasis()*btVector3((btScalar)this->p1->x(), (btScalar)this->p1->y(), (btScalar)this->p1->z());
127 btVector3 p2_w = t.getOrigin()+ t.getBasis()*btVector3((btScalar)this->p2->x(), (btScalar)this->p2->y(), (btScalar)this->p2->z());
128 btVector3 p3_w = t.getOrigin()+ t.getBasis()*btVector3((btScalar)this->p3->x(), (btScalar)this->p3->y(), (btScalar)this->p3->z());
129
130 collision::ChCollisionModelBullet* triModel = (collision::ChCollisionModelBullet*)this->getUserPointer();
131
132 btVector3 venvelope (triModel->GetEnvelope(), triModel->GetEnvelope(), triModel->GetEnvelope());
133 btVector3 vsphereswept ((btScalar)this->sphereswept_rad,(btScalar)this->sphereswept_rad,(btScalar)this->sphereswept_rad);
134
135 aabbMin = btVector3((btScalar)ChMin(ChMin(p1_w.x(),p2_w.x()),p3_w.x()),
136 (btScalar)ChMin(ChMin(p1_w.y(),p2_w.y()),p3_w.y()),
137 (btScalar)ChMin(ChMin(p1_w.z(),p2_w.z()),p3_w.z())) - venvelope - vsphereswept;
138
139 aabbMax = btVector3((btScalar)ChMax(ChMax(p1_w.x(),p2_w.x()),p3_w.x()),
140 (btScalar)ChMax(ChMax(p1_w.y(),p2_w.y()),p3_w.y()),
141 (btScalar)ChMax(ChMax(p1_w.z(),p2_w.z()),p3_w.z())) + venvelope + vsphereswept;
142 }
143
144
145