Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/external/bullet/BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.cpp @ 8740

Last change on this file since 8740 was 7983, checked in by rgrieder, 14 years ago

Updated Bullet Physics Engine from v2.74 to v2.77

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
17#include "btScaledBvhTriangleMeshShape.h"
18
19btScaledBvhTriangleMeshShape::btScaledBvhTriangleMeshShape(btBvhTriangleMeshShape* childShape,const btVector3& localScaling)
20:m_localScaling(localScaling),m_bvhTriMeshShape(childShape)
21{
22        m_shapeType = SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE;
23}
24
25btScaledBvhTriangleMeshShape::~btScaledBvhTriangleMeshShape()
26{
27}
28
29
30class btScaledTriangleCallback : public btTriangleCallback
31{
32        btTriangleCallback* m_originalCallback;
33
34        btVector3       m_localScaling;
35
36public:
37
38        btScaledTriangleCallback(btTriangleCallback* originalCallback,const 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
54void    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,scaledAabbMax;
60
61        ///support negative scaling
62        scaledAabbMin[0] = m_localScaling.getX() >= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
63        scaledAabbMin[1] = m_localScaling.getY() >= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
64        scaledAabbMin[2] = m_localScaling.getZ() >= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
65       
66        scaledAabbMax[0] = m_localScaling.getX() <= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
67        scaledAabbMax[1] = m_localScaling.getY() <= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
68        scaledAabbMax[2] = m_localScaling.getZ() <= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
69       
70       
71        m_bvhTriMeshShape->processAllTriangles(&scaledCallback,scaledAabbMin,scaledAabbMax);
72}
73
74
75void    btScaledBvhTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
76{
77        btVector3 localAabbMin = m_bvhTriMeshShape->getLocalAabbMin();
78        btVector3 localAabbMax = m_bvhTriMeshShape->getLocalAabbMax();
79
80        btVector3 tmpLocalAabbMin = localAabbMin * m_localScaling;
81        btVector3 tmpLocalAabbMax = localAabbMax * m_localScaling;
82
83        localAabbMin[0] = (m_localScaling.getX() >= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
84        localAabbMin[1] = (m_localScaling.getY() >= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
85        localAabbMin[2] = (m_localScaling.getZ() >= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
86        localAabbMax[0] = (m_localScaling.getX() <= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
87        localAabbMax[1] = (m_localScaling.getY() <= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
88        localAabbMax[2] = (m_localScaling.getZ() <= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
89
90        btVector3 localHalfExtents = btScalar(0.5)*(localAabbMax-localAabbMin);
91        btScalar margin = m_bvhTriMeshShape->getMargin();
92        localHalfExtents += btVector3(margin,margin,margin);
93        btVector3 localCenter = btScalar(0.5)*(localAabbMax+localAabbMin);
94       
95        btMatrix3x3 abs_b = trans.getBasis().absolute(); 
96
97        btVector3 center = trans(localCenter);
98
99        btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
100                   abs_b[1].dot(localHalfExtents),
101                  abs_b[2].dot(localHalfExtents));
102        aabbMin = center - extent;
103        aabbMax = center + extent;
104
105}
106
107void    btScaledBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
108{
109        m_localScaling = scaling;
110}
111
112const btVector3& btScaledBvhTriangleMeshShape::getLocalScaling() const
113{
114        return m_localScaling;
115}
116
117void    btScaledBvhTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
118{
119        ///don't make this a movable object!
120//      btAssert(0);
121}
Note: See TracBrowser for help on using the repository browser.