void FixedPlaneMesh::TrianglatePolygon(const double3& normal, std::vector<Point3D>& pts, ListOfvertices& results) { if (pts.size() < 3) return ; if (pts.size() ==3) { VertexInfo vi1(pts[0], normal, mColor); VertexInfo vi2(pts[1], normal, mColor); VertexInfo vi3(pts[2], normal, mColor); results.push_back(vi1); results.push_back(vi2); results.push_back(vi3); return ; } typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Triangulation_vertex_base_2<K> Vb; typedef CGAL::Constrained_triangulation_face_base_2<K> Fb; typedef CGAL::Triangulation_data_structure_2<Vb,Fb> TDS; //typedef CGAL::Exact_predicates_tag Itag; typedef CGAL::Constrained_Delaunay_triangulation_2<K, TDS, CGAL::No_intersection_tag> CDT; vec3<double> origin = pts[0]; vec3<double> N = normal; vec3<double> U = normalize(pts[1] - origin); vec3<double> V = cross(N, U); CDT cdt; CDT::Vertex_handle vh1, vh2, vh3; vec3<double> v0 = PosToLocal(U, V, N, origin, pts[0]); CDT::Point p0(v0.x, v0.y); vh1 = vh3 = cdt.insert(p0); for ( int i = 1; i< pts.size() ; i++) { vec3<double> v1 = PosToLocal(U, V, N, origin, pts[i]); CDT::Point p1(v1.x, v1.y); vh2 = cdt.insert(p1); cdt.insert_constraint(vh1, vh2); vh1 = vh2; } cdt.insert_constraint(vh2, vh3); int count = cdt.number_of_faces() ; results.reserve(count*3); for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); ++fit) { vec2<double> v0(fit->vertex(2)->point().x(),fit->vertex(2)->point().y() ); vec2<double> v1(fit->vertex(1)->point().x(),fit->vertex(1)->point().y() ); vec2<double> v2(fit->vertex(0)->point().x(),fit->vertex(0)->point().y() ); if (IsEqual(cross(v0- v2, v1-v2), (double)0., (double)EPSF )) continue; // vec3<double > p0(v0, 0.0); vec3<double > p1(v1, 0.0); vec3<double > p2(v2, 0.0); p0 = PosToGlobal(U, V, N, origin, p0); p1 = PosToGlobal(U, V, N, origin, p1); p2 = PosToGlobal(U, V, N, origin, p2); VertexInfo vi1(p0, N, mColor); VertexInfo vi2(p1, N, mColor); VertexInfo vi3(p2, N, mColor); results.push_back(vi1); results.push_back(vi2); results.push_back(vi3); } }
bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D, const std::vector<int>& segments2D, char* outputErrorStr/*=0*/) { #if defined(USE_CGAL_LIB) //CGAL boilerplate typedef CGAL::Exact_predicates_inexact_constructions_kernel K; //We define a vertex_base with info. The "info" (size_t) allow us to keep track of the original point index. typedef CGAL::Triangulation_vertex_base_with_info_2<size_t, K> Vb; typedef CGAL::Constrained_triangulation_face_base_2<K> Fb; typedef CGAL::No_intersection_tag Itag; //This tag could ben changed if we decide to handle intersection typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds; typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds, Itag> CDT; typedef CDT::Point cgalPoint; std::vector< std::pair<cgalPoint, size_t > > constraints; size_t constrCount = segments2D.size(); try { constraints.reserve(constrCount); } catch (const std::bad_alloc&) { if (outputErrorStr) strcpy(outputErrorStr, "Not enough memory"); return false; }; //We create the Constrained Delaunay Triangulation (CDT) CDT cdt; //We build the constraints for(size_t i = 0; i < constrCount; ++i) { const CCVector2 * pt = &points2D[segments2D[i]]; constraints.push_back(std::make_pair(cgalPoint(pt->x, pt->y), segments2D[i])); } //The CDT is built according to the constraints cdt.insert(constraints.begin(), constraints.end()); m_numberOfTriangles = static_cast<unsigned >(cdt.number_of_faces()); m_triIndexes = new int[cdt.number_of_faces()*3]; //The cgal data structure is converted into CC one if (m_numberOfTriangles > 0) { int faceCount = 0; for (CDT::Face_iterator face = cdt.faces_begin(); face != cdt.faces_end(); ++face, faceCount+=3) { m_triIndexes[0+faceCount] = static_cast<int>(face->vertex(0)->info()); m_triIndexes[1+faceCount] = static_cast<int>(face->vertex(1)->info()); m_triIndexes[2+faceCount] = static_cast<int>(face->vertex(2)->info()); }; } m_globalIterator = m_triIndexes; m_globalIteratorEnd = m_triIndexes + 3*m_numberOfTriangles; return true; #else if (outputErrorStr) strcpy(outputErrorStr, "CGAL library not supported"); return false; #endif }
int main() { typedef viennagrid::plc_2d_mesh mesh_type; mesh_type mesh; typedef viennagrid::result_of::point<mesh_type>::type point_type; typedef viennagrid::result_of::element<mesh_type, viennagrid::vertex_tag>::type vertex_type; typedef viennagrid::result_of::handle<mesh_type, viennagrid::vertex_tag>::type vertex_handle_type; typedef viennagrid::result_of::element<mesh_type, viennagrid::line_tag>::type line_type; typedef viennagrid::result_of::handle<mesh_type, viennagrid::line_tag>::type line_handle_type; typedef viennagrid::result_of::element<mesh_type, viennagrid::plc_tag>::type plc_type; typedef viennagrid::result_of::handle<mesh_type, viennagrid::plc_tag>::type plc_handle_type; plc_handle_type plc_handle; { std::vector<vertex_handle_type> v; v.push_back( viennagrid::make_vertex( mesh, point_type(0, 0) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(10, 0) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(20, 10) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(20, 20) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(10, 20) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(0, 10) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(5, 5) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(10, 10) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(12, 10) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(10, 12) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(8, 10) ) ); v.push_back( viennagrid::make_vertex( mesh, point_type(15, 15) ) ); std::vector<line_handle_type> lines; { std::vector<vertex_handle_type>::iterator start = v.begin(); std::vector<vertex_handle_type>::iterator end = v.begin() + 7; std::vector<vertex_handle_type>::iterator it1 = start; std::vector<vertex_handle_type>::iterator it2 = it1; ++it2; for (; it2 != end; ++it1, ++it2) lines.push_back( viennagrid::make_line(mesh, *it1, *it2) ); lines.push_back( viennagrid::make_line(mesh, *it1, *start) ); } { std::vector<vertex_handle_type>::iterator start = v.begin() + 7; std::vector<vertex_handle_type>::iterator end = v.begin() + 10; std::vector<vertex_handle_type>::iterator it1 = start; std::vector<vertex_handle_type>::iterator it2 = it1; ++it2; for (; it2 != end; ++it1, ++it2) lines.push_back( viennagrid::make_line(mesh, *it1, *it2) ); lines.push_back( viennagrid::make_line(mesh, *it1, *start) ); } lines.push_back( viennagrid::make_element<line_type>( mesh, v.begin() + 9, v.begin() + 11 ) ); vertex_handle_type point = v[11]; std::vector<point_type> hole_points; hole_points.push_back( point_type(10.5, 10.5) ); plc_handle = viennagrid::make_plc( mesh, lines.begin(), lines.end(), &point, &point + 1, hole_points.begin(), hole_points.end() ); } plc_type & plc = viennagrid::dereference_handle(mesh, plc_handle); typedef viennagrid::result_of::element_range<plc_type, viennagrid::vertex_tag>::type vertex_range_type; typedef viennagrid::result_of::iterator<vertex_range_type>::type vertex_range_iterator; typedef viennagrid::result_of::element_range<plc_type, viennagrid::line_tag>::type line_range_type; typedef viennagrid::result_of::iterator<line_range_type>::type line_range_iterator; typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; typedef CGAL::Triangulation_vertex_base_2<Kernel> VertexBase; typedef CGAL::Delaunay_mesh_face_base_2<Kernel> FaceBase; typedef CGAL::Triangulation_data_structure_2<VertexBase, FaceBase> Triangulation_structure; typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel, Triangulation_structure> CDT; typedef CGAL::Delaunay_mesh_size_criteria_2<CDT> Criteria; typedef CDT::Vertex_handle Vertex_handle; typedef CDT::Point Point; CDT cdt; std::map<vertex_handle_type, Vertex_handle> vertex_handle_map; vertex_range_type vertices = viennagrid::elements<viennagrid::vertex_tag>(plc); for (vertex_range_iterator it = vertices.begin(); it != vertices.end(); ++it) { vertex_handle_type const & vtx_handle = it.handle(); vertex_type const & vtx = *it; point_type const & vgrid_point = viennagrid::point( mesh, vtx ); Vertex_handle handle = cdt.insert( Point(vgrid_point[0], vgrid_point[1]) ); vertex_handle_map[vtx_handle] = handle; } line_range_type lines = viennagrid::elements<viennagrid::line_tag>(plc); for (line_range_iterator it = lines.begin(); it != lines.end(); ++it) { line_type & line = *it; vertex_handle_type vgrid_v0 = viennagrid::elements<viennagrid::vertex_tag>(line).handle_at(0); vertex_handle_type vgrid_v1 = viennagrid::elements<viennagrid::vertex_tag>(line).handle_at(1); Vertex_handle cgal_v0 = vertex_handle_map[vgrid_v0]; Vertex_handle cgal_v1 = vertex_handle_map[vgrid_v1]; cdt.insert_constraint(cgal_v0, cgal_v1); } std::vector<point_type> & vgrid_list_of_holes = viennagrid::hole_points(plc); std::list<Point> cgal_list_of_holes; for (std::vector<point_type>::iterator it = vgrid_list_of_holes.begin(); it != vgrid_list_of_holes.end(); ++it) cgal_list_of_holes.push_back( Point( (*it)[0], (*it)[1] ) ); CGAL::refine_Delaunay_mesh_2(cdt, cgal_list_of_holes.begin(), cgal_list_of_holes.end(), Criteria()); std::cout << "Number of vertices: " << cdt.number_of_vertices() << std::endl; std::cout << "Number of finite faces: " << cdt.number_of_faces() << std::endl; typedef viennagrid::triangular_2d_mesh triangle_mesh_type; triangle_mesh_type triangle_mesh; typedef viennagrid::result_of::point<triangle_mesh_type>::type triangle_point_type; typedef viennagrid::result_of::element<triangle_mesh_type, viennagrid::vertex_tag>::type triangle_vertex_type; typedef viennagrid::result_of::handle<triangle_mesh_type, viennagrid::vertex_tag>::type triangle_vertex_handle_type; typedef viennagrid::result_of::element<triangle_mesh_type, viennagrid::line_tag>::type triangle_line_type; typedef viennagrid::result_of::handle<triangle_mesh_type, viennagrid::line_tag>::type triangle_line_handle_type; typedef viennagrid::result_of::element<triangle_mesh_type, viennagrid::triangle_tag>::type triangle_triangle_type; typedef viennagrid::result_of::handle<triangle_mesh_type, viennagrid::triangle_tag>::type triangle_triangle__handle_type; std::map<Point, triangle_vertex_handle_type> points; int mesh_faces_counter = 0; for(CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); ++fit) { if(fit->is_in_domain()) { typedef CDT::Triangle Triangle; Triangle tri = cdt.triangle(fit); triangle_vertex_handle_type vgrid_vtx[3]; for (int i = 0; i < 3; ++i) { std::map<Point, triangle_vertex_handle_type>::iterator pit = points.find( tri[i] ); if (pit == points.end()) { vgrid_vtx[i] = viennagrid::make_vertex( triangle_mesh, triangle_point_type(tri[i].x(), tri[i].y()) ); points[ tri[i] ] = vgrid_vtx[i]; } else vgrid_vtx[i] = pit->second; } viennagrid::make_element<triangle_triangle_type>( triangle_mesh, vgrid_vtx, vgrid_vtx+3 ); std::cout << tri << std::endl; ++mesh_faces_counter; } } std::cout << "Number of faces in the mesh mesh: " << mesh_faces_counter << std::endl; std::copy( viennagrid::elements<triangle_triangle_type>(triangle_mesh).begin(), viennagrid::elements<triangle_triangle_type>(triangle_mesh).end(), std::ostream_iterator<triangle_triangle_type>(std::cout, "\n") ); viennagrid::io::vtk_writer<triangle_mesh_type> vtk_writer; vtk_writer(triangle_mesh, "test"); }
int main() { //construct two non-intersecting nested polygons Polygon_2 polygon1; polygon1.push_back(Point_2(0.0, 0.0)); polygon1.push_back(Point_2(2.0, 0.0)); polygon1.push_back(Point_2(1.7, 1.0)); polygon1.push_back(Point_2(2.0, 2.0)); polygon1.push_back(Point_2(0.0, 2.0)); Polygon_2 polygon2; polygon2.push_back(Point_2(0.5, 0.5)); polygon2.push_back(Point_2(1.5, 0.5)); polygon2.push_back(Point_2(1.5, 1.5)); polygon2.push_back(Point_2(0.5, 1.5)); //Insert the polyons into a constrained triangulation CDT cdt; insert_polygon(cdt, polygon1); insert_polygon(cdt, polygon2); //Extract point and provide the an index std::vector< triangulation_point > points ; for ( CDT::Vertex_iterator it = cdt.vertices_begin(); it != cdt.vertices_end(); ++it ){ it->info() = points.size() ; points.push_back( it->point() ); } //Mark facets that are inside the domain bounded by the polygon mark_domains(cdt); // int count = 0; for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); ++fit) { if (fit->info().in_domain()){ ++count; } } /* * export */ std::ofstream ofs("polygon_triangulation2.obj"); if ( ! ofs.good() ){ std::cout << "can't open file" << std::endl; return 1 ; } //-- print vertices ofs << "# " << points.size() << " vertices"<< std::endl ; for ( size_t i = 0; i < points.size(); i++ ){ ofs << "v " << points[i] << " 0.0" << std::endl; } //-- print faces ofs << "# " << cdt.number_of_faces() << " faces"<< std::endl ; // warning : Delaunay_triangulation_2::All_faces_iterator iterator over infinite faces for ( CDT::Finite_faces_iterator it = cdt.finite_faces_begin(); it != cdt.finite_faces_end(); ++it ) { //ignore holes if ( ! it->info().in_domain() ){ continue ; } size_t ia = it->vertex(0)->info(); size_t ib = it->vertex(1)->info(); size_t ic = it->vertex(2)->info(); assert( it->is_valid() ); //assert ( ia < cdt.number_of_vertices() || ib < tri.number_of_vertices() || ic < tri.number_of_vertices() ) ; ofs << "f " << ( ia + 1 ) << " " << ( ib + 1 ) << " " << ( ic + 1 ) << std::endl; } return 0; }
bool Delaunay2dMesh::buildMesh( const std::vector<CCVector2>& points2D, const std::vector<int>& segments2D, char* outputErrorStr/*=0*/) { #if defined(USE_TRIANGLE_LIB) //we use the external library 'Triangle' triangulateio in; memset(&in,0,sizeof(triangulateio)); in.numberofpoints = static_cast<int>(points2D.size()); in.pointlist = (REAL*)(&points2D[0]); in.segmentlist = (int*)(&segments2D[0]); assert((segments2D.size() & 1) == 0); in.numberofsegments = static_cast<int>(segments2D.size()/2); triangulateio out; memset(&out,0,sizeof(triangulateio)); try { triangulate ( "pczBPNIOQY", &in, &out, 0 ); } catch (std::exception& e) { if (outputErrorStr) strcpy(outputErrorStr,e.what()); return false; } catch (...) { if (outputErrorStr) strcpy(outputErrorStr,"Unknown error"); return false; } m_numberOfTriangles = out.numberoftriangles; if (m_numberOfTriangles > 0) { m_triIndexes = out.trianglelist; //remove non existing points int* _tri = out.trianglelist; for (int i=0; i<out.numberoftriangles; ) { if ( _tri[0] >= in.numberofpoints || _tri[1] >= in.numberofpoints || _tri[2] >= in.numberofpoints) { int lasTriIndex = (out.numberoftriangles-1) * 3; _tri[0] = out.trianglelist[lasTriIndex + 0]; _tri[1] = out.trianglelist[lasTriIndex + 1]; _tri[2] = out.trianglelist[lasTriIndex + 2]; --out.numberoftriangles; } else { _tri += 3; ++i; } } //Reduce memory size if (out.numberoftriangles < static_cast<int>(m_numberOfTriangles)) { assert(out.numberoftriangles > 0); realloc(m_triIndexes, sizeof(int)*out.numberoftriangles*3); m_numberOfTriangles = out.numberoftriangles; } } trifree(out.segmentmarkerlist); trifree(out.segmentlist); m_globalIterator = m_triIndexes; m_globalIteratorEnd = m_triIndexes + 3*m_numberOfTriangles; return true; #elif defined(USE_CGAL_LIB) //CGAL boilerplate typedef CGAL::Exact_predicates_inexact_constructions_kernel K; //We define a vertex_base with info. The "info" (size_t) allow us to keep track of the original point index. typedef CGAL::Triangulation_vertex_base_with_info_2<size_t, K> Vb; typedef CGAL::Constrained_triangulation_face_base_2<K> Fb; typedef CGAL::No_intersection_tag Itag; //This tag could ben changed if we decide to handle intersection typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds; typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds, Itag> CDT; typedef CDT::Point cgalPoint; std::vector< std::pair<cgalPoint, size_t > > constraints; size_t constrCount = segments2D.size(); try { constraints.reserve(constrCount); } catch (const std::bad_alloc&) { if (outputErrorStr) strcpy(outputErrorStr, "Not enough memory"); return false; }; //We create the Constrained Delaunay Triangulation (CDT) CDT cdt; //We build the constraints for(size_t i = 0; i < constrCount; ++i) { const CCVector2 * pt = &points2D[segments2D[i]]; constraints.push_back(std::make_pair(cgalPoint(pt->x, pt->y), segments2D[i])); } //The CDT is built according to the constraints cdt.insert(constraints.begin(), constraints.end()); m_numberOfTriangles = static_cast<unsigned >(cdt.number_of_faces()); m_triIndexes = new int[cdt.number_of_faces()*3]; //The cgal data structure is converted into CC one if (m_numberOfTriangles > 0) { int faceCount = 0; for (CDT::Face_iterator face = cdt.faces_begin(); face != cdt.faces_end(); ++face, faceCount+=3) { m_triIndexes[0+faceCount] = static_cast<int>(face->vertex(0)->info()); m_triIndexes[1+faceCount] = static_cast<int>(face->vertex(1)->info()); m_triIndexes[2+faceCount] = static_cast<int>(face->vertex(2)->info()); }; } m_globalIterator = m_triIndexes; m_globalIteratorEnd = m_triIndexes + 3*m_numberOfTriangles; return true; #else if (outputErrorStr) strcpy(outputErrorStr, "Triangle library not supported"); return false; #endif }