Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletCollision / Gimpact / btGImpactShape.cpp
1 /*
2 This source file is part of GIMPACT Library.
3
4 For the latest info, see http://gimpact.sourceforge.net/
5
6 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
7 email: projectileman@yahoo.com
8
9
10 This software is provided 'as-is', without any express or implied warranty.
11 In no event will the authors be held liable for any damages arising from the use of this software.
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it freely,
14 subject to the following restrictions:
15
16 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.
17 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19 */
20
21
22 #include "btGImpactShape.h"
23 #include "btGImpactMassUtil.h"
24
25
26 #define CALC_EXACT_INERTIA 1
27
28 void btGImpactCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
29 {
30         lockChildShapes();
31 #ifdef CALC_EXACT_INERTIA
32         inertia.setValue(0.f,0.f,0.f);
33
34         int i = this->getNumChildShapes();
35         btScalar shapemass = mass/btScalar(i);
36
37         while(i--)
38         {
39                 btVector3 temp_inertia;
40                 m_childShapes[i]->calculateLocalInertia(shapemass,temp_inertia);
41                 if(childrenHasTransform())
42                 {
43                         inertia = gim_inertia_add_transformed( inertia,temp_inertia,m_childTransforms[i]);
44                 }
45                 else
46                 {
47                         inertia = gim_inertia_add_transformed( inertia,temp_inertia,btTransform::getIdentity());
48                 }
49
50         }
51
52 #else
53
54         // Calc box inertia
55
56         btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
57         btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
58         btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
59         const btScalar x2 = lx*lx;
60         const btScalar y2 = ly*ly;
61         const btScalar z2 = lz*lz;
62         const btScalar scaledmass = mass * btScalar(0.08333333);
63
64         inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
65
66 #endif
67         unlockChildShapes();
68 }
69
70
71
72 void btGImpactMeshShapePart::calculateLocalInertia(btScalar mass,btVector3& inertia) const
73 {
74         lockChildShapes();
75
76
77 #ifdef CALC_EXACT_INERTIA
78         inertia.setValue(0.f,0.f,0.f);
79
80         int i = this->getVertexCount();
81         btScalar pointmass = mass/btScalar(i);
82
83         while(i--)
84         {
85                 btVector3 pointintertia;
86                 this->getVertex(i,pointintertia);
87                 pointintertia = gim_get_point_inertia(pointintertia,pointmass);
88                 inertia+=pointintertia;
89         }
90
91 #else
92
93         // Calc box inertia
94
95         btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
96         btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
97         btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
98         const btScalar x2 = lx*lx;
99         const btScalar y2 = ly*ly;
100         const btScalar z2 = lz*lz;
101         const btScalar scaledmass = mass * btScalar(0.08333333);
102
103         inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
104
105 #endif
106
107         unlockChildShapes();
108 }
109
110 void btGImpactMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
111 {
112
113 #ifdef CALC_EXACT_INERTIA
114         inertia.setValue(0.f,0.f,0.f);
115
116         int i = this->getMeshPartCount();
117         btScalar partmass = mass/btScalar(i);
118
119         while(i--)
120         {
121                 btVector3 partinertia;
122                 getMeshPart(i)->calculateLocalInertia(partmass,partinertia);
123                 inertia+=partinertia;
124         }
125
126 #else
127
128         // Calc box inertia
129
130         btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
131         btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
132         btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
133         const btScalar x2 = lx*lx;
134         const btScalar y2 = ly*ly;
135         const btScalar z2 = lz*lz;
136         const btScalar scaledmass = mass * btScalar(0.08333333);
137
138         inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
139
140 #endif
141 }
142
143 void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
144 {
145 }
146
147
148 void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
149 {
150         lockChildShapes();
151         btAABB box;
152         box.m_min = aabbMin;
153         box.m_max = aabbMax;
154
155         btAlignedObjectArray<int> collided;
156         m_box_set.boxQuery(box,collided);
157
158         if(collided.size()==0)
159         {
160                 unlockChildShapes();
161                 return;
162         }
163
164         int part = (int)getPart();
165         btPrimitiveTriangle triangle;
166         int i = collided.size();
167         while(i--)
168         {
169                 this->getPrimitiveTriangle(collided[i],triangle);
170                 callback->processTriangle(triangle.m_vertices,part,collided[i]);
171         }
172         unlockChildShapes();
173
174 }
175
176 void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
177 {
178         int i = m_mesh_parts.size();
179         while(i--)
180         {
181                 m_mesh_parts[i]->processAllTriangles(callback,aabbMin,aabbMax);
182         }
183 }
184
185
186 ///fills the dataBuffer and returns the struct name (and 0 on failure)
187 const char*     btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
188 {
189         btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*) dataBuffer;
190
191         btCollisionShape::serialize(&trimeshData->m_collisionShapeData,serializer);
192
193         m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
194
195         trimeshData->m_collisionMargin = float(m_collisionMargin);
196
197         localScaling.serializeFloat(trimeshData->m_localScaling);
198
199         trimeshData->m_gimpactSubType = int(getGImpactShapeType());
200
201         return "btGImpactMeshShapeData";
202 }
203