BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/IO/File_scanner_OFF.h
Go to the documentation of this file.
00001 // Copyright (c) 1997  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/File_scanner_OFF.h $
00019 // $Id: File_scanner_OFF.h 29581 2006-03-17 15:50:13Z spion $
00020 // 
00021 //
00022 // Author(s)     : Lutz Kettner  <kettner@mpi-sb.mpg.de>
00023 
00024 #ifndef CGAL_IO_FILE_SCANNER_OFF_H
00025 #define CGAL_IO_FILE_SCANNER_OFF_H 1
00026 
00027 #include <CGAL/basic.h>
00028 #include <CGAL/known_bit_size_integers.h>
00029 #include <cstddef>
00030 #include <CGAL/IO/binary_file_io.h>
00031 #include <CGAL/IO/File_header_OFF.h>
00032 #include <iostream>
00033 
00034 #include <CGAL/Point_3.h>
00035 #include <CGAL/Vector_3.h>
00036 
00037 CGAL_BEGIN_NAMESPACE
00038 
00039 class File_scanner_OFF : public File_header_OFF {
00040     std::istream&  m_in;
00041     bool           normals_read;
00042     void skip_comment() { m_in >> skip_comment_OFF; }
00043 public:
00044     File_scanner_OFF( std::istream& in, bool verbose = false)
00045       : File_header_OFF(verbose), m_in(in), normals_read(false) {
00046         in >> static_cast<File_header_OFF&>( *this);
00047     }
00048     File_scanner_OFF( std::istream& in, const File_header_OFF& header)
00049       : File_header_OFF(header), m_in(in), normals_read(false) {}
00050 
00051     std::istream& in() { return m_in; }
00052 
00053     // The scan_vertex() routine is provided for multiple
00054     // coordinate types to support parameterized polytopes.
00055     void scan_vertex( float&  x, float&  y, float&  z) {
00056         if ( binary()) {
00057             I_Binary_read_big_endian_float32( m_in, x);
00058             I_Binary_read_big_endian_float32( m_in, y);
00059             I_Binary_read_big_endian_float32( m_in, z);
00060             if ( is_homogeneous()) {
00061                 float w;
00062                 I_Binary_read_big_endian_float32( m_in, w);
00063                 x /= w;
00064                 y /= w;
00065                 z /= w;
00066             }
00067         } else {
00068             skip_comment();
00069             m_in >> x >> y >> z;
00070             if ( is_homogeneous()) {
00071                 float w;
00072                 m_in >> w;
00073                 x /= w;
00074                 y /= w;
00075                 z /= w;
00076             }
00077         }
00078     }
00079     void scan_vertex( double& x, double& y, double& z) {
00080         if ( binary()) {
00081             float f;
00082             I_Binary_read_big_endian_float32( m_in, f);
00083             x = f;
00084             I_Binary_read_big_endian_float32( m_in, f);
00085             y = f;
00086             I_Binary_read_big_endian_float32( m_in, f);
00087             z = f;
00088             if ( is_homogeneous()) {
00089                 I_Binary_read_big_endian_float32( m_in, f);
00090                 x /= f;
00091                 y /= f;
00092                 z /= f;
00093             }
00094         } else {
00095             skip_comment();
00096             m_in >> x >> y >> z;
00097             if ( is_homogeneous()) {
00098                 double w;
00099                 m_in >> w;
00100                 x /= w;
00101                 y /= w;
00102                 z /= w;
00103             }
00104         }
00105     }
00106     void scan_vertex( int& x, int& y, int& z) {
00107         if ( binary()) {
00108             float fx, fy, fz;
00109             I_Binary_read_big_endian_float32( m_in, fx);
00110             I_Binary_read_big_endian_float32( m_in, fy);
00111             I_Binary_read_big_endian_float32( m_in, fz);
00112             if ( is_homogeneous()) {
00113                 float fw;
00114                 I_Binary_read_big_endian_float32( m_in, fw);
00115                 x = int( fx / fw);
00116                 y = int( fy / fw);
00117                 y = int( fz / fw);
00118             } else {
00119                 x = int(fx);
00120                 y = int(fy);
00121                 z = int(fz);
00122             }
00123         } else {
00124             skip_comment();
00125             if ( is_homogeneous()) {
00126                 double fx, fy, fz, fw;
00127                 m_in >> fx >> fy >> fz >> fw;
00128                 x = int( fx / fw);
00129                 y = int( fy / fw);
00130                 y = int( fz / fw);
00131             } else {
00132                 double d;
00133                 m_in >> d;
00134                 x = int(d);
00135                 m_in >> d;
00136                 y = int(d);
00137                 m_in >> d;
00138                 z = int(d);
00139             }
00140         }
00141     }
00142 
00143     void scan_vertex( float&  x, float&  y, float&  z, float&  w) {
00144         w = 1;
00145         if ( binary()) {
00146             I_Binary_read_big_endian_float32( m_in, x);
00147             I_Binary_read_big_endian_float32( m_in, y);
00148             I_Binary_read_big_endian_float32( m_in, z);
00149             if ( is_homogeneous())
00150                 I_Binary_read_big_endian_float32( m_in, w);
00151         } else {
00152             skip_comment();
00153             m_in >> x >> y >> z;
00154             if ( is_homogeneous())
00155                 m_in >> w;
00156         }
00157     }
00158     void scan_vertex( double& x, double& y, double& z, double& w) {
00159         w = 1;
00160         if ( binary()) {
00161             float f;
00162             I_Binary_read_big_endian_float32( m_in, f);
00163             x = f;
00164             I_Binary_read_big_endian_float32( m_in, f);
00165             y = f;
00166             I_Binary_read_big_endian_float32( m_in, f);
00167             z = f;
00168             if ( is_homogeneous()) {
00169                 I_Binary_read_big_endian_float32( m_in, f);
00170                 w = f;
00171             }
00172         } else {
00173             skip_comment();
00174             m_in >> x >> y >> z;
00175             if ( is_homogeneous())
00176                 m_in >> w;
00177         }
00178     }
00179     void scan_vertex( int& x, int& y, int& z, int& w) {
00180         w = 1;
00181         if ( binary()) {
00182             float f;
00183             I_Binary_read_big_endian_float32( m_in, f);
00184             x = int(f);
00185             I_Binary_read_big_endian_float32( m_in, f);
00186             y = int(f);
00187             I_Binary_read_big_endian_float32( m_in, f);
00188             z = int(f);
00189             if ( is_homogeneous()) {
00190                 I_Binary_read_big_endian_float32( m_in, f);
00191                 w = int(f);
00192             }
00193         } else {
00194             skip_comment();
00195             double d;
00196             m_in >> d;
00197             x = int(d);
00198             m_in >> d;
00199             y = int(d);
00200             m_in >> d;
00201             z = int(d);
00202             if ( is_homogeneous()) {
00203                 m_in >> d;
00204                 w = int(d);
00205             }
00206         }
00207     }
00208 
00209     void scan_normal( float&  x, float&  y, float&  z) {
00210         if ( has_normals()) {
00211             normals_read = true;
00212             if ( binary()) {
00213                 I_Binary_read_big_endian_float32( m_in, x);
00214                 I_Binary_read_big_endian_float32( m_in, y);
00215                 I_Binary_read_big_endian_float32( m_in, z);
00216                 if ( is_homogeneous()) {
00217                     float w;
00218                     I_Binary_read_big_endian_float32( m_in, w);
00219                     x /= w;
00220                     y /= w;
00221                     z /= w;
00222                 }
00223             } else {
00224                 m_in >> x >> y >> z;
00225                 if ( is_homogeneous()) {
00226                     float w;
00227                     m_in >> w;
00228                     x /= w;
00229                     y /= w;
00230                     z /= w;
00231                 }
00232             }
00233         }
00234     }
00235     void scan_normal( double& x, double& y, double& z) {
00236         if ( has_normals()) {
00237             normals_read = true;
00238             if ( binary()) {
00239                 float fx, fy, fz;
00240                 I_Binary_read_big_endian_float32( m_in, fx);
00241                 I_Binary_read_big_endian_float32( m_in, fy);
00242                 I_Binary_read_big_endian_float32( m_in, fz);
00243                 if ( is_homogeneous()) {
00244                     float fw;
00245                     I_Binary_read_big_endian_float32( m_in, fw);
00246                     x = fx / fw;
00247                     y = fy / fw;
00248                     y = fz / fw;
00249                 } else {
00250                     x = fx;
00251                     y = fy;
00252                     z = fz;
00253                 }
00254             } else {
00255                 if ( is_homogeneous()) {
00256                     float fx, fy, fz, fw;
00257                     m_in >> fx >> fy >> fz >> fw;
00258                     x = fx / fw;
00259                     y = fy / fw;
00260                     y = fz / fw;
00261                 } else
00262                     m_in >> x >> y >> z;
00263             }
00264         }
00265     }
00266     void scan_normal( int& x, int& y, int& z) {
00267         if ( has_normals()) {
00268             normals_read = true;
00269             if ( binary()) {
00270                 float fx, fy, fz;
00271                 I_Binary_read_big_endian_float32( m_in, fx);
00272                 I_Binary_read_big_endian_float32( m_in, fy);
00273                 I_Binary_read_big_endian_float32( m_in, fz);
00274                 if ( is_homogeneous()) {
00275                     float fw;
00276                     I_Binary_read_big_endian_float32( m_in, fw);
00277                     x = int( fx / fw);
00278                     y = int( fy / fw);
00279                     y = int( fz / fw);
00280                 } else {
00281                     x = int(fx);
00282                     y = int(fy);
00283                     z = int(fz);
00284                 }
00285             } else {
00286                 if ( is_homogeneous()) {
00287                     float fx, fy, fz, fw;
00288                     m_in >> fx >> fy >> fz >> fw;
00289                     x = int( fx / fw);
00290                     y = int( fy / fw);
00291                     y = int( fz / fw);
00292                 } else {
00293                     double d;
00294                     m_in >> d;
00295                     x = int(d);
00296                     m_in >> d;
00297                     y = int(d);
00298                     m_in >> d;
00299                     z = int(d);
00300                 }
00301             }
00302         }
00303     }
00304 
00305     void scan_normal( float&  x, float&  y, float&  z, float&  w) {
00306         w = 1;
00307         if ( has_normals()) {
00308             normals_read = true;
00309             if ( binary()) {
00310                 I_Binary_read_big_endian_float32( m_in, x);
00311                 I_Binary_read_big_endian_float32( m_in, y);
00312                 I_Binary_read_big_endian_float32( m_in, z);
00313                 if ( is_homogeneous())
00314                     I_Binary_read_big_endian_float32( m_in, w);
00315             } else {
00316                 m_in >> x >> y >> z;
00317                 if ( is_homogeneous())
00318                     m_in >> w;
00319             }
00320         }
00321     }
00322     void scan_normal( double& x, double& y, double& z, double& w) {
00323         w = 1;
00324         if ( has_normals()) {
00325             normals_read = true;
00326             if ( binary()) {
00327                 float f;
00328                 I_Binary_read_big_endian_float32( m_in, f);
00329                 x = f;
00330                 I_Binary_read_big_endian_float32( m_in, f);
00331                 y = f;
00332                 I_Binary_read_big_endian_float32( m_in, f);
00333                 z = f;
00334                 if ( is_homogeneous()) {
00335                     I_Binary_read_big_endian_float32( m_in, f);
00336                     w = f;
00337                 }
00338             } else {
00339                 m_in >> x >> y >> z;
00340                 if ( is_homogeneous())
00341                     m_in >> w;
00342             }
00343         }
00344     }
00345     void scan_normal( int& x, int& y, int& z, int& w) {
00346         w = 1;
00347         if ( has_normals()) {
00348             normals_read = true;
00349             if ( binary()) {
00350                 float f;
00351                 I_Binary_read_big_endian_float32( m_in, f);
00352                 x = int(f);
00353                 I_Binary_read_big_endian_float32( m_in, f);
00354                 y = int(f);
00355                 I_Binary_read_big_endian_float32( m_in, f);
00356                 z = int(f);
00357                 if ( is_homogeneous()) {
00358                     I_Binary_read_big_endian_float32( m_in, f);
00359                     w = int(f);
00360                 }
00361             } else {
00362                 double d;
00363                 m_in >> d;
00364                 x = int(d);
00365                 m_in >> d;
00366                 y = int(d);
00367                 m_in >> d;
00368                 z = int(d);
00369                 if ( is_homogeneous()) {
00370                     m_in >> d;
00371                     w = int(d);
00372                 }
00373             }
00374         }
00375     }
00376 
00377     void skip_to_next_vertex( int current_vertex);
00378 
00379     void scan_facet( Integer32& size, int CGAL_assertion_code(current_facet)) {
00380         CGAL_assertion( current_facet < size_of_facets());
00381         if ( binary())
00382             I_Binary_read_big_endian_integer32( m_in, size);
00383         else {
00384             skip_comment();
00385             m_in >> size;
00386         }
00387     }
00388 
00389     void scan_facet_vertex_index( Integer32& index,
00390                                   int current_facet) {
00391         if ( binary())
00392             I_Binary_read_big_endian_integer32( m_in, index);
00393         else
00394             m_in >> index;
00395         if( ! m_in) {
00396             if ( verbose()) {
00397                 std::cerr << " " << std::endl;
00398                 std::cerr << "File_scanner_OFF::" << std::endl;
00399                 std::cerr << "scan_facet_vertex_index(): input error:  "
00400                              "cannot read OFF file beyond facet "
00401                           << current_facet << "." << std::endl;
00402             }
00403             set_off_header( false);
00404             return;
00405         }
00406         index -= index_offset();
00407         if( index < 0 || index >= size_of_vertices()) {
00408             m_in.clear( std::ios::badbit);
00409             if ( verbose()) {
00410                 std::cerr << " " << std::endl;
00411                 std::cerr << "File_scanner_OFF::" << std::endl;
00412                 std::cerr << "scan_facet_vertex_index(): input error: "
00413                              "facet " << current_facet << ": vertex index "
00414                           << index + index_offset() << ": is out of range."
00415                           << std::endl;
00416             }
00417             set_off_header( false);
00418             return;
00419         }
00420     }
00421 
00422     void skip_to_next_facet( int current_facet);
00423 };
00424 
00425 template < class Point> inline
00426 Point&
00427 file_scan_vertex( File_scanner_OFF& scanner, Point& p) {
00428     typedef typename Point::R R;
00429     typedef typename R::RT    RT;
00430     double x, y, z, w;
00431     scanner.scan_vertex( x, y, z, w);
00432     if ( w == 1)
00433         p = Point( RT(x), RT(y), RT(z));
00434     else
00435         p = Point( RT(x), RT(y), RT(z), RT(w));
00436     return p;
00437 }
00438 
00439 template < class Vector> inline
00440 Vector&
00441 file_scan_normal( File_scanner_OFF& scanner, Vector& v) {
00442     typedef typename Vector::R R;
00443     typedef typename R::RT     RT;
00444     double x, y, z, w;
00445     scanner.scan_normal( x, y, z, w);
00446     if ( w == 1)
00447         v = Vector( RT(x), RT(y), RT(z));
00448     else
00449         v = Vector( RT(x), RT(y), RT(z), RT(w));
00450     return v;
00451 }
00452 
00453 CGAL_END_NAMESPACE
00454 
00455 #endif // CGAL_IO_FILE_SCANNER_OFF_H //
00456 // EOF //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines