Esempio n. 1
0
bool CGALBuilder::triangulate()
{
	typedef CGAL::Triangulation_vertex_base_with_info_2<VertexInfo,CGAL::Kernel3> VertexBase;
	typedef CGAL::Triangulation_face_base_with_info_2<FaceInfo,CGAL::Kernel3> Info;
	typedef CGAL::Constrained_triangulation_face_base_2<CGAL::Kernel3,Info> FaceBase;
	typedef CGAL::Triangulation_data_structure_2<VertexBase,FaceBase> TDS;
	typedef CGAL::Exact_predicates_tag Tag;
	typedef CGAL::Constrained_triangulation_2<CGAL::Kernel3,TDS,Tag> CT;
	typedef CT::Vertex_handle VertexHandle;
	typedef CT::Face_iterator FaceIterator;


	QList<CGAL::Point3> points3=primitive.getPoints();
	int total=points3.size();
	if(total<3)
		return false;
	else if(total==3)
		return true;

	CT ct;
	TDS::size_type count=0;
	for(CGALPolygon* pg: primitive.getCGALPolygons()) {
		QList<int> indexes=pg->getIndexes();
		if(indexes.size()<3) continue;
		CGALProjection* pro=pg->getProjection();
		QList<CGAL::Point2> points2;
		for(auto i: indexes) {
			CGAL::Point2 p2=pro->project(points3.at(i));
			VertexHandle h=ct.insert(p2);
			h->info().index = i;
			points2.append(p2);
			++count;
		}

#if CGAL_VERSION_NR < CGAL_VERSION_NUMBER(4,6,0)
		insert_constraint(ct,points2.begin(),points2.end(),true);
#else
		ct.insert_constraint(points2.begin(),points2.end(),true);
#endif
	}

	if(count<3||ct.number_of_vertices()<count)
		return false;

	if(ct.number_of_faces()>3)
		markDomains(ct);

	primitive.clearPolygons();

	for(FaceIterator f=ct.finite_faces_begin(); f!=ct.finite_faces_end(); ++f) {
		if(f->info().inDomain()) {
			auto* pg=primitive.createCGALPolygon();
			for(auto i=0; i<3; ++i) {
				VertexInfo info=f->vertex(i)->info();
				if(info.isValid())
					pg->append(info.index);
			}
			pg->calculatePlane();
		}
	}
	return true;
}