1fac15387697f70da3896826d0041ee9369bcd6d
[blender.git] / extern / bullet2 / src / BulletCollision / CollisionShapes / btScaledBvhTriangleMeshShape.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2008 Erwin Coumans  http://continuousphysics.com/Bullet/
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 "btScaledBvhTriangleMeshShape.h"
17
18 btScaledBvhTriangleMeshShape::btScaledBvhTriangleMeshShape(btBvhTriangleMeshShape* childShape,const btVector3& localScaling)
19 :m_localScaling(localScaling),m_bvhTriMeshShape(childShape)
20 {
21         m_shapeType = SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE;
22 }
23
24 btScaledBvhTriangleMeshShape::~btScaledBvhTriangleMeshShape()
25 {
26 }
27
28
29 class btScaledTriangleCallback : public btTriangleCallback
30 {
31         btTriangleCallback* m_originalCallback;
32
33         btVector3       m_localScaling;
34
35 public:
36
37         btScaledTriangleCallback(btTriangleCallback* originalCallback,const btVector3& localScaling)
38                 :m_originalCallback(originalCallback),
39                 m_localScaling(localScaling)
40         {
41         }
42
43         virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
44         {
45                 btVector3 newTriangle[3];
46                 newTriangle[0] = triangle[0]*m_localScaling;
47                 newTriangle[1] = triangle[1]*m_localScaling;
48                 newTriangle[2] = triangle[2]*m_localScaling;
49                 m_originalCallback->processTriangle(&newTriangle[0],partId,triangleIndex);
50         }
51 };
52
53 void    btScaledBvhTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
54 {
55         btScaledTriangleCallback scaledCallback(callback,m_localScaling);
56         
57         btVector3 invLocalScaling(1.f/m_localScaling.getX(),1.f/m_localScaling.getY(),1.f/m_localScaling.getZ());
58         btVector3 scaledAabbMin,scaledAabbMax;
59
60         ///support negative scaling
61         scaledAabbMin[0] = m_localScaling.getX() >= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
62         scaledAabbMin[1] = m_localScaling.getY() >= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
63         scaledAabbMin[2] = m_localScaling.getZ() >= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
64         
65         scaledAabbMax[0] = m_localScaling.getX() <= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
66         scaledAabbMax[1] = m_localScaling.getY() <= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
67         scaledAabbMax[2] = m_localScaling.getZ() <= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
68         
69         
70         m_bvhTriMeshShape->processAllTriangles(&scaledCallback,scaledAabbMin,scaledAabbMax);
71 }
72
73
74 void    btScaledBvhTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
75 {
76         btVector3 localAabbMin = m_bvhTriMeshShape->getLocalAabbMin();
77         btVector3 localAabbMax = m_bvhTriMeshShape->getLocalAabbMax();
78
79         btVector3 tmpLocalAabbMin = localAabbMin * m_localScaling;
80         btVector3 tmpLocalAabbMax = localAabbMax * m_localScaling;
81
82         localAabbMin[0] = (m_localScaling.getX() >= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
83         localAabbMin[1] = (m_localScaling.getY() >= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
84         localAabbMin[2] = (m_localScaling.getZ() >= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
85         localAabbMax[0] = (m_localScaling.getX() <= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
86         localAabbMax[1] = (m_localScaling.getY() <= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
87         localAabbMax[2] = (m_localScaling.getZ() <= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
88
89         btVector3 localHalfExtents = btScalar(0.5)*(localAabbMax-localAabbMin);
90         btScalar margin = m_bvhTriMeshShape->getMargin();
91         localHalfExtents += btVector3(margin,margin,margin);
92         btVector3 localCenter = btScalar(0.5)*(localAabbMax+localAabbMin);
93         
94         btMatrix3x3 abs_b = trans.getBasis().absolute();  
95
96         btVector3 center = trans(localCenter);
97
98         btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
99                    abs_b[1].dot(localHalfExtents),
100                   abs_b[2].dot(localHalfExtents));
101         aabbMin = center - extent;
102         aabbMax = center + extent;
103
104 }
105
106 void    btScaledBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
107 {
108         m_localScaling = scaling;
109 }
110
111 const btVector3& btScaledBvhTriangleMeshShape::getLocalScaling() const
112 {
113         return m_localScaling;
114 }
115
116 void    btScaledBvhTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
117 {
118         ///don't make this a movable object!
119 //      btAssert(0);
120 }