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.

281 lines
7.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 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 <iostream>
  24. #include <cmath>
  25. #include "vrml1_base.h"
  26. #include "vrml1_shapehints.h"
  27. #include "plugins/3dapi/ifsg_all.h"
  28. WRL1SHAPEHINTS::WRL1SHAPEHINTS( NAMEREGISTER* aDictionary ) : WRL1NODE( aDictionary )
  29. {
  30. m_order = ORD_UNKNOWN;
  31. m_Type = WRL1_SHAPEHINTS;
  32. m_crease = 0.5;
  33. m_crease = 0.733; // approx 42 degrees; this is larger than VRML spec.
  34. return;
  35. }
  36. WRL1SHAPEHINTS::WRL1SHAPEHINTS( NAMEREGISTER* aDictionary, WRL1NODE* aParent ) :
  37. WRL1NODE( aDictionary )
  38. {
  39. m_order = ORD_UNKNOWN;
  40. m_Type = WRL1_SHAPEHINTS;
  41. m_Parent = aParent;
  42. if( NULL != m_Parent )
  43. m_Parent->AddChildNode( this );
  44. return;
  45. }
  46. WRL1SHAPEHINTS::~WRL1SHAPEHINTS()
  47. {
  48. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 2 )
  49. std::cerr << " * [INFO] Destroying ShapeHints node\n";
  50. #endif
  51. return;
  52. }
  53. bool WRL1SHAPEHINTS::AddRefNode( WRL1NODE* aNode )
  54. {
  55. // this node may not own or reference any other node
  56. #ifdef DEBUG_VRML1
  57. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  58. std::cerr << " * [BUG] AddRefNode is not applicable\n";
  59. #endif
  60. return false;
  61. }
  62. bool WRL1SHAPEHINTS::AddChildNode( WRL1NODE* aNode )
  63. {
  64. // this node may not own or reference any other node
  65. #ifdef DEBUG_VRML1
  66. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  67. std::cerr << " * [BUG] AddChildNode is not applicable\n";
  68. #endif
  69. return false;
  70. }
  71. bool WRL1SHAPEHINTS::Read( WRLPROC& proc, WRL1BASE* aTopNode )
  72. {
  73. if( NULL == aTopNode )
  74. {
  75. #ifdef DEBUG_VRML1
  76. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  77. std::cerr << " * [BUG] aTopNode is NULL\n";
  78. #endif
  79. return false;
  80. }
  81. size_t line, column;
  82. proc.GetFilePosData( line, column );
  83. char tok = proc.Peek();
  84. if( proc.eof() )
  85. {
  86. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  87. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  88. std::cerr << " * [INFO] bad file format; unexpected eof at line ";
  89. std::cerr << line << ", column " << column << "\n";
  90. #endif
  91. return false;
  92. }
  93. if( '{' != tok )
  94. {
  95. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  96. std::cerr << proc.GetError() << "\n";
  97. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  98. std::cerr << " * [INFO] bad file format; expecting '{' but got '" << tok;
  99. std::cerr << "' at line " << line << ", column " << column << "\n";
  100. #endif
  101. return false;
  102. }
  103. proc.Pop();
  104. std::string glob;
  105. while( true )
  106. {
  107. if( proc.Peek() == '}' )
  108. {
  109. proc.Pop();
  110. break;
  111. }
  112. if( !proc.ReadName( glob ) )
  113. {
  114. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  115. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  116. std::cerr << proc.GetError() << "\n";
  117. #endif
  118. return false;
  119. }
  120. // expecting one of:
  121. // vertexOrdering
  122. // shapeType
  123. // faceType
  124. // creaseAngle
  125. if( !glob.compare( "vertexOrdering" ) )
  126. {
  127. if( !proc.ReadName( glob ) )
  128. {
  129. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  130. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  131. std::cerr << proc.GetError() << "\n";
  132. #endif
  133. return false;
  134. }
  135. if( !glob.compare( "UNKNOWN_ORDERING" ) )
  136. m_order = ORD_UNKNOWN;
  137. else if( !glob.compare( "CLOCKWISE" ) )
  138. m_order = ORD_CLOCKWISE;
  139. else if( !glob.compare( "COUNTERCLOCKWISE" ) )
  140. m_order = ORD_CCW;
  141. else
  142. {
  143. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  144. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  145. std::cerr << " * [INFO] bad ShapeHints at line " << line << ", column ";
  146. std::cerr << column << " (invalid value '" << glob << "')\n";
  147. std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
  148. #endif
  149. return false;
  150. }
  151. }
  152. else if( !glob.compare( "shapeType" ) )
  153. {
  154. if( !proc.ReadName( glob ) )
  155. {
  156. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  157. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  158. std::cerr << proc.GetError() << "\n";
  159. #endif
  160. return false;
  161. }
  162. // expected values:
  163. // UNKNOWN_SHAPE_TYPE
  164. // SOLID
  165. }
  166. else if( !glob.compare( "faceType" ) )
  167. {
  168. if( !proc.ReadName( glob ) )
  169. {
  170. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  171. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  172. std::cerr << proc.GetError() << "\n";
  173. #endif
  174. return false;
  175. }
  176. // expected values:
  177. // UNKNOWN_FACE_TYPE
  178. // CONVEX
  179. }
  180. else if( !glob.compare( "creaseAngle" ) )
  181. {
  182. float tmp;
  183. if( !proc.ReadSFFloat( tmp ) )
  184. {
  185. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  186. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  187. std::cerr << proc.GetError() << "\n";
  188. #endif
  189. return false;
  190. }
  191. if( tmp < 0.0 )
  192. tmp = 0.0;
  193. else if( tmp > M_PI )
  194. tmp = M_PI;
  195. m_crease = tmp;
  196. }
  197. else
  198. {
  199. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  200. std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
  201. std::cerr << " * [INFO] bad ShapeHints at line " << line << ", column ";
  202. std::cerr << column << " (unexpected keyword '" << glob << "')\n";
  203. std::cerr << " * [INFO] file: '" << proc.GetFileName() << "'\n";
  204. #endif
  205. return false;
  206. }
  207. } // while( true ) -- reading contents of ShapeHints{}
  208. return true;
  209. }
  210. SGNODE* WRL1SHAPEHINTS::TranslateToSG( SGNODE* aParent, WRL1STATUS* sp )
  211. {
  212. // note: this is not fully implemented since it is unlikely we shall
  213. // ever make use of the fields shapeType, faceType, and creaseAngle
  214. if( NULL == sp )
  215. {
  216. #if defined( DEBUG_VRML1 ) && ( DEBUG_VRML1 > 1 )
  217. std::cerr << " * [INFO] bad model: no base data given\n";
  218. #endif
  219. return NULL;
  220. }
  221. sp->order = m_order;
  222. sp->creaseLimit = cosf(m_crease);
  223. if( sp->creaseLimit < 0.0 )
  224. sp->creaseLimit = 0.0;
  225. return NULL;
  226. }