KiCad PCB EDA Suite
vrml2_inline.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  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #include <iostream>
26 #include <sstream>
27 #include <wx/log.h>
28 
29 #include "vrml2_inline.h"
30 #include "vrml2_base.h"
31 #include "plugins/3dapi/ifsg_all.h"
32 
33 
35 {
36  m_VRML2Base = NULL;
38  m_Parent = NULL;
39 
40  return;
41 }
42 
43 
45 {
46  m_VRML2Base = NULL;
48  m_Parent = aParent;
49 
50  if( NULL != m_Parent )
51  m_Parent->AddChildNode( this );
52 
53  return;
54 }
55 
56 
58 {
59  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 2 )
60  wxLogTrace( MASK_VRML, " * [INFO] Destroying Inline node\n" );
61  #endif
62 
63  return;
64 }
65 
66 
68 {
69  // this node is never dangling
70  return false;
71 }
72 
73 
74 bool WRL2INLINE::Read( WRLPROC& proc, WRL2BASE* aTopNode )
75 {
76  if( aTopNode == NULL || aTopNode->GetNodeType() != WRL2_BASE )
77  return false;
78 
79  m_VRML2Base = aTopNode;
80  size_t line, column;
81  proc.GetFilePosData( line, column );
82  char tok = proc.Peek();
83 
84  if( proc.eof() )
85  {
86  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
87  do {
88  std::ostringstream ostr;
89  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
90  ostr << " * [INFO] bad file format; unexpected eof at line ";
91  ostr << line << ", column " << column;
92  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
93  } while( 0 );
94  #endif
95 
96  return false;
97  }
98 
99  if( '{' != tok )
100  {
101  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
102  do {
103  std::ostringstream ostr;
104  ostr << proc.GetError() << "\n";
105  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
106  ostr << " * [INFO] bad file format; expecting '{' but got '" << tok;
107  ostr << "' at line " << line << ", column " << column;
108  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
109  } while( 0 );
110  #endif
111 
112  return false;
113  }
114 
115  proc.Pop();
116 
117  while( ( tok = proc.Peek() ) )
118  {
119  std::string glob;
120 
121  if( tok == '}' )
122  {
123  proc.Pop();
124  return true;
125  }
126 
127  if( !proc.ReadName( glob ) )
128  {
129  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
130  do {
131  std::ostringstream ostr;
132  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
133  ostr << proc.GetError();
134  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
135  } while( 0 );
136  #endif
137 
138  return false;
139  }
140 
141  proc.GetFilePosData( line, column );
142 
143  // expecting one of 'url', 'bboxCenter', 'bboxSize'
144  if( !glob.compare( "url" ) )
145  {
146  if( !proc.ReadMFString( url ) )
147  {
148  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
149  do {
150  std::ostringstream ostr;
151  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
152  ostr << " * [INFO] invalid url at line " << line << ", column ";
153  ostr << column << "\n";
154  ostr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
155  ostr << " * [INFO] message: '" << proc.GetError() << "'";
156  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
157  } while( 0 );
158  #endif
159 
160  return false;
161  }
162  }
163  else if( !glob.compare( "bboxCenter" ) )
164  {
165  if( !proc.ReadSFVec3f( bboxCenter ) )
166  {
167  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
168  do {
169  std::ostringstream ostr;
170  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
171  ostr << " * [INFO] invalid bboxCenter at line " << line << ", column ";
172  ostr << column << "\n";
173  ostr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
174  ostr << " * [INFO] message: '" << proc.GetError() << "'";
175  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
176  } while( 0 );
177  #endif
178 
179  return false;
180  }
181  }
182  else if( !glob.compare( "bboxSize" ) )
183  {
184  if( !proc.ReadSFVec3f( bboxSize ) )
185  {
186  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
187  do {
188  std::ostringstream ostr;
189  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
190  ostr << " * [INFO] invalid bboxSize at line " << line << ", column ";
191  ostr << column << "\n";
192  ostr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
193  ostr << " * [INFO] message: '" << proc.GetError() << "'";
194  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
195  } while( 0 );
196  #endif
197 
198  return false;
199  }
200  }
201  else
202  {
203  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
204  do {
205  std::ostringstream ostr;
206  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
207  ostr << " * [INFO] bad Inline at line " << line << ", column ";
208  ostr << column << "\n";
209  ostr << " * [INFO] file: '" << proc.GetFileName() << "'";
210  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
211  } while( 0 );
212  #endif
213 
214  return false;
215  }
216  }
217 
218  proc.GetFilePosData( line, column );
219 
220  #if defined( DEBUG_VRML2 ) && ( DEBUG_VRML2 > 1 )
221  do {
222  std::ostringstream ostr;
223  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
224  ostr << " * [INFO] bad Inline at line " << line << ", column ";
225  ostr << column << " (no closing brace)\n";
226  ostr << " * [INFO] file: '" << proc.GetFileName() << "'";
227  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
228  } while( 0 );
229  #endif
230 
231  return false;
232 }
233 
234 
236 {
237  // this node may not own or reference any other node
238 
239  #ifdef DEBUG_VRML2
240  do {
241  std::ostringstream ostr;
242  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
243  ostr << " * [BUG] AddRefNode is not applicable";
244  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
245  } while( 0 );
246  #endif
247 
248  return false;
249 }
250 
251 
253 {
254  // this node may not own or reference any other node
255 
256  #ifdef DEBUG_VRML2
257  do {
258  std::ostringstream ostr;
259  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
260  ostr << " * [BUG] AddChildNode is not applicable\n";
261  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
262  } while( 0 );
263  #endif
264 
265  return false;
266 }
267 
268 
270 {
271  if( NULL == aParent || NULL == m_VRML2Base )
272  return NULL;
273 
274  if( url.empty() )
275  return NULL;
276 
277  S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
278 
279  if( ptype != S3D::SGTYPE_TRANSFORM )
280  {
281  #ifdef DEBUG_VRML2
282  do {
283  std::ostringstream ostr;
284  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
285  ostr << " * [BUG] Inline does not have a Transform parent (parent ID: ";
286  ostr << ptype << ")";
287  wxLogTrace( MASK_VRML, "%s\n", ostr.str().c_str() );
288  } while( 0 );
289  #endif
290 
291  return NULL;
292  }
293 
294  SGNODE* np = m_VRML2Base->GetInlineData( url.front() );
295 
296  if( NULL == np )
297  return NULL;
298 
299  bool OK = false;
300 
301  if( NULL == S3D::GetSGNodeParent( np ) )
302  OK = S3D::AddSGNodeChild( aParent, np );
303  else
304  OK = S3D::AddSGNodeRef( aParent, np );
305 
306  if( !OK )
307  return NULL;
308 
309  return np;
310 }
virtual ~WRL2INLINE()
SGNODE * TranslateToSG(SGNODE *aParent) override
Function TranslateToSG produces a representation of the data using the intermediate scenegraph struct...
void Pop(void)
Definition: wrlproc.cpp:2007
WRL2NODE represents the base class of all VRML2 nodes.
Definition: vrml2_node.h:58
bool GetFilePosData(size_t &line, size_t &column)
Definition: wrlproc.cpp:1951
bool ReadSFVec3f(WRLVEC3F &aSFVec3f)
Definition: wrlproc.cpp:1065
#define MASK_VRML
Definition: wrltypes.h:37
bool AddRefNode(WRL2NODE *aNode) override
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition: ifsg_api.cpp:636
WRL2NODES m_Type
Definition: vrml2_node.h:62
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
collects header files for all SG* wrappers and the API
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:645
WRLVEC3F bboxSize
Definition: vrml2_inline.h:46
SGNODE * GetInlineData(const std::string &aName)
Definition: vrml2_base.cpp:109
bool isDangling(void) override
Function isDangling returns true if the object does not have a parent which is a logical container fo...
std::vector< std::string > url
Definition: vrml2_inline.h:44
#define NULL
WRL2NODE * m_Parent
Definition: vrml2_node.h:61
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition: ifsg_api.cpp:654
WRL2NODES GetNodeType(void) const
Function GetNodeType returns the type of this node instance.
Definition: vrml2_node.cpp:212
char Peek(void)
Definition: wrlproc.cpp:1979
std::string GetFileName(void)
Definition: wrlproc.cpp:1967
WRL2BASE represents the top node of a VRML2 model.
Definition: vrml2_base.h:59
SGTYPES
Definition: sg_types.h:34
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition: ifsg_api.cpp:627
virtual bool AddChildNode(WRL2NODE *aNode)
Definition: vrml2_node.cpp:377
bool ReadName(std::string &aName)
Definition: wrlproc.cpp:280
WRL2BASE * m_VRML2Base
Definition: vrml2_inline.h:43
WRLVEC3F bboxCenter
Definition: vrml2_inline.h:45
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
std::string GetError(void)
Definition: wrlproc.cpp:1945
bool eof(void)
Definition: wrlproc.cpp:1939
bool ReadMFString(std::vector< std::string > &aMFString)
Definition: wrlproc.cpp:1145
bool AddChildNode(WRL2NODE *aNode) override