KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml2_transform.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#include <iostream>
22#include <sstream>
23#include <cmath>
24#include <wx/log.h>
25
26#include "vrml2_base.h"
27#include "vrml2_transform.h"
29
30
35
36
38{
40 m_Parent = aParent;
41
42 if( nullptr != m_Parent )
43 m_Parent->AddChildNode( this );
44}
45
46
48{
49 wxLogTrace( traceVrmlPlugin,
50 wxT( " * [INFO] Destroying Transform node with %zu children, %zu"
51 "references, and %zu back pointers." ),
52 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
53}
54
55
57{
58 // a Transform node is never dangling
59 return false;
60}
61
62
63bool WRL2TRANSFORM::Read( WRLPROC& proc, WRL2BASE* aTopNode )
64{
65 /*
66 * Structure of a Transform node (p.120):
67 *
68 * Transform {
69 * eventIn MFNode addChildren
70 * eventIn MFNode removeChildren
71 * exposedField SFVec3f center 0 0 0
72 * exposedField MFNode children []
73 * exposedField SFRotation rotation 0 0 1 0
74 * exposedField SFVec3f scale 1 1 1
75 * exposedField SFRotation scaleOrientation 0 0 1 0
76 * exposedField SFVec3f translation 0 0 0
77 * field SFVec3f bboxCenter 0 0 0
78 * field SFVec3f bboxSize 0 0 0
79 * }
80 */
81
82 wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
83
84 center.x = 0.0;
85 center.y = 0.0;
86 center.z = 0.0;
87
91
92 rotation.x = 0.0;
93 rotation.y = 0.0;
94 rotation.z = 1.0;
95 rotation.w = 0.0;
96
98
99 scale.x = 1.0;
100 scale.y = 1.0;
101 scale.z = 1.0;
102
103 size_t line, column;
104 proc.GetFilePosData( line, column );
105
106 char tok = proc.Peek();
107
108 if( proc.eof() )
109 {
110 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
111 " * [INFO] bad file format; unexpected eof %s." ),
112 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
113
114 return false;
115 }
116
117 if( '{' != tok )
118 {
119 wxLogTrace( traceVrmlPlugin,
120 wxT( "%s:%s:%d\n"
121 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
122 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
123
124 return false;
125 }
126
127 proc.Pop();
128 std::string glob;
129
130 while( true )
131 {
132 if( proc.Peek() == '}' )
133 {
134 proc.Pop();
135 break;
136 }
137
138 if( !proc.ReadName( glob ) )
139 {
140 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
141 "%s" ),
142 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
143
144 return false;
145 }
146
147 // expecting one of:
148 // center
149 // children
150 // rotation
151 // scale
152 // ScaleOrientation
153 // translation
154 if( !glob.compare( "center" ) )
155 {
156 if( !proc.ReadSFVec3f( center ) )
157 {
158 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
159 " * [INFO] invalid center %s\n"
160 " * [INFO] file: '%s'\n"
161 "%s" ),
162 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
163 proc.GetFileName(), proc.GetError() );
164
165 return false;
166 }
167
168 // Convert from 1 VRML Unit = 0.1 inch to 1 VRML Unit = 1 mm
169 // Only apply if using legacy unit conversion mode
170 if( aTopNode->GetApplyUnitConversion() )
171 {
172 center.x *= 2.54f;
173 center.y *= 2.54f;
174 center.z *= 2.54f;
175 }
176 }
177 else if( !glob.compare( "rotation" ) )
178 {
179 if( !proc.ReadSFRotation( rotation ) )
180 {
181 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
182 " * [INFO] invalid rotation %s\n"
183 " * [INFO] file: '%s'\n"
184 "%s" ),
185 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
186 proc.GetFileName(), proc.GetError() );
187
188 return false;
189 }
190 }
191 else if( !glob.compare( "scale" ) )
192 {
193 if( !proc.ReadSFVec3f( scale ) )
194 {
195 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
196 " * [INFO] invalid scale %s\n"
197 " * [INFO] file: '%s'\n"
198 "%s" ),
199 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
200 proc.GetFileName(), proc.GetError() );
201
202 return false;
203 }
204
205 // If this is a top-level transform (parent is WRL2_BASE) with a non-unity scale,
206 // disable unit conversion. This handles PCBnew-exported VRML which includes a
207 // top-level scale factor, meaning coordinates are already properly scaled.
208 if( m_Parent != nullptr && m_Parent->GetNodeType() == WRL2NODES::WRL2_BASE
209 && HasNonUnityScale() )
210 {
211 aTopNode->SetApplyUnitConversion( false );
212
213 wxLogTrace( traceVrmlPlugin,
214 wxT( " * [INFO] Detected top-level scale in VRML file, "
215 "disabling unit conversion." ) );
216 }
217 }
218 else if( !glob.compare( "scaleOrientation" ) )
219 {
220 if( !proc.ReadSFRotation( scaleOrientation ) )
221 {
222 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
223 " * [INFO] invalid scaleOrientation %s\n"
224 " * [INFO] file: '%s'\n"
225 "%s" ),
226 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
227 proc.GetFileName(), proc.GetError() );
228
229 return false;
230 }
231 }
232 else if( !glob.compare( "translation" ) )
233 {
234 if( !proc.ReadSFVec3f( translation ) )
235 {
236 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
237 " * [INFO] invalid translation %s\n"
238 " * [INFO] file: '%s'\n"
239 "%s" ),
240 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
241 proc.GetFileName(), proc.GetError() );
242
243 return false;
244 }
245
246 // Convert from 1 VRML Unit = 0.1 inch to 1 VRML Unit = 1 mm
247 // Only apply if using legacy unit conversion mode
248 if( aTopNode->GetApplyUnitConversion() )
249 {
250 translation.x *= 2.54f;
251 translation.y *= 2.54f;
252 translation.z *= 2.54f;
253 }
254 }
255 else if( !glob.compare( "children" ) )
256 {
257 if( !readChildren( proc, aTopNode ) )
258 return false;
259 }
260 else
261 {
262 wxLogTrace( traceVrmlPlugin,
263 wxT( "%s:%s:%d\n"
264 " * [INFO] invalid Transform %s.\n"
265 " * [INFO] file: '%s'\n" ),
266 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
267 proc.GetFileName() );
268
269 return false;
270 }
271 } // while( true ) -- reading contents of Transform{}
272
273 return true;
274}
275
276
278{
279 // Check if any scale component differs from 1.0 by more than a small tolerance
280 const float tolerance = 0.001f;
281
282 return ( std::fabs( scale.x - 1.0f ) > tolerance )
283 || ( std::fabs( scale.y - 1.0f ) > tolerance )
284 || ( std::fabs( scale.z - 1.0f ) > tolerance );
285}
286
287
289{
290 wxCHECK_MSG( aNode, false, wxT( "Invalid node." ) );
291
292 // take possession if the node is dangling WRL2_SHAPE
293
294 if( WRL2NODES::WRL2_SHAPE == aNode->GetNodeType() && aNode->isDangling() )
295 {
296 WRL2NODE* np = aNode->GetParent();
297
298 if( nullptr != np )
299 aNode->SetParent( this );
300
301
302 if( !WRL2NODE::AddChildNode( aNode ) )
303 {
304 aNode->SetParent( nullptr );
305 return false;
306 }
307 }
308
309 if( !WRL2NODE::AddRefNode( aNode ) )
310 return false;
311
312 return true;
313}
314
315
317{
318 char tok = proc.Peek();
319
320 if( proc.eof() )
321 {
322 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
323 " * [INFO] bad file format; unexpected eof %s." ),
324 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
325
326 return false;
327 }
328
329 if( '[' != tok )
330 {
331 // since there are no delimiters we expect a single child
332 if( !aTopNode->ReadNode( proc, this, nullptr ) )
333 return false;
334
335 if( proc.Peek() == ',' )
336 proc.Pop();
337
338 return true;
339 }
340
341 proc.Pop();
342
343 while( true )
344 {
345 if( proc.Peek() == ']' )
346 {
347 proc.Pop();
348 break;
349 }
350
351 if( !aTopNode->ReadNode( proc, this, nullptr ) )
352 return false;
353
354 if( proc.Peek() == ',' )
355 proc.Pop();
356 }
357
358 return true;
359}
360
361
363{
364 wxLogTrace( traceVrmlPlugin,
365 wxT( " * [INFO] Translating Switch with %zu children, %zu references, and"
366 "%zu back pointers." ),
367 m_Children.size(), m_Refs.size(), m_BackPointers.size() );
368
369 if( m_Children.empty() && m_Refs.empty() )
370 return nullptr;
371
372 S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
373
374 wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
375 wxString::Format( wxT( "Transform does not have a Transform parent (parent "
376 "ID: %d)." ), ptype ) );
377
378 if( m_sgNode )
379 {
380 if( nullptr != aParent )
381 {
382 if( nullptr == S3D::GetSGNodeParent( m_sgNode )
383 && !S3D::AddSGNodeChild( aParent, m_sgNode ) )
384 {
385 return nullptr;
386 }
387 else if( aParent != S3D::GetSGNodeParent( m_sgNode )
388 && !S3D::AddSGNodeRef( aParent, m_sgNode ) )
389 {
390 return nullptr;
391 }
392 }
393
394 return m_sgNode;
395 }
396
397 IFSG_TRANSFORM txNode( aParent );
398
399 std::list< WRL2NODE* >::iterator sC = m_Children.begin();
400 std::list< WRL2NODE* >::iterator eC = m_Children.end();
401 WRL2NODES type;
402
403 // Include only the following in a Transform node:
404 // Shape
405 // Switch
406 // Transform
407 // Inline
408 bool test = false; // set to true if there are any subnodes for display
409
410 for( int i = 0; i < 2; ++i )
411 {
412 while( sC != eC )
413 {
414 type = (*sC)->GetNodeType();
415
416 switch( type )
417 {
422
423 if( nullptr != (*sC)->TranslateToSG( txNode.GetRawPtr() ) )
424 test = true;
425
426 break;
427
428 default:
429 break;
430 }
431
432 ++ sC;
433 }
434
435 sC = m_Refs.begin();
436 eC = m_Refs.end();
437 }
438
439 if( false == test )
440 {
441 txNode.Destroy();
442 return nullptr;
443 }
444
445 txNode.SetScale( SGPOINT( scale.x, scale.y, scale.z ) );
446 txNode.SetCenter( SGPOINT( center.x, center.y, center.z ) );
450 txNode.SetRotation( SGVECTOR( rotation.x, rotation.y, rotation.z), rotation.w );
451
452 m_sgNode = txNode.GetRawPtr();
453
454 return m_sgNode;
455}
SGNODE * GetRawPtr(void) noexcept
Return the raw internal SGNODE pointer.
Definition ifsg_node.cpp:61
void Destroy(void)
Delete the object held by this wrapper.
Definition ifsg_node.cpp:51
The wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH.
bool SetTranslation(const SGPOINT &aTranslation) noexcept
bool SetScaleOrientation(const SGVECTOR &aScaleAxis, double aAngle)
bool SetCenter(const SGPOINT &aCenter) noexcept
bool SetRotation(const SGVECTOR &aRotationAxis, double aAngle)
bool SetScale(const SGPOINT &aScale) noexcept
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)
void SetApplyUnitConversion(bool apply)
bool GetApplyUnitConversion(void) const
virtual bool isDangling(void)=0
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
SGNODE * m_sgNode
Definition vrml2_node.h:174
std::list< WRL2NODE * > m_BackPointers
Definition vrml2_node.h:169
WRL2NODE * GetParent(void) const
virtual bool SetParent(WRL2NODE *aParent, bool doUnlink=true)
Set the parent WRL2NODE of this object.
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)
WRL2NODES GetNodeType(void) const
std::list< WRL2NODE * > m_Refs
Definition vrml2_node.h:171
virtual bool AddChildNode(WRL2NODE *aNode)
WRLROTATION scaleOrientation
WRLVEC3F translation
virtual ~WRL2TRANSFORM()
bool HasNonUnityScale() const
bool readChildren(WRLPROC &proc, WRL2BASE *aTopNode)
WRLVEC3F bboxCenter
WRLROTATION rotation
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
bool AddRefNode(WRL2NODE *aNode) override
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
void Pop(void)
Definition wrlproc.cpp:2031
char Peek(void)
Definition wrlproc.cpp:2003
std::string GetFileName(void)
Definition wrlproc.cpp:1991
bool GetFilePosData(size_t &line, size_t &column)
Definition wrlproc.cpp:1962
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
bool ReadSFRotation(WRLROTATION &aSFRotation)
Definition wrlproc.cpp:933
bool ReadSFVec3f(WRLVEC3F &aSFVec3f)
Definition wrlproc.cpp:1078
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
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition ifsg_api.cpp:481
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition ifsg_api.cpp:490
SGTYPES
Definition sg_types.h:32
@ SGTYPE_TRANSFORM
Definition sg_types.h:33
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:508
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:499
WRL2NODES
Definition wrltypes.h:121
@ WRL2_TRANSFORM
Definition wrltypes.h:174
@ WRL2_INLINE
Definition wrltypes.h:147
@ WRL2_SWITCH
Definition wrltypes.h:168