⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_stack.h

📁 很多二维 三维几何计算算法 C++ 类库
💻 H
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) 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.3-branch/Nef_3/demo/Nef_3/include/CGAL/Nef_3/demo_stack.h $// $Id: demo_stack.h 38299 2007-04-18 14:31:06Z hachenb $// //// Author(s)     : Lutz Kettner//                 Peter Hachenberger//// Demo program maintaining a stack of Nef polyhedra in the space and// a manipulation language for stack ops, file loading and saving, etc.// ============================================================================#ifndef CGAL_NEF_DEMO_STACK_H#define CGAL_NEF_DEMO_STACK_H#include <CGAL/rational_rotation.h>#include <CGAL/Polyhedron_3.h>#include <CGAL/Nef_polyhedron_3.h>#include <CGAL/IO/Polyhedron_iostream.h>#include <CGAL/IO/Nef_polyhedron_iostream_3.h>#include <CGAL/Nef_3/SNC_items.h>#ifdef CGAL_NEF3_OLD_VISUALIZATION#elif defined CGAL_USE_QT#include <CGAL/IO/Qt_widget_Nef_3.h>#include <qapplication.h>#endif#include <iostream>#include <fstream>#include <vector>#include <cstring>#include <cmath>#include <cstddef>using std::cout;using std::cerr;using std::endl;using std::strcmp;using std::exit;CGAL_BEGIN_NAMESPACEtemplate<typename Kernel, typename Items = CGAL::SNC_items>class demo_stack {  typedef typename Kernel::RT                    NT;  typedef CGAL::Polyhedron_3<Kernel>             Polyhedron;  typedef CGAL::Nef_polyhedron_3<Kernel, Items>  Nef_polyhedron;  typedef std::vector< Nef_polyhedron>           Nef_vector;  typedef typename Nef_vector::iterator          Iterator;  Nef_vector nef;  // contains stack of Nef_polyhedronpublic:  void help_message( std::ostream& out) {      out << "Usage: nef_3 [<Options>] <Command> [<Command> ...]\n"          "Options:\n"          "    -h/-help    this message\n"          "Command: all commands work on the top of a stack of Nef polyhedra:\n"          "    h/help/?               this message\n"          "    pop                    removes top from stack.\n"          "    dup                    duplicates top of stack.\n"          "    dupn <n>               duplicates <n>-th element (top = 1st element).\n"          "    swap                   swaps top two elements on stack.\n"          "    swapn <n>              swaps <n>-th element with top (top = 1st element).\n"          "    clear                  clears stack\n"          "    size                   prints stack and top polyhedron size to stdout.\n"          "    bytes                  prints the number of bytes used by top.\n"          "    simple                 tests if top is convertible to Polyhedron_2.\n"          "    valid                  tests if the data structure of top is valid.\n"          "    plane <a> <b> <c> <d>  creates a halfspace bounded by the plane ax+by+cz+d=0.\n"          "    loadnef3 <filename>    loads nef3 file and pushes it on stack.\n"          "    loadoff <filename>     loads file in OFF format and pushes it on stack.\n"          "    saveoff <filename>     saves top in OFF format if top is simple.\n"          "    dump                   dump Ascii description of top to stderr.\n"          // "    sorted                 dump standard Ascii description of top to stderr. \n"          "    vis                    visualize it in OpenGL if available\n"          "The following commands take their arguments from the stack, where the\n"          "top of the stack is the first argument. They remove those arguments from\n"          "the stack and push the result onto the stack.\n"          "    trans <x> <y> <z> <w>  translate top with homogeneous vector (x,y,z,w).\n"          "    scale <s> <w>          scale top with rational scale factor (s/w).\n"          "    rotx <double>          rotate (approx) <double> degrees around x-axis.\n"          "    roty <double>          rotate (approx) <double> degrees around y-axis.\n"          "    rotz <double>          rotate (approx) <double> degrees around z-axis.\n"          "    inters                 intersection of two polyhedra.\n"          "    union                  union of two polyhedra.\n"          "    diff                   top polyhedron minus the second polyhedron.\n"          "    symdiff                symmetric difference of two polyhedra.\n"          "    compl                  complement of top polyhedron.\n"          "    int                    interior of top polyhedron.\n"          "    clos                   closure of top polyhedron.\n"          "    bnd                    boundary of top polyhedron.\n"          "    reg                    regularization of top polyhedron.\n" << endl;  }  // assert that there are at least n arguments left for the command  bool assert_argc( const char* command, int n, int arg_left) {    if ( n > arg_left) {        cerr << "Error: command '" << command << "' needs " << n             << " arguments." << endl;        return false;    }    return true;  }public:  // evaluate the commands (and arguments) in argv[0..argc-1].  // returns 0 if all is o.k., and != 0 otherwise.  int eval( int argc, char* argv[]) {    CGAL::Timer t;    int error = 0;    for ( int i = 0; error == 0 && i < argc; ++i) {        if ( strcmp( argv[i], "h") == 0 || strcmp( argv[i], "help") == 0            || strcmp( argv[i], "?") == 0) {            help_message( cerr);        } else if ( strcmp( argv[i], "pop") == 0) {            if ( nef.size() == 0) {                cerr << "Error: '" << argv[i] << "' on empty stack." << endl;                error = 2;                continue;            }            nef.pop_back();        } else if ( strcmp( argv[i], "dup") == 0) {            if ( nef.size() == 0) {                cerr << "Error: '" << argv[i] << "' on empty stack." << endl;                error = 2;                continue;            }            nef.push_back( nef.back());        } else if ( strcmp( argv[i], "dupn") == 0) {            if ( assert_argc( argv[i], 1, argc - i - 1)) {                int k = std::atoi( argv[i+1]);                if ( k > 0 && (size_t)k <= nef.size())                    nef.push_back( nef[ nef.size() - k]);                else {                    cerr << "Error: 'dupn' argument out of range." << endl;                    error = 2;                }                ++i;            } else {                error = 4;            }        } else if ( strcmp( argv[i], "swap") == 0) {            if ( nef.size() < 2) {                cerr << "Error: '" << argv[i] << "': less than 2 elements on "                        "stack." << endl;                error = 2;                continue;            }            Iterator ni = nef.end();            std::swap( ni[-1], ni[-2]);        } else if ( strcmp( argv[i], "swapn") == 0) {            if ( assert_argc( argv[i], 1, argc - i - 1)) {                int k = std::atoi( argv[i+1]);                if ( k > 0 && (size_t)k <= nef.size()) {                    Iterator ni = nef.end();                    std::swap( ni[-1], ni[-k]);                } else {                    cerr << "Error: 'swapn' argument out of range." << endl;                    error = 2;                }                ++i;            } else {                error = 4;            }        } else if ( strcmp( argv[i], "clear") == 0) {            nef.clear();	} else if ( strcmp( argv[i], "size") == 0) {	  cout << "Size of stack = " << nef.size() << endl;	  Nef_polyhedron exp = nef.back();	  cout << "Top: Number of vertices = " << exp.number_of_vertices()	       << endl;	  cout << "Top: Number of edges    = " << exp.number_of_edges()	       << endl;	  cout << "Top: Number of facets   = " << exp.number_of_facets()	       << endl;	  cout << "Top: Number of volumes  = " << exp.number_of_volumes()	       << endl;	} else if ( strcmp( argv[i], "bytes") == 0) {	  if ( nef.size() == 0) {	    cerr << "Error: '" << argv[i] << "' on empty stack." << endl;	    error = 2;	    continue;	  }	  cout << "Top uses " << nef.back().bytes() << " bytes" << std::endl; 	} else if ( strcmp( argv[i], "bytes_reduced") == 0) {	  if ( nef.size() == 0) {	    cerr << "Error: '" << argv[i] << "' on empty stack." << endl;	    error = 2;	    continue;	  }	  cout << "Reduced Version of top uses " << nef.back().bytes_reduced() << " bytes" << std::endl;         } else if ( strcmp( argv[i], "simple") == 0) {            if ( nef.size() == 0) {                cerr << "Error: '" << argv[i] << "' on empty stack." << endl;                error = 2;                continue;            }            if ( nef.back().is_simple())                cout << "Top of stack is simple." << endl;            else                cout << "Top of stack is _not_ simple." << endl;        } else if ( strcmp( argv[i], "valid") == 0) {            if ( nef.size() == 0) {                cerr << "Error: '" << argv[i] << "' on empty stack." << endl;                error = 2;                continue;            }	    if ( assert_argc( argv[i], 2, argc - i - 1)) {	      bool verb( std::atoi( argv[i+1]));	      int level( std::atoi( argv[i+2]));	      if ( nef.back().is_valid(verb, level))                cout << "Top of stack is valid." << endl;	      else                cout << "Top of stack is _NOT_ valid." << endl;	      i += 2;	    } else {	      error = 4;	    }        } else if ( strcmp( argv[i], "loadnef3") == 0) {            if ( assert_argc( argv[i], 1, argc - i - 1)) {	      std::ifstream in(argv[i+1]);	      if ( ! in) {		cerr << "Error: loadnef3 cannot open file '" << argv[i+1]		     << "'." << endl;		error = 5;	      } else {	     		Nef_polyhedron nf;		in >> nf;		if ( ! in) {		  cerr << "Error: loadnef3 cannot read nef3 file '" 		       << argv[i+1] << "' correctly." << endl;		  error = 5;		} else {		  nef.push_back( nf);		  ++i;		}	      }            } else {	      error = 4;            }        } else if ( strcmp( argv[i], "loadoff") == 0) {            if ( assert_argc( argv[i], 1, argc - i - 1)) {                std::ifstream in( argv[i+1]);                if ( ! in) {                    cerr << "Error: loadoff cannot open file '" << argv[i+1]                         << "'." << endl;                    error = 5;                } else {                    Polyhedron poly;                    in >> poly;                    if ( ! in) {                        cerr << "Error: loadoff cannot read OFF file '"                              << argv[i+1] << "' correctly." << endl;                        error = 5;                    } else {		        Nef_polyhedron nf(poly);		        nef.push_back( nf);                    }                }                ++i;            } else {                error = 4;            }        } else if ( strcmp( argv[i], "saveoff") == 0) {            if ( nef.size() == 0) {                cerr << "Error: '" << argv[i] << "' on empty stack." << endl;                error = 2;                continue;            }            if ( assert_argc( argv[i], 1, argc - i - 1)) {                if ( ! nef.back().is_simple()) {                    cerr << "Error: saveoff file '" << argv[i+1]                         << "': top is not simple." << endl;                    error = 6;                    continue;                }                std::ofstream out( argv[i+1]);                if ( ! out) {                    cerr << "Error: saveoff cannot create file '"                         << argv[i+1] << "'." << endl;                    error = 5;                } else {                    Polyhedron poly;                    nef.back().convert_to_Polyhedron(poly);                    out << poly;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -