📄 polyhedron_ex.h
字号:
#ifndef POLYHEDRON_EX_H_INCLUDED#define POLYHEDRON_EX_H_INCLUDED#include <CGAL/Cartesian.h>#include <CGAL/Polyhedron_3.h>#include <CGAL/IO/Polyhedron_iostream.h>#include <algorithm>#include <vector>#include <list>#include <fstream>// CGAL kerneltypedef CGAL::Cartesian<double> My_kernel;// compute facet centerstruct Facet_center{ typedef My_kernel::Vector_3 Vector_3; typedef My_kernel::Point_3 Point_3; template<class Facet> void operator()(Facet& f) { Vector_3 vec(0.0,0.0,0.0); int degree = 0; typedef typename Facet::Halfedge_around_facet_const_circulator circ; circ h = f.facet_begin(); do { vec = vec + (h->vertex()->point()-CGAL::ORIGIN); degree++; } while (++h != f.facet_begin()); f.center() = CGAL::ORIGIN + (vec/degree); }};template<class Refs, class T>class My_facet : public CGAL::HalfedgeDS_face_base<Refs, T>{public: typedef My_kernel::Vector_3 Vector_3; typedef My_kernel::Point_3 Point_3; // life cycle // no constructors to repeat, since only // default constructor mandatory My_facet() { m_tag = -1; // uninitialized } // center Point_3& center() { return m_center; } const Point_3& center() const { return m_center; } // tag int tag() const { return m_tag; } void tag(int tag) { m_tag = tag; } // distance double distance(Point_3& point) const { Vector_3 vec = (point-m_center); return std::sqrt(vec*vec); }// Fieldsprivate: // facet data int m_tag; Point_3 m_center;};template<class Refs, class Tprev, class Tvertex, class Tface>class My_halfedge : public CGAL::HalfedgeDS_halfedge_base<Refs,Tprev,Tvertex,Tface>{private: int m_tag; // parameterization bool m_is_parameterized; int m_seaming; // seaming status double m_u; // texture coordinates double m_v; int m_index; // for parameterization // surface cutting double m_distance;public: // life cycle // no constructors to repeat, since only // default constructor mandatory My_halfedge() { m_tag = -1; // uninitialized m_u = 0.0; m_v = 0.0; m_index = -1; // uninitialized m_seaming = -1; // uninitialized m_is_parameterized = false; } // tag int tag() const { return m_tag; } void tag(int tag) { m_tag = tag; } // seaming status int seaming() const { return m_seaming; } void seaming(int seaming) { m_seaming = seaming; } // precomputed distance double distance() const { return m_distance; } void distance(double distance) { m_distance = distance; } // texture coordinates double u() const { return m_u; } double v() const { return m_v; } void uv(double u, double v) { m_u = u; m_v = v; } // param. bool is_parameterized() const { return m_is_parameterized; } void is_parameterized(bool is) { m_is_parameterized = is; } // index int index() const { return m_index; } void index(int i) { m_index = i; }};// A redefined vertex class for the Polyhedron_3template<class Refs, class T, class P>class My_vertex : public CGAL::HalfedgeDS_vertex_base<Refs, T, P>{ // index int m_index; // misc int m_tag; // seaming status int m_seaming;public: // life cycle My_vertex() { init(); } // repeat mandatory constructors My_vertex(const P& pt) : CGAL::HalfedgeDS_vertex_base<Refs, T, P>(pt) { init(); } void init() { m_index = -1; // uninitialized m_tag = -1; // uninitialized m_seaming = -1; // uninitialized } // index int index() const { return m_index; } void index(int i) { m_index = i; } // tag int tag() const { return m_tag; } void tag(int tag) { m_tag = tag; } // seaming status int seaming() const { return m_seaming; } void seaming(int seaming) { m_seaming = seaming; }};// A redefined items class for the Polyhedron_3 with a refined vertex, facet and halfedge classesstruct My_items : public CGAL::Polyhedron_items_3{ typedef My_kernel::Vector_3 Vector_3; typedef My_kernel::Point_3 Point_3; // wrap vertex template<class Refs, class Traits> struct Vertex_wrapper { typedef typename Traits::Point_3 Point_3; typedef My_vertex<Refs, CGAL::Tag_true, Point_3> Vertex; }; // wrap facet template<class Refs, class Traits> struct Face_wrapper { typedef My_facet<Refs, CGAL::Tag_true> Face; }; // wrap halfedge template<class Refs, class Traits> struct Halfedge_wrapper { typedef My_halfedge<Refs, CGAL::Tag_true, CGAL::Tag_true, CGAL::Tag_true> Halfedge; };};class Polyhedron_ex : public CGAL::Polyhedron_3<My_kernel,My_items>{public: typedef My_kernel::Vector_3 Vector_3; typedef My_kernel::Point_3 Point_3; public: // life cycle Polyhedron_ex() {} virtual ~Polyhedron_ex() {} // facet centers void compute_facet_centers() { std::for_each(facets_begin(),facets_end(),Facet_center()); } // tag all facets void tag_facets(const int tag) { Facet_iterator pFace; for(pFace = facets_begin(); pFace != facets_end(); pFace++) pFace->tag(tag); } // get closest inner facet Facet_handle get_closest_inner_facet(Point_3& point) { Facet_iterator pFace = facets_begin(); Facet_handle pClosest = pFace; double minimum = pFace->distance(point); for(;pFace != facets_end(); pFace++) { if(is_inner(pFace)) { double distance = pFace->distance(point); if(distance < minimum) { pClosest = pFace; minimum = distance; } } } return pClosest; } bool is_inner(Facet_handle pFace) { typedef Halfedge_around_facet_const_circulator circ; circ h = pFace->facet_begin(); do { if(h->opposite()->is_border()) return false; } while(++h != pFace->facet_begin()); return true; } // tag all vertices void tag_vertices(const int tag) { Vertex_iterator iter; for(iter = vertices_begin(); iter != vertices_end(); iter++) iter->tag(tag); } // tag all halfedges void tag_halfedges(const int tag) { Halfedge_iterator iter; for(iter = halfedges_begin(); iter != halfedges_end(); iter++) iter->tag(tag); } // compute bounding interval
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -