KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml1_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 <wx/log.h>
24
25#include "vrml1_base.h"
26#include "vrml1_transform.h"
28
29
34
35
37 WRL1NODE( aDictionary )
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
56bool WRL1TRANSFORM::Read( WRLPROC& proc, WRL1BASE* aTopNode )
57{
58 /*
59 * Structure of a Transform node:
60 *
61 * Transform {
62 * SFVec3f center 0 0 0
63 * SFRotation rotation 0 0 1 0
64 * SFVec3f scale 1 1 1
65 * SFRotation scaleOrientation 0 0 1 0
66 * SFVec3f translation 0 0 0
67 * }
68 */
69
70 wxCHECK_MSG( aTopNode, false, wxT( "Invalid top node." ) );
71
72 center.x = 0.0;
73 center.y = 0.0;
74 center.z = 0.0;
75
77
78 rotation.x = 0.0;
79 rotation.y = 0.0;
80 rotation.z = 1.0;
81 rotation.w = 0.0;
82
84
85 scale.x = 1.0;
86 scale.y = 1.0;
87 scale.z = 1.0;
88
89 char tok = proc.Peek();
90
91 if( proc.eof() )
92 {
93 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
94 " * [INFO] bad file format; unexpected eof %s." ),
95 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
96
97 return false;
98 }
99
100 if( '{' != tok )
101 {
102 wxLogTrace( traceVrmlPlugin,
103 wxT( "%s:%s:%d\n"
104 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
105 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
106
107 return false;
108 }
109
110 proc.Pop();
111 std::string glob;
112
113 while( true )
114 {
115 if( proc.Peek() == '}' )
116 {
117 proc.Pop();
118 break;
119 }
120
121 if( !proc.ReadName( glob ) )
122 {
123 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
124 "%s" ),
125 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
126
127 return false;
128 }
129
130 // expecting one of:
131 // center
132 // rotation
133 // scale
134 // ScaleOrientation
135 // translation
136
137 if( !glob.compare( "center" ) )
138 {
139 if( !proc.ReadSFVec3f( center ) )
140 {
141 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
142 " * [INFO] invalid center %s\n"
143 " * [INFO] file: '%s'\n"
144 "%s" ),
145 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
146 proc.GetFileName(), proc.GetError() );
147
148 return false;
149 }
150
151 // convert from 1 VRML Unit = 0.1 inch to 1 VRML Unit = 1 mm
152 center.x *= 2.54f;
153 center.y *= 2.54f;
154 center.z *= 2.54f;
155 }
156 else if( !glob.compare( "rotation" ) )
157 {
158 if( !proc.ReadSFRotation( rotation ) )
159 {
160 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
161 " * [INFO] invalid rotation %s\n"
162 " * [INFO] file: '%s'\n"
163 "%s" ),
164 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
165 proc.GetFileName(), proc.GetError() );
166
167 return false;
168 }
169 }
170 else if( !glob.compare( "scaleFactor" ) )
171 {
172 if( !proc.ReadSFVec3f( scale ) )
173 {
174 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
175 " * [INFO] invalid scale %s\n"
176 " * [INFO] file: '%s'\n"
177 "%s" ),
178 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
179 proc.GetFileName(), proc.GetError() );
180
181 return false;
182 }
183 }
184 else if( !glob.compare( "scaleOrientation" ) )
185 {
186 if( !proc.ReadSFRotation( scaleOrientation ) )
187 {
188 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
189 " * [INFO] invalid scaleOrientation %s\n"
190 " * [INFO] file: '%s'\n"
191 "%s" ),
192 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
193 proc.GetFileName(), proc.GetError() );
194
195 return false;
196 }
197 }
198 else if( !glob.compare( "translation" ) )
199 {
200 if( !proc.ReadSFVec3f( translation ) )
201 {
202 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
203 " * [INFO] invalid translation %s\n"
204 " * [INFO] file: '%s'\n"
205 "%s" ),
206 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
207 proc.GetFileName(), proc.GetError() );
208
209 return false;
210 }
211
212 // convert from 1 VRML Unit = 0.1 inch to 1 VRML Unit = 1 mm
213 translation.x *= 2.54f;
214 translation.y *= 2.54f;
215 translation.z *= 2.54f;
216 }
217 else
218 {
219 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
220 " * [INFO] invalid Transform %s\n"
221 " * [INFO] file: '%s'" ),
222 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
223 proc.GetFileName() );
224
225 return false;
226 }
227 } // while( true ) -- reading contents of Transform{}
228
229 return true;
230}
231
232
234{
235 // this node may not own or reference any other node
236 wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable" ) );
237}
238
239
241{
242 // this node may not own or reference any other node
243 wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
244}
245
246
248{
249 if( nullptr == m_Parent )
250 return nullptr;
251
252 if( WRL1NODES::WRL1_BASE == m_Parent->GetNodeType() )
253 return nullptr;
254
255 wxCHECK_MSG( sp, nullptr, wxT( "Bad model: no base data given" ) );
256
257 // rotation
258 float rX, rY, rZ, rW;
259 rX = rotation.x;
260 rY = rotation.y;
261 rZ = rotation.z;
262 rW = rotation.w;
263 glm::mat4 rM = glm::rotate( glm::mat4( 1.0f ), rW, glm::vec3( rX, rY, rZ ) );
264
265 // translation
266 float dX, dY, dZ;
267 dX = translation.x;
268 dY = translation.y;
269 dZ = translation.z;
270 glm::mat4 tM = glm::translate( glm::mat4( 1.0f ), glm::vec3( dX, dY, dZ ) );
271
272 // center
273 dX = center.x;
274 dY = center.y;
275 dZ = center.z;
276 glm::mat4 cM = glm::translate( glm::mat4( 1.0f ), glm::vec3( dX, dY, dZ ) );
277 glm::mat4 ncM = glm::translate( glm::mat4( 1.0f ), glm::vec3( -dX, -dY, -dZ ) );
278
279 // scale
280 glm::mat4 sM = glm::scale( glm::mat4( 1.0 ), glm::vec3( scale.x, scale.y, scale.z ) );
281
282 // scaleOrientation
283 rX = scaleOrientation.x;
284 rY = scaleOrientation.y;
285 rZ = scaleOrientation.z;
286 rW = scaleOrientation.w;
287 glm::mat4 srM = glm::rotate( glm::mat4( 1.0f ), rW, glm::vec3( rX, rY, rZ ) );
288 glm::mat4 nsrM = glm::rotate( glm::mat4( 1.0f ), -rW, glm::vec3( rX, rY, rZ ) );
289
290 // resultant transform:
291 // tx' = tM * cM * rM * srM * sM * nsrM * ncM
292 sp->txmatrix = sp->txmatrix * tM * cM * rM * srM * sM * nsrM * ncM;
293
294 return nullptr;
295}
The base class of all Scene Graph nodes.
Definition sg_node.h:71
Represent the top node of a VRML1 model.
Definition vrml1_base.h:42
WRL1NODE(NAMEREGISTER *aDictionary)
WRL1NODES m_Type
Definition vrml1_node.h:223
std::list< WRL1NODE * > m_BackPointers
Definition vrml1_node.h:226
std::list< WRL1NODE * > m_Children
Definition vrml1_node.h:227
WRL1NODE * m_Parent
Definition vrml1_node.h:222
std::list< WRL1NODE * > m_Refs
Definition vrml1_node.h:228
WRLROTATION rotation
WRL1TRANSFORM(NAMEREGISTER *aDictionary)
bool AddChildNode(WRL1NODE *aNode) override
WRLVEC3F translation
virtual ~WRL1TRANSFORM()
bool AddRefNode(WRL1NODE *aNode) override
WRLROTATION scaleOrientation
bool Read(WRLPROC &proc, WRL1BASE *aTopNode) override
SGNODE * TranslateToSG(SGNODE *aParent, WRL1STATUS *sp) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
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
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
glm::mat4 txmatrix
Definition vrml1_node.h:99
@ WRL1_BASE
Definition wrltypes.h:53
@ WRL1_TRANSFORM
Definition wrltypes.h:86