29#include <wx/filename.h>
42#define SG_VERSION_TAG "VERSION:2"
78 bool reuse,
bool renameNodes )
80 if(
nullptr == filename || filename[0] == 0 )
83 wxString ofile = wxString::FromUTF8Unchecked( filename );
85 if( wxFileName::Exists( ofile ) )
91 if( !wxFileName::FileExists( ofile ) )
101 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] failed to open file '%s'" ),
102 __FILE__, __FUNCTION__, __LINE__, filename );
107 op.imbue( std::locale::classic() );
108 op <<
"#VRML V2.0 utf8\n";
126 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] problems encountered writing file '%s'" ),
127 __FILE__, __FUNCTION__, __LINE__, filename );
158 const char* aPluginInfo )
160 if(
nullptr == aFileName || aFileName[0] == 0 )
163 wxString ofile = wxString::FromUTF8Unchecked( aFileName );
165 wxCHECK( aNode,
false );
167 if( wxFileName::Exists( ofile ) )
171 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] file exists not overwriting '%s'" ),
172 __FILE__, __FUNCTION__, __LINE__, aFileName );
178 if( !wxFileName::FileExists( aFileName ) )
180 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] specified path is a directory '%s'" ),
181 __FILE__, __FUNCTION__, __LINE__, aFileName );
191 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] failed to open file '%s'" ),
192 __FILE__, __FUNCTION__, __LINE__, aFileName );
199 if(
nullptr != aPluginInfo && aPluginInfo[0] != 0 )
200 output <<
"(" << aPluginInfo <<
")";
202 output <<
"(INTERNAL:0.0.0.0)";
204 bool rval = aNode->
WriteCache( output,
nullptr );
209 wxLogTrace( MASK_3D_SG,
210 wxT(
"%s:%s:%d * [INFO] problems encountered writing cache file '%s'" ),
211 __FILE__, __FUNCTION__, __LINE__, aFileName );
214 wxRemoveFile( ofile );
222 bool (*aTagCheck)(
const char*,
void* ) )
224 if(
nullptr == aFileName || aFileName[0] == 0 )
227 wxString ofile = wxString::FromUTF8Unchecked( aFileName );
229 if( !wxFileName::FileExists( aFileName ) )
231 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] no such file '%s'" ),
232 __FILE__, __FUNCTION__, __LINE__, aFileName );
237 std::unique_ptr<SGNODE> np = std::make_unique<SCENEGRAPH>(
nullptr );
243 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] failed to open file '%s'" ),
244 __FILE__, __FUNCTION__, __LINE__, aFileName );
259 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; missing left parenthesis"
260 " at position '%d'" ),
261 __FILE__, __FUNCTION__, __LINE__,
262 static_cast<int>( file.tellg() ) );
270 while(
')' != schar && file.good() )
272 name.push_back( schar );
294 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; missing left parenthesis"
295 " at position '%d'" ),
296 __FILE__, __FUNCTION__, __LINE__,
297 static_cast<int>( file.tellg() ) );
305 while(
')' != schar && file.good() )
307 name.push_back( schar );
312 if(
nullptr != aTagCheck &&
nullptr != aPluginMgr
313 && !aTagCheck(
name.c_str(), aPluginMgr ) )
321 bool rval = np->ReadCache( file,
nullptr );
326 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] problems encountered reading cache file "
328 __FILE__, __FUNCTION__, __LINE__,
340 if(
nullptr == aNode )
347 std::vector< SMESH > meshes;
361 materials.
matorder.push_back( &app );
362 materials.
matmap.emplace( &app, 0 );
364 if( aNode->
Prepare(
nullptr, materials, meshes ) )
372 size_t j = materials.
matorder.size();
375 for(
size_t i = 0; i < j; ++i )
385 for(
size_t i = 0; i < j; ++i )
386 lmesh[i] = meshes[i];
394 size_t j = meshes.size();
396 for(
size_t i = 0; i < j; ++i )
405 if(
nullptr == aModel ||
nullptr == *aModel )
448 unsigned char* Revision )
noexcept
466 glm::dvec3 tri = glm::dvec3( 0.0, 0.0, 0.0 );
469 pts[0] = glm::dvec3( p1.
x, p1.
y, p1.
z );
470 pts[1] = glm::dvec3( p2.
x, p2.
y, p2.
z );
471 pts[2] = glm::dvec3( p3.
x, p3.
y, p3.
z );
478 tri = glm::cross( pts[1] - pts[0], pts[2] - pts[0] );
479 (void)glm::normalize( tri );
481 return SGVECTOR( tri.x, tri.y, tri.z );
487 if(
nullptr == aNode )
496 if(
nullptr == aNode )
505 if(
nullptr == aParent ||
nullptr == aChild )
514 if(
nullptr == aParent ||
nullptr == aChild )
523 if(
nullptr == aObject ||
nullptr == aRefPtr || aObject != *aRefPtr )
Define the basic data set required to represent a 3D model.
bool Prepare(const glm::dmat4 *aTransform, S3D::MATLIST &materials, std::vector< SMESH > &meshes)
Defines the generic material appearance of a scenegraph object.
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const noexcept
The base class of all Scene Graph nodes.
void ResetNodeIndex(void) noexcept
Reset the global SG* node indices in preparation for write operations.
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Write this node's data to a binary cache file.
virtual bool AddRefNode(SGNODE *aNode)=0
SGNODE * GetParent(void) const noexcept
Returns a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
virtual void ReNameNodes(void)=0
Rename a node and all its child nodes in preparation for write operations.
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
virtual bool AddChildNode(SGNODE *aNode)=0
void AssociateWrapper(SGNODE **aWrapperRef) noexcept
Associate this object with a handle to itself.
virtual bool WriteVRML(std::ostream &aFile, bool aReuseFlag)=0
Writes this node's data to a VRML file.
static void formatMaterial(SMATERIAL &mat, SGAPPEARANCE const *app)
void Free3DModel(S3DMODEL &aModel)
defines the API calls for the manipulation of SG* classes
SGLIB_API void Init3DMaterial(SMATERIAL &aMat)
Function Init3DMaterial initializes an SMATERIAL struct.
void INIT_SMESH(SMESH &aMesh) noexcept
SGLIB_API void RenameNodes(SGNODE *aNode)
Function RenameNodes renames a node and all children nodes based on the current values of the global ...
SGLIB_API void Free3DMesh(SMESH &aMesh)
Function Free3DMesh frees memory used internally by an SMESH structure.
SGLIB_API SGNODE * ReadCache(const char *aFileName, void *aPluginMgr, bool(*aTagCheck)(const char *, void *))
Function ReadCache reads a binary cache file and creates an SGNODE tree.
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
SGLIB_API void Init3DMesh(SMESH &aMesh)
Function Init3DMesh creates and initializes an SMESH struct.
SGLIB_API bool WriteCache(const char *aFileName, bool overwrite, SGNODE *aNode, const char *aPluginInfo)
Function WriteCache writes the SGNODE tree to a binary cache file.
SGLIB_API bool WriteVRML(const char *filename, bool overwrite, SGNODE *aTopNode, bool reuse, bool renameNodes)
Function WriteVRML writes out the given node and its subnodes to a VRML2 file.
void INIT_SMATERIAL(SMATERIAL &aMaterial)
SGLIB_API S3DMODEL * New3DModel(void)
Function New3DModel creates and initializes an S3DMODEL struct.
SGLIB_API void ResetNodeIndex(SGNODE *aNode)
Function ResetNodeIndex resets the global SG* class indices.
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
SGLIB_API SGVECTOR CalcTriNorm(const SGPOINT &p1, const SGPOINT &p2, const SGPOINT &p3)
Function CalcTriNorm returns the normal vector of a triangle described by vertices p1,...
SGLIB_API void DestroyNode(SGNODE *aNode) noexcept
Function DestroyNode deletes the given SG* class node.
bool degenerate(glm::dvec3 *pts) noexcept
void FREE_SMESH(SMESH &aMesh) noexcept
void INIT_S3DMODEL(S3DMODEL &aModel) noexcept
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
void FREE_S3DMODEL(S3DMODEL &aModel)
SGLIB_API void GetLibVersion(unsigned char *Major, unsigned char *Minor, unsigned char *Patch, unsigned char *Revision) noexcept
Function GetLibVersion retrieves version information of the kicad_3dsg library.
SGLIB_API void AssociateSGNodeWrapper(SGNODE *aObject, SGNODE **aRefPtr)
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
SGLIB_API void Destroy3DModel(S3DMODEL **aModel)
Function Destroy3DModel frees memory used by an S3DMODEL structure and sets the pointer to the struct...
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
defines the library version of the intermediate scenegraph (SG) implementation
#define KICADSG_VERSION_MAJOR
#define KICADSG_VERSION_REVISION
#define KICADSG_VERSION_PATCH
#define KICADSG_VERSION_MINOR
#define OPEN_OSTREAM(var, name)
#define CLOSE_STREAM(var)
#define OPEN_ISTREAM(var, name)
Store the a model based on meshes and materials.
SMATERIAL * m_Materials
The materials list of this model.
unsigned int m_MeshesSize
Number of meshes in the array.
SMESH * m_Meshes
The meshes list of this model.
unsigned int m_MaterialsSize
Number of materials in the material array.
std::vector< SGAPPEARANCE const * > matorder
std::map< SGAPPEARANCE const *, int > matmap
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Per-vertex normal/color/texcoors structure.