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  * Copyright (C) 2020 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 <iostream>
26 #include <sstream>
27 #include <wx/log.h>
28 
29 #include "3d_cache/sg/sg_coords.h"
30 #include "3d_cache/sg/sg_helpers.h"
31 #include "3d_cache/sg/sg_normals.h"
32 #include "3d_cache/sg/sg_faceset.h"
33 
34 
35 SGCOORDS::SGCOORDS( SGNODE* aParent ) : SGNODE( aParent )
36 {
38 
39  if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
40  {
41  m_Parent = nullptr;
42 
43  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [BUG] inappropriate parent to SGCOORDS (type %s)",
44  __FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
45  }
46  else if( nullptr != aParent && S3D::SGTYPE_FACESET == aParent->GetNodeType() )
47  {
48  m_Parent->AddChildNode( this );
49  }
50 }
51 
52 
54 {
55  coords.clear();
56 }
57 
58 
59 bool SGCOORDS::SetParent( SGNODE* aParent, bool notify )
60 {
61  if( nullptr != m_Parent )
62  {
63  if( aParent == m_Parent )
64  return true;
65 
66  // handle the change in parents
67  if( notify )
68  m_Parent->unlinkChildNode( this );
69 
70  m_Parent = nullptr;
71 
72  if( nullptr == aParent )
73  return true;
74  }
75 
76  // only a SGFACESET may be parent to a SGCOORDS
77  if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
78  return false;
79 
80  m_Parent = aParent;
81 
82  if( m_Parent )
83  m_Parent->AddChildNode( this );
84 
85  return true;
86 }
87 
88 
89 SGNODE* SGCOORDS::FindNode(const char *aNodeName, const SGNODE *aCaller) noexcept
90 {
91  if( nullptr == aNodeName || 0 == aNodeName[0] )
92  return nullptr;
93 
94  if( !m_Name.compare( aNodeName ) )
95  return this;
96 
97  return nullptr;
98 }
99 
100 
101 void SGCOORDS::unlinkChildNode( const SGNODE* aCaller ) noexcept
102 {
103  wxCHECK( aCaller, /* void */ );
104 }
105 
106 
107 void SGCOORDS::unlinkRefNode( const SGNODE* aCaller ) noexcept
108 {
109  wxCHECK( aCaller, /* void */ );
110 }
111 
112 
113 bool SGCOORDS::AddRefNode( SGNODE* aNode ) noexcept
114 {
115  wxCHECK( aNode, false );
116 
117  return false;
118 }
119 
120 
121 bool SGCOORDS::AddChildNode( SGNODE* aNode ) noexcept
122 {
123  wxCHECK( aNode, false );
124 
125  return false;
126 }
127 
128 
129 bool SGCOORDS::GetCoordsList( size_t& aListSize, SGPOINT*& aCoordsList )
130 {
131  if( coords.empty() )
132  {
133  aListSize = 0;
134  aCoordsList = nullptr;
135  return false;
136  }
137 
138  aListSize = coords.size();
139  aCoordsList = &coords[0];
140  return true;
141 }
142 
143 
144 void SGCOORDS::SetCoordsList( size_t aListSize, const SGPOINT* aCoordsList )
145 {
146  coords.clear();
147 
148  if( 0 == aListSize || nullptr == aCoordsList )
149  return;
150 
151  for( size_t i = 0; i < aListSize; ++i )
152  coords.push_back( aCoordsList[i] );
153 }
154 
155 
156 void SGCOORDS::AddCoord( double aXValue, double aYValue, double aZValue )
157 {
158  coords.emplace_back( aXValue, aYValue, aZValue );
159 }
160 
161 
162 void SGCOORDS::AddCoord( const SGPOINT& aPoint )
163 {
164  coords.push_back( aPoint );
165 }
166 
167 
169 {
170  m_written = false;
171 
172  // rename this node
173  m_Name.clear();
174  GetName();
175 }
176 
177 
178 bool SGCOORDS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
179 {
180  if( coords.empty() )
181  return false;
182 
183  if( aReuseFlag )
184  {
185  if( !m_written )
186  {
187  aFile << " coord DEF " << GetName() << " Coordinate { point [\n ";
188  m_written = true;
189  }
190  else
191  {
192  aFile << " coord USE " << GetName() << "\n";
193  return true;
194  }
195  }
196  else
197  {
198  aFile << " coord Coordinate { point [\n ";
199  }
200 
201  std::string tmp;
202  size_t n = coords.size();
203  bool nline = false;
204  SGPOINT pt;
205 
206  for( size_t i = 0; i < n; )
207  {
208  // ensure VRML output has 1U = 0.1 inch as per legacy kicad expectations
209  pt = coords[i];
210  pt.x /= 2.54;
211  pt.y /= 2.54;
212  pt.z /= 2.54;
213  S3D::FormatPoint( tmp, pt );
214  aFile << tmp ;
215  ++i;
216 
217  if( i < n )
218  {
219  aFile << ",";
220 
221  if( nline )
222  {
223  aFile << "\n ";
224  nline = false;
225  }
226  else
227  {
228  nline = true;
229  }
230 
231  }
232  }
233 
234  aFile << "] }\n";
235 
236  return true;
237 }
238 
239 
240 bool SGCOORDS::WriteCache( std::ostream& aFile, SGNODE* parentNode )
241 {
242  if( nullptr == parentNode )
243  {
244  wxCHECK( m_Parent, false );
245 
246  SGNODE* np = m_Parent;
247 
248  while( nullptr != np->GetParent() )
249  np = np->GetParent();
250 
251  if( np->WriteCache( aFile, nullptr ) )
252  {
253  m_written = true;
254  return true;
255  }
256 
257  return false;
258  }
259 
260  wxCHECK( parentNode == m_Parent, false );
261 
262  if( !aFile.good() )
263  {
264  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] bad stream", __FILE__, __FUNCTION__, __LINE__ );
265 
266  return false;
267  }
268 
269  aFile << "[" << GetName() << "]";
270  size_t npts = coords.size();
271  aFile.write( (char*)&npts, sizeof(size_t) );
272 
273  for( size_t i = 0; i < npts; ++i )
274  S3D::WritePoint( aFile, coords[i] );
275 
276  if( aFile.fail() )
277  return false;
278 
279  m_written = true;
280  return true;
281 }
282 
283 
284 bool SGCOORDS::ReadCache( std::istream& aFile, SGNODE* parentNode )
285 {
286  wxCHECK( coords.empty(), false );
287 
288  size_t npts;
289  aFile.read( (char*) &npts, sizeof( size_t ) );
290  SGPOINT tmp;
291 
292  if( aFile.fail() )
293  return false;
294 
295  for( size_t i = 0; i < npts; ++i )
296  {
297  if( !S3D::ReadPoint( aFile, tmp ) || aFile.fail() )
298  return false;
299 
300  coords.push_back( tmp );
301  }
302 
303  return true;
304 }
305 
306 
307 bool SGCOORDS::CalcNormals( SGFACESET* callingNode, SGNODE** aPtr )
308 {
309  if( aPtr )
310  *aPtr = nullptr;
311 
312  if( nullptr == m_Parent || nullptr == callingNode )
313  return false;
314 
315  // the parent and all references must have indices; collect all
316  // indices into one std::vector<>
317  std::vector< int > ilist;
318  SGNORMALS* np = nullptr;
319 
320  if( callingNode == m_Parent )
321  {
322  ((SGFACESET*)m_Parent)->GatherCoordIndices( ilist );
323 
324  std::list< SGNODE* >::iterator sB = m_BackPointers.begin();
325  std::list< SGNODE* >::iterator eB = m_BackPointers.end();
326 
327  while( sB != eB )
328  {
329  SGFACESET* fp = (SGFACESET*)(*sB);
330  fp->GatherCoordIndices( ilist );
331  ++sB;
332  }
333 
334  np = ( (SGFACESET*) m_Parent )->m_Normals;
335 
336  if( !np )
337  np = new SGNORMALS( m_Parent );
338 
339  }
340  else
341  {
342  callingNode->GatherCoordIndices( ilist );
343  np = callingNode->m_Normals;
344 
345  if( !np )
346  np = new SGNORMALS( callingNode );
347 
348  }
349 
350  if( S3D::CalcTriangleNormals( coords, ilist, np->norms ) )
351  {
352  if( aPtr )
353  *aPtr = np;
354 
355  return true;
356  }
357 
358  delete np;
359 
360  return false;
361 }
void unlinkRefNode(const SGNODE *aNode) noexcept override
Remove pointers to a referenced node.
Definition: sg_coords.cpp:107
Define an indexed face set for a scenegraph.
Definition: sg_faceset.h:46
void SetCoordsList(size_t aListSize, const SGPOINT *aCoordsList)
Definition: sg_coords.cpp:144
double x
Definition: sg_base.h:70
void ReNameNodes(void) override
Rename a node and all its child nodes in preparation for write operations.
Definition: sg_coords.cpp:168
virtual ~SGCOORDS()
Definition: sg_coords.cpp:53
SGNODE * m_Parent
Pointer to parent node; may be NULL for top level transform.
Definition: sg_node.h:227
void FormatPoint(std::string &result, const SGPOINT &point)
Definition: sg_helpers.cpp:103
SGCOORDS(SGNODE *aParent)
Definition: sg_coords.cpp:35
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Writes this node's data to a VRML file.
Definition: sg_coords.cpp:178
SGNODE * FindNode(const char *aNodeName, const SGNODE *aCaller) noexcept override
Search the tree of linked nodes and return a reference to the first node found with the given name.
Definition: sg_coords.cpp:89
bool CalcTriangleNormals(std::vector< SGPOINT > coords, std::vector< int > &index, std::vector< SGVECTOR > &norms)
Definition: sg_helpers.cpp:352
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Reads binary format data from a cache file.
Definition: sg_coords.cpp:284
double y
Definition: sg_base.h:71
bool GetCoordsList(size_t &aListSize, SGPOINT *&aCoordsList)
Definition: sg_coords.cpp:129
The base class of all Scene Graph nodes.
Definition: sg_node.h:74
Define a set of vertex normals for a scene graph object.
Definition: sg_normals.h:38
bool CalcNormals(SGFACESET *callingNode, SGNODE **aPtr=nullptr)
Calculate normals for this coordinate list and sets the normals list in the parent SGFACESET.
Definition: sg_coords.cpp:307
const char * GetName(void)
Definition: sg_node.cpp:146
bool m_written
Set to true when the object has been written after a ReNameNodes().
Definition: sg_node.h:230
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
void unlinkChildNode(const SGNODE *aNode) noexcept override
Remove references to an owned child.
Definition: sg_coords.cpp:101
std::vector< SGVECTOR > norms
Definition: sg_normals.h:64
bool WritePoint(std::ostream &aFile, const SGPOINT &aPoint)
Definition: sg_helpers.cpp:152
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Set the parent SGNODE of this object.
Definition: sg_coords.cpp:59
SGNODE * GetParent(void) const noexcept
Returns a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
Definition: sg_node.cpp:110
std::string m_Name
name to use for referencing the entity by name.
Definition: sg_node.h:229
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Write this node's data to a binary cache file.
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
bool AddRefNode(SGNODE *aNode) noexcept override
Definition: sg_coords.cpp:113
bool ReadPoint(std::istream &aFile, SGPOINT &aPoint)
Definition: sg_helpers.cpp:266
bool AddChildNode(SGNODE *aNode) noexcept override
Definition: sg_coords.cpp:121
void GatherCoordIndices(std::vector< int > &aIndexList)
Add all internal coordinate indices to the given list in preparation for a normals calculation.
Definition: sg_faceset.cpp:943
std::vector< SGPOINT > coords
Definition: sg_coords.h:72
double z
Definition: sg_base.h:72
virtual bool AddChildNode(SGNODE *aNode)=0
std::list< SGNODE * > m_BackPointers
nodes which hold a reference to this.
Definition: sg_node.h:226
SGNORMALS * m_Normals
Definition: sg_faceset.h:82
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
void AddCoord(double aXValue, double aYValue, double aZValue)
Definition: sg_coords.cpp:156
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Write this node's data to a binary cache file.
Definition: sg_coords.cpp:240