KiCad PCB EDA Suite
sg_node.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015 Cirilo Bernardo <[email protected]>
5 * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <algorithm>
26#include <cmath>
27#include <cstring>
28#include <iostream>
29#include <sstream>
30#include <wx/log.h>
31
32#include "3d_cache/sg/sg_node.h"
34
35
36static const std::string node_names[S3D::SGTYPE_END + 1] = {
37 "TXFM",
38 "APP",
39 "COL",
40 "COLIDX",
41 "FACE",
42 "COORD",
43 "COORDIDX",
44 "NORM",
45 "SHAPE",
46 "INVALID"
47};
48
49
50static unsigned int node_counts[S3D::SGTYPE_END] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
51
52
53char const* S3D::GetNodeTypeName( S3D::SGTYPES aType ) noexcept
54{
55 return node_names[aType].c_str();
56}
57
58
59static void getNodeName( S3D::SGTYPES nodeType, std::string& aName )
60{
61 if( nodeType < 0 || nodeType >= S3D::SGTYPE_END )
62 {
64 return;
65 }
66
67 unsigned int seqNum = node_counts[nodeType];
68 ++node_counts[nodeType];
69
70 std::ostringstream ostr;
71 ostr << node_names[nodeType] << "_" << seqNum;
72 aName = ostr.str();
73}
74
75
77{
78 m_Parent = aParent;
79 m_Association = nullptr;
80 m_written = false;
82}
83
84
86{
87 if( m_Parent )
89
90 if( m_Association )
91 *m_Association = nullptr;
92
93 std::list< SGNODE* >::iterator sBP = m_BackPointers.begin();
94 std::list< SGNODE* >::iterator eBP = m_BackPointers.end();
95
96 while( sBP != eBP )
97 {
98 (*sBP)->unlinkRefNode( this );
99 ++sBP;
100 }
101}
102
103
104S3D::SGTYPES SGNODE::GetNodeType( void ) const noexcept
105{
106 return m_SGtype;
107}
108
109
110SGNODE* SGNODE::GetParent( void ) const noexcept
111{
112 return m_Parent;
113}
114
115
116bool SGNODE::SwapParent( SGNODE* aNewParent )
117{
118 if( aNewParent == m_Parent )
119 return true;
120
121 if( nullptr == aNewParent )
122 return false;
123
124 if( nullptr == m_Parent )
125 {
126 if( aNewParent->AddChildNode( this ) )
127 return true;
128
129 return false;
130 }
131
132 if( aNewParent->GetNodeType() != m_Parent->GetNodeType() )
133 return false;
134
135 SGNODE* oldParent = m_Parent;
136 m_Parent->unlinkChildNode( this );
137 m_Parent = nullptr;
138 aNewParent->unlinkRefNode( this );
139 aNewParent->AddChildNode( this );
140 oldParent->AddRefNode( this );
141
142 return true;
143}
144
145
146const char* SGNODE::GetName( void )
147{
148 if( m_Name.empty() )
150
151 return m_Name.c_str();
152}
153
154
155void SGNODE::SetName( const char* aName )
156{
157 if( nullptr == aName || 0 == aName[0] )
159 else
160 m_Name = aName;
161}
162
163
164const char* SGNODE::GetNodeTypeName( S3D::SGTYPES aNodeType ) const noexcept
165{
166 return node_names[aNodeType].c_str();
167}
168
169
171{
172 if( nullptr == aNode )
173 return;
174
175 std::list< SGNODE* >::iterator np =
176 std::find( m_BackPointers.begin(), m_BackPointers.end(), aNode );
177
178 if( np != m_BackPointers.end() )
179 return;
180
181 m_BackPointers.push_back( aNode );
182}
183
184
185void SGNODE::delNodeRef( const SGNODE* aNode )
186{
187 if( nullptr == aNode )
188 return;
189
190 std::list< SGNODE* >::iterator np =
191 std::find( m_BackPointers.begin(), m_BackPointers.end(), aNode );
192
193 if( np != m_BackPointers.end() )
194 {
195 m_BackPointers.erase( np );
196 return;
197 }
198
199 wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [BUG] delNodeRef() did not find its target, this "
200 "node type %d, referenced node type %d" ),
201 __FILE__, __FUNCTION__, __LINE__,
202 m_SGtype,
203 aNode->GetNodeType() );
204}
205
206
207void SGNODE::AssociateWrapper( SGNODE** aWrapperRef ) noexcept
208{
209 wxCHECK( aWrapperRef && *aWrapperRef == this, /* void */ );
210
211 // if there is an existing association then break it and emit a warning
212 // just in case the behavior is undesired
213 if( m_Association )
214 {
215 *m_Association = nullptr;
216
217 wxLogTrace( MASK_3D_SG, wxT( "%s:%s:%d * [WARNING] association being broken with "
218 "previous wrapper" ),
219 __FILE__, __FUNCTION__, __LINE__ );
220 }
221
222 m_Association = aWrapperRef;
223}
224
225void SGNODE::DisassociateWrapper( SGNODE** aWrapperRef ) noexcept
226{
227 if( !m_Association )
228 return;
229
230 wxCHECK( aWrapperRef, /* void */ );
231
232 wxCHECK( *aWrapperRef == *m_Association && aWrapperRef == m_Association, /* void */ );
233
234 m_Association = nullptr;
235}
236
237
238void SGNODE::ResetNodeIndex( void ) noexcept
239{
240 for( int i = 0; i < (int)S3D::SGTYPE_END; ++i )
241 node_counts[i] = 1;
242}
243
244
245bool S3D::GetMatIndex( MATLIST& aList, SGNODE* aNode, int& aIndex )
246{
247 aIndex = 0;
248
249 wxCHECK( aNode && S3D::SGTYPE_APPEARANCE == aNode->GetNodeType(), false );
250
251 SGAPPEARANCE* node = (SGAPPEARANCE*)aNode;
252
253 std::map< SGAPPEARANCE const*, int >::iterator it = aList.matmap.find( node );
254
255 if( it != aList.matmap.end() )
256 {
257 aIndex = it->second;
258 return true;
259 }
260
261 int idx = (int)aList.matorder.size();
262 aList.matorder.push_back( node );
263 aList.matmap.emplace( node, idx );
264 aIndex = idx;
265
266 return true;
267}
268
269
271{
272 aMaterial = {};
273}
274
275
276void S3D::INIT_SMESH( SMESH& aMesh ) noexcept
277{
278 aMesh = {};
279}
280
281
282void S3D::INIT_S3DMODEL( S3DMODEL& aModel ) noexcept
283{
284 aModel = {};
285}
286
287
288void S3D::FREE_SMESH( SMESH& aMesh ) noexcept
289{
290 if( nullptr != aMesh.m_Positions )
291 {
292 delete [] aMesh.m_Positions;
293 aMesh.m_Positions = nullptr;
294 }
295
296 if( nullptr != aMesh.m_Normals )
297 {
298 delete [] aMesh.m_Normals;
299 aMesh.m_Normals = nullptr;
300 }
301
302 if( nullptr != aMesh.m_Texcoords )
303 {
304 delete [] aMesh.m_Texcoords;
305 aMesh.m_Texcoords = nullptr;
306 }
307
308 if( nullptr != aMesh.m_Color )
309 {
310 delete [] aMesh.m_Color;
311 aMesh.m_Color = nullptr;
312 }
313
314 if( nullptr != aMesh.m_FaceIdx )
315 {
316 delete [] aMesh.m_FaceIdx;
317 aMesh.m_FaceIdx = nullptr;
318 }
319
320 aMesh.m_VertexSize = 0;
321 aMesh.m_FaceIdxSize = 0;
322 aMesh.m_MaterialIdx = 0;
323}
324
325
327{
328 if( nullptr != aModel.m_Materials )
329 {
330 delete [] aModel.m_Materials;
331 aModel.m_Materials = nullptr;
332 }
333
334 aModel.m_MaterialsSize = 0;
335
336 if( nullptr != aModel.m_Meshes )
337 {
338 for( unsigned int i = 0; i < aModel.m_MeshesSize; ++i )
339 FREE_SMESH( aModel.m_Meshes[i] );
340
341 delete [] aModel.m_Meshes;
342 aModel.m_Meshes = nullptr;
343 }
344
345 aModel.m_MeshesSize = 0;
346}
define an internal structure to be used by the 3D renders
Defines the generic material appearance of a scenegraph object.
Definition: sg_appearance.h:38
The base class of all Scene Graph nodes.
Definition: sg_node.h:75
void SetName(const char *aName)
Definition: sg_node.cpp:155
void ResetNodeIndex(void) noexcept
Reset the global SG* node indices in preparation for write operations.
Definition: sg_node.cpp:238
virtual bool AddRefNode(SGNODE *aNode)=0
const char * GetName(void)
Definition: sg_node.cpp:146
SGNODE * GetParent(void) const noexcept
Returns a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
Definition: sg_node.cpp:110
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104
virtual bool AddChildNode(SGNODE *aNode)=0
virtual ~SGNODE()
Definition: sg_node.cpp:85
SGNODE ** m_Association
Handle to the instance held by a wrapper.
Definition: sg_node.h:233
void AssociateWrapper(SGNODE **aWrapperRef) noexcept
Associate this object with a handle to itself.
Definition: sg_node.cpp:207
std::list< SGNODE * > m_BackPointers
nodes which hold a reference to this.
Definition: sg_node.h:226
const char * GetNodeTypeName(S3D::SGTYPES aNodeType) const noexcept
Definition: sg_node.cpp:164
SGNODE * m_Parent
Pointer to parent node; may be NULL for top level transform.
Definition: sg_node.h:227
SGNODE(SGNODE *aParent)
Definition: sg_node.cpp:76
std::string m_Name
name to use for referencing the entity by name.
Definition: sg_node.h:229
void addNodeRef(SGNODE *aNode)
Add a pointer to a node which references this node, but does not own.
Definition: sg_node.cpp:170
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
void DisassociateWrapper(SGNODE **aWrapperRef) noexcept
Remove the association between an IFSG* wrapper object and this object.
Definition: sg_node.cpp:225
void delNodeRef(const SGNODE *aNode)
Remove a pointer to a node which references this node, but does not own.
Definition: sg_node.cpp:185
bool m_written
Set to true when the object has been written after a ReNameNodes().
Definition: sg_node.h:230
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
bool SwapParent(SGNODE *aNewParent)
Swap the ownership with the given parent.
Definition: sg_node.cpp:116
virtual void unlinkRefNode(const SGNODE *aNode)=0
Remove pointers to a referenced node.
void INIT_SMESH(SMESH &aMesh) noexcept
Definition: sg_node.cpp:276
char const * GetNodeTypeName(S3D::SGTYPES aType) noexcept
Return the name of the given type of node.
Definition: sg_node.cpp:53
bool GetMatIndex(MATLIST &aList, SGNODE *aNode, int &aIndex)
Definition: sg_node.cpp:245
void INIT_SMATERIAL(SMATERIAL &aMaterial)
Definition: sg_node.cpp:270
SGTYPES
Definition: sg_types.h:35
@ SGTYPE_APPEARANCE
Definition: sg_types.h:37
@ SGTYPE_END
Definition: sg_types.h:45
void FREE_SMESH(SMESH &aMesh) noexcept
Definition: sg_node.cpp:288
void INIT_S3DMODEL(S3DMODEL &aModel) noexcept
Definition: sg_node.cpp:282
void FREE_S3DMODEL(S3DMODEL &aModel)
Definition: sg_node.cpp:326
static const std::string node_names[S3D::SGTYPE_END+1]
Definition: sg_node.cpp:36
static unsigned int node_counts[S3D::SGTYPE_END]
Definition: sg_node.cpp:50
static void getNodeName(S3D::SGTYPES nodeType, std::string &aName)
Definition: sg_node.cpp:59
Store the a model based on meshes and materials.
Definition: c3dmodel.h:91
SMATERIAL * m_Materials
The materials list of this model.
Definition: c3dmodel.h:96
unsigned int m_MeshesSize
Number of meshes in the array.
Definition: c3dmodel.h:92
SMESH * m_Meshes
The meshes list of this model.
Definition: c3dmodel.h:93
unsigned int m_MaterialsSize
Number of materials in the material array.
Definition: c3dmodel.h:95
std::vector< SGAPPEARANCE const * > matorder
Definition: sg_node.h:56
std::map< SGAPPEARANCE const *, int > matmap
Definition: sg_node.h:57
Per-vertex normal/color/texcoors structure.
Definition: c3dmodel.h:77