Simplified Convex Decomposition Demo

Post Reply
User avatar
KKlouzal
Posts: 65
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Simplified Convex Decomposition Demo

Post by KKlouzal »

Are there any simplified convex decomposition demos or examples floating around out there? A forum/google search only returns a handful of results about the subject.

I've been using the supplied ConvexDecompositionDemo however the code feels like a mess to me and I'm having a hard time deciphering out the bare bones code that's needed to get the job done.

Function to retrieve raw Vertex and Index data from Irrlicht:

Code: Select all

	std::vector<HACD::Vec3<HACD::Real>> v_Vertices;
	std::vector<HACD::Vec3<long>> v_Indices;

	for (irr::u32 m = 0; m < MBCount; m++)
	{
		irr::scene::IMeshBuffer* MB = GetMesh()->getMeshBuffer(m);

		irr::video::S3DVertex* Vertices = (irr::video::S3DVertex*)MB->getVertices();
		irr::u16* Indices = MB->getIndices();

		for (irr::u32 i = 0; i < MB->getIndexCount(); i += 3)
		{
			irr::u32 Idx1 = Indices[i];
			irr::u32 Idx2 = Indices[i + 1];
			irr::u32 Idx3 = Indices[i + 2];

			v_Indices.push_back(HACD::Vec3<long>(Idx1, Idx2, Idx3));

			irr::core::vector3df Tri1Pos = Vertices[Indices[Idx1]].Pos;
			irr::core::vector3df Tri2Pos = Vertices[Indices[Idx2]].Pos;
			irr::core::vector3df Tri3Pos = Vertices[Indices[Idx3]].Pos;

			v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri1Pos.X, Tri1Pos.Y, Tri1Pos.Z));
			v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri2Pos.X, Tri2Pos.Y, Tri2Pos.Z));
			v_Vertices.push_back(HACD::Vec3<HACD::Real>(Tri3Pos.X, Tri3Pos.Y, Tri3Pos.Z));
		}
	}
CreateShapeHACD Function:

Code: Select all

	btCompoundShape* CreateShapeHACD(std::vector<HACD::Vec3<HACD::Real>> i_Vertices, std::vector<HACD::Vec3<long>> i_Indices)
	{
		HACD::HACD myHACD;
		myHACD.SetPoints(&i_Vertices[0]);
		myHACD.SetNPoints(i_Vertices.size());
		myHACD.SetTriangles(&i_Indices[0]);
		myHACD.SetNTriangles(i_Indices.size());
		myHACD.SetCompacityWeight(0.1);
		myHACD.SetVolumeWeight(0.0);
		myHACD.SetNClusters(1);                  // minimum number of clusters
		myHACD.SetNVerticesPerCH(100);            // max of 100 vertices per convex-hull
		myHACD.SetConcavity(100);               // maximum concavity
		myHACD.SetAddExtraDistPoints(false);
		myHACD.SetAddNeighboursDistPoints(false);
		myHACD.SetAddFacesPoints(false);
		myHACD.Compute();
		//
		//   Process the HACD data into individual Convex Hull Shapes
		//   And add them into one main Compound Shape
		//
		btAlignedObjectArray<btConvexHullShape*> m_convexShapes;
		btAlignedObjectArray<btVector3> m_convexCentroids;
		btCompoundShape* Compound = new btCompoundShape();
		btVector3 Centroid(0, 0, 0);
		btTransform Trans;
		Trans.setIdentity();
		//
		//   Create the individual pieces of the btCompoundShape
		//
		for (unsigned int c = 0; c < myHACD.GetNClusters(); c++)
		{
			size_t nPoints = myHACD.GetNPointsCH(c);
			size_t nTriangles = myHACD.GetNTrianglesCH(c);

			HACD::Vec3<HACD::Real>* PointsCH = new HACD::Vec3<HACD::Real>[nPoints];
			HACD::Vec3<long>* TrianglesCH = new HACD::Vec3<long>[nTriangles];
			myHACD.GetCH(c, PointsCH, TrianglesCH);

			btAlignedObjectArray<btVector3> h_Vertices;

			Centroid.setValue(0, 0, 0);
			for (size_t v = 0; v < nPoints; v++)
			{
				btVector3 Vertex(PointsCH[v].X(), PointsCH[v].Y(), PointsCH[v].Z());
				Centroid += Vertex;
				h_Vertices.push_back(Vertex);
			}
			Centroid /= (float(nPoints));
			m_convexCentroids.push_back(Centroid);

			delete[] PointsCH;
			delete[] TrianglesCH;

			btConvexHullShape* ConvexShape = new btConvexHullShape(&(h_Vertices[0].getX()), h_Vertices.size());

			ConvexShape->setMargin(0.01F);
			m_convexShapes.push_back(ConvexShape);
		}
		//
		//	Finally add the Convex Hull Shapes into the Compound Shape
		//
		for (unsigned int i = 0; i < m_convexShapes.size(); i++)
		{
			Trans.setOrigin(m_convexCentroids[i]);
			Compound->addChildShape(Trans, m_convexShapes[i]);
		}
		//
		//	Return our HACD Compound Shape
		//
		return Compound;
	}
The code in the supplied demo that deals with the 'ConvexDecomposition::ConvexDecompInterface' class is giving me a headache, especially when trying to remove it's usage along with the code dealing with importing/exporting to a .obj file.
Last edited by KKlouzal on Tue Sep 09, 2014 7:02 pm, edited 6 times in total.
User avatar
KKlouzal
Posts: 65
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Re: Simplified Convex Decomposition Demo

Post by KKlouzal »

Here is a video showing the previous code without the Centroid calculations commented out: http://youtu.be/XW9yLiZkrDk?t=1m

The bunny model at 1:00 in the video might have other problems but I can only hope it suffers from the same issue the cubes have except it's just extremely amplified due to the fact that the bunny model has so many more vertices than the cubes.
Last edited by KKlouzal on Tue Sep 09, 2014 7:00 pm, edited 3 times in total.
User avatar
KKlouzal
Posts: 65
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Re: Simplified Convex Decomposition Demo

Post by KKlouzal »

Has nobody ever used HACD before? :<
Granyte
Posts: 77
Joined: Tue Dec 27, 2011 11:51 am

Re: Simplified Convex Decomposition Demo

Post by Granyte »

I had similar issues with the HACD and no answer so I just let it go but if any one could help that would be great
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Simplified Convex Decomposition Demo

Post by Erwin Coumans »

I'll create a cleaner HACD example in the new Bullet OpenGL 3 demo framework. There is also another implementation/algorithm (CoRise) that is faster, but I haven't looked into it much yet. It is unpublished work, once published I will add it to Bullet.
User avatar
KKlouzal
Posts: 65
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Re: Simplified Convex Decomposition Demo

Post by KKlouzal »

Thank you very much for the reply Erwin, I was pulling my hair out with this. I'm looking forward to the new demo. If it were able to show how to take a vector of raw vertices and a vector of raw indices and output a btCompoundShape as I'm trying to do here that would probably be as simplistic as it can get. (CoRise) sounds great if it is indeed faster, cant wait for it!
User avatar
KKlouzal
Posts: 65
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Re: Simplified Convex Decomposition Demo

Post by KKlouzal »

I've narrowed the issue down to the centroid calculation. If I remove these lines:

Code: Select all

Centroid /= (float(nPoints));
Centroid += Vertex;
Then the cube models vertices are correct, the bunny model's vertices are not all scattered around anymore however they are all piled on top of each other as if the individual convex hulls are all in the same position, which is expected since the center points are all 0,0,0. I'm not sure how to properly calculate the centroid position. Any help here would be greatly appreciated!
Post Reply