📄 sphere_geometry_ogl.h
字号:
glEnd(); glDisable(GL_COLOR_MATERIAL); } virtual void print() const { std::cerr << "triangle " << t_; }};//----------------------------------------------------------------------------// the sphere://---------------------------------------------------------------------------- enum { SM_FACES, SM_SKELETON, SM_TRIANGULATION }; enum { SM_AXES, SM_CUBE };class Unit_sphere : public OGL_base_object { typedef std::list<Gen_object*> Object_list; typedef Object_list::const_iterator Object_const_iterator; typedef Object_list::iterator Object_iterator; GLUquadricObj* sphere_; int style_; bool initialized_; Object_list objects_,triangles_,triangle_edges_; GLuint sphere_list_; std::vector<bool> switches;public:void init() { style_ = SM_FACES; switches[0] = true; switches[1] = false; gluQuadricNormals(sphere_,GLenum(GLU_SMOOTH));}Unit_sphere() : switches(2) { sphere_ = gluNewQuadric(); initialized_ = false; init(); }void clear_list() { while ( objects_.begin() != objects_.end() ) { delete (*objects_.begin()); objects_.pop_front(); } while ( triangles_.begin() != triangles_.end() ) { delete (*triangles_.begin()); triangles_.pop_front(); } while ( triangle_edges_.begin() != triangle_edges_.end() ) { delete (*triangle_edges_.begin()); triangle_edges_.pop_front(); }}void copy_list(const Unit_sphere& S){ Object_const_iterator it; CGAL_forall_iterators (it,S.objects_) objects_.push_back( (*it)->clone() ); CGAL_forall_iterators (it,S.triangles_) triangles_.push_back( (*it)->clone() ); CGAL_forall_iterators (it,S.triangle_edges_) triangle_edges_.push_back( (*it)->clone() );}void print() const{ std::cerr << "Dumping Unit_sphere:\n"; for (Object_const_iterator it = objects_.begin(); it != objects_.end(); ++it) (*it)->print(); std::cerr << std::endl; for (Object_const_iterator it = triangles_.begin(); it != triangles_.end(); ++it) (*it)->print(); std::cerr << std::endl;}Unit_sphere(const Unit_sphere& S) : switches(2){ CGAL_NEF_TRACEN("copyconstruction"); sphere_ = gluNewQuadric(); initialized_ = S.initialized_; style_ = S.style_; switches[0] = S.switches[0]; switches[1] = S.switches[1]; copy_list(S);}Unit_sphere& operator=(const Unit_sphere& S){ CGAL_NEF_TRACEN("assignment"); initialized_ = S.initialized_; style_ = S.style_; switches[0] = S.switches[0]; switches[1] = S.switches[1]; clear_list(); copy_list(S); return *this;}~Unit_sphere() { clear_list(); gluDeleteQuadric(sphere_); }template <typename R>void push_back(const CGAL::Sphere_point<R>& p, CGAL::Color c = CGAL::YELLOW, unsigned w = 5){ objects_.push_back(new Sphere_point<R>(p,c,w)); }template <typename R>void push_back(const CGAL::Sphere_segment<R>& s, CGAL::Color c = CGAL::BLACK, unsigned w = 1){ objects_.push_back(new Sphere_segment<R>(s,c,w)); }template <typename R>void push_back(const CGAL::Sphere_circle<R>& s, CGAL::Color c = CGAL::BLACK, unsigned w = 1){ objects_.push_back(new Sphere_circle<R>(s,c,w)); }template <typename R>void push_back(const CGAL::Sphere_triangle<R>& t, CGAL::Color c = CGAL::WHITE){ triangles_.push_back(new Sphere_triangle<R>(t,c)); }template <typename R>void push_back_triangle_edge(const CGAL::Sphere_segment<R>& s, CGAL::Color c = CGAL::BLUE, unsigned w = 1){ triangle_edges_.push_back(new Sphere_segment<R>(s,c,w)); }void set_style(int style) { style_ = style; }void toggle(int index) { switches[index] = !switches[index]; }private:void construct_axes() const{ int i; register double f(1.02); glNewList(sphere_list_+3, GL_COMPILE); glLineWidth(2.0); // red x-axis and equator glColor3f(1.0,0.0,0.0); glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0); glVertex3f(1.2,0.0,0.0); glEnd(); glBegin(GL_LINE_LOOP); for(i=0;i<100;i++) glVertex3d(f*cos(2.0*CGAL_PI*i/100.0),f*sin(2.0*CGAL_PI*i/100.0),0.0); glEnd(); // green y-axis and equator glColor3f(0.0,1.0,0.0); glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.2,0.0); glEnd(); glBegin(GL_LINE_LOOP); for(i=0;i<100;i++) glVertex3d(0.0,f*cos(2.0*CGAL_PI*i/100.0),f*sin(2.0*CGAL_PI*i/100.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,1.2); glEnd(); glBegin(GL_LINE_LOOP); for(i=0;i<100;i++) glVertex3d(f*cos(2.0*CGAL_PI*i/100.0),0.0,f*sin(2.0*CGAL_PI*i/100.0)); glEnd(); // six coordinate points in pink: glPointSize(10); glColor3f(1,0,1); glBegin(GL_POINTS); glVertex3d(1,0,0); glVertex3d(0,1,0); glVertex3d(0,0,1); glVertex3d(-1,0,0); glVertex3d(0,-1,0); glVertex3d(0,0,-1); glEnd(); glEndList();}void construct_cube() const{ glNewList(sphere_list_+4, GL_COMPILE); glColor3f(1,1,0); // yellow glLineWidth(2.0); glBegin(GL_LINE_LOOP); glVertex3f(-1.0,-1.0,-1.0); glVertex3f( 1.0,-1.0,-1.0); glVertex3f( 1.0, 1.0,-1.0); glVertex3f(-1.0, 1.0,-1.0); glEnd(); glBegin(GL_LINE_LOOP); glVertex3f(-1.0,-1.0, 1.0); glVertex3f( 1.0,-1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); glEnd(); glBegin(GL_LINES); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0, 1.0); glVertex3f( 1.0,-1.0,-1.0); glVertex3f( 1.0,-1.0, 1.0); glVertex3f( 1.0, 1.0,-1.0); glVertex3f( 1.0, 1.0, 1.0); glVertex3f(-1.0, 1.0,-1.0); glVertex3f(-1.0, 1.0, 1.0); glEnd(); glEndList();}void construct(){ initialized_=true; sphere_list_ = glGenLists(5); CGAL_assertion_msg(sphere_list_!=0,"no display list."); // skeleton: glNewList(sphere_list_, GL_COMPILE); for (Object_const_iterator it = objects_.begin(); it != objects_.end(); ++it) (*it)->draw(); glEndList(); // triangles: glNewList(sphere_list_+1, GL_COMPILE); for (Object_const_iterator it = triangles_.begin(); it != triangles_.end(); ++it) (*it)->draw(); glEndList(); glNewList(sphere_list_+2, GL_COMPILE); for (Object_const_iterator it = triangle_edges_.begin(); it != triangle_edges_.end(); ++it) (*it)->draw(); glEndList(); // orientation features: construct_axes(); construct_cube();}public:void draw(GLdouble z_vec[3]) const// void draw() const{ gluQuadricDrawStyle(sphere_,GLenum(GLU_FILL)); glEnable(GL_LIGHTING); if ( style_ == SM_SKELETON ) { glEnable(GL_COLOR_MATERIAL); glColor3f(0.7,0.7,0.7); gluSphere(sphere_,shrink_fac,50,50); glDisable(GL_COLOR_MATERIAL); } glDisable(GL_LIGHTING); if ( !initialized_ ) const_cast<Unit_sphere*>(this)->construct(); if ( style_ == SM_FACES || style_ == SM_TRIANGULATION ) { glEnable(GL_LIGHTING); glCallList(sphere_list_+1); // triangle list glDisable(GL_LIGHTING); } if ( style_ == SM_TRIANGULATION ) { glCallList(sphere_list_+2); // triangle edge list } if ( style_ != SM_TRIANGULATION ) { glCallList(sphere_list_); } if ( switches[0] ) glCallList(sphere_list_+3); if ( switches[1] ) glCallList(sphere_list_+4);}};template <typename Map_>class SM_BooleColor { typedef typename Map_::SVertex_const_handle SVertex_const_handle; typedef typename Map_::SHalfedge_const_handle SHalfedge_const_handle; typedef typename Map_::SHalfloop_const_handle SHalfloop_const_handle; typedef typename Map_::SFace_const_handle SFace_const_handle; typedef typename Map_::Mark Mark; public: Color color(SVertex_const_handle, Mark m) const { return ( m ? CGAL::BLACK : CGAL::WHITE ); } Color color(SHalfedge_const_handle, Mark m) const { return ( m ? CGAL::BLACK : CGAL::WHITE ); } Color color(SHalfloop_const_handle, Mark m) const { return ( m ? CGAL::BLACK : CGAL::WHITE ); } Color color(SFace_const_handle, Mark m) const { return ( m ? DGREY : LGREY ); }};template <typename Nef_polyhedron>class NefS2_to_UnitSphere{ typedef typename Nef_polyhedron::Sphere_map Sphere_map; typedef CGAL::OGL::SM_BooleColor<Sphere_map> Color_; typedef typename Sphere_map::Sphere_kernel Sphere_kernel; typedef SM_decorator<Sphere_map> Decorator; typedef CGAL::SM_triangulator<Decorator> Base; typedef typename Sphere_map::SVertex_const_handle SVertex_const_handle; typedef typename Sphere_map::SHalfedge_const_handle SHalfedge_const_handle; typedef typename Sphere_map::SFace_const_handle SFace_const_handle; typedef typename Sphere_map::SVertex_const_iterator SVertex_const_iterator; typedef typename Sphere_map::SHalfedge_const_iterator SHalfedge_const_iterator; typedef typename Sphere_map::SFace_const_iterator SFace_const_iterator; typedef typename Sphere_map::Mark Mark; typedef typename Sphere_kernel::Sphere_point Sphere_point; typedef typename Sphere_kernel::Sphere_segment Sphere_segment; typedef typename Sphere_kernel::Sphere_circle Sphere_circle; typedef typename Sphere_kernel::Sphere_triangle Sphere_triangle; typedef Color_ Color_objects; public: static CGAL::OGL::Unit_sphere convert(const Nef_polyhedron& E_, const Color_objects& CO_ = Color_objects()) { Sphere_map MT_(true); Base T_(&MT_, &E_); CGAL::OGL::Unit_sphere S_; T_.triangulate(); SHalfedge_const_iterator e; CGAL_forall_sedges(e,E_) { if ( e->source() == e->twin()->source() ) { S_.push_back(E_.circle(e), CO_.color(e,E_.mark(e)));} else { S_.push_back(Sphere_segment(E_.point(E_.source(e)), E_.point(E_.target(e)), E_.circle(e)),CO_.color(e,E_.mark(e))); } } // draw sphere circles underlying loops of E_: if ( E_.has_shalfloop() ) S_.push_back( Sphere_circle(E_.circle(E_.shalfloop())), CO_.color(E_.shalfloop(),E_.mark(E_.shalfloop()))); // draw points underlying vertices of E_: SVertex_const_iterator v; CGAL_forall_svertices(v,E_) S_.push_back(E_.point(v),CO_.color(v,E_.mark(v))); Unique_hash_map<SHalfedge_const_iterator,bool> Done(false); CGAL_forall_shalfedges(e,T_) { if ( Done[e] ) continue; SHalfedge_const_handle en(e->snext()),enn(en->snext()); CGAL_NEF_TRACEV(T_.incident_triangle(e)); CGAL_NEF_TRACEN(T_.incident_mark(e)<<T_.incident_mark(en)<<T_.incident_mark(enn)); CGAL_assertion(T_.incident_mark(e)==T_.incident_mark(en) && T_.incident_mark(en)==T_.incident_mark(enn)); Mark m = T_.incident_mark(e); Sphere_triangle t = T_.incident_triangle(e); S_.push_back(t, (m ? DGREY : LGREY) ); Done[e]=Done[en]=Done[enn]=true; } Done.clear(false); CGAL_forall_shalfedges(e,T_) { if ( Done[e] ) continue; S_.push_back_triangle_edge(Sphere_segment(E_.point(E_.source(e)), E_.point(E_.target(e)), E_.circle(e))); Done[e]=Done[e->twin()]=true; } return S_; }};} // OGLCGAL_END_NAMESPACEtemplate <class R>std::ostream& operator<<(std::ostream& os, const CGAL::OGL::Sphere_segment<R>& s){ CGAL::OGL::VSegment::const_iterator it; os << s.original() << " "; for (it = s.begin(); it != s.end(); ++it) os << *it; return os;}template <class R>std::ostream& operator<<(std::ostream& os, const CGAL::OGL::Sphere_circle<R>& s){ CGAL::OGL::VSegment::const_iterator it; os << s.original() << " "; for (it = s.begin(); it != s.end(); ++it) os << *it; return os;}template <class R>std::ostream& operator<<(std::ostream& os, const CGAL::OGL::Sphere_point<R>& p){ os << p.original() << CGAL::OGL::VPoint(p); return os; }#endif //CGAL_SPHERE_GEOMETRY_OGL_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -