Page 1 of 1

Subdivide mesh into connected pieces

Posted: Sun Feb 24, 2013 8:32 pm
by eimixas
for example i have cube and sphere in one mesh, i want to split them to separate meshes (cube in one, sphere in other).
(simple selection with polyline is not sollution for all cases)

In my algorithm i need:
to get triangles that share same vertice (has same summit). and after collecting all triangles that are connected in this way - i could move it them to other mesh.

I looked GenericMesh, GenericIndexedMesh, ccMesh - for methods i need, but could not find any similar.
Searched CCLib - no luck.

Maybe there is any already?

Re: Subdivide mesh into connected pieces

Posted: Sun Feb 24, 2013 9:13 pm
by daniel
Indeed, there's no such method.

Those kind of algorithm are easy to implement, but they tend to be greedy (in terms of memory).

Re: Subdivide mesh into connected pieces

Posted: Mon Feb 25, 2013 1:47 pm
by eimixas
Yes it is gready, for mesh with ~1M triangles - i used 50M memory (with assumtion that 1 vertice could be shared between max 12 triangles).

Code: Select all

ccMesh *pMesh = static_cast<ccMesh*>(Entity);
ccGenericPointCloud *pCloud = pMesh->getAssociatedCloud();
...
CCLib::ReferenceCloud *selectedCloud = new CCLib::ReferenceCloud(pCloud);
...
ccGenericMesh *newMesh = pMesh->createNewMeshFromSelection(true, selectedCloud, pCloud);
in selectedCloud i add indexes of interconected points.
But I got problem: createNewMeshFromSelection returns NULL, because of code in ccMesh.cpp

Code: Select all

	ccGenericPointCloud::VisibilityTableType* visibilityArray = m_associatedCloud->getTheVisibilityArray();
	if (!visibilityArray || !visibilityArray->isAllocated())
	{
		//ccConsole::Error(QString("[Mesh %1] Visibility table not instantiated!").arg(getName()));
		return NULL;
	}
What is visibilityArray ?
Why do i need it?

Re: Subdivide mesh into connected pieces

Posted: Mon Feb 25, 2013 2:48 pm
by daniel
The 'visibilityArray' is in fact the array used to flag 'visible/selected' vertices. The 'createNewMeshFromSelection' will create a mesh by keeping only the 'selected/visible' vertices (all triangles with at least one unselected vertex won't be exported). It's the method used after manually segmenting a mesh (verties are either inside or outside the polyline).

It's a simple byte array, with one value per point. If you want to use it you must enable it then set for each vertex either 0 (unselected/invisible) or any other value (e.g. 255, which means that the vertex is visible/selected).

Re: Subdivide mesh into connected pieces

Posted: Mon Feb 25, 2013 3:41 pm
by eimixas
I will try,
but isn't it a duplicate for second method parameter "CCLib::ReferenceCloud* selection"?

if visibility array is used - i do not need "CCLib::ReferenceCloud* selection" - right?

Re: Subdivide mesh into connected pieces

Posted: Mon Feb 25, 2013 8:15 pm
by eimixas
Tried with visibily array - but without selection array - works.
But with "removeSelectedVertices" - triangles are removed from current Mesh, but points are left in PointCloud, should it be removed too?

I tried to remove it manualy:

Code: Select all

	ccGenericPointCloud *deletedCloud = pCloud->createNewCloudFromVisibilitySelection(true);
	if (deletedCloud != NULL)
		delete deletedCloud;
but then i get error at ccMesh.cpp 427 line:
assert(tri[0]<m_associatedCloud->size() && tri[1]<m_associatedCloud->size() && tri[2]<m_associatedCloud->size());

Re: Subdivide mesh into connected pieces

Posted: Tue Feb 26, 2013 8:32 am
by daniel
No, you must use the 'visibilityArray' structure (the optional 'selection' and 'vertices' input are just in case you already have instantiated those structures - in order to avoid duplication - or if you want that the method use a particular point cloud). You must respect the same points order however.

Re: Subdivide mesh into connected pieces

Posted: Tue Feb 26, 2013 8:38 am
by daniel
The issue here is that triangles that stay in the mesh keep their old indexes. So if you remove points, those indexes won't be valid anymore. The main issue is that vertices can be shared by multiple meshes. So in this case a single mesh don't know the others so we can't delete vertices that may be used by others...

You can however generate two new meshes (by inverting the visibility info between each) then delete the original mesh.