MOAB: Mesh Oriented datABase  (version 5.4.1)
ReadOBJ.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 //-----------------------------------------------------------------------------
00017 // Filename : ReadOBJ.hpp
00018 //
00019 // Purpose  : Wavefront obj file reader
00020 //
00021 // Creators : Chelsea D'Angelo, Paul Wilson, Andrew Davis
00022 //
00023 // Date     : 02/16
00024 //
00025 // Owner    : Chelsea D'Angelo
00026 //-----------------------------------------------------------------------------
00027 
00028 /**
00029  * This class will read in an obj file and populate a MOAB instance with the
00030  * vertex and connectivity information in the file.  A specification for obj files
00031  * can be found here: https://en.wikipedia.org/wiki/Wavefront_.obj_file
00032  * This reader only supports a subset of the full file structure, namely,
00033  * object names, group names, vertices, and faces.
00034  *
00035  * Overview of the supported structure:
00036  *
00037  * Heading
00038  * Object line: o object_name
00039  * Group line: g group_name1
00040  * Vertex lines: v x y z
00041  * Face lines (tri): f v1 v2 v3
00042  * Face lines (quad): f v1 v2 v3 v4
00043  *
00044  * Lines that begin w/ anything other than 'o ', 'g ', 'v ', and 'f ' are not
00045  * supported. If a valid, but unsupported line is found, it will be ignored.
00046  * If an invalid line is found, an error will be produced.
00047  * Face lines that contain 'vertex\texture\normal' are handled by ignoring the
00048  * texture and normal
00049  *
00050  *
00051  * A new meshset will be created for each object or group in the file.
00052  * Each object and group must have a name, or the line will be ignored.
00053  * Groups are thought to be collections of elements with no dimension or category and
00054  * will therefore only be assigned name and id tags.
00055  * Objects are thought to be closed surfaces.  A surface meshset will be
00056  * created for each object.  A volume meshset that directly corresponds
00057  * to the surface meshset will also be created.  These will have name, id,
00058  * categorty, and dimension tags.  A parent-child relationship exists
00059  * between the volume and surface meshsets.
00060  * Vertices will be created and added to a global vertex meshset.
00061  * Triangular faces will be created and added as members of the surface meshets.
00062  */
00063 
00064 #ifndef READ_OBJ_HPP
00065 #define READ_OBJ_HPP
00066 
00067 #ifndef IS_BUILDING_MB
00068 #error "ReadOBJ.hpp isn't supposed to be included into an application"
00069 #endif
00070 
00071 #include <iostream>
00072 #include <fstream>
00073 #include <sstream>
00074 #include <vector>
00075 #include <map>
00076 
00077 #include "moab/Interface.hpp"
00078 #include "moab/ReaderIface.hpp"
00079 #include "FileTokenizer.hpp"
00080 #include "moab/RangeMap.hpp"
00081 #include "MBTagConventions.hpp"
00082 
00083 /* struct vertex is a structure that stores coordinates
00084    of vertices. This is a convenience structure making
00085    i/o easier.
00086 */
00087 struct vertex
00088 {
00089     int vertex_id;
00090     double coord[3];
00091 };
00092 
00093 /* struct face is a structure that stores connectivity.
00094    This is a convenience structure makin i/o easier.
00095 */
00096 struct face
00097 {
00098     int face_id;
00099     moab::EntityHandle conn[3];
00100 };
00101 namespace moab
00102 {
00103 
00104 /* Supported obj file keywords
00105  */
00106 enum keyword_type
00107 {
00108     obj_undefined = 0,
00109     object_start,
00110     group_start,
00111     face_start,
00112     vertex_start,
00113     valid_unsupported
00114 };
00115 
00116 class ReadUtilIface;
00117 class GeomTopoTool;
00118 
00119 class ReadOBJ : public ReaderIface
00120 {
00121 
00122   public:
00123     //! factory method
00124     static ReaderIface* factory( Interface* );
00125 
00126     ErrorCode load_file( const char* file_name,
00127                          const EntityHandle* file_set,
00128                          const FileOptions& opts,
00129                          const SubsetList* subset_list = 0,
00130                          const Tag* file_id_tag        = 0 );
00131 
00132     ErrorCode read_tag_values( const char* file_name,
00133                                const char* tag_name,
00134                                const FileOptions& opts,
00135                                std::vector< int >& tag_values_out,
00136                                const SubsetList* subset_list = 0 );
00137 
00138     //! Constructor
00139     ReadOBJ( Interface* impl = NULL );
00140 
00141     //! Destructor
00142     virtual ~ReadOBJ();
00143 
00144   private:
00145     ReadUtilIface* readMeshIface;
00146 
00147     //! interface instance
00148     Interface* MBI;
00149 
00150     GeomTopoTool* myGeomTool;
00151 
00152     Tag geom_tag, id_tag, name_tag, category_tag, faceting_tol_tag, geometry_resabs_tag, obj_name_tag;
00153 
00154     /*  The keyword type function matches the first character extracted from each line to a type of
00155      * line
00156      */
00157     keyword_type get_keyword( std::vector< std::string > tokens );
00158 
00159     /*  The match function searches a list of map keys for a match with the token
00160      */
00161     template < typename T >
00162     std::string match( const std::string& token, std::map< std::string, T >& tokenList );
00163 
00164     /* The tokenize function takes a string as input and splits it into
00165      * a vector of strings based on the delimiter
00166      */
00167     static const char* delimiters;
00168 
00169     void tokenize( const std::string& str, std::vector< std::string >& tokens, const char* delimiters );
00170 
00171     /*
00172      * The create_object funtion will create a new meshset for
00173      * each object that contains all vertices and faces
00174      */
00175     ErrorCode create_new_object( std::string object_name, int object_id, EntityHandle& curr_obj_meshset );
00176 
00177     ErrorCode create_new_group( std::string object_name, int curr_object, EntityHandle& object_meshset );
00178 
00179     /* create_new_vertex converts tokenized string input to
00180        vertex structure
00181      */
00182     ErrorCode create_new_vertex( std::vector< std::string > v_tokens, EntityHandle& vertex_eh );
00183 
00184     /* create_new_face converts tokenized string input to
00185      * face structure
00186      */
00187     ErrorCode create_new_face( std::vector< std::string > f_tokens,
00188                                const std::vector< EntityHandle >& vertex_list,
00189                                EntityHandle& face_eh );
00190 
00191     /*
00192      * The split_quad function creates 1 new vertex and 4 new tri faces
00193      * from a quad face.
00194      */
00195 
00196     ErrorCode split_quad( std::vector< std::string > f_tokens,
00197                           std::vector< EntityHandle >& vertex_list,
00198                           Range& face_eh );
00199 
00200     ErrorCode create_tri_faces( std::vector< EntityHandle > quad_vert_eh,
00201                                 //                     EntityHandle center_vertex_eh,
00202                                 Range& face_eh );
00203 };
00204 
00205 }  // namespace moab
00206 
00207 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines