You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

539 lines
13 KiB

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