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