00001
00002
00003
00004
00006
00007 #ifndef _ENRICHED_POLYHEDRON_H
00008 #define _ENRICHED_POLYHEDRON_H
00009
00010
00011 #include <CGAL/Cartesian.h>
00012 #include <CGAL/HalfedgeDS_vector.h>
00013 #include <CGAL/Polyhedron_3.h>
00014
00015
00016 #include <list>
00017
00023 template <class kernel, class items>
00024 class Enriched_polyhedron : public CGAL::Polyhedron_3<kernel,items,CGAL::HalfedgeDS_vector>
00025 {
00026 public :
00027 typedef typename kernel::FT FT;
00028 typedef typename kernel::Point_3 Point;
00029 typedef typename kernel::Vector_3 Vector;
00030
00031 typedef typename CGAL::Polyhedron_3<kernel,items,CGAL::HalfedgeDS_vector> base;
00032 typedef typename base::Facet Facet;
00033 typedef typename base::Vertex Vertex;
00034 typedef typename base::Facet_handle Facet_handle;
00035 typedef typename base::Vertex_handle Vertex_handle;
00036 typedef typename base::Halfedge_handle Halfedge_handle;
00037 typedef typename base::Facet_iterator Facet_iterator;
00038 typedef typename base::Halfedge_iterator Halfedge_iterator;
00039 typedef typename base::Vertex_iterator Vertex_iterator;
00040 typedef typename base::Halfedge_around_vertex_circulator Halfedge_around_vertex_circulator;
00041 typedef typename base::Halfedge_around_facet_circulator Halfedge_around_facet_circulator;
00042
00043 public :
00044
00045
00046 Enriched_polyhedron()
00047 {
00048 };
00049 virtual ~Enriched_polyhedron()
00050 {
00051 };
00052
00053
00054
00055 void compute_normals_per_facet()
00056 {
00057 for(Facet_iterator pFacet = facets_begin(); pFacet != facets_end(); pFacet++)
00058 compute_facet_normal(pFacet);
00059 };
00060 void compute_normals_per_vertex()
00061 {
00062 for(Vertex_iterator pVertex = vertices_begin(); pVertex != vertices_end(); pVertex++)
00063 compute_vertex_normal(pVertex);
00064 };
00065 void compute_normals()
00066 {
00067 compute_normals_per_facet();
00068 compute_normals_per_vertex();
00069 };
00070
00071
00072
00073 void set_index_vertices()
00074 {
00075 long index = 0;
00076 for(Vertex_iterator pVertex = vertices_begin(); pVertex != vertices_end(); pVertex++)
00077 pVertex->tag(index++);
00078 };
00079
00080
00081 void set_index_edges()
00082 {
00083 long index = 0;
00084 for(Halfedge_iterator pHalfedge = halfedges_begin(); pHalfedge != halfedges_end(); pHalfedge++)
00085 pHalfedge->tag(-1);
00086 for(Halfedge_iterator pHalfedge = halfedges_begin(); pHalfedge != halfedges_end(); pHalfedge++)
00087 {
00088 if(pHalfedge->opposite()->tag() == -1) pHalfedge->tag(index);
00089 else pHalfedge->tag(pHalfedge->opposite()->tag());
00090 index++;
00091 }
00092 };
00093
00094 void compute_facet_normal(Facet_handle pFacet)
00095 {
00096 typename Facet::Normal_3 sum = CGAL::NULL_VECTOR;
00097 typename Facet::Halfedge_around_facet_circulator h = pFacet->facet_begin();
00098 do {
00099 typename Facet::Normal_3 normal = CGAL::cross_product(
00100 h->next()->vertex()->point() - h->vertex()->point(),
00101 h->next()->next()->vertex()->point() - h->next()->vertex()->point());
00102 double sqnorm = normal * normal;
00103 if(sqnorm != 0)
00104 normal = normal / (float)std::sqrt(sqnorm);
00105 sum = sum + normal;
00106 } while(++h != pFacet->facet_begin());
00107
00108 float sqnorm = sum * sum;
00109 if(sqnorm != 0.0)
00110 pFacet->normal() = sum / std::sqrt(sqnorm);
00111 else
00112 {
00113 pFacet->normal() = CGAL::NULL_VECTOR;
00114 std::cout << "degenerate face" << std::endl;
00115 }
00116 };
00117
00118 void compute_vertex_normal(Vertex_handle pVertex)
00119 {
00120 typename Vertex::Normal_3 normal = CGAL::NULL_VECTOR;
00121 typename Vertex::Halfedge_around_vertex_circulator pHalfedge = pVertex->vertex_begin();
00122 typename Vertex::Halfedge_around_vertex_circulator begin = pHalfedge;
00123 CGAL_For_all(pHalfedge,begin)
00124 if(!pHalfedge->is_border())
00125 normal = normal + pHalfedge->facet()->normal();
00126 float sqnorm = normal * normal;
00127 if(sqnorm != 0.0f)
00128 pVertex->normal() = normal / (float)std::sqrt(sqnorm);
00129 else
00130 pVertex->normal() = CGAL::NULL_VECTOR;
00131 };
00132
00133 };
00134
00135
00136 #endif