2013-03-27 18:34:23 +00:00
/*
* This program source code file is part of KiCad , a free EDA CAD application .
*
* Copyright ( C ) 2013 Tuomas Vaherkoski < tuomasvaherkoski @ gmail . com >
2015-02-15 21:30:34 +00:00
* Copyright ( C ) 1992 - 2015 KiCad Developers , see AUTHORS . txt for contributors .
2013-03-27 18:34:23 +00:00
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , you may find one here :
* http : //www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http : //www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA
*/
/**
* @ file modelparsers . h
*/
# ifndef MODELPARSERS_H
# define MODELPARSERS_H
# include <map>
2015-04-13 20:56:15 +00:00
# include <memory>
2013-03-27 18:34:23 +00:00
# include <vector>
# include <wx/string.h>
2014-07-30 09:01:25 +00:00
# include <3d_mesh_model.h>
2013-03-27 18:34:23 +00:00
class S3D_MASTER ;
class X3D_MODEL_PARSER ;
/**
* abstract class S3D_MODEL_PARSER
* Base class for 3 D model parsers .
*/
class S3D_MODEL_PARSER
{
public :
2015-02-15 21:30:34 +00:00
S3D_MODEL_PARSER ( S3D_MASTER * aMaster ) :
2013-03-28 16:51:22 +00:00
master ( aMaster )
2013-03-27 18:34:23 +00:00
{ }
2015-03-28 11:33:56 +00:00
virtual ~ S3D_MODEL_PARSER ( ) { }
2013-03-27 18:34:23 +00:00
S3D_MASTER * GetMaster ( )
{
return master ;
}
/**
* Function Create
* Factory method for creating concrete 3 D model parsers
2013-03-28 16:51:22 +00:00
* Notice that the caller is responsible to delete created parser .
2013-03-27 18:34:23 +00:00
*
* @ param aMaster is master object that the parser will fill .
* @ param aExtension is file extension of the file you are going to parse .
*/
2013-03-28 16:51:22 +00:00
static S3D_MODEL_PARSER * Create ( S3D_MASTER * aMaster , const wxString aExtension ) ;
2014-08-21 11:59:57 +00:00
2013-03-27 18:34:23 +00:00
/**
2015-03-28 11:33:56 +00:00
* virtual Function
2013-03-27 18:34:23 +00:00
* Concrete parsers should implement this function
2014-08-21 11:59:57 +00:00
* @ param aFilename = the full file name of the file to load
2015-03-13 19:27:25 +00:00
* @ return true if as succeeded
*/
2015-04-13 20:56:15 +00:00
virtual bool Load ( const wxString & aFilename ) {
2015-03-28 11:33:56 +00:00
return false ;
} ;
2015-03-13 19:27:25 +00:00
2015-04-13 20:56:15 +00:00
S3D_MESH_PTRS childs ;
2013-03-27 18:34:23 +00:00
private :
S3D_MASTER * master ;
} ;
class wxXmlNode ;
/**
* class X3D_MODEL_PARSER
* Implements parser for X3D file format ( VRML2 .0 successor )
* X3D files can be exported from eg . Blender */
class X3D_MODEL_PARSER : public S3D_MODEL_PARSER
{
public :
2013-03-28 16:51:22 +00:00
X3D_MODEL_PARSER ( S3D_MASTER * aMaster ) ;
2013-03-27 18:34:23 +00:00
~ X3D_MODEL_PARSER ( ) ;
2014-08-21 11:59:57 +00:00
2015-03-13 19:27:25 +00:00
bool Load ( const wxString & aFilename ) ;
2013-03-27 18:34:23 +00:00
typedef std : : map < wxString , wxString > PROPERTY_MAP ;
2015-02-15 21:30:34 +00:00
typedef std : : vector < wxXmlNode * > NODE_LIST ;
2013-03-27 18:34:23 +00:00
/**
* Function GetChildsByName
* Searches all child nodes with aName .
*
* @ param aParent is node to search from
* @ param aName is the name of node you try to find
* @ param aResult contains found nodes
*/
2013-03-28 16:51:22 +00:00
static void GetChildsByName ( wxXmlNode * aParent , const wxString aName , NODE_LIST & aResult ) ;
2013-03-27 18:34:23 +00:00
/**
* Function GetNodeProperties
* Collects all node properties to map .
*
2013-04-25 16:29:35 +00:00
* @ param aNode is an XML node .
* @ param aProps contains map of found properties .
2013-03-27 18:34:23 +00:00
*/
2013-03-28 16:51:22 +00:00
static void GetNodeProperties ( wxXmlNode * aNode , PROPERTY_MAP & aProps ) ;
2013-03-27 18:34:23 +00:00
private :
2015-02-15 21:30:34 +00:00
wxString m_Filename ;
2015-04-13 20:56:15 +00:00
S3D_MESH_PTR m_model ;
2014-07-30 09:01:25 +00:00
2013-03-28 16:51:22 +00:00
std : : vector < wxString > vrml_materials ;
std : : vector < wxString > vrml_points ;
std : : vector < wxString > vrml_coord_indexes ;
2013-03-27 18:34:23 +00:00
void readTransform ( wxXmlNode * aTransformNode ) ;
void readMaterial ( wxXmlNode * aMatNode ) ;
2013-03-28 16:51:22 +00:00
void readIndexedFaceSet ( wxXmlNode * aFaceNode , PROPERTY_MAP & aTransfromProps ) ;
2013-03-27 18:34:23 +00:00
bool parseDoubleTriplet ( const wxString & aData , S3D_VERTEX & aResult ) ;
2013-03-28 16:51:22 +00:00
void rotate ( S3D_VERTEX & aCoordinate , S3D_VERTEX & aRotAxis , double angle ) ;
2013-03-27 18:34:23 +00:00
} ;
2014-07-30 09:01:25 +00:00
2015-02-15 21:30:34 +00:00
typedef std : : map < std : : string , std : : vector < glm : : vec3 > > VRML2_COORDINATE_MAP ;
2015-04-13 20:56:15 +00:00
typedef std : : map < std : : string , S3D_MESH_PTR > VRML2_DEF_GROUP_MAP ;
2014-07-30 09:01:25 +00:00
2013-03-27 18:34:23 +00:00
/**
2014-07-30 09:01:25 +00:00
* class VRML2_MODEL_PARSER
2013-03-27 18:34:23 +00:00
* Parses
*/
2015-03-13 19:27:25 +00:00
class VRML2_MODEL_PARSER
2013-03-27 18:34:23 +00:00
{
public :
2015-03-13 19:27:25 +00:00
VRML2_MODEL_PARSER ( S3D_MODEL_PARSER * aModelParser ) ;
2014-07-30 09:01:25 +00:00
~ VRML2_MODEL_PARSER ( ) ;
2014-08-21 11:59:57 +00:00
2015-03-13 19:27:25 +00:00
bool Load ( const wxString & aFilename ) ;
2013-03-27 18:34:23 +00:00
2015-03-28 11:33:56 +00:00
/**
* Function Load
* Load a VRML2 filename and apply a transformation to the root
* @ param aFilename file name with path
* @ param aTransformationModel a model with translation , rotation and scale to apply to default root
* @ return bool - true if finnished with success
*/
2015-04-13 20:56:15 +00:00
bool Load ( const wxString & aFilename , S3D_MESH_PTR aTransformationModel ) ;
2015-03-28 11:33:56 +00:00
2013-03-27 18:34:23 +00:00
/**
2014-07-30 09:01:25 +00:00
* Return string representing VRML2 file in vrml2 format
* Function Load must be called before this function , otherwise empty
* data set is returned .
2013-03-27 18:34:23 +00:00
*/
2014-07-30 09:01:25 +00:00
wxString VRML2_representation ( ) ;
private :
2015-04-13 20:56:15 +00:00
int loadFileModel ( S3D_MESH_PTR transformationModel ) ;
2014-07-30 09:01:25 +00:00
int read_Transform ( ) ;
int read_DEF ( ) ;
2015-02-15 21:30:34 +00:00
int read_DEF_Coordinate ( ) ;
2014-07-30 09:01:25 +00:00
int read_Shape ( ) ;
2015-03-28 16:16:40 +00:00
int read_appearance ( ) ;
2014-07-30 09:01:25 +00:00
int read_Appearance ( ) ;
int read_material ( ) ;
int read_Material ( ) ;
int read_IndexedFaceSet ( ) ;
2015-02-15 21:30:34 +00:00
int read_IndexedLineSet ( ) ;
2014-07-30 09:01:25 +00:00
int read_Coordinate ( ) ;
2015-02-15 21:30:34 +00:00
int read_CoordinateDef ( ) ;
2014-07-30 09:01:25 +00:00
int read_Normal ( ) ;
2014-07-31 07:01:30 +00:00
int read_NormalIndex ( ) ;
2014-07-30 09:01:25 +00:00
int read_Color ( ) ;
int read_coordIndex ( ) ;
int read_colorIndex ( ) ;
2015-03-28 11:33:56 +00:00
int read_geometry ( ) ;
int read_IndexedFaceSet_USE ( ) ;
int read_Transform_USE ( ) ;
int read_Inline ( ) ;
/** Function debug_enter
* Used in debug to increase a ' ' in the m_debugSpacer ,
* should be called after the first debug comment in a function
*/
void debug_enter ( ) ;
/** Function debug_exit
* Used in debug to decrease a ' ' in the m_debugSpacer ,
* should be called before the last debug comment in a funtion before exit
*/
void debug_exit ( ) ;
2015-02-15 21:30:34 +00:00
bool m_normalPerVertex ;
bool colorPerVertex ;
2015-04-13 20:56:15 +00:00
S3D_MESH_PTR m_model ; ///< It stores the current model that the parsing is adding data
2015-02-15 21:30:34 +00:00
FILE * m_file ;
2015-03-28 11:33:56 +00:00
wxFileName m_Filename ;
2015-02-15 21:30:34 +00:00
VRML2_COORDINATE_MAP m_defCoordinateMap ;
2015-03-29 16:13:58 +00:00
VRML2_DEF_GROUP_MAP m_defGroupMap ; ///< Stores a list of labels for groups and meshs that will be used later by the USE keyword
2015-03-13 19:27:25 +00:00
S3D_MODEL_PARSER * m_ModelParser ;
S3D_MASTER * m_Master ;
2015-03-29 16:13:58 +00:00
wxString m_debugSpacer ; ///< Used to give identation space
2015-03-29 10:59:53 +00:00
2015-03-29 16:13:58 +00:00
int m_counter_DEF_GROUP ; ///< Counts the number of DEF * GROUPS used
int m_counter_USE_GROUP ; ///< Counts the number of USE * used, in the end, if m_counter_DEF_GROUP > 0 and m_counter_USE_GROUP == 0 then it will add the first group with childs
bool m_discardLastGeometry ; ///< If true, it should not store the latest loaded geometry (used to discard IndexedLineSet, but load it)
2014-07-30 09:01:25 +00:00
} ;
/**
* class VRML1_MODEL_PARSER
* Parses
*/
2015-03-13 19:27:25 +00:00
class VRML1_MODEL_PARSER
2014-07-30 09:01:25 +00:00
{
public :
2015-03-13 19:27:25 +00:00
VRML1_MODEL_PARSER ( S3D_MODEL_PARSER * aModelParser ) ;
2014-07-30 09:01:25 +00:00
~ VRML1_MODEL_PARSER ( ) ;
2014-08-21 11:59:57 +00:00
2015-03-13 19:27:25 +00:00
bool Load ( const wxString & aFilename ) ;
2013-03-28 16:51:22 +00:00
/**
2014-07-30 09:01:25 +00:00
* Return string representing VRML2 file in vrml2 format
* Function Load must be called before this function , otherwise empty
* data set is returned .
2013-03-28 16:51:22 +00:00
*/
2014-07-30 09:01:25 +00:00
wxString VRML2_representation ( ) ;
private :
int read_separator ( ) ;
int readMaterial ( ) ;
int readCoordinate3 ( ) ;
int readIndexedFaceSet ( ) ;
int readMaterial_ambientColor ( ) ;
int readMaterial_diffuseColor ( ) ;
int readMaterial_emissiveColor ( ) ;
int readMaterial_specularColor ( ) ;
int readMaterial_shininess ( ) ;
int readMaterial_transparency ( ) ;
int readCoordinate3_point ( ) ;
int readIndexedFaceSet_coordIndex ( ) ;
int readIndexedFaceSet_materialIndex ( ) ;
2015-02-15 21:30:34 +00:00
bool m_normalPerVertex ;
bool colorPerVertex ;
2015-04-13 20:56:15 +00:00
S3D_MESH_PTR m_model ;
2015-02-15 21:30:34 +00:00
FILE * m_file ;
wxString m_Filename ;
2015-03-13 19:27:25 +00:00
S3D_MODEL_PARSER * m_ModelParser ;
S3D_MASTER * m_Master ;
2013-03-27 18:34:23 +00:00
} ;
2014-07-30 09:01:25 +00:00
/**
* class VRML_MODEL_PARSER
* Parses
*/
class VRML_MODEL_PARSER : public S3D_MODEL_PARSER
{
public :
2015-03-05 19:46:38 +00:00
/**
* ctor : initialize a VRML file parser
* @ param aMaster = a ref to a 3 D footprint shape description to fill
* by the vrml file data
*/
2014-07-30 09:01:25 +00:00
VRML_MODEL_PARSER ( S3D_MASTER * aMaster ) ;
~ VRML_MODEL_PARSER ( ) ;
2014-08-21 11:59:57 +00:00
2015-03-05 19:46:38 +00:00
/**
* Function load
* Load a 3 D file and build a S3D_MASTER shape .
* file has . vrml ext and can be VRML 1 or VRML 2 format
* @ param aFilename = the full filename to read
* @ param aVrmlunits_to_3Dunits = the csaling factor to convert the 3 D file unit
* to our internal units .
*/
2015-03-13 19:27:25 +00:00
bool Load ( const wxString & aFilename ) ;
2014-07-30 09:01:25 +00:00
} ;
2013-03-27 18:34:23 +00:00
# endif // MODELPARSERS_H