Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
ReadSTL.hpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #ifndef READ_STL_HPP
00017 #define READ_STL_HPP
00018 
00019 #include <cstdio>
00020 #include "moab/Forward.hpp"
00021 #include "moab/ReaderIface.hpp"
00022 
00023 namespace moab
00024 {
00025 
00026 class ReadUtilIface;
00027 
00028 /**
00029  * \brief ASCII and Binary Stereo Lithography File readers.
00030  * \author Jason Kraftcheck
00031  *
00032  * STL files contain no connectivity infomration.  Each triangle
00033  * is specified as by three sets of single-precision coordinate
00034  * triples.  This reader does not use ANY tolerance when comparing
00035  * vertex locations to recover connectivity.  The points must be
00036  * EXACTLY equal (including the sign on zero values.)  If the file
00037  * was written by an application which represented connectivity
00038  * explicitly, there is no reason for the vertex coordinates to
00039  * be anything other than exactly equal.
00040  *
00041  * For binary STL files, the defacto standard is that they be written
00042  * with a little-endian byte order.  The reader will attempt to
00043  * determine the byte order automatically, and if it is ambiguous,
00044  * will default to little-endian.  The byte ordering may be forced by
00045  * by creating an integer tag named "__STL_BYTE_ORDER" and setting a
00046  * global/mesh value for the tag as 1 for big-endian or 0 for
00047  * little-endian.
00048  *
00049  * For binary files, this reader relies on the file size to determine
00050  * the validity of the file and may use it in guessing the byte order.
00051  * This should not be an issue, as the file size can be determined
00052  * exactly from the number of triangles for a valid file.  However, if
00053  * for some reason the file is readable even though it is invalid (e.g.
00054  * it is some hybrid file with STL data in the beginning and some app-
00055  * specific data appended to the end of the file) the check on the file
00056  * size can be disabled by giving the reader a something other than a
00057  * regular file to read from.  For example, on Unix-like systems, have
00058  * the reader read from a FIFO instead of a file:
00059  *   mkfifo /tmp/fifo.stlb
00060  *   cat my_binary_file.stlb > /tmp/fifo.stlb
00061  * and instruct the MOAB-based application to read from /tmp/fifo.stlb
00062  */
00063 class ReadSTL : public ReaderIface
00064 {
00065 
00066   public:
00067     //! factory method for STL reader
00068     static ReaderIface* factory( Interface* );
00069 
00070     //! Generic file loading code for both binary and ASCII readers.
00071     //! Calls reader-specific read_triangles function to do actual I/O.
00072     ErrorCode load_file( const char* file_name,
00073                          const EntityHandle* file_set,
00074                          const FileOptions& opts,
00075                          const SubsetList* subset_list = 0,
00076                          const Tag* file_id_tag        = 0 );
00077 
00078     ErrorCode read_tag_values( const char* file_name,
00079                                const char* tag_name,
00080                                const FileOptions& opts,
00081                                std::vector< int >& tag_values_out,
00082                                const SubsetList* subset_list = 0 );
00083 
00084     //! Constructor
00085     ReadSTL( Interface* impl = NULL );
00086 
00087     //! Destructor
00088     virtual ~ReadSTL();
00089 
00090     // An object to hold vertex coordinates, and an operator
00091     // for storing them in a STL tree-based container.
00092     struct Point
00093     {
00094         float coords[3];
00095         bool operator<( const Point& ) const;
00096     };
00097     // Data returned by read_triangles.
00098     struct Triangle
00099     {
00100         Point points[3];
00101     };
00102 
00103     enum ByteOrder
00104     {
00105         STL_BIG_ENDIAN,
00106         STL_LITTLE_ENDIAN,
00107         STL_UNKNOWN_BYTE_ORDER
00108     };
00109 
00110   protected:
00111     // I/O specific part of reader - read ASCII file
00112     ErrorCode ascii_read_triangles( const char* file_name, std::vector< Triangle >& tris_out );
00113 
00114     // I/O specific part of reader - read binary file
00115     ErrorCode binary_read_triangles( const char* file_name, ByteOrder byte_order, std::vector< Triangle >& tris_out );
00116 
00117     ReadUtilIface* readMeshIface;
00118 
00119     //! interface instance
00120     Interface* mdbImpl;
00121 };
00122 
00123 }  // namespace moab
00124 
00125 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines