KiCad PCB EDA Suite
sg_helpers.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 
26 #include <iomanip>
27 #include <iostream>
28 #include <map>
29 #include <sstream>
30 #include <utility>
31 #include <wx/log.h>
32 
33 #include "3d_cache/sg/sg_helpers.h"
34 #include "3d_cache/sg/sg_node.h"
35 
36 
37 void S3D::FormatFloat( std::string& result, double value )
38 {
39  if( value < 1e-8 && value > -1e-8 )
40  {
41  result = "0";
42  return;
43  }
44 
45  // note: many VRML implementations use float so we use the max.
46  // precision here of 8 digits.
47  std::ostringstream out;
48  out << std::setprecision( 8 ) << value;
49 
50  result = out.str();
51 
52  size_t p = result.find( '.' );
53 
54  // trim trailing 0 if appropriate
55  if( std::string::npos == p )
56  return;
57 
58  p = result.find_first_of( "eE" );
59 
60  if( std::string::npos == p )
61  {
62  while( '0' == *( result.rbegin() ) )
63  result.erase( result.size() - 1 );
64 
65  return;
66  }
67 
68  if( '0' != result.at( p - 1 ) )
69  return;
70 
71  // trim all 0 to the left of 'p'
72  std::string tmp = result.substr( p );
73  result = result.substr( 0, p );
74 
75  while( '0' == *( result.rbegin() ) )
76  result.erase( result.size() - 1 );
77 
78  result.append( tmp );
79 }
80 
81 
82 void S3D::FormatOrientation( std::string& result, const SGVECTOR& axis, double rotation )
83 {
84  double aX;
85  double aY;
86  double aZ;
87 
88  axis.GetVector( aX, aY, aZ );
89  FormatFloat( result, aX );
90  std::string tmp;
91  FormatFloat( tmp, aY );
92  result.append( " " );
93  result.append( tmp );
94  FormatFloat( tmp, aZ );
95  result.append( " " );
96  result.append( tmp );
97  FormatFloat( tmp, rotation );
98  result.append( " " );
99  result.append( tmp );
100 }
101 
102 
103 void S3D::FormatPoint( std::string& result, const SGPOINT& point )
104 {
105  FormatFloat( result, point.x );
106 
107  std::string tmp;
108  FormatFloat( tmp, point.y );
109  result.append( " " );
110  result.append( tmp );
111 
112  FormatFloat( tmp, point.z );
113  result.append( " " );
114  result.append( tmp );
115 }
116 
117 
118 void S3D::FormatVector( std::string& result, const SGVECTOR& aVector )
119 {
120  double X, Y, Z;
121  aVector.GetVector( X, Y, Z );
122  FormatFloat( result, X );
123 
124  std::string tmp;
125  FormatFloat( tmp, Y );
126  result.append( " " );
127  result.append( tmp );
128 
129  FormatFloat( tmp, Z );
130  result.append( " " );
131  result.append( tmp );
132 }
133 
134 
135 void S3D::FormatColor( std::string& result, const SGCOLOR& aColor )
136 {
137  float R, G, B;
138  aColor.GetColor( R, G, B );
139  FormatFloat( result, R );
140 
141  std::string tmp;
142  FormatFloat( tmp, G );
143  result.append( " " );
144  result.append( tmp );
145 
146  FormatFloat( tmp, B );
147  result.append( " " );
148  result.append( tmp );
149 }
150 
151 
152 bool S3D::WritePoint( std::ostream& aFile, const SGPOINT& aPoint )
153 {
154  aFile.write( (char*) &aPoint.x, sizeof( aPoint.x ) );
155  aFile.write( (char*) &aPoint.y, sizeof( aPoint.y ) );
156  aFile.write( (char*) &aPoint.z, sizeof( aPoint.z ) );
157 
158  if( aFile.fail() )
159  return false;
160 
161  return true;
162 }
163 
164 
165 bool S3D::WriteVector( std::ostream& aFile, const SGVECTOR& aVector )
166 {
167  double x, y, z;
168  aVector.GetVector( x, y, z );
169  aFile.write( (char*) &x, sizeof( double ) );
170  aFile.write( (char*) &y, sizeof( double ) );
171  aFile.write( (char*) &z, sizeof( double ) );
172 
173  if( aFile.fail() )
174  return false;
175 
176  return true;
177 }
178 
179 
180 bool S3D::WriteColor( std::ostream& aFile, const SGCOLOR& aColor )
181 {
182  float r, g, b;
183  aColor.GetColor( r, g, b );
184  aFile.write( (char*) &r, sizeof( float ) );
185  aFile.write( (char*) &g, sizeof( float ) );
186  aFile.write( (char*) &b, sizeof( float ) );
187 
188  if( aFile.fail() )
189  return false;
190 
191  return true;
192 }
193 
194 
195 S3D::SGTYPES S3D::ReadTag( std::istream& aFile, std::string& aName )
196 {
197  char schar;
198  aFile.get( schar );
199 
200  if( '[' != schar )
201  {
202  wxLogTrace( MASK_3D_SG,
203  "%s:%s:%d * [INFO] corrupt data; missing left bracket at position %d",
204  __FILE__, __FUNCTION__, __LINE__, static_cast<int>( aFile.tellg() ) );
205 
206  return S3D::SGTYPE_END;
207  }
208 
209  std::string name;
210  aFile.get( schar );
211 
212  while( ']' != schar && aFile.good() )
213  {
214  name.push_back( schar );
215  aFile.get( schar );
216  }
217 
218  if( schar != ']' )
219  {
220  wxLogTrace( MASK_3D_SG,
221  "%s:%s:%d * [INFO] corrupt data; could not find right bracket",
222  __FILE__, __FUNCTION__, __LINE__ );
223 
224  return S3D::SGTYPE_END;
225  }
226 
227  aName = name;
228  size_t upos = name.find( '_' );
229 
230  if( std::string::npos == upos )
231  {
232  wxLogTrace( MASK_3D_SG,
233  "%s:%s:%d * [INFO] corrupt data; no underscore in name '%s'",
234  __FILE__, __FUNCTION__, __LINE__, name );
235 
236  return S3D::SGTYPE_END;
237  }
238 
239  name = name.substr( 0, upos );
240  S3D::SGTYPES types[S3D::SGTYPE_END] = {
250  };
251 
252  for( int i = 0; i < S3D::SGTYPE_END; ++i )
253  {
254  if( !name.compare( S3D::GetNodeTypeName( types[i] ) ) )
255  return types[i];
256  }
257 
258  wxLogTrace( MASK_3D_SG,
259  "%s:%s:%d * [INFO] corrupt data; no node type matching '%s'",
260  __FILE__, __FUNCTION__, __LINE__, name );
261 
262  return S3D::SGTYPE_END;
263 }
264 
265 
266 bool S3D::ReadPoint( std::istream& aFile, SGPOINT& aPoint )
267 {
268  aFile.read( (char*) &aPoint.x, sizeof( aPoint.x ) );
269  aFile.read( (char*) &aPoint.y, sizeof( aPoint.y ) );
270  aFile.read( (char*) &aPoint.z, sizeof( aPoint.z ) );
271 
272  if( aFile.fail() )
273  return false;
274 
275  return true;
276 }
277 
278 
279 bool S3D::ReadVector( std::istream& aFile, SGVECTOR& aVector )
280 {
281  double x, y, z;
282  aFile.read( (char*) &x, sizeof( double ) );
283  aFile.read( (char*) &y, sizeof( double ) );
284  aFile.read( (char*) &z, sizeof( double ) );
285  aVector.SetVector( x, y, z );
286 
287  if( aFile.fail() )
288  return false;
289 
290  return true;
291 }
292 
293 
294 bool S3D::ReadColor( std::istream& aFile, SGCOLOR& aColor )
295 {
296  float r, g, b;
297  aFile.read( (char*) &r, sizeof( float ) );
298  aFile.read( (char*) &g, sizeof( float ) );
299  aFile.read( (char*) &b, sizeof( float ) );
300  aColor.SetColor( r, g, b );
301 
302  if( aFile.fail() )
303  return false;
304 
305  return true;
306 }
307 
308 
309 bool S3D::degenerate( glm::dvec3* pts ) noexcept
310 {
311  double dx, dy, dz;
312 
313  dx = pts[1].x - pts[0].x;
314  dy = pts[1].y - pts[0].y;
315  dz = pts[1].z - pts[0].z;
316 
317  if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
318  return true;
319 
320  dx = pts[2].x - pts[0].x;
321  dy = pts[2].y - pts[0].y;
322  dz = pts[2].z - pts[0].z;
323 
324  if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
325  return true;
326 
327  dx = pts[2].x - pts[1].x;
328  dy = pts[2].y - pts[1].y;
329  dz = pts[2].z - pts[1].z;
330 
331  if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
332  return true;
333 
334  return false;
335 }
336 
337 
338 static void calcTriad( glm::dvec3* pts, glm::dvec3& tri )
339 {
340  if( S3D::degenerate( pts ) )
341  {
342  // degenerate points should contribute nothing to the result
343  tri = glm::dvec3( 0.0, 0.0, 0.0 );
344  return;
345  }
346 
347  // normal * 2 * area
348  tri = glm::cross( pts[1] - pts[0], pts[2] - pts[0] );
349 }
350 
351 
352 bool S3D::CalcTriangleNormals( std::vector< SGPOINT > coords, std::vector< int >& index,
353  std::vector< SGVECTOR >& norms )
354 {
355  size_t vsize = coords.size();
356 
357  if( vsize < 3 )
358  {
359  wxLogTrace( MASK_3D_SG,
360  "%s:%s:%d * [INFO] invalid vertex set (fewer than 3 vertices)",
361  __FILE__, __FUNCTION__, __LINE__ );
362 
363  return false;
364  }
365 
366  size_t isize = index.size();
367 
368  if( 0 != isize % 3 || index.empty() )
369  {
370  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] invalid index set (not multiple of 3)",
371  __FILE__, __FUNCTION__, __LINE__ );
372 
373  return false;
374  }
375 
376  if( !norms.empty() )
377  {
378  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] normals set is not empty",
379  __FILE__, __FUNCTION__, __LINE__ );
380 
381  return false;
382  }
383 
384  std::map< int, std::list< glm::dvec3 > >vmap;
385 
386  int p1, p2, p3;
387 
388  // create the map of indices to facet sets
389  for( size_t i = 0; i < isize; )
390  {
391  p1 = index[i++];
392  p2 = index[i++];
393  p3 = index[i++];
394 
395  if( p1 < 0 || p1 >= (int)vsize || p2 < 0 || p2 >= (int)vsize || p3 < 0 || p3 >= (int)vsize )
396  {
397 #ifdef DEBUG
398  std::ostringstream ostr;
399  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
400  ostr << " * [INFO] invalid index set; index out of bounds";
401  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
402 #endif
403 
404  return false;
405  }
406 
407  glm::dvec3 tri;
408  glm::dvec3 trip[3];
409  trip[0] = glm::dvec3( coords[p1].x, coords[p1].y, coords[p1].z );
410  trip[1] = glm::dvec3( coords[p2].x, coords[p2].y, coords[p2].z );
411  trip[2] = glm::dvec3( coords[p3].x, coords[p3].y, coords[p3].z );
412  calcTriad( trip, tri );
413 
414  std::map< int, std::list< glm::dvec3 > >::iterator ip = vmap.find( p1 );
415 
416  if( ip != vmap.end() )
417  {
418  ip->second.push_back( tri );
419  }
420  else
421  {
422  vmap.insert( std::pair < int, std::list < glm::dvec3 > >
423  ( p1, std::list < glm::dvec3 >( 1, tri ) ) );
424  }
425 
426  ip = vmap.find( p2 );
427 
428  if( ip != vmap.end() )
429  {
430  ip->second.push_back( tri );
431  }
432  else
433  {
434  vmap.insert( std::pair < int, std::list < glm::dvec3 > >
435  ( p2, std::list < glm::dvec3 >( 1, tri ) ) );
436  }
437 
438  ip = vmap.find( p3 );
439 
440  if( ip != vmap.end() )
441  {
442  ip->second.push_back( tri );
443  }
444  else
445  {
446  vmap.insert( std::pair < int, std::list < glm::dvec3 > >
447  ( p3, std::list < glm::dvec3 >( 1, tri ) ) );
448  }
449  }
450 
451  std::map< int, std::list< glm::dvec3 > >::iterator sM = vmap.begin();
452  std::map< int, std::list< glm::dvec3 > >::iterator eM = vmap.end();
453  size_t idx = 0;
454 
455  while( sM != eM )
456  {
457  size_t item = sM->first;
458 
459  // assign any skipped coordinates a normal of (0,0,1)
460  while( item > idx )
461  {
462  norms.emplace_back( 0, 0, 1 );
463  ++idx;
464  }
465 
466  std::list< glm::dvec3 >::iterator sT = sM->second.begin();
467  std::list< glm::dvec3 >::iterator eT = sM->second.end();
468  glm::dvec3 norm( 0.0, 0.0, 0.0 );
469 
470  while( sT != eT )
471  {
472  norm += *sT;
473  ++sT;
474  }
475 
476  norms.emplace_back( norm.x, norm.y, norm.z );
477 
478  ++idx;
479  ++sM;
480  }
481 
482  if( norms.size() != coords.size() )
483  {
484  wxLogTrace( MASK_3D_SG,
485  "%s:%s:%d * [BUG] number of normals does not equal number of vertices",
486  __FILE__, __FUNCTION__, __LINE__ );
487 
488  return false;
489  }
490 
491  return true;
492 }
void FormatOrientation(std::string &result, const SGVECTOR &axis, double rotation)
Definition: sg_helpers.cpp:82
double x
Definition: sg_base.h:70
bool degenerate(glm::dvec3 *pts) noexcept
Definition: sg_helpers.cpp:309
void FormatPoint(std::string &result, const SGPOINT &point)
Definition: sg_helpers.cpp:103
#define G(x, y, z)
Definition: md5_hash.cpp:16
bool ReadColor(std::istream &aFile, SGCOLOR &aColor)
Definition: sg_helpers.cpp:294
void FormatColor(std::string &result, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:135
bool ReadVector(std::istream &aFile, SGVECTOR &aVector)
Definition: sg_helpers.cpp:279
bool CalcTriangleNormals(std::vector< SGPOINT > coords, std::vector< int > &index, std::vector< SGVECTOR > &norms)
Definition: sg_helpers.cpp:352
void SetVector(double aXVal, double aYVal, double aZVal)
Definition: sg_base.cpp:233
bool SetColor(float aRedVal, float aGreenVal, float aBlueVal)
Definition: sg_base.cpp:85
double y
Definition: sg_base.h:71
bool WriteColor(std::ostream &aFile, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:180
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const noexcept
Definition: sg_base.cpp:59
void FormatFloat(std::string &result, double value)
Definition: sg_helpers.cpp:37
void FormatVector(std::string &result, const SGVECTOR &aVector)
Definition: sg_helpers.cpp:118
S3D::SGTYPES ReadTag(std::istream &aFile, std::string &aName)
Read the text tag of a binary cache file which is the NodeTag and unique ID number combined.
Definition: sg_helpers.cpp:195
bool WriteVector(std::ostream &aFile, const SGVECTOR &aVector)
Definition: sg_helpers.cpp:165
bool WritePoint(std::ostream &aFile, const SGPOINT &aPoint)
Definition: sg_helpers.cpp:152
SGTYPES
Definition: sg_types.h:34
const char * name
Definition: DXF_plotter.cpp:59
bool ReadPoint(std::istream &aFile, SGPOINT &aPoint)
Definition: sg_helpers.cpp:266
static void calcTriad(glm::dvec3 *pts, glm::dvec3 &tri)
Definition: sg_helpers.cpp:338
double z
Definition: sg_base.h:72
char const * GetNodeTypeName(S3D::SGTYPES aType) noexcept
Return the name of the given type of node.
Definition: sg_node.cpp:53
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
void GetVector(double &aXVal, double &aYVal, double &aZVal) const noexcept
Definition: sg_base.cpp:225