AMFImporter_Geometry.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /// \file AMFImporter_Geometry.cpp
  35. /// \brief Parsing data from geometry nodes.
  36. /// \date 2016
  37. /// \author [email protected]
  38. #ifndef ASSIMP_BUILD_NO_AMF_IMPORTER
  39. #include "AMFImporter.hpp"
  40. #include "AMFImporter_Macro.hpp"
  41. namespace Assimp
  42. {
  43. // <mesh>
  44. // </mesh>
  45. // A 3D mesh hull.
  46. // Multi elements - Yes.
  47. // Parent element - <object>.
  48. void AMFImporter::ParseNode_Mesh()
  49. {
  50. CAMFImporter_NodeElement* ne;
  51. // create new mesh object.
  52. ne = new CAMFImporter_NodeElement_Mesh(mNodeElement_Cur);
  53. // Check for child nodes
  54. if(!mReader->isEmptyElement())
  55. {
  56. bool vert_read = false;
  57. ParseHelper_Node_Enter(ne);
  58. MACRO_NODECHECK_LOOPBEGIN("mesh");
  59. if(XML_CheckNode_NameEqual("vertices"))
  60. {
  61. // Check if data already defined.
  62. if(vert_read) Throw_MoreThanOnceDefined("vertices", "Only one vertices set can be defined for <mesh>.");
  63. // read data and set flag about it
  64. ParseNode_Vertices();
  65. vert_read = true;
  66. continue;
  67. }
  68. if(XML_CheckNode_NameEqual("volume")) { ParseNode_Volume(); continue; }
  69. MACRO_NODECHECK_LOOPEND("mesh");
  70. ParseHelper_Node_Exit();
  71. }// if(!mReader->isEmptyElement())
  72. else
  73. {
  74. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  75. }// if(!mReader->isEmptyElement()) else
  76. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  77. }
  78. // <vertices>
  79. // </vertices>
  80. // The list of vertices to be used in defining triangles.
  81. // Multi elements - No.
  82. // Parent element - <mesh>.
  83. void AMFImporter::ParseNode_Vertices()
  84. {
  85. CAMFImporter_NodeElement* ne;
  86. // create new mesh object.
  87. ne = new CAMFImporter_NodeElement_Vertices(mNodeElement_Cur);
  88. // Check for child nodes
  89. if(!mReader->isEmptyElement())
  90. {
  91. ParseHelper_Node_Enter(ne);
  92. MACRO_NODECHECK_LOOPBEGIN("vertices");
  93. if(XML_CheckNode_NameEqual("vertex")) { ParseNode_Vertex(); continue; }
  94. MACRO_NODECHECK_LOOPEND("vertices");
  95. ParseHelper_Node_Exit();
  96. }// if(!mReader->isEmptyElement())
  97. else
  98. {
  99. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  100. }// if(!mReader->isEmptyElement()) else
  101. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  102. }
  103. // <vertex>
  104. // </vertex>
  105. // A vertex to be referenced in triangles.
  106. // Multi elements - Yes.
  107. // Parent element - <vertices>.
  108. void AMFImporter::ParseNode_Vertex()
  109. {
  110. CAMFImporter_NodeElement* ne;
  111. // create new mesh object.
  112. ne = new CAMFImporter_NodeElement_Vertex(mNodeElement_Cur);
  113. // Check for child nodes
  114. if(!mReader->isEmptyElement())
  115. {
  116. bool col_read = false;
  117. bool coord_read = false;
  118. ParseHelper_Node_Enter(ne);
  119. MACRO_NODECHECK_LOOPBEGIN("vertex");
  120. if(XML_CheckNode_NameEqual("color"))
  121. {
  122. // Check if data already defined.
  123. if(col_read) Throw_MoreThanOnceDefined("color", "Only one color can be defined for <vertex>.");
  124. // read data and set flag about it
  125. ParseNode_Color();
  126. col_read = true;
  127. continue;
  128. }
  129. if(XML_CheckNode_NameEqual("coordinates"))
  130. {
  131. // Check if data already defined.
  132. if(coord_read) Throw_MoreThanOnceDefined("coordinates", "Only one coordinates set can be defined for <vertex>.");
  133. // read data and set flag about it
  134. ParseNode_Coordinates();
  135. coord_read = true;
  136. continue;
  137. }
  138. if(XML_CheckNode_NameEqual("metadata")) { ParseNode_Metadata(); continue; }
  139. MACRO_NODECHECK_LOOPEND("vertex");
  140. ParseHelper_Node_Exit();
  141. }// if(!mReader->isEmptyElement())
  142. else
  143. {
  144. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  145. }// if(!mReader->isEmptyElement()) else
  146. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  147. }
  148. // <coordinates>
  149. // </coordinates>
  150. // Specifies the 3D location of this vertex.
  151. // Multi elements - No.
  152. // Parent element - <vertex>.
  153. //
  154. // Children elements:
  155. // <x>, <y>, <z>
  156. // Multi elements - No.
  157. // X, Y, or Z coordinate, respectively, of a vertex position in space.
  158. void AMFImporter::ParseNode_Coordinates()
  159. {
  160. CAMFImporter_NodeElement* ne;
  161. // create new color object.
  162. ne = new CAMFImporter_NodeElement_Coordinates(mNodeElement_Cur);
  163. CAMFImporter_NodeElement_Coordinates& als = *((CAMFImporter_NodeElement_Coordinates*)ne);// alias for convenience
  164. // Check for child nodes
  165. if(!mReader->isEmptyElement())
  166. {
  167. bool read_flag[3] = { false, false, false };
  168. ParseHelper_Node_Enter(ne);
  169. MACRO_NODECHECK_LOOPBEGIN("coordinates");
  170. MACRO_NODECHECK_READCOMP_F("x", read_flag[0], als.Coordinate.x);
  171. MACRO_NODECHECK_READCOMP_F("y", read_flag[1], als.Coordinate.y);
  172. MACRO_NODECHECK_READCOMP_F("z", read_flag[2], als.Coordinate.z);
  173. MACRO_NODECHECK_LOOPEND("coordinates");
  174. ParseHelper_Node_Exit();
  175. // check that all components was defined
  176. if((read_flag[0] && read_flag[1] && read_flag[2]) == 0) throw DeadlyImportError("Not all coordinate's components are defined.");
  177. }// if(!mReader->isEmptyElement())
  178. else
  179. {
  180. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  181. }// if(!mReader->isEmptyElement()) else
  182. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  183. }
  184. // <volume
  185. // materialid="" - Which material to use.
  186. // type="" - What this volume describes can be “region” or “support”. If none specified, “object” is assumed. If support, then the geometric
  187. // requirements 1-8 listed in section 5 do not need to be maintained.
  188. // >
  189. // </volume>
  190. // Defines a volume from the established vertex list.
  191. // Multi elements - Yes.
  192. // Parent element - <mesh>.
  193. void AMFImporter::ParseNode_Volume()
  194. {
  195. std::string materialid;
  196. std::string type;
  197. CAMFImporter_NodeElement* ne;
  198. // Read attributes for node <color>.
  199. MACRO_ATTRREAD_LOOPBEG;
  200. MACRO_ATTRREAD_CHECK_RET("materialid", materialid, mReader->getAttributeValue);
  201. MACRO_ATTRREAD_CHECK_RET("type", type, mReader->getAttributeValue);
  202. MACRO_ATTRREAD_LOOPEND;
  203. // create new object.
  204. ne = new CAMFImporter_NodeElement_Volume(mNodeElement_Cur);
  205. // and assign read data
  206. ((CAMFImporter_NodeElement_Volume*)ne)->MaterialID = materialid;
  207. ((CAMFImporter_NodeElement_Volume*)ne)->Type = type;
  208. // Check for child nodes
  209. if(!mReader->isEmptyElement())
  210. {
  211. bool col_read = false;
  212. ParseHelper_Node_Enter(ne);
  213. MACRO_NODECHECK_LOOPBEGIN("volume");
  214. if(XML_CheckNode_NameEqual("color"))
  215. {
  216. // Check if data already defined.
  217. if(col_read) Throw_MoreThanOnceDefined("color", "Only one color can be defined for <volume>.");
  218. // read data and set flag about it
  219. ParseNode_Color();
  220. col_read = true;
  221. continue;
  222. }
  223. if(XML_CheckNode_NameEqual("triangle")) { ParseNode_Triangle(); continue; }
  224. if(XML_CheckNode_NameEqual("metadata")) { ParseNode_Metadata(); continue; }
  225. MACRO_NODECHECK_LOOPEND("volume");
  226. ParseHelper_Node_Exit();
  227. }// if(!mReader->isEmptyElement())
  228. else
  229. {
  230. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  231. }// if(!mReader->isEmptyElement()) else
  232. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  233. }
  234. // <triangle>
  235. // </triangle>
  236. // Defines a 3D triangle from three vertices, according to the right-hand rule (counter-clockwise when looking from the outside).
  237. // Multi elements - Yes.
  238. // Parent element - <volume>.
  239. //
  240. // Children elements:
  241. // <v1>, <v2>, <v3>
  242. // Multi elements - No.
  243. // Index of the desired vertices in a triangle or edge.
  244. void AMFImporter::ParseNode_Triangle()
  245. {
  246. CAMFImporter_NodeElement* ne;
  247. // create new color object.
  248. ne = new CAMFImporter_NodeElement_Triangle(mNodeElement_Cur);
  249. CAMFImporter_NodeElement_Triangle& als = *((CAMFImporter_NodeElement_Triangle*)ne);// alias for convenience
  250. // Check for child nodes
  251. if(!mReader->isEmptyElement())
  252. {
  253. bool col_read = false, tex_read = false;
  254. bool read_flag[3] = { false, false, false };
  255. ParseHelper_Node_Enter(ne);
  256. MACRO_NODECHECK_LOOPBEGIN("triangle");
  257. if(XML_CheckNode_NameEqual("color"))
  258. {
  259. // Check if data already defined.
  260. if(col_read) Throw_MoreThanOnceDefined("color", "Only one color can be defined for <triangle>.");
  261. // read data and set flag about it
  262. ParseNode_Color();
  263. col_read = true;
  264. continue;
  265. }
  266. if(XML_CheckNode_NameEqual("texmap"))// new name of node: "texmap".
  267. {
  268. // Check if data already defined.
  269. if(tex_read) Throw_MoreThanOnceDefined("texmap", "Only one texture coordinate can be defined for <triangle>.");
  270. // read data and set flag about it
  271. ParseNode_TexMap();
  272. tex_read = true;
  273. continue;
  274. }
  275. else if(XML_CheckNode_NameEqual("map"))// old name of node: "map".
  276. {
  277. // Check if data already defined.
  278. if(tex_read) Throw_MoreThanOnceDefined("map", "Only one texture coordinate can be defined for <triangle>.");
  279. // read data and set flag about it
  280. ParseNode_TexMap(true);
  281. tex_read = true;
  282. continue;
  283. }
  284. MACRO_NODECHECK_READCOMP_U32("v1", read_flag[0], als.V[0]);
  285. MACRO_NODECHECK_READCOMP_U32("v2", read_flag[1], als.V[1]);
  286. MACRO_NODECHECK_READCOMP_U32("v3", read_flag[2], als.V[2]);
  287. MACRO_NODECHECK_LOOPEND("triangle");
  288. ParseHelper_Node_Exit();
  289. // check that all components was defined
  290. if((read_flag[0] && read_flag[1] && read_flag[2]) == 0) throw DeadlyImportError("Not all vertices of the triangle are defined.");
  291. }// if(!mReader->isEmptyElement())
  292. else
  293. {
  294. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  295. }// if(!mReader->isEmptyElement()) else
  296. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  297. }
  298. }// namespace Assimp
  299. #endif // !ASSIMP_BUILD_NO_AMF_IMPORTER