Esempio n. 1
0
// The Real Core Function doing the actual mesh processing.
// Move Vertex of a random quantity
bool PoissonPlugin::applyFilter(QAction *filter, MeshDocument &md, RichParameterSet & par, vcg::CallBackPos *cb)
{
	MeshModel &m=*md.mm();
  MeshModel &pm =*md.addNewMesh("","Poisson mesh");
  vector<Point3D<float> > Pts(m.cm.vn);
	vector<Point3D<float> > Nor(m.cm.vn); 	
	CoredVectorMeshData mesh;

	if (m.hasDataMask(MeshModel::MM_WEDGTEXCOORD)){
		m.clearDataMask(MeshModel::MM_WEDGTEXCOORD);
	}
	if (m.hasDataMask(MeshModel::MM_VERTTEXCOORD)){
		m.clearDataMask(MeshModel::MM_VERTTEXCOORD);
	}

    //Useless control on the normals. It can just avoid crashes derived from an importer setting up to [0.0f,0.0f,0.0f] the normal vectors of a mesh without per-vertex normal attribute.
    int zeronrm = 0;
    for(CMeshO::VertexIterator vi=m.cm.vert.begin(); vi!=m.cm.vert.end(); ++vi)
    {
        if(!(*vi).IsD())
        {
            if ((*vi).N() == vcg::Point3f(0.0f,0.0f,0.0f))
                ++zeronrm;
        }
    }

    if (zeronrm == m.cm.vn)
    {
        Log(GLLogStream::SYSTEM,"All the normal vectors are set to [0.0,0.0,0.0]. Poisson reconstruction filter requires a set of valid per-vertex normal. Filter will be aborted.");
        return false;
    }

    int cnt=0;
	for(CMeshO::VertexIterator vi=m.cm.vert.begin(); vi!=m.cm.vert.end(); ++vi)
	if(!(*vi).IsD()){
			(*vi).N().Normalize();
			for(int ii=0;ii<3;++ii){
					Pts[cnt].coords[ii]=(*vi).P()[ii];
					Nor[cnt].coords[ii]=(*vi).N()[ii];
			}
			++cnt;
		}
	assert(cnt==m.cm.vn);
	// Log function dump textual info in the lower part of the MeshLab screen. 
	PoissonParam pp;
	pp.Depth=par.getInt("OctDepth");
	pp.SamplesPerNode = par.getFloat("SamplesPerNode");
	pp.SolverDivide=par.getInt("SolverDivide");
	pp.Offset = par.getFloat("Offset");
	Point3D<float> center;
	float scale;
	
	int ret= Execute2(pp, Pts, Nor, mesh,center,scale,cb);
	mesh.resetIterator();
	int vm = mesh.outOfCorePointCount()+mesh.inCorePoints.size();
	int fm = mesh.triangleCount();

	Log("Successfully created a mesh of %i vert and %i faces",vm,fm);
	
	//m.cm.Clear();
	
	tri::Allocator<CMeshO>::AddVertices(pm.cm,vm);
	tri::Allocator<CMeshO>::AddFaces(pm.cm,fm);

  Point3D<float> p;
	int i;
	for (i=0; i < int(mesh.inCorePoints.size()); i++){
		p=mesh.inCorePoints[i];
		pm.cm.vert[i].P()[0] = p.coords[0]*scale+center.coords[0];
		pm.cm.vert[i].P()[1] = p.coords[1]*scale+center.coords[1];
		pm.cm.vert[i].P()[2] = p.coords[2]*scale+center.coords[2];
		}
	for (int ii=0; ii < mesh.outOfCorePointCount(); ii++){
		mesh.nextOutOfCorePoint(p);
		pm.cm.vert[ii+i].P()[0] = p.coords[0]*scale+center.coords[0];
		pm.cm.vert[ii+i].P()[1] = p.coords[1]*scale+center.coords[1];
		pm.cm.vert[ii+i].P()[2] = p.coords[2]*scale+center.coords[2];
	}

TriangleIndex tIndex;
int inCoreFlag;
int nr_faces=mesh.triangleCount();	
for (i=0; i < nr_faces; i++){
		//
		// create and fill a struct that the ply code can handle
		//
		mesh.nextTriangle(tIndex,inCoreFlag);
		if(!(inCoreFlag & CoredMeshData::IN_CORE_FLAG[0])){tIndex.idx[0]+=int(mesh.inCorePoints.size());}
		if(!(inCoreFlag & CoredMeshData::IN_CORE_FLAG[1])){tIndex.idx[1]+=int(mesh.inCorePoints.size());}
		if(!(inCoreFlag & CoredMeshData::IN_CORE_FLAG[2])){tIndex.idx[2]+=int(mesh.inCorePoints.size());}
		for(int j=0; j < 3; j++)
		{
			pm.cm.face[i].V(j) = &pm.cm.vert[tIndex.idx[j]];
		}
		//ply_put_element(ply, (void *) &ply_face);
		//delete[] ply_face.vertices;
	}  // for, write faces


//	for(int i=0;i<mesh.inCorePoints.size();++i){
//		mesh.triangles[i].idx[0]+=mesh.inCorePoints.size();
//		mesh.triangles[i].idx[1]+=mesh.inCorePoints.size();
//		mesh.triangles[i].idx[2]+=mesh.inCorePoints.size();
//		}
//	Build(m.cm,mesh.inCorePoints,mesh.triangles);
	Log("Successfully created a mesh of %i faces",pm.cm.vn);
	
	pm.UpdateBoxAndNormals();
	return true;
}
Esempio n. 2
0
int qPoissonReconPlugin::doAction(ccHObject::Container& selectedEntities,
								  unsigned& uiModificationFlags,
								  ccProgressDialog* progressCb/*=NULL*/,
								  QWidget* parent/*=NULL*/)
{
	//we need one point cloud
    unsigned selNum = selectedEntities.size();
    if (selNum!=1)
        return -1;

	//a real point cloud
    ccHObject* ent = selectedEntities[0];
	if (!ent->isA(CC_POINT_CLOUD))
		return -1;

	//with normals!
    ccPointCloud* pc = static_cast<ccPointCloud*>(ent);
	if (!pc->hasNormals())
		return -2;

	bool ok;
	#if (QT_VERSION >= QT_VERSION_CHECK(4, 5, 0))
	int depth = QInputDialog::getInt(0, "Poisson reconstruction","Octree depth:", 8, 1, 24, 1, &ok);
	#else
	int depth = QInputDialog::getInteger(0, "Poisson reconstruction","Octree depth:", 8, 1, 24, 1, &ok);
	#endif

	if (!ok)
		return 1;

	 //TODO: faster, lighter
	unsigned i,count = pc->size();
	float* points = new float[count*3];
	if (!points)
		return -3;
	float* normals = new float[count*3];
	if (!normals)
	{
		delete[] points;
		return -3;
	}

	float* _points = points;
	float* _normals = normals;
	for (i=0;i<count;++i)
	{
		const CCVector3* P = pc->getPoint(i);
		*_points++ = (float)P->x;
		*_points++ = (float)P->y;
		*_points++ = (float)P->z;

		const PointCoordinateType* N = pc->getPointNormal(i);
		*_normals++ = (float)N[0];
		*_normals++ = (float)N[1];
		*_normals++ = (float)N[2];
	}

	/*** RECONSTRUCTION PROCESS ***/

	CoredVectorMeshData mesh;
	PoissonReconLib::PoissonReconResultInfo info;
	bool result = false;

	if (progressCb)
	{
		progressCb->setCancelButton(0);
		progressCb->setRange(0,0);
		progressCb->setInfo("Operation in progress");
		progressCb->setMethodTitle("Poisson Reconstruction");
		progressCb->start();
		//QApplication::processEvents();

		//run in a separate thread
		s_points = points;
		s_normals = normals;
		s_count = count;
		s_depth = depth;
		s_mesh = &mesh;
		s_info = &info;
		QFuture<void> future = QtConcurrent::run(doReconstruct);

		unsigned progress = 0;
		while (!future.isFinished())
		{
		    #if defined(_WIN32) || defined(WIN32)
			::Sleep(500);
			#else
			sleep(500);
			#endif

			progressCb->update(++progress);
			//Qtconcurrent::run can't be canceled!
			/*if (progressCb->isCancelRequested())
			{
				future.cancel();
				future.waitForFinished();
				s_result = false;
				break;
			}
			//*/
		}

		result = s_result;

		progressCb->stop();
		QApplication::processEvents();
	}
	else
	{
		result = PoissonReconLib::reconstruct(count, points, normals, mesh, depth, &info);
	}

	delete[] points;
	points=0;
	delete[] normals;
	normals=0;

	if (!result || mesh.polygonCount() < 1)
		return -4;

	unsigned nic         = (unsigned)mesh.inCorePoints.size();
	unsigned noc         = (unsigned)mesh.outOfCorePointCount();
	unsigned nr_vertices = nic+noc;
	unsigned nr_faces    = (unsigned)mesh.polygonCount();

	ccPointCloud* newPC = new ccPointCloud("vertices");
	newPC->reserve(nr_vertices);

	//we enlarge bounding box a little bit (2%)
	PointCoordinateType bbMin[3],bbMax[3];
	pc->getBoundingBox(bbMin,bbMax);
	CCVector3 boxHalfDiag = (CCVector3(bbMax)-CCVector3(bbMin))*0.51f;
	CCVector3 boxCenter = (CCVector3(bbMax)+CCVector3(bbMin))*0.5f;
	CCVector3 filterMin = boxCenter-boxHalfDiag;
	CCVector3 filterMax = boxCenter+boxHalfDiag;

	Point3D<float> p;
	CCVector3 p2;
	for (i=0; i<nic; i++)
	{
		p = mesh.inCorePoints[i];
		p2.x = p.coords[0]*info.scale+info.center[0];
		p2.y = p.coords[1]*info.scale+info.center[1];
		p2.z = p.coords[2]*info.scale+info.center[2];
		newPC->addPoint(p2);
	}
	for (i=0; i<noc; i++)
	{
		mesh.nextOutOfCorePoint(p);
		p2.x = p.coords[0]*info.scale+info.center[0];
		p2.y = p.coords[1]*info.scale+info.center[1];
		p2.z = p.coords[2]*info.scale+info.center[2];
		newPC->addPoint(p2);
	}

	ccMesh* newMesh = new ccMesh(newPC);
	newMesh->setName(QString("Mesh[%1] (level %2)").arg(pc->getName()).arg(depth));
	newMesh->reserve(nr_faces);
	newMesh->addChild(newPC);

	std::vector<CoredVertexIndex> vertices;
	for (i=0; i < nr_faces; i++)
	{
		mesh.nextPolygon(vertices);

		if (vertices.size()!=3)
		{
			//Can't handle anything else than triangles yet!
			assert(false);
		}
		else
		{
			for (std::vector<CoredVertexIndex>::iterator it = vertices.begin(); it != vertices.end(); ++it)
				if (!it->inCore)
					it->idx += nic;

			newMesh->addTriangle(vertices[0].idx,
								vertices[1].idx,
								vertices[2].idx);
		}
	}

	newPC->setVisible(false);
	newMesh->setVisible(true);
	newMesh->computeNormals();

	//output mesh
	selectedEntities.push_back(newMesh);

	//currently selected entities parameters may have changed!
    uiModificationFlags |= CC_PLUGIN_REFRESH_ENTITY_BROWSER;
    //currently selected entities appearance may have changed!
    uiModificationFlags |= CC_PLUGIN_REFRESH_GL_WINDOWS;

    return 1;
}