KiCad PCB EDA Suite
Loading...
Searching...
No Matches
x3d_ops.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#include <wx/tokenzr.h>
22#include <wx/xml/xml.h>
23
24#include "x3d_appearance.h"
25#include "x3d_coords.h"
26#include "x3d_ops.h"
27#include "x3d_transform.h"
28
29
30bool X3D::ReadTransform( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
31{
32 // note: we must have a parent or else we will have a memory leak
33 if( nullptr == aParent || nullptr == aNode )
34 return false;
35
36 wxXmlAttribute* prop;
37
38 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
39 {
40 const wxString& pname = prop->GetName();
41
42 if( pname == wxT( "USE" ) )
43 {
44 X3DNODE* np = aDict.FindName( prop->GetValue() );
45
46 if( nullptr == np )
47 return false;
48
49 if( !aParent->AddRefNode( np ) )
50 return false;
51
52 return true;
53 }
54 }
55
56 X3DNODE* node = new X3DTRANSFORM;
57
58 if( !node->Read( aNode, aParent, aDict ) )
59 {
60 delete node;
61 return false;
62 }
63
64 return true;
65}
66
67
68bool X3D::ReadSwitch( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
69{
70 // XXX - TO BE IMPLEMENTED
71 return false;
72}
73
74
75bool X3D::ReadShape( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
76{
77 // note: we must have a parent or else we will have a memory leak
78 if( nullptr == aParent || nullptr == aNode )
79 return false;
80
81 wxXmlAttribute* prop;
82
83 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
84 {
85 const wxString& pname = prop->GetName();
86
87 if( pname == wxT( "USE" ) )
88 {
89 X3DNODE* np = aDict.FindName( prop->GetValue() );
90
91 if( nullptr == np )
92 return false;
93
94 if( !aParent->AddRefNode( np ) )
95 return false;
96
97 return true;
98 }
99 }
100
101 X3DNODE* node = new X3DSHAPE;
102
103 if( !node->Read( aNode, aParent, aDict ) )
104 {
105 delete node;
106 return false;
107 }
108
109 return true;
110}
111
112
113bool X3D::ReadAppearance( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
114{
115 // note: we must have a parent or else we will have a memory leak
116 if( nullptr == aParent || nullptr == aNode )
117 return false;
118
119 wxXmlAttribute* prop;
120
121 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
122 {
123 const wxString& pname = prop->GetName();
124
125 if( pname == wxT( "USE" ) )
126 {
127 X3DNODE* np = aDict.FindName( prop->GetValue() );
128
129 if( nullptr == np )
130 return false;
131
132 if( !aParent->AddRefNode( np ) )
133 return false;
134
135 return true;
136 }
137 }
138
139 X3DNODE* node = new X3DAPP;
140
141 if( !node->Read( aNode, aParent, aDict ) )
142 {
143 delete node;
144 return false;
145 }
146
147 return true;
148}
149
150
151bool X3D::ReadIndexedFaceSet( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
152{
153 // note: we must have a parent or else we will have a memory leak
154 if( nullptr == aParent || nullptr == aNode )
155 return false;
156
157 wxXmlAttribute* prop;
158
159 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
160 {
161 const wxString& pname = prop->GetName();
162
163 if( pname == wxT( "USE" ) )
164 {
165 X3DNODE* np = aDict.FindName( prop->GetValue() );
166
167 if( nullptr == np )
168 return false;
169
170 if( !aParent->AddRefNode( np ) )
171 return false;
172
173 return true;
174 }
175 }
176
177 X3DNODE* node = new X3DIFACESET;
178
179 if( !node->Read( aNode, aParent, aDict ) )
180 {
181 delete node;
182 return false;
183 }
184
185 return true;
186}
187
188
189bool X3D::ReadCoordinates( wxXmlNode* aNode, X3DNODE* aParent, X3D_DICT& aDict )
190{
191 // note: we must have a parent or else we will have a memory leak
192 if( nullptr == aParent || nullptr == aNode )
193 return false;
194
195 wxXmlAttribute* prop;
196
197 for( prop = aNode->GetAttributes(); prop != nullptr; prop = prop->GetNext() )
198 {
199 const wxString& pname = prop->GetName();
200
201 if( pname == wxT( "USE" ) )
202 {
203 X3DNODE* np = aDict.FindName( prop->GetValue() );
204
205 if( nullptr == np )
206 return false;
207
208 if( !aParent->AddRefNode( np ) )
209 return false;
210
211 return true;
212 }
213 }
214
215 X3DNODE* node = new X3DCOORDS;
216
217 if( !node->Read( aNode, aParent, aDict ) )
218 {
219 delete node;
220 return false;
221 }
222
223 return true;
224}
225
226
227bool X3D::ParseSFBool( const wxString& aSource, bool& aResult )
228{
229 wxStringTokenizer tokens( aSource );
230 wxString val = tokens.GetNextToken();
231
232 if( val == wxT( "TRUE" ) || val == wxT( "1" ) )
233 {
234 aResult = true;
235 return true;
236 }
237
238 if( val == wxT( "FALSE" ) || val == wxT( "0" ) )
239 {
240 aResult = false;
241 return true;
242 }
243
244 return false;
245}
246
247
248bool X3D::ParseSFFloat( const wxString& aSource, float& aResult )
249{
250 wxStringTokenizer tokens( aSource );
251
252 double x = 0;
253 bool ret = tokens.GetNextToken().ToCDouble( &x );
254
255 aResult = x;
256 return ret;
257}
258
259
260bool X3D::ParseSFVec3( const wxString& aSource, WRLVEC3F& aResult )
261{
262 wxStringTokenizer tokens( aSource );
263
264 double x = 0;
265 double y = 0;
266 double z = 0;
267
268 bool ret = tokens.GetNextToken().ToCDouble( &x )
269 && tokens.GetNextToken().ToCDouble( &y )
270 && tokens.GetNextToken().ToCDouble( &z );
271
272 aResult.x = x;
273 aResult.y = y;
274 aResult.z = z;
275
276 return ret;
277}
278
279
280bool X3D::ParseSFRotation( const wxString& aSource, WRLROTATION& aResult )
281{
282 wxStringTokenizer tokens( aSource );
283
284 double x = 0;
285 double y = 0;
286 double z = 0;
287 double w = 0;
288
289 bool ret = tokens.GetNextToken().ToCDouble( &x )
290 && tokens.GetNextToken().ToCDouble( &y )
291 && tokens.GetNextToken().ToCDouble( &z )
292 && tokens.GetNextToken().ToCDouble( &w );
293
294 aResult.x = x;
295 aResult.y = y;
296 aResult.z = z;
297 aResult.w = w;
298
299 return ret;
300}
The base class of all X3D nodes.
Definition x3d_base.h:71
virtual bool Read(wxXmlNode *aNode, X3DNODE *aTopNode, X3D_DICT &aDict)=0
virtual bool AddRefNode(X3DNODE *aNode)=0
X3DNODE * FindName(const wxString &aName)
Definition x3d_base.cpp:64
bool ParseSFBool(const wxString &aSource, bool &aResult)
Definition x3d_ops.cpp:227
bool ParseSFRotation(const wxString &aSource, WRLROTATION &aResult)
Definition x3d_ops.cpp:280
bool ReadIndexedFaceSet(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:151
bool ParseSFVec3(const wxString &aSource, WRLVEC3F &aResult)
Definition x3d_ops.cpp:260
bool ReadSwitch(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:68
bool ReadShape(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:75
bool ReadTransform(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:30
bool ReadCoordinates(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:189
bool ParseSFFloat(const wxString &aSource, float &aResult)
Definition x3d_ops.cpp:248
bool ReadAppearance(wxXmlNode *aNode, X3DNODE *aParent, X3D_DICT &aDict)
Definition x3d_ops.cpp:113
glm::vec4 WRLROTATION
Definition wrltypes.h:185
glm::vec3 WRLVEC3F
Definition wrltypes.h:184