KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml1_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-2016 Cirilo Bernardo <[email protected]>
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <set>
23#include <map>
24#include <utility>
25#include <iterator>
26#include <cctype>
27#include <iostream>
28#include <algorithm>
29#include <sstream>
30#include <wx/log.h>
31
32#include "vrml1_node.h"
33
34
35bool NAMEREGISTER::AddName( const std::string& aName, WRL1NODE* aNode )
36{
37 if( aName.empty() )
38 return false;
39
40 std::map< std::string, WRL1NODE* >::iterator ir = reg.find( aName );
41
42 if( ir != reg.end() )
43 reg.erase( ir );
44
45 reg.emplace( aName, aNode );
46
47 return true;
48}
49
50
51bool NAMEREGISTER::DelName( const std::string& aName, WRL1NODE* aNode )
52{
53 if( aName.empty() )
54 return false;
55
56 std::map< std::string, WRL1NODE* >::iterator ir = reg.find( aName );
57
58 if( ir != reg.end() && ir->second == aNode )
59 {
60 reg.erase( ir );
61 return true;
62 }
63
64 return false;
65}
66
67
68WRL1NODE* NAMEREGISTER::FindName( const std::string& aName )
69{
70 if( aName.empty() )
71 return nullptr;
72
73 std::map< std::string, WRL1NODE* >::iterator ir = reg.find( aName );
74
75 if( ir != reg.end() )
76 return ir->second;
77
78 return nullptr;
79}
80
81
82typedef std::pair< std::string, WRL1NODES > NODEITEM;
83typedef std::map< std::string, WRL1NODES > NODEMAP;
85
86
88{
89 m_sgNode = nullptr;
90 m_Parent = nullptr;
92 m_dictionary = aDictionary;
93
94 if( nodenames.empty() )
95 {
96 nodenames.emplace( NODEITEM( "AsciiText", WRL1NODES::WRL1_ASCIITEXT ) );
97 nodenames.emplace( NODEITEM( "Cone", WRL1NODES::WRL1_CONE ) );
98 nodenames.emplace( NODEITEM( "Coordinate3", WRL1NODES::WRL1_COORDINATE3 ) );
99 nodenames.emplace( NODEITEM( "Cube", WRL1NODES::WRL1_CUBE ) );
100 nodenames.emplace( NODEITEM( "Cylinder", WRL1NODES::WRL1_CYLINDER ) );
101 nodenames.emplace( NODEITEM( "DirectionalLight", WRL1NODES::WRL1_DIRECTIONALLIGHT ) );
102 nodenames.emplace( NODEITEM( "FontStyle", WRL1NODES::WRL1_FONTSTYLE ) );
103 nodenames.emplace( NODEITEM( "Group", WRL1NODES::WRL1_GROUP ) );
104 nodenames.emplace( NODEITEM( "IndexedFaceSet", WRL1NODES::WRL1_INDEXEDFACESET ) );
105 nodenames.emplace( NODEITEM( "IndexedLineSet", WRL1NODES::WRL1_INDEXEDLINESET ) );
106 nodenames.emplace( NODEITEM( "Info", WRL1NODES::WRL1_INFO ) );
107 nodenames.emplace( NODEITEM( "LOD", WRL1NODES::WRL1_LOD ) );
108 nodenames.emplace( NODEITEM( "Material", WRL1NODES::WRL1_MATERIAL ) );
109 nodenames.emplace( NODEITEM( "MaterialBinding", WRL1NODES::WRL1_MATERIALBINDING ) );
110 nodenames.emplace( NODEITEM( "MatrixTransform", WRL1NODES::WRL1_MATRIXTRANSFORM ) );
111 nodenames.emplace( NODEITEM( "Normal", WRL1NODES::WRL1_NORMAL ) );
112 nodenames.emplace( NODEITEM( "NormalBinding", WRL1NODES::WRL1_NORMALBINDING ) );
113 nodenames.emplace( NODEITEM( "OrthographicCamera", WRL1NODES::WRL1_ORTHOCAMERA ) );
114 nodenames.emplace( NODEITEM( "PerspectiveCamera", WRL1NODES::WRL1_PERSPECTIVECAMERA ) );
115 nodenames.emplace( NODEITEM( "PointLight", WRL1NODES::WRL1_POINTLIGHT ) );
116 nodenames.emplace( NODEITEM( "PointSet", WRL1NODES::WRL1_POINTSET ) );
117 nodenames.emplace( NODEITEM( "Rotation", WRL1NODES::WRL1_ROTATION ) );
118 nodenames.emplace( NODEITEM( "Scale", WRL1NODES::WRL1_SCALE ) );
119 nodenames.emplace( NODEITEM( "Separator", WRL1NODES::WRL1_SEPARATOR ) );
120 nodenames.emplace( NODEITEM( "ShapeHints", WRL1NODES::WRL1_SHAPEHINTS ) );
121 nodenames.emplace( NODEITEM( "Sphere", WRL1NODES::WRL1_SPHERE ) );
122 nodenames.emplace( NODEITEM( "SpotLight", WRL1NODES::WRL1_SPOTLIGHT ) );
123 nodenames.emplace( NODEITEM( "Switch", WRL1NODES::WRL1_SWITCH ) );
124 nodenames.emplace( NODEITEM( "Texture2", WRL1NODES::WRL1_TEXTURE2 ) );
125 nodenames.emplace( NODEITEM( "Testure2Transform", WRL1NODES::WRL1_TEXTURE2TRANSFORM ) );
126 nodenames.emplace( NODEITEM( "TextureCoordinate2", WRL1NODES::WRL1_TEXTURECOORDINATE2 ) );
127 nodenames.emplace( NODEITEM( "Transform", WRL1NODES::WRL1_TRANSFORM ) );
128 nodenames.emplace( NODEITEM( "Translation", WRL1NODES::WRL1_TRANSLATION ) );
129 nodenames.emplace( NODEITEM( "WWWAnchor", WRL1NODES::WRL1_WWWANCHOR ) );
130 nodenames.emplace( NODEITEM( "WWWInline", WRL1NODES::WRL1_WWWINLINE ) );
131 }
132}
133
134
136{
137 wxLogTrace( traceVrmlPlugin,
138 wxT( " * [INFO] ^^ Destroying Type %d with %lu children, %lu references, and %lu "
139 "back pointers." ),
140 m_Type, m_Children.size(), m_Refs.size(), m_BackPointers.size() );
141
142 m_Items.clear();
143
144 if( m_dictionary && !m_Name.empty() )
145 m_dictionary->DelName( m_Name, this );
146
147 if( m_Parent )
148 m_Parent->unlinkChildNode( this );
149
150 std::list< WRL1NODE* >::iterator sBP = m_BackPointers.begin();
151 std::list< WRL1NODE* >::iterator eBP = m_BackPointers.end();
152
153 while( sBP != eBP )
154 {
155 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO]%sType %d is unlinking ref #%d" ),
156 wxString( ' ', (size_t) std::distance( sBP, m_BackPointers.begin() ) * 2 ),
157 m_Type, std::distance( sBP, m_BackPointers.begin() ) );
158
159 (*sBP)->unlinkRefNode( this );
160
161 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO]%sType %d has unlinked ref #%d" ),
162 wxString( ' ', (size_t) std::distance( sBP, m_BackPointers.begin() ) * 2 ),
163 m_Type, std::distance( sBP, m_BackPointers.begin() ) );
164
165 ++sBP;
166 }
167
168 m_Refs.clear();
169
170 std::list< WRL1NODE* >::iterator sC = m_Children.begin();
171 std::list< WRL1NODE* >::iterator eC = m_Children.end();
172
173 while( sC != eC )
174 {
175 (*sC)->SetParent( nullptr, false );
176
177 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO]%sType %d has unlinked child #%d" ),
178 wxString( ' ', (size_t) std::distance( sC, m_Children.begin() ) * 2 ),
179 m_Type, std::distance( sC, m_Children.begin() ) );
180
181 delete *sC;
182
183 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO]%sType %d has deleted child #%d" ),
184 wxString( ' ', (size_t) std::distance( sC, m_Children.begin() ) * 2 ),
185 m_Type, std::distance( sC, m_Children.begin() ) );
186
187 ++sC;
188 }
189
190 m_Children.clear();
191}
192
193
195{
196 std::list< WRL1NODE* >::iterator sC = m_Children.begin();
197 std::list< WRL1NODE* >::iterator eC = m_Children.end();
198
199 while( sC != eC )
200 {
201 (*sC)->cancelDict();
202 ++sC;
203 }
204
205 if( m_Type == WRL1NODES::WRL1_BASE && nullptr != m_dictionary )
206 delete m_dictionary;
207
208 m_dictionary = nullptr;
209}
210
211
213{
214 // the parent node must never be added as a backpointer
215 if( aNode == m_Parent )
216 return;
217
218 std::list< WRL1NODE* >::iterator sR = m_BackPointers.begin();
219 std::list< WRL1NODE* >::iterator eR = m_BackPointers.end();
220
221 while( sR != eR )
222 {
223 if( *sR == aNode )
224 return;
225
226 ++sR;
227 }
228
229 m_BackPointers.push_back( aNode );
230}
231
232
234{
235 std::list< WRL1NODE* >::iterator np = std::find( m_BackPointers.begin(),
236 m_BackPointers.end(), aNode );
237
238 if( np != m_BackPointers.end() )
239 {
240 m_BackPointers.erase( np );
241 return;
242 }
243
244 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
245 " * [BUG] delNodeRef() did not find its target." ),
246 __FILE__, __FUNCTION__, __LINE__ );
247}
248
249
251{
252 return m_Type;
253}
254
255
257{
258 return m_Parent;
259}
260
261
262std::string WRL1NODE::GetName( void )
263{
264 return m_Name;
265}
266
267
268bool WRL1NODE::SetName( const std::string& aName )
269{
270 if( aName.empty() )
271 return false;
272
273 if( isdigit( aName[0] ) )
274 {
275 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
276 " * [INFO] invalid node name '%s' (begins with digit)" ),
277 __FILE__, __FUNCTION__, __LINE__, aName );
278
279 return false;
280 }
281
282 // The character '+' is not allowed in names as per the VRML1 specification;
283 // however many parsers accept them and many bad generators use them so the rules
284 // have been relaxed here.
285 #define BAD_CHARS1 "\"\'#,.\\[]{}\x00\x01\x02\x03\x04\x05\x06\x09\x0A\x0B\x0C\x0D\x0E\x0F"
286 #define BAD_CHARS2 "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
287
288 if( std::string::npos != aName.find_first_of( BAD_CHARS1 )
289 || std::string::npos != aName.find_first_of( BAD_CHARS2 ) )
290 {
291 wxLogTrace( traceVrmlPlugin,
292 wxT( "%s:%s:%d\n"
293 " * [INFO] invalid node name '%s' (contains invalid character)" ),
294 __FILE__, __FUNCTION__, __LINE__, aName );
295
296 return false;
297 }
298
299 m_Name = aName;
300
301 if( m_dictionary )
302 m_dictionary->AddName( aName, this );
303
304 return true;
305}
306
307
308const char* WRL1NODE::GetNodeTypeName( WRL1NODES aNodeType ) const
309{
310 if( aNodeType < WRL1NODES::WRL1_BASE || aNodeType >= WRL1NODES::WRL1_END )
311 return "*INVALID_TYPE*";
312
313 if( aNodeType == WRL1NODES::WRL1_BASE )
314 return "*VIRTUAL_BASE*";
315
316 NODEMAP::iterator it = nodenames.begin();
317 advance( it, ( static_cast<int>( aNodeType ) - static_cast<int>( WRL1NODES::WRL1_BEGIN ) ) );
318
319 return it->first.c_str();
320}
321
322
323WRL1NODES WRL1NODE::getNodeTypeID( const std::string& aNodeName )
324{
325 NODEMAP::iterator it = nodenames.find( aNodeName );
326
327 if( nodenames.end() != it )
328 return it->second;
329
331}
332
333
334size_t WRL1NODE::GetNItems( void ) const
335{
336 return m_Items.size();
337}
338
339
340std::string WRL1NODE::GetError( void )
341{
342 return m_error;
343}
344
345
346WRL1NODE* WRL1NODE::FindNode( const std::string& aNodeName )
347{
348 if( nullptr == m_dictionary )
349 return nullptr;
350
351 return m_dictionary->FindName( aNodeName );
352}
353
354
355bool WRL1NODE::SetParent( WRL1NODE* aParent, bool doUnlink )
356{
357 if( aParent == m_Parent )
358 return true;
359
360 if( nullptr != m_Parent && doUnlink )
361 m_Parent->unlinkChildNode( this );
362
363 m_Parent = aParent;
364
365 if( nullptr != m_Parent )
366 m_Parent->AddChildNode( this );
367
368 return true;
369}
370
371
373{
374 wxCHECK_MSG( aNode, false, wxT( "Invalid node pointer." ) );
375 wxCHECK_MSG( aNode->GetNodeType() != WRL1NODES::WRL1_BASE, false,
376 wxT( "Attempting to add a base node to another node." ) );
377
378 std::list< WRL1NODE* >::iterator sC = m_Children.begin();
379 std::list< WRL1NODE* >::iterator eC = m_Children.end();
380
381 while( sC != eC )
382 {
383 if( *sC == aNode )
384 return false;
385
386 ++sC;
387 }
388
389 m_Children.push_back( aNode );
390 addItem( aNode );
391
392 if( aNode->GetParent() != this )
393 aNode->SetParent( this );
394
395 return true;
396}
397
398
400{
401 wxCHECK_MSG( aNode, false, wxT( "Invalid node pointer." ) );
402 wxCHECK_MSG( aNode->GetNodeType() != WRL1NODES::WRL1_BASE, false,
403 wxT( "Attempt to add a base node reference to another base node" ) );
404
405 // note: the VRML1 spec does not prevent the reuse of a node at
406 // the same level; for example a Coordinate3 node can be recalled
407 // at any time to set the current coordinate set.
408 m_Refs.push_back( aNode );
409 aNode->addNodeRef( this );
410 addItem( aNode );
411
412 return true;
413}
414
415
417{
418 std::list< WRL1NODE* >::iterator sL = m_Children.begin();
419 std::list< WRL1NODE* >::iterator eL = m_Children.end();
420
421 while( sL != eL )
422 {
423 if( *sL == aNode )
424 {
425 m_Children.erase( sL );
426 delItem( aNode );
427 return;
428 }
429
430 ++sL;
431 }
432}
433
434
436{
437 std::list< WRL1NODE* >::iterator sL = m_Refs.begin();
438 std::list< WRL1NODE* >::iterator eL = m_Refs.end();
439
440 while( sL != eL )
441 {
442 if( *sL == aNode )
443 {
444 m_Refs.erase( sL );
445 delItem( aNode );
446 return;
447 }
448
449 ++sL;
450 }
451}
452
453
455{
456 m_Items.push_back( aNode );
457}
458
459
460void WRL1NODE::delItem( const WRL1NODE* aNode )
461{
462 std::list< WRL1NODE* >::iterator sL = m_Items.begin();
463 std::list< WRL1NODE* >::iterator eL = m_Items.end();
464
465 while( sL != eL )
466 {
467 if( *sL == aNode )
468 {
469 m_Items.erase( sL );
470 return;
471 }
472
473 ++sL;
474 }
475}
bool AddName(const std::string &aName, WRL1NODE *aNode)
WRL1NODE * FindName(const std::string &aName)
bool DelName(const std::string &aName, WRL1NODE *aNode)
std::map< std::string, WRL1NODE * > reg
Definition vrml1_node.h:51
The base class of all VRML1 nodes.
Definition vrml1_node.h:113
std::string m_Name
Definition vrml1_node.h:224
WRL1NODE * GetParent(void) const
Return a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
std::string GetError(void)
void delNodeRef(WRL1NODE *aNode)
Remove a pointer to a node which references, but does not own, this node.
WRL1NODE(NAMEREGISTER *aDictionary)
WRL1NODES m_Type
Definition vrml1_node.h:223
WRL1NODES getNodeTypeID(const std::string &aNodeName)
Return the ID based on the given aNodeName or WRL1_INVALID (WRL1_END) if no such node name exists.
std::list< WRL1NODE * > m_Items
Definition vrml1_node.h:229
NAMEREGISTER * m_dictionary
Definition vrml1_node.h:242
virtual WRL1NODE * FindNode(const std::string &aNodeName)
Search the tree of linked nodes and returns a reference to the current node with the given name.
void addNodeRef(WRL1NODE *aNode)
Add a pointer to a node which references, but does not own, this node.
void cancelDict(void)
virtual bool AddRefNode(WRL1NODE *aNode)
virtual bool AddChildNode(WRL1NODE *aNode)
virtual std::string GetName(void)
virtual bool SetParent(WRL1NODE *aParent, bool doUnlink=true)
Set the parent WRL1NODE of this object.
std::list< WRL1NODE * > m_BackPointers
Definition vrml1_node.h:226
virtual void unlinkChildNode(const WRL1NODE *aNode)
Remove references to an owned child; it is invoked by the child upon destruction to ensure that the p...
std::list< WRL1NODE * > m_Children
Definition vrml1_node.h:227
virtual ~WRL1NODE()
size_t GetNItems(void) const
std::string m_error
Definition vrml1_node.h:230
virtual bool SetName(const std::string &aName)
WRL1NODE * m_Parent
Definition vrml1_node.h:222
void addItem(WRL1NODE *aNode)
const char * GetNodeTypeName(WRL1NODES aNodeType) const
std::list< WRL1NODE * > m_Refs
Definition vrml1_node.h:228
virtual void unlinkRefNode(const WRL1NODE *aNode)
Remove pointers to a referenced node; it is invoked by the referenced node upon destruction to ensure...
SGNODE * m_sgNode
Definition vrml1_node.h:233
WRL1NODES GetNodeType(void) const
Return the type of this node instance.
void delItem(const WRL1NODE *aNode)
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:59
std::map< std::string, std::vector< SGNODE * > > NODEMAP
Definition loadmodel.cpp:91
std::pair< std::string, std::vector< SGNODE * > > NODEITEM
Definition loadmodel.cpp:92
#define BAD_CHARS2
#define BAD_CHARS1
static NODEMAP nodenames
WRL1NODES
Definition wrltypes.h:52
@ WRL1_INFO
Definition wrltypes.h:65
@ WRL1_NORMALBINDING
Definition wrltypes.h:71
@ WRL1_ASCIITEXT
Definition wrltypes.h:55
@ WRL1_FONTSTYLE
Definition wrltypes.h:61
@ WRL1_SEPARATOR
Definition wrltypes.h:78
@ WRL1_INDEXEDFACESET
Definition wrltypes.h:63
@ WRL1_PERSPECTIVECAMERA
Definition wrltypes.h:73
@ WRL1_DIRECTIONALLIGHT
Definition wrltypes.h:60
@ WRL1_WWWANCHOR
Definition wrltypes.h:88
@ WRL1_COORDINATE3
Definition wrltypes.h:57
@ WRL1_TRANSLATION
Definition wrltypes.h:87
@ WRL1_GROUP
Definition wrltypes.h:62
@ WRL1_BASE
Definition wrltypes.h:53
@ WRL1_TRANSFORM
Definition wrltypes.h:86
@ WRL1_BEGIN
Definition wrltypes.h:54
@ WRL1_MATERIALBINDING
Definition wrltypes.h:68
@ WRL1_INDEXEDLINESET
Definition wrltypes.h:64
@ WRL1_MATRIXTRANSFORM
Definition wrltypes.h:69
@ WRL1_MATERIAL
Definition wrltypes.h:67
@ WRL1_SPOTLIGHT
Definition wrltypes.h:81
@ WRL1_CONE
Definition wrltypes.h:56
@ WRL1_INVALID
Definition wrltypes.h:90
@ WRL1_POINTSET
Definition wrltypes.h:75
@ WRL1_NORMAL
Definition wrltypes.h:70
@ WRL1_TEXTURE2
Definition wrltypes.h:83
@ WRL1_ORTHOCAMERA
Definition wrltypes.h:72
@ WRL1_WWWINLINE
Definition wrltypes.h:89
@ WRL1_TEXTURECOORDINATE2
Definition wrltypes.h:85
@ WRL1_SHAPEHINTS
Definition wrltypes.h:79
@ WRL1_TEXTURE2TRANSFORM
Definition wrltypes.h:84
@ WRL1_CYLINDER
Definition wrltypes.h:59
@ WRL1_SWITCH
Definition wrltypes.h:82
@ WRL1_CUBE
Definition wrltypes.h:58
@ WRL1_POINTLIGHT
Definition wrltypes.h:74
@ WRL1_SPHERE
Definition wrltypes.h:80
@ WRL1_ROTATION
Definition wrltypes.h:76
@ WRL1_SCALE
Definition wrltypes.h:77