39 if( value < 1e-8 && value > -1e-8 )
47 std::ostringstream out;
48 out << std::setprecision( 8 ) << value;
52 size_t p = result.find(
'.' );
55 if( std::string::npos == p )
58 p = result.find_first_of(
"eE" );
60 if( std::string::npos == p )
62 while(
'0' == *( result.rbegin() ) )
63 result.erase( result.size() - 1 );
68 if(
'0' != result.at( p - 1 ) )
72 std::string tmp = result.substr( p );
73 result = result.substr( 0, p );
75 while(
'0' == *( result.rbegin() ) )
76 result.erase( result.size() - 1 );
109 result.append(
" " );
110 result.append( tmp );
113 result.append(
" " );
114 result.append( tmp );
126 result.append(
" " );
127 result.append( tmp );
130 result.append(
" " );
131 result.append( tmp );
143 result.append(
" " );
144 result.append( tmp );
147 result.append(
" " );
148 result.append( tmp );
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 ) );
169 aFile.write( (
char*) &x,
sizeof(
double ) );
170 aFile.write( (
char*) &y,
sizeof(
double ) );
171 aFile.write( (
char*) &z,
sizeof(
double ) );
184 aFile.write( (
char*) &r,
sizeof(
float ) );
185 aFile.write( (
char*) &g,
sizeof(
float ) );
186 aFile.write( (
char*) &b,
sizeof(
float ) );
202 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; missing left bracket at "
204 __FILE__, __FUNCTION__, __LINE__,
205 static_cast<int>( aFile.tellg() ) );
213 while(
']' != schar && aFile.good() )
215 name.push_back( schar );
221 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; could not find right "
223 __FILE__, __FUNCTION__, __LINE__ );
229 size_t upos =
name.find(
'_' );
231 if( std::string::npos == upos )
233 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; no underscore in name '%s'" ),
234 __FILE__, __FUNCTION__, __LINE__,
259 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] corrupt data; no node type matching '%s'" ),
260 __FILE__, __FUNCTION__, __LINE__,
269 aFile.read( (
char*) &aPoint.
x,
sizeof( aPoint.
x ) );
270 aFile.read( (
char*) &aPoint.
y,
sizeof( aPoint.
y ) );
271 aFile.read( (
char*) &aPoint.
z,
sizeof( aPoint.
z ) );
283 aFile.read( (
char*) &x,
sizeof(
double ) );
284 aFile.read( (
char*) &y,
sizeof(
double ) );
285 aFile.read( (
char*) &z,
sizeof(
double ) );
298 aFile.read( (
char*) &r,
sizeof(
float ) );
299 aFile.read( (
char*) &g,
sizeof(
float ) );
300 aFile.read( (
char*) &b,
sizeof(
float ) );
314 dx = pts[1].x - pts[0].x;
315 dy = pts[1].y - pts[0].y;
316 dz = pts[1].z - pts[0].z;
318 if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
321 dx = pts[2].x - pts[0].x;
322 dy = pts[2].y - pts[0].y;
323 dz = pts[2].z - pts[0].z;
325 if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
328 dx = pts[2].x - pts[1].x;
329 dy = pts[2].y - pts[1].y;
330 dz = pts[2].z - pts[1].z;
332 if( ( dx*dx + dy*dy + dz*dz ) < 1e-15 )
339static void calcTriad( glm::dvec3* pts, glm::dvec3& tri )
344 tri = glm::dvec3( 0.0, 0.0, 0.0 );
349 tri = glm::cross( pts[1] - pts[0], pts[2] - pts[0] );
354 std::vector< SGVECTOR >& norms )
356 size_t vsize = coords.size();
360 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] invalid vertex set (fewer than 3 "
362 __FILE__, __FUNCTION__, __LINE__ );
367 size_t isize = index.size();
369 if( 0 != isize % 3 || index.empty() )
371 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] invalid index set (not multiple of 3)" ),
372 __FILE__, __FUNCTION__, __LINE__ );
379 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [INFO] normals set is not empty" ),
380 __FILE__, __FUNCTION__, __LINE__ );
385 std::map< int, std::list< glm::dvec3 > >vmap;
390 for(
size_t i = 0; i < isize; )
396 if( p1 < 0 || p1 >= (
int)vsize || p2 < 0 || p2 >= (
int)vsize || p3 < 0 || p3 >= (
int)vsize )
399 std::ostringstream ostr;
400 ostr << __FILE__ <<
": " << __FUNCTION__ <<
": " << __LINE__ <<
"\n";
401 ostr <<
" * [INFO] invalid index set; index out of bounds";
402 wxLogTrace( MASK_3D_SG, wxT(
"%s\n" ), ostr.str().c_str() );
410 trip[0] = glm::dvec3( coords[p1].x, coords[p1].y, coords[p1].z );
411 trip[1] = glm::dvec3( coords[p2].x, coords[p2].y, coords[p2].z );
412 trip[2] = glm::dvec3( coords[p3].x, coords[p3].y, coords[p3].z );
415 std::map< int, std::list< glm::dvec3 > >::iterator ip = vmap.find( p1 );
417 if( ip != vmap.end() )
419 ip->second.push_back( tri );
423 vmap.emplace( p1, std::list < glm::dvec3 >( 1, tri ) );
426 ip = vmap.find( p2 );
428 if( ip != vmap.end() )
430 ip->second.push_back( tri );
434 vmap.emplace( p2, std::list < glm::dvec3 >( 1, tri ) );
437 ip = vmap.find( p3 );
439 if( ip != vmap.end() )
441 ip->second.push_back( tri );
445 vmap.emplace( p3, std::list < glm::dvec3 >( 1, tri ) );
449 std::map< int, std::list< glm::dvec3 > >::iterator sM = vmap.begin();
450 std::map< int, std::list< glm::dvec3 > >::iterator eM = vmap.end();
455 size_t item = sM->first;
460 norms.emplace_back( 0, 0, 1 );
464 std::list< glm::dvec3 >::iterator sT = sM->second.begin();
465 std::list< glm::dvec3 >::iterator eT = sM->second.end();
466 glm::dvec3 norm( 0.0, 0.0, 0.0 );
474 norms.emplace_back( norm.x, norm.y, norm.z );
480 if( norms.size() != coords.size() )
482 wxLogTrace( MASK_3D_SG, wxT(
"%s:%s:%d * [BUG] number of normals does not equal number "
484 __FILE__, __FUNCTION__, __LINE__ );
bool SetColor(float aRedVal, float aGreenVal, float aBlueVal)
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const noexcept
void GetVector(double &aXVal, double &aYVal, double &aZVal) const noexcept
void SetVector(double aXVal, double aYVal, double aZVal)
bool ReadVector(std::istream &aFile, SGVECTOR &aVector)
bool ReadPoint(std::istream &aFile, SGPOINT &aPoint)
bool WritePoint(std::ostream &aFile, const SGPOINT &aPoint)
char const * GetNodeTypeName(S3D::SGTYPES aType) noexcept
Return the name of the given type of node.
bool WriteColor(std::ostream &aFile, const SGCOLOR &aColor)
bool CalcTriangleNormals(std::vector< SGPOINT > coords, std::vector< int > &index, std::vector< SGVECTOR > &norms)
void FormatColor(std::string &result, const SGCOLOR &aColor)
void FormatFloat(std::string &result, double value)
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.
bool WriteVector(std::ostream &aFile, const SGVECTOR &aVector)
void FormatVector(std::string &result, const SGVECTOR &aVector)
void FormatPoint(std::string &result, const SGPOINT &point)
bool degenerate(glm::dvec3 *pts) noexcept
bool ReadColor(std::istream &aFile, SGCOLOR &aColor)
void FormatOrientation(std::string &result, const SGVECTOR &axis, double rotation)
static void calcTriad(glm::dvec3 *pts, glm::dvec3 &tri)
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...