The main building block of a DCEL is a list of half edges.Every actual edge is represented by two half edges going in opposite directions,and these are called twins
template<classtype = float,size_t dim = DIM3> struct FaceDCEL { //Pointer to one of the counter clockwise edge EdgeDCEL<type, dim>* outer = nullptr; //Pointer to first halfedges which represnt std::vector<EdgeDCEL<type, dim>*> inner; voidprint(){ if (outer) { auto edge_ptr = outer; auto next_ptr = outer->next;
edge_ptr->origin->print(); while (next_ptr != edge_ptr) { next_ptr->origin->print(); next_ptr = next_ptr->next; } } } std::vector<EdgeDCEL<type,dim>*> getEdgeList(){ std::vector<EdgeDCEL<type, dim>*> edge_list; if (outer) { auto edge_ptr = outer; auto next_ptr = outer->next; edge_list.push_back(edge_list); edge_ptr->origin->print(); while (next_ptr != edge_ptr) { edge_list.push_back(next_ptr); next_ptr = next_ptr->next; } } } std::vector<Vector<float,dim>> getPoints(){ std::vector<Vector<float, dim>> point_list; if (outer) { auto edge_ptr = outer; auto next_ptr = outer->next; point_list.push_back(edge_ptr->origin->point); while (next_ptr != edge_ptr) { point_list.push_back(next_ptr->origin->point); next_ptr = next_ptr->next; } } return point_list; } };
template<classtype = float,size_t dim = DIM3> class PolygonDCEL { typedef Vector<type, dim> VectorNf; std::vector<VertexDCEL<type, dim>*> vertex_list; std::vector<EdgeDCEL<type, dim>*> edge_list; std::vector<FaceDCEL<type, dim>*> face_list; EdgeDCEL<type, dim>* empty_edge = newEdgeDCEL<type, dim>(); public: //Construct the DCEL using given points //Assume the given points list is for polygon and have counter clockwise order explicitPolygonDCEL(std::vector<VectorNf>&);
//Insert an edge between virtices _v1 and _v2 given that lies boolsplit(VertexDCEL<type, dim>* _v1, VertexDCEL<type, dim>* _v2);
// Join the two faces sepated by edge between _v1 and _v2 booljoin(VertexDCEL<type, dim>* _v1, VertexDCEL<type, dim>* _v2);
//Return the all the vertices across all the faces std::vector<VertexDCEL<type, dim>*> getVertexList();
template<classtype,size_t dim> inline PolygonDCEL<type, dim>::PolygonDCEL(std::vector<VectorNf>& _points) { int size = _points.size(); //Polygon should have atleast tree vertices if (size < 3) return;
for (size_t i = 0; i < _points.size(); ++i) { vertex_list.push_back(newVertex2dDCEL<type, dim>(_points[i])); }
for (size_t i = 0; i <= vertex_list.size() - 2; ++i) { auto hfedge = newEdgeDCEL<type, dim>(vertex_list[i]); auto edge_twin = newEdgeDCEL<type, dim>(vertex_list[i + 1]);
//Set the prev and next for the element middle of the list (2:size-2) for (size_t i = 2; i <= edge_list.size() - 3; ++i) { //偶数,计数 if (i % 2 == 0) { edge_list[i]->next = edge_list[i + 2]; edge_list[i]->prev = edge_list[i - 2]; } else { edge_list[i]->next = edge_list[i - 2]; edge_list[i]->prev = edge_list[i + 2]; } }
for (size_t i = 0; i <= vertex_list.size() - 2; ++i) { auto hfedge = newEdgeDCEL<type, dim>(vertex_list[i]); auto edge_twin = newEdgeDCEL<type, dim>(vertex_list[i + 1]);
//Set the prev and next for the element middle of the list (2:size-2) for (size_t i = 2; i <= edge_list.size() - 3; ++i) { //偶数,计数 if (i % 2 == 0) { edge_list[i]->next = edge_list[i + 2]; edge_list[i]->prev = edge_list[i - 2]; } else { edge_list[i]->next = edge_list[i - 2]; edge_list[i]->prev = edge_list[i + 2]; } }