KiCad PCB EDA Suite
sg_coords.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-2017 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 #include <iostream>
25 #include <sstream>
26 #include <wx/log.h>
27 
28 #include "3d_cache/sg/sg_coords.h"
29 #include "3d_cache/sg/sg_helpers.h"
30 #include "3d_cache/sg/sg_normals.h"
31 #include "3d_cache/sg/sg_faceset.h"
32 
33 
34 SGCOORDS::SGCOORDS( SGNODE* aParent ) : SGNODE( aParent )
35 {
37 
38  if( NULL != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
39  {
40  m_Parent = NULL;
41 
42 #ifdef DEBUG
43  std::ostringstream ostr;
44  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
45  ostr << " * [BUG] inappropriate parent to SGCOORDS (type ";
46  ostr << aParent->GetNodeType() << ")";
47  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
48 #endif
49  }
50  else if( NULL != aParent && S3D::SGTYPE_FACESET == aParent->GetNodeType() )
51  {
52  m_Parent->AddChildNode( this );
53  }
54 
55  return;
56 }
57 
58 
60 {
61  coords.clear();
62  return;
63 }
64 
65 
66 bool SGCOORDS::SetParent( SGNODE* aParent, bool notify )
67 {
68  if( NULL != m_Parent )
69  {
70  if( aParent == m_Parent )
71  return true;
72 
73  // handle the change in parents
74  if( notify )
75  m_Parent->unlinkChildNode( this );
76 
77  m_Parent = NULL;
78 
79  if( NULL == aParent )
80  return true;
81  }
82 
83  // only a SGFACESET may be parent to a SGCOORDS
84  if( NULL != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
85  return false;
86 
87  m_Parent = aParent;
88 
89  if( m_Parent )
90  m_Parent->AddChildNode( this );
91 
92  return true;
93 }
94 
95 
96 SGNODE* SGCOORDS::FindNode(const char *aNodeName, const SGNODE *aCaller)
97 {
98  if( NULL == aNodeName || 0 == aNodeName[0] )
99  return NULL;
100 
101  if( !m_Name.compare( aNodeName ) )
102  return this;
103 
104  return NULL;
105 }
106 
107 
108 void SGCOORDS::unlinkChildNode( const SGNODE* aCaller )
109 {
110  #ifdef DEBUG
111  std::ostringstream ostr;
112  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
113  ostr << " * [BUG] unexpected code branch; node should have no children or refs";
114  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
115  #endif
116 
117  return;
118 }
119 
120 
121 void SGCOORDS::unlinkRefNode( const SGNODE* aCaller )
122 {
123  #ifdef DEBUG
124  std::ostringstream ostr;
125  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
126  ostr << " * [BUG] unexpected code branch; node should have no children or refs";
127  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
128  #endif
129 
130  return;
131 }
132 
133 
135 {
136  #ifdef DEBUG
137  std::ostringstream ostr;
138  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
139  ostr << " * [BUG] this node does not accept children or refs";
140  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
141  #endif
142 
143  return false;
144 }
145 
146 
148 {
149  #ifdef DEBUG
150  std::ostringstream ostr;
151  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
152  ostr << " * [BUG] this node does not accept children or refs";
153  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
154  #endif
155 
156  return false;
157 }
158 
159 
160 bool SGCOORDS::GetCoordsList( size_t& aListSize, SGPOINT*& aCoordsList )
161 {
162  if( coords.empty() )
163  {
164  aListSize = 0;
165  aCoordsList = NULL;
166  return false;
167  }
168 
169  aListSize = coords.size();
170  aCoordsList = &coords[0];
171  return true;
172 }
173 
174 
175 void SGCOORDS::SetCoordsList( size_t aListSize, const SGPOINT* aCoordsList )
176 {
177  coords.clear();
178 
179  if( 0 == aListSize || NULL == aCoordsList )
180  return;
181 
182  for( size_t i = 0; i < aListSize; ++i )
183  coords.push_back( aCoordsList[i] );
184 
185  return;
186 }
187 
188 
189 void SGCOORDS::AddCoord( double aXValue, double aYValue, double aZValue )
190 {
191  coords.push_back( SGPOINT( aXValue, aYValue, aZValue ) );
192  return;
193 }
194 
195 
196 void SGCOORDS::AddCoord( const SGPOINT& aPoint )
197 {
198  coords.push_back( aPoint );
199  return;
200 }
201 
202 
204 {
205  m_written = false;
206 
207  // rename this node
208  m_Name.clear();
209  GetName();
210 }
211 
212 
213 bool SGCOORDS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
214 {
215  if( coords.empty() )
216  return false;
217 
218  if( aReuseFlag )
219  {
220  if( !m_written )
221  {
222  aFile << " coord DEF " << GetName() << " Coordinate { point [\n ";
223  m_written = true;
224  }
225  else
226  {
227  aFile << " coord USE " << GetName() << "\n";
228  return true;
229  }
230  }
231  else
232  {
233  aFile << " coord Coordinate { point [\n ";
234  }
235 
236  std::string tmp;
237  size_t n = coords.size();
238  bool nline = false;
239  SGPOINT pt;
240 
241  for( size_t i = 0; i < n; )
242  {
243  // ensure VRML output has 1U = 0.1 inch as per legacy kicad expectations
244  pt = coords[i];
245  pt.x /= 2.54;
246  pt.y /= 2.54;
247  pt.z /= 2.54;
248  S3D::FormatPoint( tmp, pt );
249  aFile << tmp ;
250  ++i;
251 
252  if( i < n )
253  {
254  aFile << ",";
255 
256  if( nline )
257  {
258  aFile << "\n ";
259  nline = false;
260  }
261  else
262  {
263  nline = true;
264  }
265 
266  }
267  }
268 
269  aFile << "] }\n";
270 
271  return true;
272 }
273 
274 
275 bool SGCOORDS::WriteCache( std::ostream& aFile, SGNODE* parentNode )
276 {
277  if( NULL == parentNode )
278  {
279  if( NULL == m_Parent )
280  {
281  #ifdef DEBUG
282  std::ostringstream ostr;
283  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
284  ostr << " * [BUG] corrupt data; m_aParent is NULL";
285  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
286  #endif
287 
288  return false;
289  }
290 
291  SGNODE* np = m_Parent;
292 
293  while( NULL != np->GetParent() )
294  np = np->GetParent();
295 
296  if( np->WriteCache( aFile, NULL ) )
297  {
298  m_written = true;
299  return true;
300  }
301 
302  return false;
303  }
304 
305  if( parentNode != m_Parent )
306  {
307  #ifdef DEBUG
308  std::ostringstream ostr;
309  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
310  ostr << " * [BUG] corrupt data; parentNode != m_aParent";
311  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
312  #endif
313 
314  return false;
315  }
316 
317  if( !aFile.good() )
318  {
319  #ifdef DEBUG
320  std::ostringstream ostr;
321  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
322  ostr << " * [INFO] bad stream";
323  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
324  #endif
325 
326  return false;
327  }
328 
329  aFile << "[" << GetName() << "]";
330  size_t npts = coords.size();
331  aFile.write( (char*)&npts, sizeof(size_t) );
332 
333  for( size_t i = 0; i < npts; ++i )
334  S3D::WritePoint( aFile, coords[i] );
335 
336  if( aFile.fail() )
337  return false;
338 
339  m_written = true;
340  return true;
341 }
342 
343 
344 bool SGCOORDS::ReadCache( std::istream& aFile, SGNODE* parentNode )
345 {
346  if( !coords.empty() )
347  {
348  #ifdef DEBUG
349  std::ostringstream ostr;
350  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
351  ostr << " * [BUG] non-empty node";
352  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
353  #endif
354 
355  return false;
356  }
357 
358  size_t npts;
359  aFile.read( (char*)&npts, sizeof(size_t) );
360  SGPOINT tmp;
361 
362  if( aFile.fail() )
363  return false;
364 
365  for( size_t i = 0; i < npts; ++i )
366  {
367  if( !S3D::ReadPoint( aFile, tmp ) || aFile.fail() )
368  return false;
369 
370  coords.push_back( tmp );
371  }
372 
373  return true;
374 }
375 
376 
377 bool SGCOORDS::CalcNormals( SGFACESET* callingNode, SGNODE** aPtr )
378 {
379  if( aPtr )
380  *aPtr = NULL;
381 
382  if( NULL == m_Parent || NULL == callingNode )
383  return false;
384 
385  // the parent and all references must have indices; collect all
386  // indices into one std::vector<>
387  std::vector< int > ilist;
388  SGNORMALS* np = NULL;
389 
390  if( callingNode == m_Parent )
391  {
392  ((SGFACESET*)m_Parent)->GatherCoordIndices( ilist );
393 
394  std::list< SGNODE* >::iterator sB = m_BackPointers.begin();
395  std::list< SGNODE* >::iterator eB = m_BackPointers.end();
396 
397  while( sB != eB )
398  {
399  SGFACESET* fp = (SGFACESET*)(*sB);
400  fp->GatherCoordIndices( ilist );
401  ++sB;
402  }
403 
404  np = ((SGFACESET*)m_Parent)->m_Normals;
405 
406  if( !np )
407  np = new SGNORMALS( m_Parent );
408 
409  }
410  else
411  {
412  callingNode->GatherCoordIndices( ilist );
413  np = callingNode->m_Normals;
414 
415  if( !np )
416  np = new SGNORMALS( callingNode );
417 
418  }
419 
420  if( S3D::CalcTriangleNormals( coords, ilist, np->norms ) )
421  {
422  if( aPtr )
423  *aPtr = np;
424 
425  return true;
426  }
427 
428  delete np;
429 
430  return false;
431 }
defines a vertex coordinate set for a scenegraph object
void SetCoordsList(size_t aListSize, const SGPOINT *aCoordsList)
Definition: sg_coords.cpp:175
double x
Definition: sg_base.h:70
void ReNameNodes(void) override
Function ReNameNodes renames a node and all its child nodes in preparation for Write() operations...
Definition: sg_coords.cpp:203
virtual ~SGCOORDS()
Definition: sg_coords.cpp:59
SGNODE * m_Parent
Definition: sg_node.h:83
void FormatPoint(std::string &result, const SGPOINT &point)
Definition: sg_helpers.cpp:108
SGCOORDS(SGNODE *aParent)
Definition: sg_coords.cpp:34
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Function WriteVRML writes this node's data to a VRML file; this includes all data of child and refere...
Definition: sg_coords.cpp:213
bool CalcTriangleNormals(std::vector< SGPOINT > coords, std::vector< int > &index, std::vector< SGVECTOR > &norms)
Definition: sg_helpers.cpp:384
bool AddRefNode(SGNODE *aNode) override
Definition: sg_coords.cpp:134
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Function ReadCache Reads binary format data from a cache file.
Definition: sg_coords.cpp:344
defines a set of vertex normals for a scene graph object
SGNODE * FindNode(const char *aNodeName, const SGNODE *aCaller) override
Function FindNode searches the tree of linked nodes and returns a reference to the first node found w...
Definition: sg_coords.cpp:96
double y
Definition: sg_base.h:71
bool GetCoordsList(size_t &aListSize, SGPOINT *&aCoordsList)
Definition: sg_coords.cpp:160
Class SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
S3D::SGTYPES GetNodeType(void) const
Function GetNodeType returns the type of this node instance.
Definition: sg_node.cpp:108
const char * GetName(void)
Definition: sg_node.cpp:150
void unlinkChildNode(const SGNODE *aNode) override
Function unlinkChild removes references to an owned child; it is invoked by the child upon destructio...
Definition: sg_coords.cpp:108
bool m_written
Definition: sg_node.h:86
SGNODE * GetParent(void) const
Function GetParent returns a pointer to the parent SGNODE of this object or NULL if the object has no...
Definition: sg_node.cpp:114
virtual void unlinkChildNode(const SGNODE *aNode)=0
Function unlinkChild removes references to an owned child; it is invoked by the child upon destructio...
std::vector< SGVECTOR > norms
Definition: sg_normals.h:38
bool AddChildNode(SGNODE *aNode) override
Definition: sg_coords.cpp:147
bool WritePoint(std::ostream &aFile, const SGPOINT &aPoint)
Definition: sg_helpers.cpp:165
defines an indexed face set for a scenegraph
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Function SetParent sets the parent SGNODE of this object.
Definition: sg_coords.cpp:66
std::string m_Name
Definition: sg_node.h:85
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Function WriteCache write's this node's data to a binary cache file; the data includes all data of ch...
S3D::SGTYPES m_SGtype
Definition: sg_node.h:84
bool ReadPoint(std::istream &aFile, SGPOINT &aPoint)
Definition: sg_helpers.cpp:296
void GatherCoordIndices(std::vector< int > &aIndexList)
Function GatherCoordIndices adds all internal coordinate indices to the given list in preparation for...
void unlinkRefNode(const SGNODE *aNode) override
Function unlinkRef removes pointers to a referenced node; it is invoked by the referenced node upon d...
Definition: sg_coords.cpp:121
std::vector< SGPOINT > coords
Definition: sg_coords.h:40
double z
Definition: sg_base.h:72
bool CalcNormals(SGFACESET *callingNode, SGNODE **aPtr=NULL)
Function CalcNormals calculates normals for this coordinate list and sets the normals list in the par...
Definition: sg_coords.cpp:377
virtual bool AddChildNode(SGNODE *aNode)=0
std::list< SGNODE * > m_BackPointers
Definition: sg_node.h:82
SGNORMALS * m_Normals
Definition: sg_faceset.h:56
defines a number of macro functions to aid in repetitious code which is probably best expressed as a ...
void AddCoord(double aXValue, double aYValue, double aZValue)
Definition: sg_coords.cpp:189
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Function WriteCache write's this node's data to a binary cache file; the data includes all data of ch...
Definition: sg_coords.cpp:275