From 2066a440d0e5c11f8b45f22367ed524c5604ea33 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 05:51:58 +0100 Subject: [PATCH 1/9] Restored the original OGL_helper.h from CGAL. --- openscad.pro | 1 + src/OGL_helper.h | 669 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 670 insertions(+) create mode 100644 src/OGL_helper.h diff --git a/openscad.pro b/openscad.pro index 5b3a3984..4fe04834 100644 --- a/openscad.pro +++ b/openscad.pro @@ -119,6 +119,7 @@ FORMS += src/MainWindow.ui \ src/Preferences.ui HEADERS += src/CGAL_renderer.h \ + src/OGL_helper.h \ src/GLView.h \ src/MainWindow.h \ src/Preferences.h \ diff --git a/src/OGL_helper.h b/src/OGL_helper.h new file mode 100644 index 00000000..46a1209b --- /dev/null +++ b/src/OGL_helper.h @@ -0,0 +1,669 @@ +// Copyright (c) 1997-2002 Max-Planck-Institute Saarbruecken (Germany). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you may redistribute it under +// the terms of the Q Public License version 1.0. +// See the file LICENSE.QPL distributed with CGAL. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.7-branch/Nef_3/include/CGAL/Nef_3/OGL_helper.h $ +// $Id: OGL_helper.h 56667 2010-06-09 07:37:13Z sloriot $ +// +// +// Author(s) : Peter Hachenberger + +#ifndef CGAL_NEF_OPENGL_HELPER_H +#define CGAL_NEF_OPENGL_HELPER_H + +#include +#include +#include +#include +#include + +#define CGAL_NEF3_MARKED_VERTEX_COLOR 183,232,92 +#define CGAL_NEF3_MARKED_EDGE_COLOR 171,216,86 +#define CGAL_NEF3_MARKED_FACET_COLOR 157,203,81 + +#define CGAL_NEF3_UNMARKED_VERTEX_COLOR 255,246,124 +#define CGAL_NEF3_UNMARKED_EDGE_COLOR 255,236,94 +#define CGAL_NEF3_UNMARKED_FACET_COLOR 249,215,44 + +#ifdef _WIN32 +#define CGAL_GLU_TESS_CALLBACK CALLBACK +#else +#define CGAL_GLU_TESS_CALLBACK +#endif + +#ifdef __APPLE__ +# include +#endif + +#if defined __APPLE__ && !defined MAC_OS_X_VERSION_10_5 + #define CGAL_GLU_TESS_DOTS ... +#else + #define CGAL_GLU_TESS_DOTS +#endif + + +namespace CGAL { + +namespace OGL { + +// ---------------------------------------------------------------------------- +// Drawable double types: +// ---------------------------------------------------------------------------- + + typedef CGAL::Simple_cartesian DKernel; + typedef DKernel::Point_3 Double_point; + typedef DKernel::Vector_3 Double_vector; + typedef DKernel::Segment_3 Double_segment; + typedef DKernel::Aff_transformation_3 Affine_3; + + // DPoint = a double point including a mark + class DPoint : public Double_point { + bool m_; + public: + DPoint() {} + DPoint(const Double_point& p, bool m) : Double_point(p) { m_ = m; } + DPoint(const DPoint& p) : Double_point(p) { m_ = p.m_; } + DPoint& operator=(const DPoint& p) + { Double_point::operator=(p); m_ = p.m_; return *this; } + bool mark() const { return m_; } + }; + + // DSegment = a double segment including a mark + class DSegment : public Double_segment { + bool m_; + public: + DSegment() {} + DSegment(const Double_segment& s, bool m) : Double_segment(s) { m_ = m; } + DSegment(const DSegment& s) : Double_segment(s) { m_ = s.m_; } + DSegment& operator=(const DSegment& s) + { Double_segment::operator=(s); m_ = s.m_; return *this; } + bool mark() const { return m_; } + }; + + // Double_triple = a class that stores a triple of double + // coordinates; we need a pointer to the coordinates in a C array + // for OpenGL + class Double_triple { + typedef double* double_ptr; + typedef const double* const_double_ptr; + double coords_[3]; + public: + Double_triple() + { coords_[0]=coords_[1]=coords_[2]=0.0; } + Double_triple(double x, double y, double z) + { coords_[0]=x; coords_[1]=y; coords_[2]=z; } + Double_triple(const Double_triple& t) + { coords_[0]=t.coords_[0]; + coords_[1]=t.coords_[1]; + coords_[2]=t.coords_[2]; + } + Double_triple& operator=(const Double_triple& t) + { coords_[0]=t.coords_[0]; + coords_[1]=t.coords_[1]; + coords_[2]=t.coords_[2]; + return *this; } + operator double_ptr() const + { return const_cast(*this).coords_; } + double operator[](unsigned i) + { CGAL_assertion(i<3); return coords_[i]; } + }; // Double_triple + + static std::ostream& operator << (std::ostream& os, + const Double_triple& t) + { os << "(" << t[0] << "," << t[1] << "," << t[2] << ")"; + return os; } + + + // DFacet stores the facet cycle vertices in a continuus C array + // of three double components, this is necessary due to the OpenGL + // tesselator input format ! + class DFacet { + typedef std::vector Coord_vector; + typedef std::vector Cycle_vector; + Coord_vector coords_; // stores all vertex coordinates + Cycle_vector fc_ends_; // stores entry points of facet cycles + Double_triple normal_; // stores normal and mark of facet + bool mark_; + + public: + typedef Coord_vector::iterator Coord_iterator; + typedef Coord_vector::const_iterator Coord_const_iterator; + + DFacet() {} + + void push_back_vertex(double x, double y, double z) + { coords_.push_back(Double_triple(x,y,z)); } + + DFacet(const DFacet& f) + { coords_ = f.coords_; + fc_ends_ = f.fc_ends_; + normal_ = f.normal_; + mark_ = f.mark_; + } + + DFacet& operator=(const DFacet& f) + { coords_ = f.coords_; + fc_ends_ = f.fc_ends_; + normal_ = f.normal_; + mark_ = f.mark_; + return *this; + } + + ~DFacet() + { coords_.clear(); fc_ends_.clear(); } + + void push_back_vertex(const Double_point& p) + { push_back_vertex(p.x(),p.y(),p.z()); } + + void set_normal(double x, double y, double z, bool m) + { double l = sqrt(x*x + y*y + z*z); + normal_ = Double_triple(x/l,y/l,z/l); mark_ = m; } + + double dx() const { return normal_[0]; } + double dy() const { return normal_[1]; } + double dz() const { return normal_[2]; } + bool mark() const { return mark_; } + double* normal() const + { return static_cast(normal_); } + + void new_facet_cycle() + { fc_ends_.push_back(coords_.size()); } + + unsigned number_of_facet_cycles() const + { return fc_ends_.size(); } + + Coord_iterator facet_cycle_begin(unsigned i) + { CGAL_assertion(i(vertex)); + GLdouble* pu(static_cast(user)); + // CGAL_NEF_TRACEN("vertexCallback coord "< vertices_; + std::list edges_; + std::list halffacets_; + + GLuint object_list_; + bool init_; + + Bbox_3 bbox_; + + int style; + std::vector switches; + + typedef std::list::const_iterator Vertex_iterator; + typedef std::list::const_iterator Edge_iterator; + typedef std::list::const_iterator Halffacet_iterator; + + public: + Polyhedron() : bbox_(-1,-1,-1,1,1,1), switches(1) { + object_list_ = 0; + init_ = false; + style = SNC_BOUNDARY; + switches[SNC_AXES] = false; + } + + /* + Polyhedron(const Polyhedron& P) : + object_list_(0), + init_(false), + bbox_(P.bbox_), + style(P.style), + switches(2) { + + switches[SNC_AXES] = P.switches[SNC_AXES]; + + Vertex_iterator v; + for(v=P.vertices_.begin();v!=P.vertices_.end();++v) + vertices_.push_back(*v); + Edge_iterator e; + for(e=P.edges_.begin();e!=P.edges_.end();++e) + edges_.push_back(*e); + Halffacet_iterator f; + for(f=P.halffacets_.begin();f!=P.halffacets_.end();++f) + halffacets_.push_back(*f); + } + + Polyhedron& operator=(const Polyhedron& P) { + if (object_list_) glDeleteLists(object_list_, 4); + object_list_ = 0; + init_ = false; + style = P.style; + switches[SNC_AXES] = P.switches[SNC_AXES]; + + Vertex_iterator v; + vertices_.clear(); + for(v=P.vertices_.begin();v!=P.vertices_.end();++v) + vertices_.push_back(*v); + Edge_iterator e; + edges_.clear(); + for(e=P.edges_.begin();e!=P.edges_.end();++e) + edges_.push_back(*e); + Halffacet_iterator f; + halffacets_.clear(); + for(f=P.halffacets_.begin();f!=P.halffacets_.end();++f) + halffacets_.push_back(*f); + init(); + return *this; + } + */ + ~Polyhedron() + { if (object_list_) glDeleteLists(object_list_, 4); } + + void push_back(const Double_point& p, bool m) { + vertices_.push_back(DPoint(p,m)); + } + void push_back(const Double_segment& s, bool m) + { edges_.push_back(DSegment(s,m)); } + void push_back(const DFacet& f) + { halffacets_.push_back(f); } + + void toggle(int index) { + switches[index] = !switches[index]; + } + + void set_style(int index) { + style = index; + } + + bool is_initialized() const { return init_; } + + Bbox_3 bbox() const { return bbox_; } + Bbox_3& bbox() { return bbox_; } + + void draw(Vertex_iterator v) const { + // CGAL_NEF_TRACEN("drawing vertex "<<*v); + CGAL::Color cf(CGAL_NEF3_MARKED_VERTEX_COLOR), + ct(CGAL_NEF3_UNMARKED_VERTEX_COLOR); // more blue-ish + CGAL::Color c = v->mark() ? ct : cf; + glPointSize(10); + glColor3ub(c.red(), c.green(), c.blue()); + glBegin(GL_POINTS); + glVertex3d(v->x(),v->y(),v->z()); +#ifdef CGAL_NEF_EMPHASIZE_VERTEX + glColor3ub(255,0,0); + glVertex3d(CGAL_NEF_EMPHASIZE_VERTEX); +#endif + glEnd(); + } + + void draw(Edge_iterator e) const { + // CGAL_NEF_TRACEN("drawing edge "<<*e); + Double_point p = e->source(), q = e->target(); + CGAL::Color cf(CGAL_NEF3_MARKED_EDGE_COLOR), + ct(CGAL_NEF3_UNMARKED_EDGE_COLOR); // more blue-ish + CGAL::Color c = e->mark() ? ct : cf; + glLineWidth(5); + glColor3ub(c.red(),c.green(),c.blue()); + glBegin(GL_LINE_STRIP); + glVertex3d(p.x(), p.y(), p.z()); + glVertex3d(q.x(), q.y(), q.z()); + glEnd(); + } + + void draw(Halffacet_iterator f) const { + // CGAL_NEF_TRACEN("drawing facet "<<(f->debug(),"")); + GLUtesselator* tess_ = gluNewTess(); + gluTessCallback(tess_, GLenum(GLU_TESS_VERTEX_DATA), + (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &vertexCallback); + gluTessCallback(tess_, GLenum(GLU_TESS_BEGIN), + (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &beginCallback); + gluTessCallback(tess_, GLenum(GLU_TESS_END), + (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &endCallback); + gluTessCallback(tess_, GLenum(GLU_TESS_ERROR), + (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &errorCallback); + gluTessProperty(tess_, GLenum(GLU_TESS_WINDING_RULE), + GLU_TESS_WINDING_POSITIVE); + + DFacet::Coord_const_iterator cit; + CGAL::Color cf(CGAL_NEF3_MARKED_FACET_COLOR), + ct(CGAL_NEF3_UNMARKED_FACET_COLOR); // more blue-ish + CGAL::Color c = (f->mark() ? ct : cf); + glColor3ub(c.red(),c.green(),c.blue()); + gluTessBeginPolygon(tess_,f->normal()); + // CGAL_NEF_TRACEN(" "); + // CGAL_NEF_TRACEN("Begin Polygon"); + gluTessNormal(tess_,f->dx(),f->dy(),f->dz()); + // forall facet cycles of f: + for(unsigned i = 0; i < f->number_of_facet_cycles(); ++i) { + gluTessBeginContour(tess_); + // CGAL_NEF_TRACEN(" Begin Contour"); + // put all vertices in facet cycle into contour: + for(cit = f->facet_cycle_begin(i); + cit != f->facet_cycle_end(i); ++cit) { + gluTessVertex(tess_, *cit, *cit); + // CGAL_NEF_TRACEN(" add Vertex"); + } + gluTessEndContour(tess_); + // CGAL_NEF_TRACEN(" End Contour"); + } + gluTessEndPolygon(tess_); + // CGAL_NEF_TRACEN("End Polygon"); + gluDeleteTess(tess_); + } + + void construct_axes() const + { + glLineWidth(2.0); + // red x-axis + glColor3f(1.0,0.0,0.0); + glBegin(GL_LINES); + glVertex3f(0.0,0.0,0.0); + glVertex3f(5000.0,0.0,0.0); + glEnd(); + // green y-axis + glColor3f(0.0,1.0,0.0); + glBegin(GL_LINES); + glVertex3f(0.0,0.0,0.0); + glVertex3f(0.0,5000.0,0.0); + glEnd(); + // blue z-axis and equator + glColor3f(0.0,0.0,1.0); + glBegin(GL_LINES); + glVertex3f(0.0,0.0,0.0); + glVertex3f(0.0,0.0,5000.0); + glEnd(); + // six coordinate points in pink: + glPointSize(10); + glBegin(GL_POINTS); + glColor3f(1.0,0.0,0.0); + glVertex3d(5,0,0); + glColor3f(0.0,1.0,0.0); + glVertex3d(0,5,0); + glColor3f(0.0,0.0,1.0); + glVertex3d(0,0,5); + glEnd(); + } + + + void fill_display_lists() { + glNewList(object_list_, GL_COMPILE); + Vertex_iterator v; + for(v=vertices_.begin();v!=vertices_.end();++v) + draw(v); + glEndList(); + + glNewList(object_list_+1, GL_COMPILE); + Edge_iterator e; + for(e=edges_.begin();e!=edges_.end();++e) + draw(e); + glEndList(); + + glNewList(object_list_+2, GL_COMPILE); + Halffacet_iterator f; + for(f=halffacets_.begin();f!=halffacets_.end();++f) + draw(f); + glEndList(); + + glNewList(object_list_+3, GL_COMPILE); // axes: + construct_axes(); + glEndList(); + + } + + void init() { + if (init_) return; + init_ = true; + switches[SNC_AXES] = false; + style = SNC_BOUNDARY; + object_list_ = glGenLists(4); + CGAL_assertion(object_list_); + fill_display_lists(); + } + + + void draw() const + { + if (!is_initialized()) const_cast(*this).init(); + double l = (std::max)( (std::max)( bbox().xmax() - bbox().xmin(), + bbox().ymax() - bbox().ymin()), + bbox().zmax() - bbox().zmin()); + if ( l < 1) // make sure that a single point doesn't screw up here + l = 1; + glScaled( 4.0/l, 4.0/l, 4.0/l); + glTranslated( -(bbox().xmax() + bbox().xmin()) / 2.0, + -(bbox().ymax() + bbox().ymin()) / 2.0, + -(bbox().zmax() + bbox().zmin()) / 2.0); + if (style == SNC_BOUNDARY) { + //glEnable(GL_LIGHTING); + glCallList(object_list_+2); // facets + //glDisable(GL_LIGHTING); + } + // move edges and vertices a bit towards the view-point, + // i.e., 1/100th of the unit vector in camera space + // double f = l / 4.0 / 100.0; + // glTranslated( z_vec[0] * f, z_vec[1] * f, z_vec[2] * f); + glCallList(object_list_+1); // edges + glCallList(object_list_); // vertices + if (switches[SNC_AXES]) glCallList(object_list_+3); // axis + } + + void debug(std::ostream& os = std::cerr) const + { + os << "OGL::Polyhedron" << std::endl; + os << "Vertices:" << std::endl; + Vertex_iterator v; + for(v=vertices_.begin();v!=vertices_.end();++v) + os << " "<<*v<<", mark="<mark()<debug(); os << std::endl; + os << std::endl; + } + + }; // Polyhedron + + template + class Nef3_Converter { + typedef typename Nef_polyhedron::SNC_structure SNC_structure; + typedef CGAL::SNC_decorator Base; + typedef CGAL::SNC_FM_decorator FM_decorator; + + public: + typedef typename SNC_structure::Vertex_const_iterator Vertex_const_iterator; + typedef typename SNC_structure::Halfedge_const_iterator Halfedge_const_iterator; + typedef typename SNC_structure::Halffacet_const_iterator Halffacet_const_iterator; + typedef typename SNC_structure::Halffacet_cycle_const_iterator Halffacet_cycle_const_iterator; + + typedef typename SNC_structure::Object_const_handle Object_const_handle; + typedef typename SNC_structure::SHalfedge_const_handle SHalfedge_const_handle; + typedef typename SNC_structure::SHalfloop_const_handle SHalfloop_const_handle; + + typedef typename SNC_structure::Vertex_const_handle Vertex_const_handle; + typedef typename SNC_structure::Halfedge_const_handle Halfedge_const_handle; + typedef typename SNC_structure::Halffacet_const_handle Halffacet_const_handle; + + typedef typename SNC_structure::Point_3 Point_3; + typedef typename SNC_structure::Vector_3 Vector_3; + typedef typename SNC_structure::Segment_3 Segment_3; + typedef typename SNC_structure::Plane_3 Plane_3; + typedef typename SNC_structure::Mark Mark; + typedef typename SNC_structure::SHalfedge_around_facet_const_circulator + SHalfedge_around_facet_const_circulator; + + private: + static OGL::Double_point double_point(const Point_3& p) + { return OGL::Double_point(CGAL::to_double(p.x()), + CGAL::to_double(p.y()), + CGAL::to_double(p.z())); } + + static OGL::Double_segment double_segment(const Segment_3& s) + { return OGL::Double_segment(double_point(s.source()), + double_point(s.target())); } + + static void draw(Vertex_const_handle v, const Nef_polyhedron& , + CGAL::OGL::Polyhedron& P) { + Point_3 bp = v->point(); + // CGAL_NEF_TRACEN("vertex " << bp); + P.push_back(double_point(bp), v->mark()); + } + + static void draw(Halfedge_const_handle e, const Nef_polyhedron& , + CGAL::OGL::Polyhedron& P) { + Vertex_const_handle s = e->source(); + Vertex_const_handle t = e->twin()->source(); + Segment_3 seg(s->point(),t->point()); + // CGAL_NEF_TRACEN("edge " << seg); + P.push_back(double_segment(seg), e->mark()); + } + + static void draw(Halffacet_const_handle f, const Nef_polyhedron& , + CGAL::OGL::Polyhedron& P) { + OGL::DFacet g; + Halffacet_cycle_const_iterator fc; // all facet cycles: + CGAL_forall_facet_cycles_of(fc,f) + if ( fc.is_shalfedge() ) { // non-trivial facet cycle + g.new_facet_cycle(); + SHalfedge_const_handle h = fc; + SHalfedge_around_facet_const_circulator hc(h), he(hc); + CGAL_For_all(hc,he){ // all vertex coordinates in facet cycle + Point_3 sp = hc->source()->source()->point(); + // CGAL_NEF_TRACEN(" ");CGAL_NEF_TRACEN("facet" << sp); + g.push_back_vertex(double_point(sp)); + } + } + Vector_3 v = f->plane().orthogonal_vector(); + g.set_normal(CGAL::to_double(v.x()), + CGAL::to_double(v.y()), + CGAL::to_double(v.z()), + f->mark()); + P.push_back(g); + } + + // Returns the bounding box of the finite vertices of the polyhedron. + // Returns $[-1,+1]^3$ as bounding box if no finite vertex exists. + + static Bbox_3 bounded_bbox(const Nef_polyhedron& N) { + bool first_vertex = true; + Bbox_3 bbox( -1.0, -1.0, -1.0, 1.0, 1.0, 1.0); + Vertex_const_iterator vi; + CGAL_forall_vertices(vi, N) { + Point_3 p = vi->point(); + double x = CGAL::to_double(p.hx()); + double y = CGAL::to_double(p.hy()); + double z = CGAL::to_double(p.hz()); + double w = CGAL::to_double(p.hw()); + if (N.is_standard(vi)) { + if(first_vertex) { + bbox = Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); + first_vertex = false; + } else { + bbox = bbox + Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); + first_vertex = false; + } + } + } + return bbox; + } + + static void set_R(Bbox_3& bbox, const Nef_polyhedron& N) { + if(N.is_standard_kernel()) return; + double size = abs(bbox.xmin()); + if(size < bbox.xmax()) size = bbox.xmax(); + if(size < bbox.ymin()) size = bbox.ymin(); + if(size < bbox.ymax()) size = bbox.ymax(); + if(size < bbox.zmin()) size = bbox.zmin(); + if(size < bbox.zmax()) size = bbox.zmax(); + N.set_size_of_infimaximal_box(size*50); + // CGAL_NEF_TRACEN("set infi box size to " << size); + Vertex_const_iterator vi; + CGAL_forall_vertices(vi, N) + if(N.is_standard(vi)) + return; + bbox = Bbox_3(bbox.xmin()*10,bbox.ymin()*10,bbox.zmin()*10, + bbox.xmax()*10,bbox.ymax()*10,bbox.zmax()*10); + } + public: + static void convert_to_OGLPolyhedron(const Nef_polyhedron& N, CGAL::OGL::Polyhedron* P) { + Bbox_3 bbox(bounded_bbox(N)); + set_R(bbox,N); + P->bbox() = bbox; + Vertex_const_iterator v; + CGAL_forall_vertices(v,*N.sncp()) draw(v,N,*P); + Halfedge_const_iterator e; + CGAL_forall_edges(e,*N.sncp()) draw(e,N,*P); + Halffacet_const_iterator f; + CGAL_forall_facets(f,*N.sncp()) draw(f,N,*P); + } + + }; // Nef3_Converter + +} // namespace OGL + +} //namespace CGAL +#endif // CGAL_NEF_OPENGL_HELPER_H From 6d315b59211c8bcc845c85826b15e98035da5553 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:28:12 +0100 Subject: [PATCH 2/9] Lightweight refactoring of OGL_helper to make it extensible. --- src/OGL_helper.h | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/OGL_helper.h b/src/OGL_helper.h index 46a1209b..2aae31b0 100644 --- a/src/OGL_helper.h +++ b/src/OGL_helper.h @@ -248,7 +248,7 @@ namespace OGL { enum { SNC_BOUNDARY, SNC_SKELETON }; class Polyhedron : public OGL_base_object { - + protected: std::list vertices_; std::list edges_; std::list halffacets_; @@ -341,11 +341,17 @@ namespace OGL { Bbox_3 bbox() const { return bbox_; } Bbox_3& bbox() { return bbox_; } + virtual CGAL::Color getVertexColor(Vertex_iterator v) const + { + CGAL::Color cf(CGAL_NEF3_MARKED_VERTEX_COLOR), + ct(CGAL_NEF3_UNMARKED_VERTEX_COLOR); // more blue-ish + CGAL::Color c = v->mark() ? ct : cf; + return c; + } + void draw(Vertex_iterator v) const { // CGAL_NEF_TRACEN("drawing vertex "<<*v); - CGAL::Color cf(CGAL_NEF3_MARKED_VERTEX_COLOR), - ct(CGAL_NEF3_UNMARKED_VERTEX_COLOR); // more blue-ish - CGAL::Color c = v->mark() ? ct : cf; + CGAL::Color c = getVertexColor(v); glPointSize(10); glColor3ub(c.red(), c.green(), c.blue()); glBegin(GL_POINTS); @@ -357,12 +363,18 @@ namespace OGL { glEnd(); } + virtual CGAL::Color getEdgeColor(Edge_iterator e) const + { + CGAL::Color cf(CGAL_NEF3_MARKED_EDGE_COLOR), + ct(CGAL_NEF3_UNMARKED_EDGE_COLOR); // more blue-ish + CGAL::Color c = e->mark() ? ct : cf; + return c; + } + void draw(Edge_iterator e) const { // CGAL_NEF_TRACEN("drawing edge "<<*e); Double_point p = e->source(), q = e->target(); - CGAL::Color cf(CGAL_NEF3_MARKED_EDGE_COLOR), - ct(CGAL_NEF3_UNMARKED_EDGE_COLOR); // more blue-ish - CGAL::Color c = e->mark() ? ct : cf; + CGAL::Color c = getEdgeColor(e); glLineWidth(5); glColor3ub(c.red(),c.green(),c.blue()); glBegin(GL_LINE_STRIP); @@ -371,6 +383,14 @@ namespace OGL { glEnd(); } + virtual CGAL::Color getFacetColor(Halffacet_iterator f) const + { + CGAL::Color cf(CGAL_NEF3_MARKED_FACET_COLOR), + ct(CGAL_NEF3_UNMARKED_FACET_COLOR); // more blue-ish + CGAL::Color c = (f->mark() ? ct : cf); + return c; + } + void draw(Halffacet_iterator f) const { // CGAL_NEF_TRACEN("drawing facet "<<(f->debug(),"")); GLUtesselator* tess_ = gluNewTess(); @@ -386,9 +406,7 @@ namespace OGL { GLU_TESS_WINDING_POSITIVE); DFacet::Coord_const_iterator cit; - CGAL::Color cf(CGAL_NEF3_MARKED_FACET_COLOR), - ct(CGAL_NEF3_UNMARKED_FACET_COLOR); // more blue-ish - CGAL::Color c = (f->mark() ? ct : cf); + CGAL::Color c = getFacetColor(f); glColor3ub(c.red(),c.green(),c.blue()); gluTessBeginPolygon(tess_,f->normal()); // CGAL_NEF_TRACEN(" "); From 40d7f266288c752e2683c026420d9f7f26e79979 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:30:25 +0100 Subject: [PATCH 3/9] Remove all the crud from CGAL_Renderer.h, and subclass of the differences. --- src/CGAL_renderer.h | 692 +++++--------------------------------------- src/mainwin.cc | 22 +- 2 files changed, 76 insertions(+), 638 deletions(-) diff --git a/src/CGAL_renderer.h b/src/CGAL_renderer.h index 07583392..8f4a1b67 100644 --- a/src/CGAL_renderer.h +++ b/src/CGAL_renderer.h @@ -1,281 +1,51 @@ -// Copyright (c) 1997-2002 Max-Planck-Institute Saarbruecken (Germany). -// All rights reserved. -// -// This file is part of CGAL (www.cgal.org); you may redistribute it under -// the terms of the Q Public License version 1.0. -// See the file LICENSE.QPL distributed with CGAL. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. -// -// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -// -// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Nef_3/include/CGAL/Nef_3/OGL_helper.h $ -// $Id: OGL_helper.h 44713 2008-08-01 15:38:58Z hachenb $ -// -// -// Author(s) : Peter Hachenberger +/* + * OpenSCAD (www.openscad.org) + * Copyright (C) 2009-2011 Clifford Wolf and + * Marius Kintel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * As a special exception, you have permission to link this program + * with the CGAL library and distribute executables, as long as you + * follow the requirements of the GNU GPL in regard to all of the + * software in the executable aside from CGAL. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ -#ifndef CGAL_NEF_OPENGL_HELPER_H -#define CGAL_NEF_OPENGL_HELPER_H +#ifndef CGAL_RENDERER_H +#define CGAL_RENDERER_H -#include -#include -#include -#include -#include +#include "OGL_helper.h" +#undef CGAL_NEF3_MARKED_VERTEX_COLOR +#undef CGAL_NEF3_MARKED_EDGE_COLOR +#undef CGAL_NEF3_MARKED_FACET_COLOR -#ifdef _WIN32 -#define CGAL_GLU_TESS_CALLBACK CALLBACK -#else -#define CGAL_GLU_TESS_CALLBACK -#endif +#undef CGAL_NEF3_UNMARKED_VERTEX_COLOR +#undef CGAL_NEF3_UNMARKED_EDGE_COLOR +#undef CGAL_NEF3_UNMARKED_FACET_COLOR -#ifdef __APPLE__ -# include -#endif - -#if defined __APPLE__ && !defined MAC_OS_X_VERSION_10_5 -#define CGAL_GLU_TESS_DOTS ... -#else -#define CGAL_GLU_TESS_DOTS -#endif - -using namespace CGAL; -using namespace CGAL::OGL; +using CGAL::OGL::SNC_BOUNDARY; +using CGAL::OGL::SNC_SKELETON; namespace OpenSCAD { namespace OGL { -// ---------------------------------------------------------------------------- -// Drawable double types: -// ---------------------------------------------------------------------------- - - typedef CGAL::Simple_cartesian DKernel; - typedef DKernel::Point_3 Double_point; - typedef DKernel::Vector_3 Double_vector; - typedef DKernel::Segment_3 Double_segment; - typedef DKernel::Aff_transformation_3 Affine_3; - - // DPoint = a double point including a mark - class DPoint : public Double_point { - bool m_; - public: - DPoint() {} - DPoint(const Double_point& p, bool m) : Double_point(p) { m_ = m; } - DPoint(const DPoint& p) : Double_point(p) { m_ = p.m_; } - DPoint& operator=(const DPoint& p) - { Double_point::operator=(p); m_ = p.m_; return *this; } - bool mark() const { return m_; } - }; - - // DSegment = a double segment including a mark - class DSegment : public Double_segment { - bool m_; - public: - DSegment() {} - DSegment(const Double_segment& s, bool m) : Double_segment(s) { m_ = m; } - DSegment(const DSegment& s) : Double_segment(s) { m_ = s.m_; } - DSegment& operator=(const DSegment& s) - { Double_segment::operator=(s); m_ = s.m_; return *this; } - bool mark() const { return m_; } - }; - - // Double_triple = a class that stores a triple of double - // coordinates; we need a pointer to the coordinates in a C array - // for OpenGL - class Double_triple { - typedef double* double_ptr; - typedef const double* const_double_ptr; - double coords_[3]; - public: - Double_triple() - { coords_[0]=coords_[1]=coords_[2]=0.0; } - Double_triple(double x, double y, double z) - { coords_[0]=x; coords_[1]=y; coords_[2]=z; } - Double_triple(const Double_triple& t) - { coords_[0]=t.coords_[0]; - coords_[1]=t.coords_[1]; - coords_[2]=t.coords_[2]; - } - Double_triple& operator=(const Double_triple& t) - { coords_[0]=t.coords_[0]; - coords_[1]=t.coords_[1]; - coords_[2]=t.coords_[2]; - return *this; } - operator double_ptr() const - { return const_cast(*this).coords_; } - double operator[](unsigned i) - { CGAL_assertion(i<3); return coords_[i]; } - }; // Double_triple - - static std::ostream& operator << (std::ostream& os, - const Double_triple& t) - { os << "(" << t[0] << "," << t[1] << "," << t[2] << ")"; - return os; } - - - // DFacet stores the facet cycle vertices in a continuus C array - // of three double components, this is necessary due to the OpenGL - // tesselator input format ! - class DFacet { - typedef std::vector Coord_vector; - typedef std::vector Cycle_vector; - Coord_vector coords_; // stores all vertex coordinates - Cycle_vector fc_ends_; // stores entry points of facet cycles - Double_triple normal_; // stores normal and mark of facet - bool mark_; - - public: - typedef Coord_vector::iterator Coord_iterator; - typedef Coord_vector::const_iterator Coord_const_iterator; - - DFacet() {} - - void push_back_vertex(double x, double y, double z) - { coords_.push_back(Double_triple(x,y,z)); } - - DFacet(const DFacet& f) - { coords_ = f.coords_; - fc_ends_ = f.fc_ends_; - normal_ = f.normal_; - mark_ = f.mark_; - } - - DFacet& operator=(const DFacet& f) - { coords_ = f.coords_; - fc_ends_ = f.fc_ends_; - normal_ = f.normal_; - mark_ = f.mark_; - return *this; - } - - ~DFacet() - { coords_.clear(); fc_ends_.clear(); } - - void push_back_vertex(const Double_point& p) - { push_back_vertex(p.x(),p.y(),p.z()); } - - void set_normal(double x, double y, double z, bool m) - { double l = sqrt(x*x + y*y + z*z); - normal_ = Double_triple(x/l,y/l,z/l); mark_ = m; } - - double dx() const { return normal_[0]; } - double dy() const { return normal_[1]; } - double dz() const { return normal_[2]; } - bool mark() const { return mark_; } - double* normal() const - { return static_cast(normal_); } - - void new_facet_cycle() - { fc_ends_.push_back(coords_.size()); } - - unsigned number_of_facet_cycles() const - { return fc_ends_.size(); } - - Coord_iterator facet_cycle_begin(unsigned i) - { CGAL_assertion(i(vertex)); - GLdouble* pu(static_cast(user)); - // CGAL_NEF_TRACEN("vertexCallback coord "< pcache; - if (dataOut) { - GLdouble *n = new GLdouble[3]; - n[0] = coords[0]; - n[1] = coords[1]; - n[2] = coords[2]; - pcache.push_back(n); - *dataOut = n; - } else { - for (std::list::const_iterator i = pcache.begin(); i != pcache.end(); i++) - delete[] *i; - pcache.clear(); - } - } - - - enum { SNC_AXES}; - enum { SNC_BOUNDARY, SNC_SKELETON }; - - class Polyhedron : public CGAL::OGL::OGL_base_object { - public: - std::list vertices_; - std::list edges_; - std::list halffacets_; - - GLuint object_list_; - bool init_; - - Bbox_3 bbox_; - - int style; - std::vector switches; - - typedef std::list::const_iterator Vertex_iterator; - typedef std::list::const_iterator Edge_iterator; - typedef std::list::const_iterator Halffacet_iterator; + class Polyhedron : public CGAL::OGL::Polyhedron { + public: enum RenderColor { CGAL_NEF3_MARKED_VERTEX_COLOR, CGAL_NEF3_MARKED_EDGE_COLOR, @@ -285,371 +55,51 @@ namespace OpenSCAD { CGAL_NEF3_UNMARKED_FACET_COLOR, NUM_COLORS }; - static unsigned char colors[NUM_COLORS][3]; - public: - Polyhedron() : bbox_(-1,-1,-1,1,1,1), switches(1) { - object_list_ = 0; - init_ = false; - style = SNC_BOUNDARY; - switches[SNC_AXES] = false; - } + CGAL::Color colors[NUM_COLORS]; - ~Polyhedron() - { if (object_list_) glDeleteLists(object_list_, 4); } - - void push_back(const Double_point& p, bool m) { - vertices_.push_back(DPoint(p,m)); - } - void push_back(const Double_segment& s, bool m) - { edges_.push_back(DSegment(s,m)); } - void push_back(const DFacet& f) - { halffacets_.push_back(f); } - - void toggle(int index) { - switches[index] = !switches[index]; - } - - void set_style(int index) { - style = index; - } - - bool is_initialized() const { return init_; } - - Bbox_3 bbox() const { return bbox_; } - Bbox_3& bbox() { return bbox_; } - - void draw(Vertex_iterator v) const { - // CGAL_NEF_TRACEN("drawing vertex "<<*v); - unsigned char *c = v->mark() ? colors[CGAL_NEF3_UNMARKED_VERTEX_COLOR] : colors[CGAL_NEF3_MARKED_VERTEX_COLOR]; - glPointSize(10); - glColor3ubv(c); - glBegin(GL_POINTS); - glVertex3d(v->x(),v->y(),v->z()); -#ifdef CGAL_NEF_EMPHASIZE_VERTEX - glColor3ub(255,0,0); - glVertex3d(CGAL_NEF_EMPHASIZE_VERTEX); -#endif - glEnd(); - } - - void draw(Edge_iterator e) const { - // CGAL_NEF_TRACEN("drawing edge "<<*e); - Double_point p = e->source(), q = e->target(); - unsigned char *c = e->mark() ? colors[CGAL_NEF3_UNMARKED_EDGE_COLOR] : colors[CGAL_NEF3_MARKED_EDGE_COLOR]; - glLineWidth(5); - glColor3ubv(c); - glBegin(GL_LINE_STRIP); - glVertex3d(p.x(), p.y(), p.z()); - glVertex3d(q.x(), q.y(), q.z()); - glEnd(); - } - - void draw(Halffacet_iterator f) const { - // CGAL_NEF_TRACEN("drawing facet "<<(f->debug(),"")); - GLUtesselator* tess_ = gluNewTess(); - gluTessCallback(tess_, GLenum(GLU_TESS_VERTEX_DATA), - (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &vertexCallback); - gluTessCallback(tess_, GLenum(GLU_TESS_COMBINE), - (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &combineCallback); - gluTessCallback(tess_, GLenum(GLU_TESS_BEGIN), - (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &beginCallback); - gluTessCallback(tess_, GLenum(GLU_TESS_END), - (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &endCallback); - gluTessCallback(tess_, GLenum(GLU_TESS_ERROR), - (GLvoid (CGAL_GLU_TESS_CALLBACK *)(CGAL_GLU_TESS_DOTS)) &errorCallback); - gluTessProperty(tess_, GLenum(GLU_TESS_WINDING_RULE), - GLU_TESS_WINDING_POSITIVE); - - DFacet::Coord_const_iterator cit; - unsigned char *c = f->mark() ? colors[CGAL_NEF3_UNMARKED_FACET_COLOR] : colors[CGAL_NEF3_MARKED_FACET_COLOR]; - glColor3ubv(c); - gluTessBeginPolygon(tess_,f->normal()); - // CGAL_NEF_TRACEN(" "); - // CGAL_NEF_TRACEN("Begin Polygon"); - gluTessNormal(tess_,f->dx(),f->dy(),f->dz()); - // forall facet cycles of f: - for(unsigned i = 0; i < f->number_of_facet_cycles(); ++i) { - gluTessBeginContour(tess_); - // CGAL_NEF_TRACEN(" Begin Contour"); - // put all vertices in facet cycle into contour: - for(cit = f->facet_cycle_begin(i); - cit != f->facet_cycle_end(i); ++cit) { - gluTessVertex(tess_, *cit, *cit); - // CGAL_NEF_TRACEN(" add Vertex"); - } - gluTessEndContour(tess_); - // CGAL_NEF_TRACEN(" End Contour"); - } - gluTessEndPolygon(tess_); - // CGAL_NEF_TRACEN("End Polygon"); - gluDeleteTess(tess_); - combineCallback(NULL, NULL, NULL, NULL); - } - - void construct_axes() const + void draw(bool showedges) const { - glLineWidth(2.0); - // red x-axis - glColor3f(1.0,0.0,0.0); - glBegin(GL_LINES); - glVertex3f(0.0,0.0,0.0); - glVertex3f(5000.0,0.0,0.0); - glEnd(); - // green y-axis - glColor3f(0.0,1.0,0.0); - glBegin(GL_LINES); - glVertex3f(0.0,0.0,0.0); - glVertex3f(0.0,5000.0,0.0); - glEnd(); - // blue z-axis and equator - glColor3f(0.0,0.0,1.0); - glBegin(GL_LINES); - glVertex3f(0.0,0.0,0.0); - glVertex3f(0.0,0.0,5000.0); - glEnd(); - // six coordinate points in pink: - glPointSize(10); - glBegin(GL_POINTS); - glColor3f(1.0,0.0,0.0); - glVertex3d(5,0,0); - glColor3f(0.0,1.0,0.0); - glVertex3d(0,5,0); - glColor3f(0.0,0.0,1.0); - glVertex3d(0,0,5); - glEnd(); + if (this->style == SNC_BOUNDARY) { + glCallList(this->object_list_+2); + if (showedges) { + glDisable(GL_LIGHTING); + glCallList(this->object_list_+1); + glCallList(this->object_list_); } + } else { + glDisable(GL_LIGHTING); + glCallList(this->object_list_+1); + glCallList(this->object_list_); + } + } + CGAL::Color getVertexColor(Vertex_iterator v) const + { + CGAL::Color c = v->mark() ? colors[CGAL_NEF3_UNMARKED_VERTEX_COLOR] : colors[CGAL_NEF3_MARKED_VERTEX_COLOR]; + return c; + } + CGAL::Color getEdgeColor(Edge_iterator e) const + { + CGAL::Color c = e->mark() ? colors[CGAL_NEF3_UNMARKED_EDGE_COLOR] : colors[CGAL_NEF3_MARKED_EDGE_COLOR]; + return c; + } - void fill_display_lists() { - glNewList(object_list_, GL_COMPILE); - Vertex_iterator v; - for(v=vertices_.begin();v!=vertices_.end();++v) - draw(v); - glEndList(); - - glNewList(object_list_+1, GL_COMPILE); - Edge_iterator e; - for(e=edges_.begin();e!=edges_.end();++e) - draw(e); - glEndList(); - - glNewList(object_list_+2, GL_COMPILE); - Halffacet_iterator f; - for(f=halffacets_.begin();f!=halffacets_.end();++f) - draw(f); - glEndList(); - - glNewList(object_list_+3, GL_COMPILE); // axes: - construct_axes(); - glEndList(); - - } - - void init() { - if (init_) return; - init_ = true; - switches[SNC_AXES] = false; - style = SNC_BOUNDARY; - object_list_ = glGenLists(4); - CGAL_assertion(object_list_); - fill_display_lists(); - } - - - void draw() const - { - if (!is_initialized()) const_cast(*this).init(); - double l = (std::max)( (std::max)( bbox().xmax() - bbox().xmin(), - bbox().ymax() - bbox().ymin()), - bbox().zmax() - bbox().zmin()); - if ( l < 1) // make sure that a single point doesn't screw up here - l = 1; - glScaled( 4.0/l, 4.0/l, 4.0/l); - glTranslated( -(bbox().xmax() + bbox().xmin()) / 2.0, - -(bbox().ymax() + bbox().ymin()) / 2.0, - -(bbox().zmax() + bbox().zmin()) / 2.0); - if (style == SNC_BOUNDARY) { - //glEnable(GL_LIGHTING); - glCallList(object_list_+2); // facets - //glDisable(GL_LIGHTING); - } - // move edges and vertices a bit towards the view-point, - // i.e., 1/100th of the unit vector in camera space - // double f = l / 4.0 / 100.0; - // glTranslated( z_vec[0] * f, z_vec[1] * f, z_vec[2] * f); - glCallList(object_list_+1); // edges - glCallList(object_list_); // vertices - if (switches[SNC_AXES]) glCallList(object_list_+3); // axis - } - - void debug(std::ostream& os = std::cerr) const - { - os << "OGL::Polyhedron" << std::endl; - os << "Vertices:" << std::endl; - Vertex_iterator v; - for(v=vertices_.begin();v!=vertices_.end();++v) - os << " "<<*v<<", mark="<mark()<debug(); os << std::endl; - os << std::endl; - } - - }; // Polyhedron - unsigned char Polyhedron::colors[][3] = { - {0xb7, 0xe8, 0x5c}, - {0xab, 0xd8, 0x56}, - {0x9d, 0xcb, 0x51}, - {0xff, 0xf6, 0x7c}, - {0xff, 0xec, 0x5e}, - {0xf9, 0xd7, 0x2c} + CGAL::Color getFacetColor(Halffacet_iterator f) const + { + CGAL::Color c = f->mark() ? colors[CGAL_NEF3_UNMARKED_FACET_COLOR] : colors[CGAL_NEF3_MARKED_FACET_COLOR]; + return c; + } }; template - class Nef3_Converter { - typedef typename Nef_polyhedron::SNC_structure SNC_structure; - typedef CGAL::SNC_decorator Base; - typedef CGAL::SNC_FM_decorator FM_decorator; - - public: - typedef typename SNC_structure::Vertex_const_iterator Vertex_const_iterator; - typedef typename SNC_structure::Halfedge_const_iterator Halfedge_const_iterator; - typedef typename SNC_structure::Halffacet_const_iterator Halffacet_const_iterator; - typedef typename SNC_structure::Halffacet_cycle_const_iterator Halffacet_cycle_const_iterator; - - typedef typename SNC_structure::Object_const_handle Object_const_handle; - typedef typename SNC_structure::SHalfedge_const_handle SHalfedge_const_handle; - typedef typename SNC_structure::SHalfloop_const_handle SHalfloop_const_handle; - - typedef typename SNC_structure::Vertex_const_handle Vertex_const_handle; - typedef typename SNC_structure::Halfedge_const_handle Halfedge_const_handle; - typedef typename SNC_structure::Halffacet_const_handle Halffacet_const_handle; - - typedef typename SNC_structure::Point_3 Point_3; - typedef typename SNC_structure::Vector_3 Vector_3; - typedef typename SNC_structure::Segment_3 Segment_3; - typedef typename SNC_structure::Plane_3 Plane_3; - typedef typename SNC_structure::Mark Mark; - typedef typename SNC_structure::SHalfedge_around_facet_const_circulator - SHalfedge_around_facet_const_circulator; - - private: - static OGL::Double_point double_point(const Point_3& p) - { return OGL::Double_point(CGAL::to_double(p.x()), - CGAL::to_double(p.y()), - CGAL::to_double(p.z())); } - - static OGL::Double_segment double_segment(const Segment_3& s) - { return OGL::Double_segment(double_point(s.source()), - double_point(s.target())); } - - static void draw(Vertex_const_handle v, const Nef_polyhedron& , - Polyhedron& P) { - Point_3 bp = v->point(); - // CGAL_NEF_TRACEN("vertex " << bp); - P.push_back(double_point(bp), v->mark()); - } - - static void draw(Halfedge_const_handle e, const Nef_polyhedron& , - Polyhedron& P) { - Vertex_const_handle s = e->source(); - Vertex_const_handle t = e->twin()->source(); - Segment_3 seg(s->point(),t->point()); - // CGAL_NEF_TRACEN("edge " << seg); - P.push_back(double_segment(seg), e->mark()); - } - - static void draw(Halffacet_const_handle f, const Nef_polyhedron& , - Polyhedron& P) { - OGL::DFacet g; - Halffacet_cycle_const_iterator fc; // all facet cycles: - CGAL_forall_facet_cycles_of(fc,f) - if ( fc.is_shalfedge() ) { // non-trivial facet cycle - g.new_facet_cycle(); - SHalfedge_const_handle h = fc; - SHalfedge_around_facet_const_circulator hc(h), he(hc); - CGAL_For_all(hc,he){ // all vertex coordinates in facet cycle - Point_3 sp = hc->source()->source()->point(); - // CGAL_NEF_TRACEN(" ");CGAL_NEF_TRACEN("facet" << sp); - g.push_back_vertex(double_point(sp)); - } - } - Vector_3 v = f->plane().orthogonal_vector(); - g.set_normal(CGAL::to_double(v.x()), - CGAL::to_double(v.y()), - CGAL::to_double(v.z()), - f->mark()); - P.push_back(g); - } - - // Returns the bounding box of the finite vertices of the polyhedron. - // Returns $[-1,+1]^3$ as bounding box if no finite vertex exists. - - static Bbox_3 bounded_bbox(const Nef_polyhedron& N) { - bool first_vertex = true; - Bbox_3 bbox( -1.0, -1.0, -1.0, 1.0, 1.0, 1.0); - Vertex_const_iterator vi; - CGAL_forall_vertices(vi, N) { - Point_3 p = vi->point(); - double x = CGAL::to_double(p.hx()); - double y = CGAL::to_double(p.hy()); - double z = CGAL::to_double(p.hz()); - double w = CGAL::to_double(p.hw()); - if (N.is_standard(vi)) { - if(first_vertex) { - bbox = Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); - first_vertex = false; - } else { - bbox = bbox + Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); - first_vertex = false; - } - } - } - return bbox; - } - - static void set_R(Bbox_3& bbox, const Nef_polyhedron& N) { - if(N.is_standard_kernel()) return; - double size = abs(bbox.xmin()); - if(size < bbox.xmax()) size = bbox.xmax(); - if(size < bbox.ymin()) size = bbox.ymin(); - if(size < bbox.ymax()) size = bbox.ymax(); - if(size < bbox.zmin()) size = bbox.zmin(); - if(size < bbox.zmax()) size = bbox.zmax(); - N.set_size_of_infimaximal_box(size*50); - // CGAL_NEF_TRACEN("set infi box size to " << size); - Vertex_const_iterator vi; - CGAL_forall_vertices(vi, N) - if(N.is_standard(vi)) - return; - bbox = Bbox_3(bbox.xmin()*10,bbox.ymin()*10,bbox.zmin()*10, - bbox.xmax()*10,bbox.ymax()*10,bbox.zmax()*10); - } + class Nef3_Converter : public CGAL::OGL::Nef3_Converter { public: - static void setColor(Polyhedron::RenderColor color_index, + static void setColor(Polyhedron* p, Polyhedron::RenderColor color_index, unsigned char r, unsigned char g, unsigned char b) { assert(color_index < Polyhedron::NUM_COLORS); - Polyhedron::colors[color_index][0] = r; - Polyhedron::colors[color_index][1] = g; - Polyhedron::colors[color_index][2] = b; + p->colors[color_index] = CGAL::Color(r,g,b); } - static void convert_to_OGLPolyhedron(const Nef_polyhedron& N, Polyhedron* P) { - Bbox_3 bbox(bounded_bbox(N)); - set_R(bbox,N); - P->bbox() = bbox; - Vertex_const_iterator v; - CGAL_forall_vertices(v,*N.sncp()) draw(v,N,*P); - Halfedge_const_iterator e; - CGAL_forall_edges(e,*N.sncp()) draw(e,N,*P); - Halffacet_const_iterator f; - CGAL_forall_facets(f,*N.sncp()) draw(f,N,*P); - } }; // Nef3_Converter @@ -657,4 +107,4 @@ namespace OpenSCAD { } // namespace OpenSCAD -#endif // CGAL_NEF_OPENGL_HELPER_H +#endif // CGAL_RENDERER_H diff --git a/src/mainwin.cc b/src/mainwin.cc index e217b1f5..d3df9080 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -78,8 +78,6 @@ #if 1 #include "CGAL_renderer.h" using OpenSCAD::OGL::Polyhedron; -using OpenSCAD::OGL::SNC_BOUNDARY; -using OpenSCAD::OGL::SNC_SKELETON; using OpenSCAD::OGL::Nef3_Converter; #else // a little hackish: we need access to default-private members of @@ -1494,15 +1492,16 @@ static void renderGLviaCGAL(void *vp) { Polyhedron *p = (Polyhedron*)m->cgal_ogl_p; if (!p) { - Nef3_Converter::setColor(Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR, + m->cgal_ogl_p = p = new Polyhedron(); + Nef3_Converter::setColor(p,Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR, Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).red(), Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).green(), Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).blue()); - Nef3_Converter::setColor(Polyhedron::CGAL_NEF3_UNMARKED_FACET_COLOR, + Nef3_Converter::setColor(p,Polyhedron::CGAL_NEF3_UNMARKED_FACET_COLOR, Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).red(), Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).green(), Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).blue()); - m->cgal_ogl_p = p = new Polyhedron(); + Nef3_Converter::convert_to_OGLPolyhedron(m->root_N->p3, p); p->init(); } @@ -1513,18 +1512,7 @@ static void renderGLviaCGAL(void *vp) #if 0 p->draw(); #else - if (p->style == SNC_BOUNDARY) { - glCallList(p->object_list_+2); - if (m->viewActionShowEdges->isChecked()) { - glDisable(GL_LIGHTING); - glCallList(p->object_list_+1); - glCallList(p->object_list_); - } - } else { - glDisable(GL_LIGHTING); - glCallList(p->object_list_+1); - glCallList(p->object_list_); - } + p->draw(m->viewActionShowEdges->isChecked()); #endif } } From 2ddbc3f1bc86253f2cfd09a9ec1757bd3656242c Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:33:47 +0100 Subject: [PATCH 4/9] Format the code nicely using astyle. --- src/CGAL_renderer.h | 104 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/CGAL_renderer.h b/src/CGAL_renderer.h index 8f4a1b67..f8b41bdd 100644 --- a/src/CGAL_renderer.h +++ b/src/CGAL_renderer.h @@ -39,71 +39,71 @@ using CGAL::OGL::SNC_BOUNDARY; using CGAL::OGL::SNC_SKELETON; -namespace OpenSCAD { +namespace OpenSCAD +{ - namespace OGL { +namespace OGL +{ - class Polyhedron : public CGAL::OGL::Polyhedron { - public: - enum RenderColor { - CGAL_NEF3_MARKED_VERTEX_COLOR, - CGAL_NEF3_MARKED_EDGE_COLOR, - CGAL_NEF3_MARKED_FACET_COLOR, - CGAL_NEF3_UNMARKED_VERTEX_COLOR, - CGAL_NEF3_UNMARKED_EDGE_COLOR, - CGAL_NEF3_UNMARKED_FACET_COLOR, - NUM_COLORS - }; - CGAL::Color colors[NUM_COLORS]; +class Polyhedron : public CGAL::OGL::Polyhedron +{ +public: + enum RenderColor { + CGAL_NEF3_MARKED_VERTEX_COLOR, + CGAL_NEF3_MARKED_EDGE_COLOR, + CGAL_NEF3_MARKED_FACET_COLOR, + CGAL_NEF3_UNMARKED_VERTEX_COLOR, + CGAL_NEF3_UNMARKED_EDGE_COLOR, + CGAL_NEF3_UNMARKED_FACET_COLOR, + NUM_COLORS + }; + CGAL::Color colors[NUM_COLORS]; - void draw(bool showedges) const - { - if (this->style == SNC_BOUNDARY) { - glCallList(this->object_list_+2); - if (showedges) { + void draw(bool showedges) const { + if(this->style == SNC_BOUNDARY) { + glCallList(this->object_list_+2); + if(showedges) { + glDisable(GL_LIGHTING); + glCallList(this->object_list_+1); + glCallList(this->object_list_); + } + } else { glDisable(GL_LIGHTING); glCallList(this->object_list_+1); glCallList(this->object_list_); - } - } else { - glDisable(GL_LIGHTING); - glCallList(this->object_list_+1); - glCallList(this->object_list_); + } + } + CGAL::Color getVertexColor(Vertex_iterator v) const { + CGAL::Color c = v->mark() ? colors[CGAL_NEF3_UNMARKED_VERTEX_COLOR] : colors[CGAL_NEF3_MARKED_VERTEX_COLOR]; + return c; } - } - CGAL::Color getVertexColor(Vertex_iterator v) const - { - CGAL::Color c = v->mark() ? colors[CGAL_NEF3_UNMARKED_VERTEX_COLOR] : colors[CGAL_NEF3_MARKED_VERTEX_COLOR]; - return c; - } - CGAL::Color getEdgeColor(Edge_iterator e) const - { - CGAL::Color c = e->mark() ? colors[CGAL_NEF3_UNMARKED_EDGE_COLOR] : colors[CGAL_NEF3_MARKED_EDGE_COLOR]; - return c; - } + CGAL::Color getEdgeColor(Edge_iterator e) const { + CGAL::Color c = e->mark() ? colors[CGAL_NEF3_UNMARKED_EDGE_COLOR] : colors[CGAL_NEF3_MARKED_EDGE_COLOR]; + return c; + } - CGAL::Color getFacetColor(Halffacet_iterator f) const - { - CGAL::Color c = f->mark() ? colors[CGAL_NEF3_UNMARKED_FACET_COLOR] : colors[CGAL_NEF3_MARKED_FACET_COLOR]; - return c; - } - }; + CGAL::Color getFacetColor(Halffacet_iterator f) const { + CGAL::Color c = f->mark() ? colors[CGAL_NEF3_UNMARKED_FACET_COLOR] : colors[CGAL_NEF3_MARKED_FACET_COLOR]; + return c; + } +}; - template - class Nef3_Converter : public CGAL::OGL::Nef3_Converter { - public: - static void setColor(Polyhedron* p, Polyhedron::RenderColor color_index, - unsigned char r, unsigned char g, unsigned char b) { - assert(color_index < Polyhedron::NUM_COLORS); - p->colors[color_index] = CGAL::Color(r,g,b); - } +template +class Nef3_Converter : public CGAL::OGL::Nef3_Converter +{ +public: + static void setColor(Polyhedron* p, Polyhedron::RenderColor color_index, + unsigned char r, unsigned char g, unsigned char b) { + assert(color_index < Polyhedron::NUM_COLORS); + p->colors[color_index] = CGAL::Color(r,g,b); + } - - }; // Nef3_Converter - } // namespace OGL +}; // Nef3_Converter + +} // namespace OGL } // namespace OpenSCAD From e02eb751b136e656e25518c41bd8572e20d46997 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:42:00 +0100 Subject: [PATCH 5/9] Move setColor into Polyhedron class which is a much more sensible place. --- src/CGAL_renderer.h | 11 +++-------- src/mainwin.cc | 6 +++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/CGAL_renderer.h b/src/CGAL_renderer.h index f8b41bdd..a2b3b72b 100644 --- a/src/CGAL_renderer.h +++ b/src/CGAL_renderer.h @@ -88,20 +88,15 @@ public: CGAL::Color c = f->mark() ? colors[CGAL_NEF3_UNMARKED_FACET_COLOR] : colors[CGAL_NEF3_MARKED_FACET_COLOR]; return c; } -}; -template -class Nef3_Converter : public CGAL::OGL::Nef3_Converter -{ -public: - static void setColor(Polyhedron* p, Polyhedron::RenderColor color_index, + void setColor(Polyhedron::RenderColor color_index, unsigned char r, unsigned char g, unsigned char b) { assert(color_index < Polyhedron::NUM_COLORS); - p->colors[color_index] = CGAL::Color(r,g,b); + this->colors[color_index] = CGAL::Color(r,g,b); } -}; // Nef3_Converter +}; // Polyhedron } // namespace OGL diff --git a/src/mainwin.cc b/src/mainwin.cc index d3df9080..24136bf8 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -78,7 +78,7 @@ #if 1 #include "CGAL_renderer.h" using OpenSCAD::OGL::Polyhedron; -using OpenSCAD::OGL::Nef3_Converter; +using CGAL::OGL::Nef3_Converter; #else // a little hackish: we need access to default-private members of // CGAL::OGL::Nef3_Converter so we can implement our own draw function @@ -1493,11 +1493,11 @@ static void renderGLviaCGAL(void *vp) Polyhedron *p = (Polyhedron*)m->cgal_ogl_p; if (!p) { m->cgal_ogl_p = p = new Polyhedron(); - Nef3_Converter::setColor(p,Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR, + p->setColor(Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR, Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).red(), Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).green(), Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).blue()); - Nef3_Converter::setColor(p,Polyhedron::CGAL_NEF3_UNMARKED_FACET_COLOR, + p->setColor(Polyhedron::CGAL_NEF3_UNMARKED_FACET_COLOR, Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).red(), Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).green(), Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).blue()); From 1d510e0870cfc4e458820a6cc01740ee17b8fbeb Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:46:22 +0100 Subject: [PATCH 6/9] Removed a huge whitespace preceeding a lines of code. --- src/mainwin.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mainwin.cc b/src/mainwin.cc index 24136bf8..3abcb88c 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -1494,13 +1494,13 @@ static void renderGLviaCGAL(void *vp) if (!p) { m->cgal_ogl_p = p = new Polyhedron(); p->setColor(Polyhedron::CGAL_NEF3_MARKED_FACET_COLOR, - Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).red(), - Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).green(), - Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).blue()); + Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).red(), + Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).green(), + Preferences::inst()->color(Preferences::CGAL_FACE_BACK_COLOR).blue()); p->setColor(Polyhedron::CGAL_NEF3_UNMARKED_FACET_COLOR, - Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).red(), - Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).green(), - Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).blue()); + Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).red(), + Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).green(), + Preferences::inst()->color(Preferences::CGAL_FACE_FRONT_COLOR).blue()); Nef3_Converter::convert_to_OGLPolyhedron(m->root_N->p3, p); p->init(); From ae8cda73a83d8b4e55eb87eb1df225d3d62e1320 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:47:24 +0100 Subject: [PATCH 7/9] Removed the remaining crud. --- src/mainwin.cc | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/src/mainwin.cc b/src/mainwin.cc index 3abcb88c..d5da7aa1 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -75,30 +75,10 @@ #ifdef ENABLE_CGAL -#if 1 #include "CGAL_renderer.h" using OpenSCAD::OGL::Polyhedron; using CGAL::OGL::Nef3_Converter; -#else -// a little hackish: we need access to default-private members of -// CGAL::OGL::Nef3_Converter so we can implement our own draw function -// that does not scale the model. so we define 'class' to 'struct' -// for this header.. -// -// theoretically there could be two problems: -// 1.) defining language keyword with the pre processor is illegal afair -// 2.) the compiler could use a different memory layout or name mangling for structs -// -// both does not seam to be the case with todays compilers... -// -#define class struct -#include -#undef class -using CGAL::OGL::Polyhedron; -using CGAL::OGL::SNC_BOUNDARY; -using CGAL::OGL::SNC_SKELETON; -using CGAL::OGL::Nef3_Converter; -#endif + #endif // ENABLE_CGAL #define QUOTE(x__) # x__ @@ -1509,11 +1489,9 @@ static void renderGLviaCGAL(void *vp) p->set_style(SNC_BOUNDARY); if (m->viewActionCGALGrid->isChecked()) p->set_style(SNC_SKELETON); -#if 0 - p->draw(); -#else + p->draw(m->viewActionShowEdges->isChecked()); -#endif + } } From a262be69f7420f01427df29f50db4c1f3d304dfc Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 06:59:23 +0100 Subject: [PATCH 8/9] A bit of encapsulation can't hurt. --- src/CGAL_renderer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CGAL_renderer.h b/src/CGAL_renderer.h index a2b3b72b..b5709b84 100644 --- a/src/CGAL_renderer.h +++ b/src/CGAL_renderer.h @@ -58,7 +58,6 @@ public: CGAL_NEF3_UNMARKED_FACET_COLOR, NUM_COLORS }; - CGAL::Color colors[NUM_COLORS]; void draw(bool showedges) const { if(this->style == SNC_BOUNDARY) { @@ -94,7 +93,8 @@ public: assert(color_index < Polyhedron::NUM_COLORS); this->colors[color_index] = CGAL::Color(r,g,b); } - +private: + CGAL::Color colors[NUM_COLORS]; }; // Polyhedron From d3c9e3ad84bdc64aef58b2b4b6417804c040d421 Mon Sep 17 00:00:00 2001 From: Giles Bathgate Date: Sat, 9 Apr 2011 07:14:04 +0100 Subject: [PATCH 9/9] Whoops accidentally removed the code that initialised the colors array. --- src/CGAL_renderer.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/CGAL_renderer.h b/src/CGAL_renderer.h index b5709b84..a8bdc43e 100644 --- a/src/CGAL_renderer.h +++ b/src/CGAL_renderer.h @@ -49,6 +49,7 @@ namespace OGL class Polyhedron : public CGAL::OGL::Polyhedron { public: + enum RenderColor { CGAL_NEF3_MARKED_VERTEX_COLOR, CGAL_NEF3_MARKED_EDGE_COLOR, @@ -59,6 +60,15 @@ public: NUM_COLORS }; + Polyhedron() { + setColor(CGAL_NEF3_MARKED_VERTEX_COLOR,0xb7,0xe8,0x5c); + setColor(CGAL_NEF3_MARKED_EDGE_COLOR,0xab,0xd8,0x56); + setColor(CGAL_NEF3_MARKED_FACET_COLOR,0x9d,0xcb,0x51); + setColor(CGAL_NEF3_UNMARKED_VERTEX_COLOR,0xff,0xf6,0x7c); + setColor(CGAL_NEF3_UNMARKED_EDGE_COLOR,0xff,0xec,0x5e); + setColor(CGAL_NEF3_UNMARKED_FACET_COLOR,0xf9,0xd7,0x2c); + } + void draw(bool showedges) const { if(this->style == SNC_BOUNDARY) { glCallList(this->object_list_+2); @@ -89,7 +99,7 @@ public: } void setColor(Polyhedron::RenderColor color_index, - unsigned char r, unsigned char g, unsigned char b) { + unsigned char r, unsigned char g, unsigned char b) { assert(color_index < Polyhedron::NUM_COLORS); this->colors[color_index] = CGAL::Color(r,g,b); }