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