AMFImporter_Geometry.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include <assimp/ParsingUtils.h>
  42. namespace Assimp {
  43. // <mesh>
  44. // </mesh>
  45. // A 3D mesh hull.
  46. // Multi elements - Yes.
  47. // Parent element - <object>.
  48. void AMFImporter::ParseNode_Mesh(XmlNode &node) {
  49. AMFNodeElementBase *ne = nullptr;
  50. // create new mesh object.
  51. ne = new AMFMesh(mNodeElement_Cur);
  52. // Check for child nodes
  53. if (0 != ASSIMP_stricmp(node.name(), "mesh")) {
  54. return;
  55. }
  56. bool found_verts = false, found_volumes = false;
  57. pugi::xml_node vertNode = node.child("vertices");
  58. if (!vertNode.empty()) {
  59. ParseNode_Vertices(vertNode);
  60. found_verts = true;
  61. }
  62. pugi::xml_node volumeNode = node.child("volume");
  63. if (!volumeNode.empty()) {
  64. ParseNode_Volume(volumeNode);
  65. found_volumes = true;
  66. }
  67. if (!found_verts && !found_volumes) {
  68. mNodeElement_Cur->Child.push_back(ne);
  69. } // if(!mReader->isEmptyElement()) else
  70. // and to node element list because its a new object in graph.
  71. mNodeElement_List.push_back(ne);
  72. }
  73. // <vertices>
  74. // </vertices>
  75. // The list of vertices to be used in defining triangles.
  76. // Multi elements - No.
  77. // Parent element - <mesh>.
  78. void AMFImporter::ParseNode_Vertices(XmlNode &node) {
  79. AMFNodeElementBase *ne = nullptr;
  80. // create new mesh object.
  81. ne = new AMFVertices(mNodeElement_Cur);
  82. // Check for child nodes
  83. pugi::xml_node vertexNode = node.child("vertex");
  84. if (!vertexNode.empty()) {
  85. ParseNode_Vertex(vertexNode);
  86. } else {
  87. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  88. } // if(!mReader->isEmptyElement()) else
  89. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  90. }
  91. // <vertex>
  92. // </vertex>
  93. // A vertex to be referenced in triangles.
  94. // Multi elements - Yes.
  95. // Parent element - <vertices>.
  96. void AMFImporter::ParseNode_Vertex(XmlNode &node) {
  97. AMFNodeElementBase *ne = nullptr;
  98. // create new mesh object.
  99. ne = new AMFVertex(mNodeElement_Cur);
  100. // Check for child nodes
  101. pugi::xml_node colorNode = node.child("color");
  102. bool col_read = false;
  103. bool coord_read = false;
  104. if (!colorNode.empty()) {
  105. ParseNode_Color(colorNode);
  106. col_read = true;
  107. }
  108. pugi::xml_node coordNode = node.child("coordinates");
  109. if (!coordNode.empty()) {
  110. ParseNode_Coordinates(coordNode);
  111. coord_read = true;
  112. }
  113. if (!coord_read && !coord_read) {
  114. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  115. }
  116. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  117. }
  118. // <coordinates>
  119. // </coordinates>
  120. // Specifies the 3D location of this vertex.
  121. // Multi elements - No.
  122. // Parent element - <vertex>.
  123. //
  124. // Children elements:
  125. // <x>, <y>, <z>
  126. // Multi elements - No.
  127. // X, Y, or Z coordinate, respectively, of a vertex position in space.
  128. void AMFImporter::ParseNode_Coordinates(XmlNode &node) {
  129. AMFNodeElementBase *ne = nullptr;
  130. // create new color object.
  131. ne = new AMFCoordinates(mNodeElement_Cur);
  132. AMFCoordinates &als = *((AMFCoordinates *)ne); // alias for convenience
  133. if (node.attributes().begin() != node.attributes().end()) {
  134. als.Coordinate.x = (ai_real)node.attribute("x").as_float();
  135. als.Coordinate.y = (ai_real)node.attribute("y").as_float();
  136. als.Coordinate.z = (ai_real)node.attribute("z").as_float();
  137. } else {
  138. mNodeElement_Cur->Child.push_back(ne);
  139. }
  140. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  141. }
  142. // <volume
  143. // materialid="" - Which material to use.
  144. // type="" - What this volume describes can be “region” or “support”. If none specified, “object” is assumed. If support, then the geometric
  145. // requirements 1-8 listed in section 5 do not need to be maintained.
  146. // >
  147. // </volume>
  148. // Defines a volume from the established vertex list.
  149. // Multi elements - Yes.
  150. // Parent element - <mesh>.
  151. void AMFImporter::ParseNode_Volume(XmlNode &node) {
  152. std::string materialid;
  153. std::string type;
  154. AMFNodeElementBase *ne = new AMFVolume(mNodeElement_Cur);
  155. // Read attributes for node <color>.
  156. // and assign read data
  157. ((AMFVolume *)ne)->MaterialID = node.attribute("materialid").as_string();
  158. ((AMFVolume *)ne)->Type = type;
  159. // Check for child nodes
  160. if (node.empty()) {
  161. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  162. }
  163. bool col_read = false;
  164. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  165. const std::string currentName = currentNode.name();
  166. if (currentName == "color") {
  167. if (col_read) Throw_MoreThanOnceDefined(currentName ,"color", "Only one color can be defined for <volume>.");
  168. ParseNode_Color(currentNode);
  169. col_read = true;
  170. } else if (currentName == "triangle") {
  171. ParseNode_Triangle(currentNode);
  172. } else if (currentName == "metadata") {
  173. ParseNode_Metadata(currentNode);
  174. } else if (currentName == "volume") {
  175. ParseNode_Metadata(currentNode);
  176. }
  177. }
  178. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  179. }
  180. // <triangle>
  181. // </triangle>
  182. // Defines a 3D triangle from three vertices, according to the right-hand rule (counter-clockwise when looking from the outside).
  183. // Multi elements - Yes.
  184. // Parent element - <volume>.
  185. //
  186. // Children elements:
  187. // <v1>, <v2>, <v3>
  188. // Multi elements - No.
  189. // Index of the desired vertices in a triangle or edge.
  190. void AMFImporter::ParseNode_Triangle(XmlNode &node) {
  191. AMFNodeElementBase *ne = new AMFTriangle(mNodeElement_Cur);
  192. // create new triangle object.
  193. AMFTriangle &als = *((AMFTriangle *)ne); // alias for convenience
  194. if (node.empty()) {
  195. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  196. }
  197. // Check for child nodes
  198. bool col_read = false, tex_read = false;
  199. bool read_flag[3] = { false, false, false };
  200. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  201. const std::string currentName = currentNode.name();
  202. if (currentName == "color") {
  203. if (col_read) Throw_MoreThanOnceDefined(currentName , "color", "Only one color can be defined for <triangle>.");
  204. ParseNode_Color(currentNode);
  205. col_read = true;
  206. } else if (currentName == "texmap") {
  207. ParseNode_TexMap(currentNode);
  208. tex_read = true;
  209. } else if (currentName == "map") {
  210. ParseNode_TexMap(currentNode, true);
  211. tex_read = true;
  212. } else if (currentName == "v1") {
  213. als.V[0] = std::atoi(currentNode.value());
  214. read_flag[0] = true;
  215. } else if (currentName == "v2") {
  216. als.V[1] = std::atoi(currentNode.value());
  217. read_flag[1] = true;
  218. } else if (currentName == "v3") {
  219. als.V[2] = std::atoi(currentNode.value());
  220. read_flag[2] = true;
  221. }
  222. }
  223. if ((read_flag[0] && read_flag[1] && read_flag[2]) == 0) {
  224. throw DeadlyImportError("Not all vertices of the triangle are defined.");
  225. }
  226. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  227. }
  228. } // namespace Assimp
  229. #endif // !ASSIMP_BUILD_NO_AMF_IMPORTER