MOAB: Mesh Oriented datABase  (version 5.2.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, const EntityHandle* file_set, const FileOptions& opts,
00127                          const SubsetList* subset_list = 0, const Tag* file_id_tag = 0 );
00128 
00129     ErrorCode read_tag_values( const char* file_name, const char* tag_name, const FileOptions& opts,
00130                                std::vector< int >& tag_values_out, const SubsetList* subset_list = 0 );
00131 
00132     //! Constructor
00133     ReadOBJ( Interface* impl = NULL );
00134 
00135     //! Destructor
00136     virtual ~ReadOBJ();
00137 
00138   private:
00139     ReadUtilIface* readMeshIface;
00140 
00141     //! interface instance
00142     Interface* MBI;
00143 
00144     GeomTopoTool* myGeomTool;
00145 
00146     Tag geom_tag, id_tag, name_tag, category_tag, faceting_tol_tag, geometry_resabs_tag, obj_name_tag;
00147 
00148     /*  The keyword type function matches the first character extracted from each line to a type of
00149      * line
00150      */
00151     keyword_type get_keyword( std::vector< std::string > tokens );
00152 
00153     /*  The match function searches a list of map keys for a match with the token
00154      */
00155     template < typename T >
00156     std::string match( const std::string& token, std::map< std::string, T >& tokenList );
00157 
00158     /* The tokenize function takes a string as input and splits it into
00159      * a vector of strings based on the delimiter
00160      */
00161     static const char* delimiters;
00162 
00163     void tokenize( const std::string& str, std::vector< std::string >& tokens, const char* delimiters );
00164 
00165     /*
00166      * The create_object funtion will create a new meshset for
00167      * each object that contains all vertices and faces
00168      */
00169     ErrorCode create_new_object( std::string object_name, int object_id, EntityHandle& curr_obj_meshset );
00170 
00171     ErrorCode create_new_group( std::string object_name, int curr_object, EntityHandle& object_meshset );
00172 
00173     /* create_new_vertex converts tokenized string input to
00174        vertex structure
00175      */
00176     ErrorCode create_new_vertex( std::vector< std::string > v_tokens, EntityHandle& vertex_eh );
00177 
00178     /* create_new_face converts tokenized string input to
00179      * face structure
00180      */
00181     ErrorCode create_new_face( std::vector< std::string > f_tokens, const std::vector< EntityHandle >& vertex_list,
00182                                EntityHandle& face_eh );
00183 
00184     /*
00185      * The split_quad function creates 1 new vertex and 4 new tri faces
00186      * from a quad face.
00187      */
00188 
00189     ErrorCode split_quad( std::vector< std::string > f_tokens, std::vector< EntityHandle >& vertex_list,
00190                           Range& face_eh );
00191 
00192     ErrorCode create_tri_faces( std::vector< EntityHandle > quad_vert_eh,
00193                                 //                     EntityHandle center_vertex_eh,
00194                                 Range& face_eh );
00195 };
00196 
00197 }  // namespace moab
00198 
00199 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines