BWAPI
|
00001 // Copyright (c) 1997,2005 Utrecht University (The Netherlands), 00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany), 00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg 00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria), 00005 // and Tel-Aviv University (Israel). All rights reserved. 00006 // 00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License as 00009 // published by the Free Software Foundation; version 2.1 of the License. 00010 // See the file LICENSE.LGPL distributed with CGAL. 00011 // 00012 // Licensees holding a valid commercial license may use this file in 00013 // accordance with the commercial license agreement provided with the software. 00014 // 00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 // 00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Stream_support/include/CGAL/IO/Scanner_OFF.h $ 00019 // $Id: Scanner_OFF.h 28567 2006-02-16 14:30:13Z lsaboret $ 00020 // 00021 // 00022 // Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de> 00023 // Ralf Osbild <osbild@mpi-sb.mpg.de> 00024 00025 #ifndef CGAL_IO_SCANNER_OFF_H 00026 #define CGAL_IO_SCANNER_OFF_H 1 00027 00028 #include <CGAL/basic.h> 00029 #include <iterator> 00030 #include <vector> 00031 #include <utility> 00032 #include <CGAL/IO/File_scanner_OFF.h> 00033 00034 CGAL_BEGIN_NAMESPACE 00035 00036 // The Facet_iterator's value type is vector<Integer32> 00037 // that contains the vertex indices. 00038 00039 template <class Pt> 00040 class I_Scanner_OFF_vertex_iterator 00041 { 00042 public: 00043 typedef std::input_iterator_tag iterator_category; 00044 typedef Pt value_type; 00045 typedef std::ptrdiff_t difference_type; 00046 typedef const Pt* pointer; 00047 typedef const Pt& reference; 00048 private: 00049 File_scanner_OFF* m_scan; 00050 std::ptrdiff_t m_cnt; 00051 Pt m_point; 00052 00053 void next_vertex() { 00054 CGAL_assertion( m_scan != NULL); 00055 if ( m_cnt < m_scan->size_of_vertices()) { 00056 file_scan_vertex( *m_scan, m_point); 00057 m_scan->skip_to_next_vertex( m_cnt); 00058 ++m_cnt; 00059 } else 00060 m_cnt = m_scan->size_of_vertices() + 1; 00061 } 00062 public: 00063 typedef Pt Point; 00064 typedef File_scanner_OFF Scanner; 00065 typedef I_Scanner_OFF_vertex_iterator<Pt> Self; 00066 00067 I_Scanner_OFF_vertex_iterator( int cnt) : m_scan(0), m_cnt(cnt+1) {} 00068 I_Scanner_OFF_vertex_iterator( Scanner& s, int cnt) 00069 : m_scan(&s), m_cnt(cnt) 00070 { 00071 next_vertex(); 00072 } 00073 std::ptrdiff_t count() const { return m_cnt; } 00074 bool operator==( const Self& i) const { return m_cnt == i.m_cnt; } 00075 bool operator!=( const Self& i) const { return m_cnt != i.m_cnt; } 00076 Self& operator++() { 00077 next_vertex(); 00078 return *this; 00079 } 00080 Self operator++(int) { 00081 Self tmp = *this; 00082 ++(*this); 00083 return tmp; 00084 } 00085 const Point& operator*() const { 00086 CGAL_assertion( m_scan != NULL); 00087 return m_point; 00088 } 00089 const Point* operator->() const { return & operator*(); } 00090 }; 00091 00092 template <class Pt, class Nrm> 00093 class I_Scanner_OFF_vertex_and_normals_iterator 00094 { 00095 public: 00096 typedef Pt Point; 00097 typedef Nrm Normal; 00098 typedef File_scanner_OFF Scanner; 00099 typedef I_Scanner_OFF_vertex_and_normals_iterator<Pt,Nrm> Self; 00100 00101 typedef std::input_iterator_tag iterator_category; 00102 typedef std::pair<Point,Normal> value_type; 00103 typedef std::ptrdiff_t difference_type; 00104 typedef const value_type* pointer; 00105 typedef const value_type& reference; 00106 private: 00107 File_scanner_OFF* m_scan; 00108 std::ptrdiff_t m_cnt; 00109 value_type m_current; 00110 00111 00112 void next() { 00113 CGAL_assertion( m_scan != NULL); 00114 if ( m_cnt < m_scan->size_of_vertices()) { 00115 file_scan_vertex( *m_scan, m_current.first); 00116 if ( m_scan->has_normals()) 00117 file_scan_normal( *m_scan, m_current.second); 00118 m_scan->skip_to_next_vertex( m_cnt); 00119 ++m_cnt; 00120 } else 00121 m_cnt = m_scan->size_of_vertices() + 1; 00122 } 00123 public: 00124 00125 I_Scanner_OFF_vertex_and_normals_iterator( int cnt) 00126 : m_scan(0), m_cnt(cnt+1) {} 00127 I_Scanner_OFF_vertex_and_normals_iterator( Scanner& s, int cnt) 00128 : m_scan(&s), m_cnt(cnt) 00129 { 00130 next(); 00131 } 00132 std::ptrdiff_t count() const { return m_cnt; } 00133 bool operator==( const Self& i) const { return m_cnt == i.m_cnt; } 00134 bool operator!=( const Self& i) const { return m_cnt != i.m_cnt; } 00135 Self& operator++() { 00136 next(); 00137 return *this; 00138 } 00139 Self operator++(int) { 00140 Self tmp = *this; 00141 ++(*this); 00142 return tmp; 00143 } 00144 reference operator*() const { 00145 CGAL_assertion( m_scan != NULL); 00146 return m_current; 00147 } 00148 pointer operator->() const { return & operator*(); } 00149 }; 00150 00151 class I_Scanner_OFF_facet_iterator 00152 { 00153 public: 00154 typedef std::input_iterator_tag iterator_category; 00155 typedef std::vector< Integer32> value_type; 00156 typedef std::ptrdiff_t difference_type; 00157 typedef value_type* pointer; 00158 typedef value_type& reference; 00159 private: 00160 File_scanner_OFF* m_scan; 00161 std::ptrdiff_t m_cnt; 00162 value_type m_indices; 00163 00164 void next_facet() { 00165 CGAL_assertion( m_scan != NULL); 00166 if ( m_cnt < m_scan->size_of_facets()) { 00167 m_indices.erase( m_indices.begin(), m_indices.end()); 00168 Integer32 no; 00169 m_scan->scan_facet( no, m_cnt); 00170 m_indices.reserve( no); 00171 Integer32 index = -1; 00172 for ( Integer32 i = 0; i < no; ++i) { 00173 m_scan->scan_facet_vertex_index( index, m_cnt); 00174 m_indices.push_back( index); 00175 } 00176 m_scan->skip_to_next_facet( m_cnt); 00177 ++ m_cnt; 00178 } else 00179 m_cnt = m_scan->size_of_facets() + 1; 00180 } 00181 public: 00182 value_type::size_type size_of_indices () const // RO 00183 { return m_indices.size(); } 00184 typedef value_type::size_type indices_size_type; // RO 00185 public: 00186 typedef File_scanner_OFF Scanner; 00187 typedef I_Scanner_OFF_facet_iterator Self; 00188 typedef value_type::iterator iterator; 00189 00190 I_Scanner_OFF_facet_iterator( int cnt) : m_scan(0), m_cnt(cnt+1) {} 00191 I_Scanner_OFF_facet_iterator( Scanner& s, int cnt) 00192 : m_scan(&s), m_cnt(cnt) 00193 { 00194 next_facet(); 00195 } 00196 std::ptrdiff_t count() const { return m_cnt; } 00197 bool operator==( const Self& i) const { return m_cnt == i.m_cnt; } 00198 bool operator!=( const Self& i) const { return m_cnt != i.m_cnt; } 00199 Self& operator++() { 00200 next_facet(); 00201 return *this; 00202 } 00203 Self operator++(int) { 00204 Self tmp = *this; 00205 ++(*this); 00206 return tmp; 00207 } 00208 value_type& operator*() { 00209 CGAL_assertion( m_scan != NULL); 00210 return m_indices; 00211 } 00212 const value_type& operator*() const { 00213 CGAL_assertion( m_scan != NULL); 00214 return m_indices; 00215 } 00216 value_type* operator->() { return & operator*(); } 00217 const value_type* operator->() const { return & operator*(); } 00218 }; 00219 00220 00221 // The distance function is implemented to work in 00222 // constant time for both iterators. 00223 00224 template <class Pt, class Distance> inline 00225 void distance( const I_Scanner_OFF_vertex_iterator<Pt>& first, 00226 const I_Scanner_OFF_vertex_iterator<Pt>& last, 00227 Distance& n) { 00228 n = Distance( last.count() - first.count()); 00229 } 00230 template <class Distance> inline 00231 void distance( const I_Scanner_OFF_facet_iterator& first, 00232 const I_Scanner_OFF_facet_iterator& last, 00233 Distance& n) { 00234 n = Distance( last.count() - first.count()); 00235 } 00236 template <class Pt> inline 00237 std::ptrdiff_t distance( const I_Scanner_OFF_vertex_iterator<Pt>& first, 00238 const I_Scanner_OFF_vertex_iterator<Pt>& last) { 00239 return last.count() - first.count(); 00240 } 00241 00242 inline 00243 std::ptrdiff_t distance( const I_Scanner_OFF_facet_iterator& first, 00244 const I_Scanner_OFF_facet_iterator& last) { 00245 return last.count() - first.count(); 00246 } 00247 00248 00249 template <class Kernel> 00250 class Scanner_OFF { 00251 File_scanner_OFF m_scan; 00252 public: 00253 typedef typename Kernel::Point_3 Point; 00254 typedef Point Pt; 00255 typedef typename Kernel::Vector_3 Normal; 00256 typedef Scanner_OFF<Kernel> Self; 00257 typedef I_Scanner_OFF_vertex_iterator<Pt> Vertex_iterator; 00258 typedef I_Scanner_OFF_vertex_and_normals_iterator<Pt,Normal> 00259 Vertex_and_normals_iterator; 00260 typedef I_Scanner_OFF_facet_iterator Facet_iterator; 00261 typedef I_Scanner_OFF_facet_iterator::iterator Index_iterator; 00262 00263 Scanner_OFF( std::istream& in, bool verbose = false) 00264 : m_scan( in, verbose) {} 00265 Scanner_OFF( std::istream& in, const File_header_OFF& header) 00266 : m_scan( in, header) {} 00267 00268 int size_of_vertices() const { return m_scan.size_of_vertices(); } 00269 int size_of_halfedges() const { return m_scan.size_of_halfedges();} 00270 int size_of_facets() const { return m_scan.size_of_facets(); } 00271 00272 bool verbose() const { return m_scan.verbose(); } 00273 bool skel() const { return m_scan.skel(); } 00274 bool off() const { return m_scan.off(); } 00275 bool binary() const { return m_scan.binary(); } 00276 bool ascii() const { return m_scan.ascii(); } 00277 00278 bool has_colors() const { return m_scan.has_colors(); } 00279 bool has_normals() const { return m_scan.has_normals(); } 00280 00281 File_header_OFF& header() { return m_scan; } 00282 const File_header_OFF& 00283 header() const { return m_scan; } 00284 00285 Vertex_iterator vertices_begin(){ return Vertex_iterator( m_scan,0);} 00286 Vertex_iterator vertices_end() { 00287 return Vertex_iterator( size_of_vertices()); 00288 } 00289 Facet_iterator facets_begin() { return Facet_iterator( m_scan,0); } 00290 Facet_iterator facets_end() { 00291 return Facet_iterator( size_of_facets()); 00292 } 00293 Vertex_and_normals_iterator vertices_and_normals_begin(){ 00294 return Vertex_and_normals_iterator( m_scan,0); 00295 } 00296 Vertex_and_normals_iterator vertices_and_normals_end() { 00297 return Vertex_and_normals_iterator( size_of_vertices()); 00298 } 00299 }; 00300 00301 CGAL_END_NAMESPACE 00302 #endif // CGAL_IO_SCANNER_OFF_H // 00303 // EOF //