2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-03-02 22:53:49 +00:00
|
|
|
* Copyright (C) 2015-2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2020-12-16 18:19:28 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2016-02-23 05:28:41 +00:00
|
|
|
#include <sstream>
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <fstream>
|
2022-02-05 19:37:51 +00:00
|
|
|
#include <memory>
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <wx/filename.h>
|
2016-02-23 05:28:41 +00:00
|
|
|
#include <wx/log.h>
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "plugins/3dapi/ifsg_api.h"
|
2016-01-29 00:52:42 +00:00
|
|
|
#include "plugins/3dapi/sg_version.h"
|
2017-03-02 22:53:49 +00:00
|
|
|
#include "streamwrapper.h"
|
2015-12-09 07:30:48 +00:00
|
|
|
#include "3d_cache/sg/sg_node.h"
|
|
|
|
#include "3d_cache/sg/scenegraph.h"
|
|
|
|
#include "3d_cache/sg/sg_appearance.h"
|
|
|
|
#include "3d_cache/sg/sg_shape.h"
|
2015-12-17 02:55:35 +00:00
|
|
|
#include "3d_cache/sg/sg_helpers.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
2016-01-31 02:21:12 +00:00
|
|
|
// version format of the cache file
|
|
|
|
#define SG_VERSION_TAG "VERSION:2"
|
|
|
|
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
static void formatMaterial( SMATERIAL& mat, SGAPPEARANCE const* app )
|
|
|
|
{
|
|
|
|
float v0, v1, v2;
|
|
|
|
|
2016-02-06 21:41:27 +00:00
|
|
|
app->ambient.GetColor( v0, v1, v2 );
|
2015-12-08 07:31:57 +00:00
|
|
|
mat.m_Ambient.x = v0;
|
2016-02-06 21:41:27 +00:00
|
|
|
mat.m_Ambient.y = v1;
|
|
|
|
mat.m_Ambient.z = v2;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
app->diffuse.GetColor( v0, v1, v2 );
|
|
|
|
mat.m_Diffuse.x = v0;
|
|
|
|
mat.m_Diffuse.y = v1;
|
|
|
|
mat.m_Diffuse.z = v2;
|
2016-02-06 21:41:27 +00:00
|
|
|
mat.m_Ambient.x *= v0;
|
|
|
|
mat.m_Ambient.y *= v1;
|
|
|
|
mat.m_Ambient.z *= v2;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
app->emissive.GetColor( v0, v1, v2 );
|
|
|
|
mat.m_Emissive.x = v0;
|
|
|
|
mat.m_Emissive.y = v1;
|
|
|
|
mat.m_Emissive.z = v2;
|
|
|
|
|
|
|
|
app->specular.GetColor( v0, v1, v2 );
|
|
|
|
mat.m_Specular.x = v0;
|
|
|
|
mat.m_Specular.y = v1;
|
|
|
|
mat.m_Specular.z = v2;
|
|
|
|
|
|
|
|
mat.m_Shininess = app->shininess;
|
|
|
|
mat.m_Transparency = app->transparency;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-29 00:52:42 +00:00
|
|
|
bool S3D::WriteVRML( const char* filename, bool overwrite, SGNODE* aTopNode,
|
2020-12-16 18:19:28 +00:00
|
|
|
bool reuse, bool renameNodes )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == filename || filename[0] == 0 )
|
2016-01-29 00:52:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString ofile = wxString::FromUTF8Unchecked( filename );
|
|
|
|
|
|
|
|
if( wxFileName::Exists( ofile ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( !overwrite )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// make sure we make no attempt to write a directory
|
2016-01-29 00:52:42 +00:00
|
|
|
if( !wxFileName::FileExists( ofile ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aTopNode && aTopNode->GetNodeType() == S3D::SGTYPE_TRANSFORM, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
OPEN_OSTREAM( op, filename );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
if( op.fail() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] failed to open file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, filename );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-27 01:21:55 +00:00
|
|
|
op.imbue( std::locale::classic() );
|
2015-12-08 07:31:57 +00:00
|
|
|
op << "#VRML V2.0 utf8\n";
|
|
|
|
|
|
|
|
if( renameNodes )
|
|
|
|
{
|
|
|
|
aTopNode->ResetNodeIndex();
|
|
|
|
aTopNode->ReNameNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
aTopNode->WriteVRML( op, reuse );
|
|
|
|
|
|
|
|
if( !op.fail() )
|
|
|
|
{
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( op );
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( op );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] problems encountered writing file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, filename );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::ResetNodeIndex( SGNODE* aNode )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
aNode->ResetNodeIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::RenameNodes( SGNODE* aNode )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
aNode->ReNameNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void S3D::DestroyNode( SGNODE* aNode ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
delete aNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-31 02:21:12 +00:00
|
|
|
bool S3D::WriteCache( const char* aFileName, bool overwrite, SGNODE* aNode,
|
2020-12-16 18:19:28 +00:00
|
|
|
const char* aPluginInfo )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aFileName || aFileName[0] == 0 )
|
2016-01-29 00:52:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString ofile = wxString::FromUTF8Unchecked( aFileName );
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode, false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-29 00:52:42 +00:00
|
|
|
if( wxFileName::Exists( ofile ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( !overwrite )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] file exists not overwriting '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
2016-02-23 05:28:41 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we make no attempt to write a directory
|
|
|
|
if( !wxFileName::FileExists( aFileName ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] specified path is a directory '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
OPEN_OSTREAM( output, aFileName );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
if( output.fail() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] failed to open file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-31 02:21:12 +00:00
|
|
|
output << "(" << SG_VERSION_TAG << ")";
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aPluginInfo && aPluginInfo[0] != 0 )
|
2016-01-31 02:21:12 +00:00
|
|
|
output << "(" << aPluginInfo << ")";
|
|
|
|
else
|
|
|
|
output << "(INTERNAL:0.0.0.0)";
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
bool rval = aNode->WriteCache( output, nullptr );
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( output );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !rval )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG,
|
|
|
|
wxT( "%s:%s:%d * [INFO] problems encountered writing cache file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
2016-01-31 02:21:12 +00:00
|
|
|
|
|
|
|
// delete the defective file
|
|
|
|
wxRemoveFile( ofile );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-31 02:21:12 +00:00
|
|
|
SGNODE* S3D::ReadCache( const char* aFileName, void* aPluginMgr,
|
2020-12-16 18:19:28 +00:00
|
|
|
bool (*aTagCheck)( const char*, void* ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aFileName || aFileName[0] == 0 )
|
|
|
|
return nullptr;
|
2016-01-29 00:52:42 +00:00
|
|
|
|
|
|
|
wxString ofile = wxString::FromUTF8Unchecked( aFileName );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
if( !wxFileName::FileExists( aFileName ) )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] no such file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
|
|
|
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 19:37:51 +00:00
|
|
|
std::unique_ptr<SGNODE> np = std::make_unique<SCENEGRAPH>( nullptr );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
OPEN_ISTREAM( file, aFileName );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
if( file.fail() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] failed to open file '%s'" ),
|
2020-12-16 18:19:28 +00:00
|
|
|
__FILE__, __FUNCTION__, __LINE__, aFileName );
|
|
|
|
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 02:21:12 +00:00
|
|
|
// from SG_VERSION_TAG 1, read the version tag; if it's not the expected tag
|
|
|
|
// then we fail to read the cache file
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
char schar;
|
|
|
|
file.get( schar );
|
|
|
|
|
|
|
|
if( '(' != schar )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; missing left parenthesis"
|
|
|
|
" at position '%d'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<int>( file.tellg() ) );
|
2016-01-31 02:21:12 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( file );
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2016-01-31 02:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file.get( schar );
|
|
|
|
|
|
|
|
while( ')' != schar && file.good() )
|
|
|
|
{
|
|
|
|
name.push_back( schar );
|
|
|
|
file.get( schar );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( name.compare( SG_VERSION_TAG ) )
|
|
|
|
{
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( file );
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2016-01-31 02:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} while( 0 );
|
|
|
|
|
|
|
|
// from SG_VERSION_TAG 2, read the PluginInfo string and check that it matches
|
|
|
|
// version tag; if it's not the expected tag then we fail to read the file
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
char schar;
|
|
|
|
file.get( schar );
|
|
|
|
|
|
|
|
if( '(' != schar )
|
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] corrupt data; missing left parenthesis"
|
|
|
|
" at position '%d'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
static_cast<int>( file.tellg() ) );
|
2016-01-31 02:21:12 +00:00
|
|
|
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( file );
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2016-01-31 02:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file.get( schar );
|
|
|
|
|
|
|
|
while( ')' != schar && file.good() )
|
|
|
|
{
|
|
|
|
name.push_back( schar );
|
|
|
|
file.get( schar );
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the plugin tag
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aTagCheck && nullptr != aPluginMgr
|
|
|
|
&& !aTagCheck( name.c_str(), aPluginMgr ) )
|
2016-01-31 02:21:12 +00:00
|
|
|
{
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( file );
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2016-01-31 02:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} while( 0 );
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
bool rval = np->ReadCache( file, nullptr );
|
2017-03-02 22:53:49 +00:00
|
|
|
CLOSE_STREAM( file );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !rval )
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [INFO] problems encountered reading cache file "
|
|
|
|
"'%s'" ),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__,
|
|
|
|
aFileName );
|
2020-12-16 18:19:28 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 19:37:51 +00:00
|
|
|
return np.release();
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 22:21:30 +00:00
|
|
|
S3DMODEL* S3D::GetModel( SCENEGRAPH* aNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
|
|
|
return nullptr;
|
2015-12-16 09:01:44 +00:00
|
|
|
|
|
|
|
if( aNode->GetNodeType() != S3D::SGTYPE_TRANSFORM )
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2015-12-16 09:01:44 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
S3D::MATLIST materials;
|
|
|
|
std::vector< SMESH > meshes;
|
|
|
|
|
|
|
|
// the materials list shall have a default color; although the VRML
|
|
|
|
// default is an opaque black, the default used here shall be a median
|
|
|
|
// gray in hopes that it may help highlight faulty models; this color is
|
|
|
|
// also typical of MCAD applications. When a model has no associated
|
|
|
|
// material color it shall be assigned the index 0.
|
2020-12-16 18:19:28 +00:00
|
|
|
SGAPPEARANCE app( nullptr );
|
2019-12-12 21:51:21 +00:00
|
|
|
app.ambient = SGCOLOR( 0.6f, 0.6f, 0.6f );
|
|
|
|
app.diffuse = SGCOLOR( 0.6f, 0.6f, 0.6f );
|
2015-12-08 07:31:57 +00:00
|
|
|
app.specular = app.diffuse;
|
2019-12-12 21:51:21 +00:00
|
|
|
app.shininess = 0.05f;
|
|
|
|
app.transparency = 0.0f;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
materials.matorder.push_back( &app );
|
2022-02-06 00:53:31 +00:00
|
|
|
materials.matmap.emplace( &app, 0 );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( aNode->Prepare( nullptr, materials, meshes ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( meshes.empty() )
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
S3DMODEL* model = S3D::New3DModel();
|
|
|
|
|
|
|
|
// add all the materials
|
|
|
|
size_t j = materials.matorder.size();
|
|
|
|
SMATERIAL* lmat = new SMATERIAL[j];
|
|
|
|
|
2016-03-10 22:37:39 +00:00
|
|
|
for( size_t i = 0; i < j; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
formatMaterial( lmat[i], materials.matorder[i] );
|
|
|
|
|
|
|
|
model->m_Materials = lmat;
|
|
|
|
model->m_MaterialsSize = j;
|
|
|
|
|
|
|
|
// add all the meshes
|
|
|
|
j = meshes.size();
|
|
|
|
SMESH* lmesh = new SMESH[j];
|
|
|
|
|
2016-03-10 22:37:39 +00:00
|
|
|
for( size_t i = 0; i < j; ++i )
|
2015-12-08 07:31:57 +00:00
|
|
|
lmesh[i] = meshes[i];
|
|
|
|
|
|
|
|
model->m_Meshes = lmesh;
|
|
|
|
model->m_MeshesSize = j;
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t j = meshes.size();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < j; ++i )
|
|
|
|
S3D::Free3DMesh( meshes[i] );
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
return nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::Destroy3DModel( S3DMODEL** aModel )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aModel || nullptr == *aModel )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
S3DMODEL* m = *aModel;
|
|
|
|
S3D::FREE_S3DMODEL( *m );
|
|
|
|
delete m;
|
2020-12-16 18:19:28 +00:00
|
|
|
*aModel = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Free3DModel( S3DMODEL& aModel )
|
|
|
|
{
|
|
|
|
S3D::FREE_S3DMODEL( aModel );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::Free3DMesh( SMESH& aMesh )
|
|
|
|
{
|
|
|
|
S3D::FREE_SMESH( aMesh );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
S3DMODEL* S3D::New3DModel( void )
|
|
|
|
{
|
|
|
|
S3DMODEL* mp = new S3DMODEL;
|
|
|
|
S3D::INIT_S3DMODEL( *mp );
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::Init3DMaterial( SMATERIAL& aMat )
|
|
|
|
{
|
|
|
|
S3D::INIT_SMATERIAL( aMat );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::Init3DMesh( SMESH& aMesh )
|
|
|
|
{
|
|
|
|
S3D::INIT_SMESH( aMesh );
|
|
|
|
}
|
2015-12-14 03:51:28 +00:00
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
void S3D::GetLibVersion( unsigned char* Major, unsigned char* Minor, unsigned char* Patch,
|
|
|
|
unsigned char* Revision ) noexcept
|
2015-12-14 03:51:28 +00:00
|
|
|
{
|
|
|
|
if( Major )
|
2016-01-29 00:52:42 +00:00
|
|
|
*Major = KICADSG_VERSION_MAJOR;
|
2015-12-14 03:51:28 +00:00
|
|
|
|
|
|
|
if( Minor )
|
2016-01-29 00:52:42 +00:00
|
|
|
*Minor = KICADSG_VERSION_MINOR;
|
2015-12-14 03:51:28 +00:00
|
|
|
|
|
|
|
if( Revision )
|
2016-01-29 00:52:42 +00:00
|
|
|
*Revision = KICADSG_VERSION_REVISION;
|
2015-12-14 03:51:28 +00:00
|
|
|
|
|
|
|
if( Patch )
|
2016-01-29 00:52:42 +00:00
|
|
|
*Patch = KICADSG_VERSION_PATCH;
|
2015-12-14 03:51:28 +00:00
|
|
|
}
|
2015-12-17 02:55:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
SGVECTOR S3D::CalcTriNorm( const SGPOINT& p1, const SGPOINT& p2, const SGPOINT& p3 )
|
|
|
|
{
|
|
|
|
glm::dvec3 tri = glm::dvec3( 0.0, 0.0, 0.0 );
|
|
|
|
glm::dvec3 pts[3];
|
|
|
|
|
|
|
|
pts[0] = glm::dvec3( p1.x, p1.y, p1.z );
|
|
|
|
pts[1] = glm::dvec3( p2.x, p2.y, p2.z );
|
|
|
|
pts[2] = glm::dvec3( p3.x, p3.y, p3.z );
|
|
|
|
|
|
|
|
// degenerate points are given a default 0, 0, 1 normal
|
|
|
|
if( S3D::degenerate( pts ) )
|
|
|
|
return SGVECTOR( 0.0, 0.0, 1.0 );
|
|
|
|
|
|
|
|
// normal
|
2016-03-15 00:38:55 +00:00
|
|
|
tri = glm::cross( pts[1] - pts[0], pts[2] - pts[0] );
|
2024-05-10 16:54:32 +00:00
|
|
|
(void)glm::normalize( tri );
|
2015-12-17 02:55:35 +00:00
|
|
|
|
|
|
|
return SGVECTOR( tri.x, tri.y, tri.z );
|
|
|
|
}
|
2016-01-02 01:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
S3D::SGTYPES S3D::GetSGNodeType( SGNODE* aNode )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
2016-01-02 01:21:02 +00:00
|
|
|
return SGTYPE_END;
|
|
|
|
|
|
|
|
return aNode->GetNodeType();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGNODE* S3D::GetSGNodeParent( SGNODE* aNode )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
|
|
|
return nullptr;
|
2016-01-02 01:21:02 +00:00
|
|
|
|
|
|
|
return aNode->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D::AddSGNodeRef( SGNODE* aParent, SGNODE* aChild )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aParent || nullptr == aChild )
|
2016-01-02 01:21:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return aParent->AddRefNode( aChild );
|
|
|
|
}
|
2016-01-04 07:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool S3D::AddSGNodeChild( SGNODE* aParent, SGNODE* aChild )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aParent || nullptr == aChild )
|
2016-01-04 07:10:15 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return aParent->AddChildNode( aChild );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::AssociateSGNodeWrapper( SGNODE* aObject, SGNODE** aRefPtr )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aObject || nullptr == aRefPtr || aObject != *aRefPtr )
|
2016-01-04 07:10:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
aObject->AssociateWrapper( aRefPtr );
|
|
|
|
}
|