visualizor.h
来自「CGAL is a collaborative effort of severa」· C头文件 代码 · 共 942 行 · 第 1/3 页
H
942 行
gluTessCallback(tess_, GLenum(GLU_TESS_VERTEX_DATA), (GLvoid (*)()) &vertexCallback); gluTessCallback(tess_, GLenum(GLU_TESS_BEGIN), (GLvoid (*)()) &beginCallback); gluTessCallback(tess_, GLenum(GLU_TESS_END), (GLvoid (*)()) &endCallback); gluTessCallback(tess_, GLenum(GLU_TESS_ERROR), (GLvoid (*)()) &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(5.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,5.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,5.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; CGAL_forall_iterators(v,vertices_) draw(v); glEndList(); glNewList(object_list_+1, GL_COMPILE); Edge_iterator e; CGAL_forall_iterators(e,edges_) draw(e); glEndList(); glNewList(object_list_+2, GL_COMPILE); Halffacet_iterator f; CGAL_forall_iterators(f,halffacets_) draw(f); glEndList(); glNewList(object_list_+3, GL_COMPILE); // axes: construct_axes(); glEndList(); } void initialize() { if (init_) return; init_ = true; axes_ = false; object_list_ = glGenLists(4); CGAL_assertion(object_list_); fill_display_lists(); } void draw( GLdouble z_vec[3]) const { if (!is_initialized()) const_cast<Polyhedron&>(*this).initialize(); 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 ( surface_ ) { //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 (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; CGAL_forall_iterators(v,vertices_) os << " "<<*v<<", mark="<<v->mark()<<std::endl; os << "Edges:" << std::endl; Edge_iterator e; CGAL_forall_iterators(e,edges_) os << " "<<*e<<", mark="<<e->mark()<<std::endl; os << "Facets:" << std::endl; Halffacet_iterator f; CGAL_forall_iterators(f,halffacets_) f->debug(); os << std::endl; os << std::endl; } }; // Polyhedron// ----------------------------------------------------------------------------// Viewer configuration:// ---------------------------------------------------------------------------- enum MenuEntries { ROTATE, SCALE, TRANSLATE, TRANS_Z, RESET_CONTROL, AXES, BOUNDARY, SKELETON, PERSP, FULLSCREEN, QUIT }; const double znear = 4.0; const double zfar = 4.0; const double eye = 6.0; const double wsize = 2.0; int window_width = 600; // Breite und int window_height = 600; // Hoehe des Fensters int window_radius = 300; // min(width,height) / 2 bool perspective = true; int mouse_x, mouse_y; // Mauskoordinaten linker button int interaction; // type of interaction in motion fct. int motion_mode = ROTATE; // Bewegen der Maus bei Mouse1 gedrueckt int submenu1, submenu2; double dx = 0; // Translation double dy = 0; // Translation double dz = 0; // Translation in Z double s = 0.4; // Skalierung Affine_3 rotation( IDENTITY); // Rotation long double factor_s; // Umrechnungsfaktor fuer Skalierung // our draw object: static std::vector<Polyhedron> polyhedra_; static std::vector<std::string> titles_;static Polyhedron& add_polyhedron() { polyhedra_.push_back(Polyhedron()); return polyhedra_.back(); }static void show (int mode){ std::vector<Polyhedron>::iterator it; switch(mode) { case ROTATE: case SCALE: case TRANSLATE: case TRANS_Z: motion_mode = mode; break; case RESET_CONTROL: dx = dy = dz = 0.0; s = 0.5; rotation = Affine_3( IDENTITY); motion_mode = ROTATE; CGAL_forall_iterators(it,polyhedra_) it->initialize(); glutPostRedisplay(); break; case AXES: CGAL_forall_iterators(it,polyhedra_) it->toggle_axes(); glutPostRedisplay(); break; case BOUNDARY: CGAL_forall_iterators(it,polyhedra_) it->boundary_on(); //CGAL_forall_iterators(it,polyhedra_) it->draw(); glutPostRedisplay(); break; case SKELETON: CGAL_forall_iterators(it,polyhedra_) it->skeleton_on(); //CGAL_forall_iterators(it,polyhedra_) it->draw(); glutPostRedisplay(); break; case PERSP: perspective = ! perspective; break; case FULLSCREEN: glutFullScreen(); break; case QUIT: exit(0); }}// Mausknopf gedruecktstatic void mouse (int button, int state, int x, int y){ mouse_x = x; mouse_y = y; interaction = 0; if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if ( GLUT_ACTIVE_SHIFT & glutGetModifiers()) interaction = SCALE; else interaction = motion_mode; } if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) if ( GLUT_ACTIVE_SHIFT & glutGetModifiers()) interaction = TRANS_Z; else interaction = TRANSLATE;}static Affine_3 virtual_sphere_transformation( double old_x, double old_y, double new_x, double new_y) { if ( old_x == new_x && old_y == new_y)// zero rotation. return Affine_3( IDENTITY); // Determine the projected vectors on the `sphere'. double dd = old_x * old_x + old_y * old_y; Double_vector v_old( old_x, old_y, ((dd < 0.5) ? std::sqrt(1-dd) : 0.5 / std::sqrt(dd))); dd = new_x * new_x + new_y * new_y; Double_vector v_new( new_x, new_y, ((dd < 0.5) ? std::sqrt(1-dd) : 0.5 / std::sqrt(dd))); Double_vector axis = cross_product( v_old, v_new); double angle = 0.0; double norm = std::sqrt( (v_old*v_old)*(v_new*v_new)); if ( norm != 0) { double x = v_old*v_new/ norm; if ( x <= -1) angle = M_PI; if ( x < 1) angle = std::acos(x); } double len = std::sqrt( double(axis * axis)); double s = std::sin( angle / 2.0) / len; double q1 = axis.x() * s; // quaternion double q2 = axis.y() * s; double q3 = axis.z() * s; double q0 = std::cos( angle / 2.0); double a = q1 * q2; double b = q0 * q3; double c = q1 * q3; double d = q0 * q2; double e = q2 * q3; double f = q0 * q1; double qq0 = q0 * q0; double qq1 = q1 * q1; double qq2 = q2 * q2; double qq3 = q3 * q3; return Affine_3( qq0 + qq1 - qq2 - qq3, 2 * (a-b), 2 * (c+d), 2 * (a+b), qq0 - qq1 + qq2 - qq3, 2 * (e-f), 2 * (c-d), 2 * (e+f), qq0 - qq1 - qq2 + qq3);}// Objekt rotieren, zoomen oder verschiebenstatic void motion (int x, int y){ switch ( interaction) { case SCALE: s *= exp( (x - mouse_x + mouse_y -y) * factor_s ); break; case ROTATE: { double old_x = 1.2 * (mouse_x - window_width/2) / window_radius; double old_y = - 1.2 * (mouse_y - window_height/2) / window_radius; double new_x = 1.2 * (x - window_width/2) / window_radius; double new_y = - 1.2 * (y - window_height/2) / window_radius; rotation = virtual_sphere_transformation( old_x, old_y, new_x, new_y) * rotation; } break; case TRANSLATE: dx += (x - mouse_x) * 2.0 / window_radius;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?