2 This source file is part of GIMPACT Library.
4 For the latest info, see http://gimpact.sourceforge.net/
6 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
7 email: projectileman@yahoo.com
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:
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.
21 #include "btGImpactConvexDecompositionShape.h"
22 #include "BulletCollision/CollisionShapes/btConvexHullShape.h"
24 #include "ConvexBuilder.h"
26 class GIM_ConvexDecomposition : public ConvexDecomposition::ConvexDecompInterface
29 btGImpactConvexDecompositionShape * m_compoundShape;
31 btAlignedObjectArray<btCollisionShape*> m_convexShapes;
37 bool m_transformSubShapes;
39 GIM_ConvexDecomposition(btGImpactConvexDecompositionShape * compoundShape,bool transformSubShapes)
43 m_compoundShape = compoundShape;
44 m_transformSubShapes = transformSubShapes;
47 virtual ~GIM_ConvexDecomposition()
50 for (i=0;i<m_convexShapes.size();i++)
52 btCollisionShape* shape = m_convexShapes[i];
58 virtual void ConvexDecompResult(ConvexDecomposition::ConvexResult &result)
61 //calc centroid, to shift vertices around center of mass
62 btVector3 centroid(0,0,0);
63 btAlignedObjectArray<btVector3> vertices;
65 if(m_transformSubShapes)
68 //const unsigned int *src = result.mHullIndices;
69 for (unsigned int i=0; i<result.mHullVcount; i++)
71 btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
76 centroid *= 1.f/(float(result.mHullVcount) );
80 for (unsigned int i=0; i<result.mHullVcount; i++)
82 btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
84 if(m_transformSubShapes)
88 vertices.push_back(vertex);
93 btCollisionShape* convexShape = new btConvexHullShape(
94 &(vertices[0].getX()),vertices.size(),sizeof(btVector3));
95 m_convexShapes.push_back(convexShape);
97 convexShape->setMargin(m_compoundShape->getMargin());
99 if(m_transformSubShapes)
103 trans.setOrigin(centroid);
107 m_compoundShape->addChildShape(trans,convexShape);
113 //trans.setOrigin(centroid);
117 m_compoundShape->addChildShape(trans,convexShape);
119 //m_compoundShape->addChildShape(convexShape);
123 void processDecomposition(int part)
125 btGImpactMeshShapePart::TrimeshPrimitiveManager * trimeshInterface =
126 m_compoundShape->getTrimeshInterface(part);
129 trimeshInterface->lock();
132 btAlignedObjectArray<float> vertices;
133 vertices.reserve(trimeshInterface->get_vertex_count()*3);
135 for(int vi = 0;vi<trimeshInterface->get_vertex_count();vi++)
138 trimeshInterface->get_vertex(vi,vec);
139 vertices.push_back(vec[0]);
140 vertices.push_back(vec[1]);
141 vertices.push_back(vec[2]);
146 btAlignedObjectArray<unsigned int> indices;
147 indices.reserve(trimeshInterface->get_primitive_count()*3);
150 for(int i = 0;i<trimeshInterface->get_primitive_count();i++)
153 trimeshInterface->get_indices(i,i0,i1,i2);
154 indices.push_back(i0);
155 indices.push_back(i1);
156 indices.push_back(i2);
159 trimeshInterface->unlock();
163 unsigned int depth = 5;
166 unsigned int maxv = 16;
167 float skinWidth = 0.0f;
170 ConvexDecomposition::DecompDesc desc;
171 desc.mVcount = trimeshInterface->get_vertex_count();
172 desc.mVertices = &vertices[0];
173 desc.mTcount = trimeshInterface->get_primitive_count();
174 desc.mIndices = &indices[0];
176 desc.mCpercent = cpercent;
177 desc.mPpercent = ppercent;
178 desc.mMaxVertices = maxv;
179 desc.mSkinWidth = skinWidth;
180 desc.mCallback = this;
182 //convexDecomposition.performConvexDecomposition(desc);
184 ConvexBuilder cb(desc.mCallback);
195 void btGImpactConvexDecompositionShape::buildConvexDecomposition(bool transformSubShapes)
198 m_decomposition = new GIM_ConvexDecomposition(this,transformSubShapes);
200 int part_count = m_trimeshInterfaces.size();
201 for (int i = 0;i<part_count ;i++ )
203 m_decomposition->processDecomposition(i);
209 btGImpactConvexDecompositionShape::~btGImpactConvexDecompositionShape()
211 delete m_decomposition;
213 void btGImpactConvexDecompositionShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
216 int part_count = m_trimeshInterfaces.size();
217 for (int part = 0;part<part_count ;part++ )
219 void * ptr = (void * )&m_trimeshInterfaces[part];
221 btGImpactMeshShapePart::TrimeshPrimitiveManager * trimeshInterface =
222 static_cast<btGImpactMeshShapePart::TrimeshPrimitiveManager *>(ptr);
224 trimeshInterface->lock();
226 btPrimitiveTriangle triangle;
229 int i = trimeshInterface->get_primitive_count();
232 trimeshInterface->get_primitive_triangle(i,triangle);
233 callback->processTriangle(triangle.m_vertices,part,i);
236 trimeshInterface->unlock();