2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 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
|
|
|
|
*/
|
|
|
|
|
2016-02-23 06:46:22 +00:00
|
|
|
#include <algorithm>
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2016-02-23 06:46:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <wx/log.h>
|
2015-12-09 07:30:48 +00:00
|
|
|
|
|
|
|
#include "3d_cache/sg/sg_node.h"
|
|
|
|
#include "plugins/3dapi/c3dmodel.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
|
2015-12-19 06:53:07 +00:00
|
|
|
static const std::string node_names[S3D::SGTYPE_END + 1] = {
|
2015-12-08 07:31:57 +00:00
|
|
|
"TXFM",
|
|
|
|
"APP",
|
|
|
|
"COL",
|
|
|
|
"COLIDX",
|
|
|
|
"FACE",
|
|
|
|
"COORD",
|
|
|
|
"COORDIDX",
|
|
|
|
"NORM",
|
|
|
|
"SHAPE",
|
|
|
|
"INVALID"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int node_counts[S3D::SGTYPE_END] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
char const* S3D::GetNodeTypeName( S3D::SGTYPES aType ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return node_names[aType].c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void getNodeName( S3D::SGTYPES nodeType, std::string& aName )
|
|
|
|
{
|
|
|
|
if( nodeType < 0 || nodeType >= S3D::SGTYPE_END )
|
|
|
|
{
|
|
|
|
aName = node_names[S3D::SGTYPE_END];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int seqNum = node_counts[nodeType];
|
|
|
|
++node_counts[nodeType];
|
|
|
|
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << node_names[nodeType] << "_" << seqNum;
|
|
|
|
aName = ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGNODE::SGNODE( SGNODE* aParent )
|
|
|
|
{
|
|
|
|
m_Parent = aParent;
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Association = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
m_written = false;
|
|
|
|
m_SGtype = S3D::SGTYPE_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SGNODE::~SGNODE()
|
|
|
|
{
|
|
|
|
if( m_Parent )
|
|
|
|
m_Parent->unlinkChildNode( this );
|
|
|
|
|
|
|
|
if( m_Association )
|
2020-12-16 18:19:28 +00:00
|
|
|
*m_Association = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
std::list< SGNODE* >::iterator sBP = m_BackPointers.begin();
|
|
|
|
std::list< SGNODE* >::iterator eBP = m_BackPointers.end();
|
|
|
|
|
|
|
|
while( sBP != eBP )
|
|
|
|
{
|
|
|
|
(*sBP)->unlinkRefNode( this );
|
|
|
|
++sBP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
S3D::SGTYPES SGNODE::GetNodeType( void ) const noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return m_SGtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
SGNODE* SGNODE::GetParent( void ) const noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return m_Parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 01:30:59 +00:00
|
|
|
bool SGNODE::SwapParent( SGNODE* aNewParent )
|
|
|
|
{
|
|
|
|
if( aNewParent == m_Parent )
|
|
|
|
return true;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNewParent )
|
2016-03-14 01:30:59 +00:00
|
|
|
return false;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == m_Parent )
|
2016-03-14 01:30:59 +00:00
|
|
|
{
|
|
|
|
if( aNewParent->AddChildNode( this ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aNewParent->GetNodeType() != m_Parent->GetNodeType() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SGNODE* oldParent = m_Parent;
|
|
|
|
m_Parent->unlinkChildNode( this );
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Parent = nullptr;
|
2016-03-14 01:30:59 +00:00
|
|
|
aNewParent->unlinkRefNode( this );
|
|
|
|
aNewParent->AddChildNode( this );
|
|
|
|
oldParent->AddRefNode( this );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
const char* SGNODE::GetName( void )
|
|
|
|
{
|
|
|
|
if( m_Name.empty() )
|
|
|
|
getNodeName( m_SGtype, m_Name );
|
|
|
|
|
|
|
|
return m_Name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
void SGNODE::SetName( const char* aName )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aName || 0 == aName[0] )
|
2015-12-08 07:31:57 +00:00
|
|
|
getNodeName( m_SGtype, m_Name );
|
|
|
|
else
|
|
|
|
m_Name = aName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
const char* SGNODE::GetNodeTypeName( S3D::SGTYPES aNodeType ) const noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return node_names[aNodeType].c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SGNODE::addNodeRef( SGNODE* aNode )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
2016-03-14 01:30:59 +00:00
|
|
|
return;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
std::list< SGNODE* >::iterator np =
|
|
|
|
std::find( m_BackPointers.begin(), m_BackPointers.end(), aNode );
|
|
|
|
|
|
|
|
if( np != m_BackPointers.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_BackPointers.push_back( aNode );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 01:30:59 +00:00
|
|
|
void SGNODE::delNodeRef( const SGNODE* aNode )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr == aNode )
|
2016-03-14 01:30:59 +00:00
|
|
|
return;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
std::list< SGNODE* >::iterator np =
|
|
|
|
std::find( m_BackPointers.begin(), m_BackPointers.end(), aNode );
|
|
|
|
|
|
|
|
if( np != m_BackPointers.end() )
|
|
|
|
{
|
|
|
|
m_BackPointers.erase( np );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxLogTrace( MASK_3D_SG, "%s:%s:%d * [BUG] delNodeRef() did not find its target, "
|
|
|
|
"this node type %d, referenced node type %d", __FILE__, __FUNCTION__, __LINE__,
|
|
|
|
m_SGtype, aNode->GetNodeType() );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGNODE::AssociateWrapper( SGNODE** aWrapperRef ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aWrapperRef && *aWrapperRef == this, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
// if there is an existing association then break it and emit a warning
|
|
|
|
// just in case the behavior is undesired
|
|
|
|
if( m_Association )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
*m_Association = nullptr;
|
|
|
|
|
|
|
|
wxLogTrace( MASK_3D_SG,
|
|
|
|
"%s:%s:%d * [WARNING] association being broken with previous wrapper",
|
|
|
|
__FILE__, __FUNCTION__, __LINE__ );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_Association = aWrapperRef;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGNODE::DisassociateWrapper( SGNODE** aWrapperRef ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( !m_Association )
|
|
|
|
return;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aWrapperRef, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( *aWrapperRef == *m_Association && aWrapperRef == m_Association, /* void */ );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
m_Association = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void SGNODE::ResetNodeIndex( void ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
for( int i = 0; i < (int)S3D::SGTYPE_END; ++i )
|
|
|
|
node_counts[i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D::GetMatIndex( MATLIST& aList, SGNODE* aNode, int& aIndex )
|
|
|
|
{
|
|
|
|
aIndex = 0;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
wxCHECK( aNode && S3D::SGTYPE_APPEARANCE == aNode->GetNodeType(), false );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
SGAPPEARANCE* node = (SGAPPEARANCE*)aNode;
|
|
|
|
|
|
|
|
std::map< SGAPPEARANCE const*, int >::iterator it = aList.matmap.find( node );
|
|
|
|
|
|
|
|
if( it != aList.matmap.end() )
|
|
|
|
{
|
|
|
|
aIndex = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = (int)aList.matorder.size();
|
|
|
|
aList.matorder.push_back( node );
|
|
|
|
aList.matmap.insert( std::pair< SGAPPEARANCE const*, int >( node, idx ) );
|
|
|
|
aIndex = idx;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::INIT_SMATERIAL( SMATERIAL& aMaterial )
|
|
|
|
{
|
2019-01-09 14:46:41 +00:00
|
|
|
aMaterial = {};
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void S3D::INIT_SMESH( SMESH& aMesh ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-01-09 14:46:41 +00:00
|
|
|
aMesh = {};
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:11:21 +00:00
|
|
|
void S3D::INIT_S3DMODEL( S3DMODEL& aModel ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-01-09 14:46:41 +00:00
|
|
|
aModel = {};
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
void S3D::FREE_SMESH( SMESH& aMesh ) noexcept
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aMesh.m_Positions )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aMesh.m_Positions;
|
2020-12-16 18:19:28 +00:00
|
|
|
aMesh.m_Positions = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aMesh.m_Normals )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aMesh.m_Normals;
|
2020-12-16 18:19:28 +00:00
|
|
|
aMesh.m_Normals = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aMesh.m_Texcoords )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aMesh.m_Texcoords;
|
2020-12-16 18:19:28 +00:00
|
|
|
aMesh.m_Texcoords = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aMesh.m_Color )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aMesh.m_Color;
|
2020-12-16 18:19:28 +00:00
|
|
|
aMesh.m_Color = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aMesh.m_FaceIdx )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aMesh.m_FaceIdx;
|
2020-12-16 18:19:28 +00:00
|
|
|
aMesh.m_FaceIdx = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aMesh.m_VertexSize = 0;
|
|
|
|
aMesh.m_FaceIdxSize = 0;
|
|
|
|
aMesh.m_MaterialIdx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D::FREE_S3DMODEL( S3DMODEL& aModel )
|
|
|
|
{
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aModel.m_Materials )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
delete [] aModel.m_Materials;
|
2020-12-16 18:19:28 +00:00
|
|
|
aModel.m_Materials = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aModel.m_MaterialsSize = 0;
|
|
|
|
|
2020-12-16 18:19:28 +00:00
|
|
|
if( nullptr != aModel.m_Meshes )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < aModel.m_MeshesSize; ++i )
|
|
|
|
FREE_SMESH( aModel.m_Meshes[i] );
|
|
|
|
|
|
|
|
delete [] aModel.m_Meshes;
|
2020-12-16 18:19:28 +00:00
|
|
|
aModel.m_Meshes = nullptr;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aModel.m_MeshesSize = 0;
|
|
|
|
}
|