KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml2_pointset.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) 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 <iostream>
23#include <sstream>
24#include <wx/log.h>
25
26#include "vrml2_base.h"
27#include "vrml2_pointset.h"
28#include "vrml2_coords.h"
29#include "vrml2_color.h"
31
32
38
39
41{
44 m_Parent = aParent;
45
46 if( nullptr != m_Parent )
47 m_Parent->AddChildNode( this );
48}
49
50
52{
53 wxLogTrace( traceVrmlPlugin,
54 wxT( " * [INFO] Destroying PointSet node with %zu children, %zu"
55 "references, and %zu back pointers." ),
56 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
57}
58
59
61{
62 color = nullptr;
63 coord = nullptr;
64}
65
66
68{
69 // nodes must be one of:
70 // Color
71 // Coordinate
72
73 switch( aType )
74 {
77 break;
78
79 default:
80 return false;
81 break;
82 }
83
84 return true;
85}
86
87
89{
90 // this node is dangling unless it has a parent of type WRL2_SHAPE
91 if( nullptr == m_Parent || m_Parent->GetNodeType() != WRL2NODES::WRL2_SHAPE )
92 return true;
93
94 return false;
95}
96
97
99{
100 wxCHECK_MSG( aNode, false, wxT( "Invalid node." ) );
101
102 WRL2NODES type = aNode->GetNodeType();
103
104 if( !checkNodeType( type ) )
105 {
106 wxLogTrace( traceVrmlPlugin,
107 wxT( "%s:%s:%d\n"
108 " * [INFO] bad file format; unexpected child node '%s'." ),
109 __FILE__, __FUNCTION__, __LINE__, aNode->GetNodeTypeName( type ) );
110
111 return false;
112 }
113
114 if( WRL2NODES::WRL2_COLOR == type )
115 {
116 if( nullptr != color )
117 {
118 wxLogTrace( traceVrmlPlugin,
119 wxT( "%s:%s:%d\n"
120 " * [INFO] bad file format; multiple color nodes." ),
121 __FILE__, __FUNCTION__, __LINE__ );
122
123 return false;
124 }
125
126 color = aNode;
127 return WRL2NODE::AddRefNode( aNode );
128 }
129
130 if( WRL2NODES::WRL2_COORDINATE == type )
131 {
132 if( nullptr != coord )
133 {
134 wxLogTrace( traceVrmlPlugin,
135 wxT( "%s:%s:%d\n"
136 " * [INFO] bad file format; multiple coord nodes." ),
137 __FILE__, __FUNCTION__, __LINE__ );
138
139 return false;
140 }
141
142 coord = aNode;
143 return WRL2NODE::AddRefNode( aNode );
144 }
145
146 return WRL2NODE::AddRefNode( aNode );
147}
148
149
151{
152 wxCHECK_MSG( aNode, false, wxT( "Invalid node." ) );
153
154 WRL2NODES type = aNode->GetNodeType();
155
156 if( !checkNodeType( type ) )
157 {
158 wxLogTrace( traceVrmlPlugin,
159 wxT( "%s:%s:%d\n"
160 " * [INFO] bad file format; unexpected child node '%s'." ),
161 __FILE__, __FUNCTION__, __LINE__, aNode->GetNodeTypeName( type ) );
162
163 return false;
164 }
165
166 if( WRL2NODES::WRL2_COLOR == type )
167 {
168 if( nullptr != color )
169 {
170 wxLogTrace( traceVrmlPlugin,
171 wxT( "%s:%s:%d\n"
172 " * [INFO] bad file format; multiple color nodes." ),
173 __FILE__, __FUNCTION__, __LINE__ );
174
175 return false;
176 }
177
178 color = aNode;
179 return WRL2NODE::AddChildNode( aNode );
180 }
181
182 if( WRL2NODES::WRL2_COORDINATE == type )
183 {
184 if( nullptr != coord )
185 {
186 wxLogTrace( traceVrmlPlugin,
187 wxT( "%s:%s:%d\n"
188 " * [INFO] bad file format; multiple coord nodes." ),
189 __FILE__, __FUNCTION__, __LINE__ );
190
191 return false;
192 }
193
194 coord = aNode;
195 return WRL2NODE::AddChildNode( aNode );
196 }
197
198 return WRL2NODE::AddChildNode( aNode );
199}
200
201
202
203bool WRL2POINTSET::Read( WRLPROC& proc, WRL2BASE* aTopNode )
204{
205 char tok = proc.Peek();
206
207 if( proc.eof() )
208 {
209 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
210 " * [INFO] bad file format; unexpected eof %s." ),
211 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
212
213 return false;
214 }
215
216 if( '{' != tok )
217 {
218 wxLogTrace( traceVrmlPlugin,
219 wxT( "%s:%s:%d\n"
220 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
221 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
222
223 return false;
224 }
225
226 proc.Pop();
227 std::string glob;
228
229 while( true )
230 {
231 if( proc.Peek() == '}' )
232 {
233 proc.Pop();
234 break;
235 }
236
237 if( !proc.ReadName( glob ) )
238 {
239 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
240 "%s" ),
241 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
242
243 return false;
244 }
245
246 // expecting one of:
247 // color
248 // coord
249 if( !glob.compare( "color" ) )
250 {
251 if( !aTopNode->ReadNode( proc, this, nullptr ) )
252 {
253 wxLogTrace( traceVrmlPlugin,
254 wxT( "%s:%s:%d\n"
255 " * [INFO] could not read color node information." ),
256 __FILE__, __FUNCTION__, __LINE__ );
257
258 return false;
259 }
260 }
261 else if( !glob.compare( "coord" ) )
262 {
263 if( !aTopNode->ReadNode( proc, this, nullptr ) )
264 {
265 wxLogTrace( traceVrmlPlugin,
266 wxT( "%s:%s:%d\n"
267 " * [INFO] could not read coord node information." ),
268 __FILE__, __FUNCTION__, __LINE__ );
269
270 return false;
271 }
272 }
273 else
274 {
275 wxLogTrace( traceVrmlPlugin,
276 wxT( "%s:%s:%d\n"
277 " * [INFO] invalid PointSet %s (no closing brace)\n"
278 " * [INFO] file: '%s'\n" ),
279 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
280 proc.GetFileName() );
281
282 return false;
283 }
284 } // while( true ) -- reading contents of PointSet{}
285
286 return true;
287}
288
289
291{
292 // note: there are no plans to support drawing of points
293 return nullptr;
294}
295
296
298{
299 if( nullptr == aNode )
300 return;
301
302 if( aNode->GetParent() == this )
303 {
304 if( aNode == color )
305 color = nullptr;
306 else if( aNode == coord )
307 coord = nullptr;
308 }
309
311}
312
313
315{
316 if( nullptr == aNode )
317 return;
318
319 if( aNode->GetParent() != this )
320 {
321 if( aNode == color )
322 color = nullptr;
323 else if( aNode == coord )
324 coord = nullptr;
325
326 }
327
329}
330
331
333{
334 if( nullptr == color )
335 return false;
336
337 return ( (WRL2COLOR*) color )->HasColors();
338}
The base class of all Scene Graph nodes.
Definition sg_node.h:71
The top node of a VRML2 model.
Definition vrml2_base.h:56
bool ReadNode(WRLPROC &proc, WRL2NODE *aParent, WRL2NODE **aNode)
std::list< WRL2NODE * > m_BackPointers
Definition vrml2_node.h:169
WRL2NODE * GetParent(void) const
virtual void unlinkChildNode(const WRL2NODE *aNode)
Remove references to an owned child.
WRL2NODE * m_Parent
Definition vrml2_node.h:165
std::list< WRL2NODE * > m_Children
Definition vrml2_node.h:170
WRL2NODES m_Type
Definition vrml2_node.h:166
virtual bool AddRefNode(WRL2NODE *aNode)
virtual void unlinkRefNode(const WRL2NODE *aNode)
Remove pointers to a referenced node.
const char * GetNodeTypeName(WRL2NODES aNodeType) const
WRL2NODES GetNodeType(void) const
std::list< WRL2NODE * > m_Refs
Definition vrml2_node.h:171
virtual bool AddChildNode(WRL2NODE *aNode)
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
bool HasColors(void)
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
WRL2NODE * coord
bool AddChildNode(WRL2NODE *aNode) override
void unlinkChildNode(const WRL2NODE *aNode) override
Remove references to an owned child.
WRL2NODE * color
void unlinkRefNode(const WRL2NODE *aNode) override
Remove pointers to a referenced node.
bool AddRefNode(WRL2NODE *aNode) override
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
bool checkNodeType(WRL2NODES aType)
void setDefaults(void)
virtual ~WRL2POINTSET()
void Pop(void)
Definition wrlproc.cpp:2031
char Peek(void)
Definition wrlproc.cpp:2003
std::string GetFileName(void)
Definition wrlproc.cpp:1991
std::string GetError(void)
Definition wrlproc.cpp:1956
bool eof(void)
Definition wrlproc.cpp:1950
bool ReadName(std::string &aName)
Definition wrlproc.cpp:285
std::string GetFilePosition() const
Definition wrlproc.cpp:1978
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:59
collects header files for all SG* wrappers and the API
WRL2NODES
Definition wrltypes.h:121
@ WRL2_COORDINATE
Definition wrltypes.h:134
@ WRL2_POINTSET
Definition wrltypes.h:158