svn merge -r 16454:16593 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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,btVector3 localScaling)
19 :m_bvhTriMeshShape(childShape),
20 m_localScaling(localScaling)
21 {
22
23 }
24
25 btScaledBvhTriangleMeshShape::~btScaledBvhTriangleMeshShape()
26 {
27 }
28
29
30 class btScaledTriangleCallback : public btTriangleCallback
31 {
32         btTriangleCallback* m_originalCallback;
33
34         btVector3       m_localScaling;
35
36 public:
37
38         btScaledTriangleCallback(btTriangleCallback* originalCallback,btVector3 localScaling)
39                 :m_originalCallback(originalCallback),
40                 m_localScaling(localScaling)
41         {
42         }
43
44         virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
45         {
46                 btVector3 newTriangle[3];
47                 newTriangle[0] = triangle[0]*m_localScaling;
48                 newTriangle[1] = triangle[1]*m_localScaling;
49                 newTriangle[2] = triangle[2]*m_localScaling;
50                 m_originalCallback->processTriangle(&newTriangle[0],partId,triangleIndex);
51         }
52 };
53
54 void    btScaledBvhTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
55 {
56         btScaledTriangleCallback scaledCallback(callback,m_localScaling);
57         
58         btVector3 invLocalScaling(1.f/m_localScaling.getX(),1.f/m_localScaling.getY(),1.f/m_localScaling.getZ());
59         btVector3 scaledAabbMin = aabbMin * invLocalScaling;
60         btVector3 scaledAabbMax = aabbMax * invLocalScaling;
61         m_bvhTriMeshShape->processAllTriangles(&scaledCallback,scaledAabbMin,scaledAabbMax);
62 }
63
64
65 void    btScaledBvhTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
66 {
67         btVector3 localAabbMin = m_bvhTriMeshShape->getLocalAabbMin();
68         btVector3 localAabbMax = m_bvhTriMeshShape->getLocalAabbMax();
69         localAabbMin *= m_localScaling;
70         localAabbMax *= m_localScaling;
71
72         btVector3 localHalfExtents = btScalar(0.5)*(localAabbMax-localAabbMin);
73         btScalar margin = m_bvhTriMeshShape->getMargin();
74         localHalfExtents += btVector3(margin,margin,margin);
75         btVector3 localCenter = btScalar(0.5)*(localAabbMax+localAabbMin);
76         
77         btMatrix3x3 abs_b = trans.getBasis().absolute();  
78
79         btPoint3 center = trans(localCenter);
80
81         btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
82                    abs_b[1].dot(localHalfExtents),
83                   abs_b[2].dot(localHalfExtents));
84         aabbMin = center - extent;
85         aabbMax = center + extent;
86
87 }
88
89 void    btScaledBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
90 {
91         m_localScaling = scaling;
92 }
93
94 const btVector3& btScaledBvhTriangleMeshShape::getLocalScaling() const
95 {
96         return m_localScaling;
97 }
98
99 void    btScaledBvhTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
100 {
101         ///don't make this a movable object!
102         btAssert(0);
103 }