BWAPI
|
00001 // Copyright (c) 1997 ETH Zurich (Switzerland). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under 00005 // the terms of the Q Public License version 1.0. 00006 // See the file LICENSE.QPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Polyhedron_IO/include/CGAL/IO/Polyhedron_scan_OFF.h $ 00015 // $Id: Polyhedron_scan_OFF.h 28567 2006-02-16 14:30:13Z lsaboret $ 00016 // 00017 // 00018 // Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de> 00019 00020 #ifndef CGAL_IO_POLYHEDRON_SCAN_OFF_H 00021 #define CGAL_IO_POLYHEDRON_SCAN_OFF_H 1 00022 00023 #include <CGAL/basic.h> 00024 #include <CGAL/IO/File_header_OFF.h> 00025 #include <CGAL/IO/File_scanner_OFF.h> 00026 #include <CGAL/Modifier_base.h> 00027 #include <CGAL/Polyhedron_incremental_builder_3.h> 00028 #include <CGAL/Polyhedron_3.h> 00029 #include <iostream> 00030 #include <cstddef> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 00034 template < class HDS> 00035 class Polyhedron_scan_OFF : public Modifier_base<HDS> { 00036 protected: 00037 std::istream& m_in; 00038 File_header_OFF m_file_header; 00039 public: 00040 00041 typedef HDS Halfedge_data_structure; 00042 00043 // DEFINITION 00044 // 00045 // Polyhedron_scan_OFF<Traits> is a polyhedral surface builder. 00046 // It scans a polyhedron given in OFF from a stream and appends it 00047 // incrementally using the incremental builder. 00048 00049 Polyhedron_scan_OFF( std::istream& in, bool verbose = false) 00050 : m_in(in), m_file_header( verbose) {} 00051 00052 // Activation 00053 void operator()( HDS& hds); 00054 00055 const File_header_OFF& header() const { return m_file_header; } 00056 }; 00057 00058 template < class HDS > 00059 void 00060 Polyhedron_scan_OFF<HDS>:: operator()( HDS& target) { 00061 File_scanner_OFF scanner( m_in, m_file_header.verbose()); 00062 if ( ! m_in) { 00063 if ( scanner.verbose()) { 00064 std::cerr << " " << std::endl; 00065 std::cerr << "Polyhedron_scan_OFF<HDS>::" << std::endl; 00066 std::cerr << "operator(): input error: file format is not in " 00067 "OFF." << std::endl; 00068 } 00069 return; 00070 } 00071 m_file_header = scanner; // Remember file header after return. 00072 00073 Polyhedron_incremental_builder_3<HDS> B( target, scanner.verbose()); 00074 B.begin_surface( scanner.size_of_vertices(), 00075 scanner.size_of_facets(), 00076 scanner.size_of_halfedges()); 00077 00078 typedef typename HDS::Traits Traits; 00079 typedef typename Traits::Point_3 Point; 00080 00081 // read in all vertices 00082 int i; 00083 for ( i = 0; i < scanner.size_of_vertices(); i++) { 00084 Point p; 00085 file_scan_vertex( scanner, p); 00086 B.add_vertex( p); 00087 scanner.skip_to_next_vertex( i); 00088 } 00089 if ( ! m_in || B.error()) { 00090 B.rollback(); 00091 m_in.clear( std::ios::badbit); 00092 return; 00093 } 00094 00095 // read in all facets 00096 for ( i = 0; i < scanner.size_of_facets(); i++) { 00097 B.begin_facet(); 00098 Integer32 no; 00099 scanner.scan_facet( no, i); 00100 if( ! m_in || B.error() || no < 3) { 00101 if ( scanner.verbose()) { 00102 std::cerr << " " << std::endl; 00103 std::cerr << "Polyhedron_scan_OFF<Traits>::" << std::endl; 00104 std::cerr << "operator()(): input error: facet " << i 00105 << " has less than 3 vertices." << std::endl; 00106 } 00107 B.rollback(); 00108 m_in.clear( std::ios::badbit); 00109 return; 00110 } 00111 for ( int j = 0; j < no; j++) { 00112 Integer32 index; 00113 scanner.scan_facet_vertex_index( index, i); 00114 B.add_vertex_to_facet( index); 00115 } 00116 B.end_facet(); 00117 scanner.skip_to_next_facet( i); 00118 } 00119 if ( ! m_in || B.error()) { 00120 B.rollback(); 00121 m_in.clear( std::ios::badbit); 00122 return; 00123 } 00124 if ( B.check_unconnected_vertices()) { 00125 if ( ! B.remove_unconnected_vertices()) { 00126 if ( scanner.verbose()) { 00127 std::cerr << " " << std::endl; 00128 std::cerr << "Polyhedron_scan_OFF<Traits>::" << std::endl; 00129 std::cerr << "operator()(): input error: cannot " 00130 "succesfully remove isolated vertices." 00131 << std::endl; 00132 } 00133 B.rollback(); 00134 m_in.clear( std::ios::badbit); 00135 return; 00136 } 00137 } 00138 B.end_surface(); 00139 } 00140 00141 CGAL_END_NAMESPACE 00142 #endif // CGAL_IO_POLYHEDRON_SCAN_OFF_H // 00143 // EOF //