visualizor.h
来自「CGAL is a collaborative effort of severa」· C头文件 代码 · 共 942 行 · 第 1/3 页
H
942 行
// 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.//// $Source: /CVSROOT/CGAL/Packages/Nef_3/include/CGAL/Nef_3/Visualizor.h,v $// $Revision: 1.10.2.1 $ $Date: 2004/12/08 19:31:04 $// $Name: $//// Author(s) : Michael Seel <seel@mpi-sb.mpg.de>// Miguel Granados <granados@mpi-sb.mpg.de>// Susan Hert <hert@mpi-sb.mpg.de>// Lutz Kettner <kettner@mpi-sb.mpg.de>#ifndef CGAL_VISUALIZOR_OPENGL_3_H#define CGAL_VISUALIZOR_OPENGL_3_H#include <CGAL/basic.h>#include <CGAL/IO/Color.h>#include <GL/glut.h>#include <cmath>#include <cstdlib>#include <cstdio>#include <string>#include <vector>#include <CGAL/Simple_cartesian.h>#include <CGAL/Nef_3/SNC_structure.h>#include <CGAL/Nef_3/SNC_decorator.h>#include <CGAL/Nef_3/SNC_FM_decorator.h>#undef CGAL_NEF_DEBUG#define CGAL_NEF_DEBUG 157#include <CGAL/Nef_2/debug.h>#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,44CGAL_BEGIN_NAMESPACEnamespace OGL {// ----------------------------------------------------------------------------// Drawable double types:// ---------------------------------------------------------------------------- typedef CGAL::Simple_cartesian<double> 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<Double_triple&>(*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<Double_triple> Coord_vector; typedef std::vector<unsigned> 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<double*>(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<number_of_facet_cycles()); if (i==0) return coords_.begin(); else return coords_.begin()+fc_ends_[i]; } Coord_iterator facet_cycle_end(unsigned i) { CGAL_assertion(i<number_of_facet_cycles()); if (i<fc_ends_.size()-1) return coords_.begin()+fc_ends_[i+1]; else return coords_.end(); } Coord_const_iterator facet_cycle_begin(unsigned i) const { CGAL_assertion(i<number_of_facet_cycles()); if (i==0) return coords_.begin(); else return coords_.begin()+fc_ends_[i]; } Coord_const_iterator facet_cycle_end(unsigned i) const { CGAL_assertion(i<number_of_facet_cycles()); if (i<fc_ends_.size()-1) return coords_.begin()+fc_ends_[i+1]; else return coords_.end(); } void debug(std::ostream& os = std::cerr) const { os << "DFacet, normal=" << normal_ << ", mark=" << mark() << std::endl; for(unsigned i=0; i<number_of_facet_cycles(); ++i) { os << " facet cycle "; // put all vertices in facet cycle into contour: Coord_const_iterator cit; for(cit = facet_cycle_begin(i); cit != facet_cycle_end(i); ++cit) os << *cit; os << std::endl; } } }; // DFacet// ----------------------------------------------------------------------------// OGL Drawable Polyhedron:// ---------------------------------------------------------------------------- inline void beginCallback(GLenum which) { glBegin(which); } inline void endCallback(void) { glEnd(); } inline void errorCallback(GLenum errorCode) { const GLubyte *estring; estring = gluErrorString(errorCode); fprintf(stderr, "Tessellation Error: %s\n", estring); exit (0); } inline void vertexCallback(GLvoid* vertex, GLvoid* user) { GLdouble* pc(static_cast<GLdouble*>(vertex)); GLdouble* pu(static_cast<GLdouble*>(user)); CGAL_NEF_TRACEN("vertexCallback coord "<<pc[0]<<","<<pc[1]<<","<<pc[2]); CGAL_NEF_TRACEN("vertexCallback normal "<<pu[0]<<","<<pu[1]<<","<<pu[2]); glNormal3dv(pu); glVertex3dv(pc); } class Polyhedron { std::list<DPoint> vertices_; std::list<DSegment> edges_; std::list<DFacet> halffacets_; GLuint object_list_; bool init_, axes_, surface_; Bbox_3 bbox_; typedef std::list<DPoint>::const_iterator Vertex_iterator; typedef std::list<DSegment>::const_iterator Edge_iterator; typedef std::list<DFacet>::const_iterator Halffacet_iterator; public: Polyhedron() : bbox_(-1,-1,-1,1,1,1) { object_list_ = 0; init_ = axes_ = false; surface_ = true; } Polyhedron(const Polyhedron& p) { object_list_ = 0; init_ = axes_ = false; surface_ = true; } Polyhedron& operator=(const Polyhedron& p) { 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_axes() { axes_ = !axes_; } void skeleton_on() { surface_ = false; } void boundary_on() { surface_ = true; } 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()); 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();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?