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