SceneCombiner.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Declares a helper class, "SceneCombiner" providing various
  34. * utilities to merge scenes.
  35. */
  36. #pragma once
  37. #ifndef AI_SCENE_COMBINER_H_INC
  38. #define AI_SCENE_COMBINER_H_INC
  39. #ifdef __GNUC__
  40. #pragma GCC system_header
  41. #endif
  42. #include <assimp/ai_assert.h>
  43. #include <assimp/types.h>
  44. #include <cstddef>
  45. #include <cstdint>
  46. #include <list>
  47. #include <set>
  48. #include <vector>
  49. struct aiScene;
  50. struct aiNode;
  51. struct aiMaterial;
  52. struct aiTexture;
  53. struct aiCamera;
  54. struct aiLight;
  55. struct aiMetadata;
  56. struct aiBone;
  57. struct aiMesh;
  58. struct aiAnimMesh;
  59. struct aiAnimation;
  60. struct aiNodeAnim;
  61. struct aiMeshMorphAnim;
  62. namespace Assimp {
  63. // ---------------------------------------------------------------------------
  64. /** \brief Helper data structure for SceneCombiner.
  65. *
  66. * Describes to which node a scene must be attached to.
  67. */
  68. struct AttachmentInfo {
  69. AttachmentInfo() :
  70. scene(nullptr),
  71. attachToNode(nullptr) {}
  72. AttachmentInfo(aiScene *_scene, aiNode *_attachToNode) :
  73. scene(_scene), attachToNode(_attachToNode) {}
  74. aiScene *scene;
  75. aiNode *attachToNode;
  76. };
  77. // ---------------------------------------------------------------------------
  78. struct NodeAttachmentInfo {
  79. NodeAttachmentInfo() :
  80. node(nullptr),
  81. attachToNode(nullptr),
  82. resolved(false),
  83. src_idx(SIZE_MAX) {}
  84. NodeAttachmentInfo(aiNode *_scene, aiNode *_attachToNode, size_t idx) :
  85. node(_scene), attachToNode(_attachToNode), resolved(false), src_idx(idx) {}
  86. aiNode *node;
  87. aiNode *attachToNode;
  88. bool resolved;
  89. size_t src_idx;
  90. };
  91. // ---------------------------------------------------------------------------
  92. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES
  93. * Generate unique names for all named scene items
  94. */
  95. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES 0x1
  96. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES
  97. * Generate unique names for materials, too.
  98. * This is not absolutely required to pass the validation.
  99. */
  100. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES 0x2
  101. /** @def AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY
  102. * Use deep copies of duplicate scenes
  103. */
  104. #define AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY 0x4
  105. /** @def AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS
  106. * If attachment nodes are not found in the given master scene,
  107. * search the other imported scenes for them in an any order.
  108. */
  109. #define AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS 0x8
  110. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY
  111. * Can be combined with AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES.
  112. * Unique names are generated, but only if this is absolutely
  113. * required to avoid name conflicts.
  114. */
  115. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY 0x10
  116. typedef std::pair<aiBone *, unsigned int> BoneSrcIndex;
  117. // ---------------------------------------------------------------------------
  118. /** @brief Helper data structure for SceneCombiner::MergeBones.
  119. */
  120. struct BoneWithHash : public std::pair<uint32_t, aiString *> {
  121. std::vector<BoneSrcIndex> pSrcBones;
  122. };
  123. // ---------------------------------------------------------------------------
  124. /** @brief Utility for SceneCombiner
  125. */
  126. struct SceneHelper {
  127. SceneHelper() :
  128. scene(nullptr),
  129. idlen(0) {
  130. id[0] = 0;
  131. }
  132. explicit SceneHelper(aiScene *_scene) :
  133. scene(_scene), idlen(0) {
  134. id[0] = 0;
  135. }
  136. AI_FORCE_INLINE aiScene *operator->() const {
  137. return scene;
  138. }
  139. // scene we're working on
  140. aiScene *scene;
  141. // prefix to be added to all identifiers in the scene ...
  142. char id[32];
  143. // and its strlen()
  144. unsigned int idlen;
  145. // hash table to quickly check whether a name is contained in the scene
  146. std::set<unsigned int> hashes;
  147. };
  148. // ---------------------------------------------------------------------------
  149. /** \brief Static helper class providing various utilities to merge two
  150. * scenes. It is intended as internal utility and NOT for use by
  151. * applications.
  152. *
  153. * The class is currently being used by various postprocessing steps
  154. * and loaders (ie. LWS).
  155. */
  156. class ASSIMP_API SceneCombiner {
  157. public:
  158. // class cannot be instanced
  159. SceneCombiner() = delete;
  160. ~SceneCombiner() = delete;
  161. // -------------------------------------------------------------------
  162. /** Merges two or more scenes.
  163. *
  164. * @param dest Receives a pointer to the destination scene. If the
  165. * pointer doesn't point to nullptr when the function is called, the
  166. * existing scene is cleared and refilled.
  167. * @param src Non-empty list of scenes to be merged. The function
  168. * deletes the input scenes afterwards. There may be duplicate scenes.
  169. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  170. */
  171. static void MergeScenes(aiScene **dest, std::vector<aiScene *> &src,
  172. unsigned int flags = 0);
  173. // -------------------------------------------------------------------
  174. /** Merges two or more scenes and attaches all scenes to a specific
  175. * position in the node graph of the master scene.
  176. *
  177. * @param dest Receives a pointer to the destination scene. If the
  178. * pointer doesn't point to nullptr when the function is called, the
  179. * existing scene is cleared and refilled.
  180. * @param master Master scene. It will be deleted afterwards. All
  181. * other scenes will be inserted in its node graph.
  182. * @param src Non-empty list of scenes to be merged along with their
  183. * corresponding attachment points in the master scene. The function
  184. * deletes the input scenes afterwards. There may be duplicate scenes.
  185. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  186. */
  187. static void MergeScenes(aiScene **dest, aiScene *master,
  188. std::vector<AttachmentInfo> &src,
  189. unsigned int flags = 0);
  190. // -------------------------------------------------------------------
  191. /** Merges two or more meshes
  192. *
  193. * The meshes should have equal vertex formats. Only components
  194. * that are provided by ALL meshes will be present in the output mesh.
  195. * An exception is made for VColors - they are set to black. The
  196. * meshes should have the same material indices, too. The output
  197. * material index is always the material index of the first mesh.
  198. *
  199. * @param dest Destination mesh. Must be empty.
  200. * @param flags Currently no parameters
  201. * @param begin First mesh to be processed
  202. * @param end Points to the mesh after the last mesh to be processed
  203. */
  204. static void MergeMeshes(aiMesh **dest, unsigned int flags,
  205. std::vector<aiMesh *>::const_iterator begin,
  206. std::vector<aiMesh *>::const_iterator end);
  207. // -------------------------------------------------------------------
  208. /** Merges two or more bones
  209. *
  210. * @param out Mesh to receive the output bone list
  211. * @param flags Currently no parameters
  212. * @param begin First mesh to be processed
  213. * @param end Points to the mesh after the last mesh to be processed
  214. */
  215. static void MergeBones(aiMesh *out, std::vector<aiMesh *>::const_iterator it,
  216. std::vector<aiMesh *>::const_iterator end);
  217. // -------------------------------------------------------------------
  218. /** Merges two or more materials
  219. *
  220. * The materials should be complementary as much as possible. In case
  221. * of a property present in different materials, the first occurrence
  222. * is used.
  223. *
  224. * @param dest Destination material. Must be empty.
  225. * @param begin First material to be processed
  226. * @param end Points to the material after the last material to be processed
  227. */
  228. static void MergeMaterials(aiMaterial **dest,
  229. std::vector<aiMaterial *>::const_iterator begin,
  230. std::vector<aiMaterial *>::const_iterator end);
  231. // -------------------------------------------------------------------
  232. /** Builds a list of uniquely named bones in a mesh list
  233. *
  234. * @param asBones Receives the output list
  235. * @param it First mesh to be processed
  236. * @param end Last mesh to be processed
  237. */
  238. static void BuildUniqueBoneList(std::list<BoneWithHash> &asBones,
  239. std::vector<aiMesh *>::const_iterator it,
  240. std::vector<aiMesh *>::const_iterator end);
  241. // -------------------------------------------------------------------
  242. /** Add a name prefix to all nodes in a scene.
  243. *
  244. * @param node Current node. This function is called recursively.
  245. * @param prefix Prefix to be added to all nodes
  246. * @param len String length
  247. */
  248. static void AddNodePrefixes(aiNode *node, const char *prefix,
  249. unsigned int len);
  250. // -------------------------------------------------------------------
  251. /** Add an offset to all mesh indices in a node graph
  252. *
  253. * @param node Current node. This function is called recursively.
  254. * @param offset Offset to be added to all mesh indices
  255. */
  256. static void OffsetNodeMeshIndices(aiNode *node, unsigned int offset);
  257. // -------------------------------------------------------------------
  258. /** Attach a list of node graphs to well-defined nodes in a master
  259. * graph. This is a helper for MergeScenes()
  260. *
  261. * @param master Master scene
  262. * @param srcList List of source scenes along with their attachment
  263. * points. If an attachment point is nullptr (or does not exist in
  264. * the master graph), a scene is attached to the root of the master
  265. * graph (as an additional child node)
  266. * @duplicates List of duplicates. If elem[n] == n the scene is not
  267. * a duplicate. Otherwise, elem[n] links scene n to its first occurrence.
  268. */
  269. static void AttachToGraph(aiScene *master,
  270. std::vector<NodeAttachmentInfo> &srcList);
  271. static void AttachToGraph(aiNode *attach,
  272. std::vector<NodeAttachmentInfo> &srcList);
  273. // -------------------------------------------------------------------
  274. /** Get a deep copy of a scene
  275. *
  276. * @param dest Receives a pointer to the destination scene
  277. * @param source Source scene - remains unmodified.
  278. * @param allocate true for allocation a new scene
  279. */
  280. static void CopyScene(aiScene **dest, const aiScene *source, bool allocate = true);
  281. // -------------------------------------------------------------------
  282. /** Get a flat copy of a scene
  283. *
  284. * Only the first hierarchy layer is copied. All pointer members of
  285. * aiScene are shared by source and destination scene. If the
  286. * pointer doesn't point to nullptr when the function is called, the
  287. * existing scene is cleared and refilled.
  288. * @param dest Receives a pointer to the destination scene
  289. * @param src Source scene - remains unmodified.
  290. */
  291. static void CopySceneFlat(aiScene **dest, const aiScene *source);
  292. // -------------------------------------------------------------------
  293. /** Get a deep copy of a mesh
  294. *
  295. * @param dest Receives a pointer to the destination mesh
  296. * @param src Source mesh - remains unmodified.
  297. */
  298. static void Copy(aiMesh **dest, const aiMesh *src);
  299. // similar to Copy():
  300. static void Copy(aiAnimMesh **dest, const aiAnimMesh *src);
  301. static void Copy(aiMaterial **dest, const aiMaterial *src);
  302. static void Copy(aiTexture **dest, const aiTexture *src);
  303. static void Copy(aiAnimation **dest, const aiAnimation *src);
  304. static void Copy(aiCamera **dest, const aiCamera *src);
  305. static void Copy(aiBone **dest, const aiBone *src);
  306. static void Copy(aiLight **dest, const aiLight *src);
  307. static void Copy(aiNodeAnim **dest, const aiNodeAnim *src);
  308. static void Copy(aiMeshMorphAnim **dest, const aiMeshMorphAnim *src);
  309. static void Copy(aiMetadata **dest, const aiMetadata *src);
  310. static void Copy(aiString **dest, const aiString *src);
  311. // recursive, of course
  312. static void Copy(aiNode **dest, const aiNode *src);
  313. private:
  314. // -------------------------------------------------------------------
  315. // Same as AddNodePrefixes, but with an additional check
  316. static void AddNodePrefixesChecked(aiNode *node, const char *prefix,
  317. unsigned int len,
  318. std::vector<SceneHelper> &input,
  319. unsigned int cur);
  320. // -------------------------------------------------------------------
  321. // Add node identifiers to a hashing set
  322. static void AddNodeHashes(aiNode *node, std::set<unsigned int> &hashes);
  323. // -------------------------------------------------------------------
  324. // Search for duplicate names
  325. static bool FindNameMatch(const aiString &name,
  326. std::vector<SceneHelper> &input, unsigned int cur);
  327. };
  328. } // namespace Assimp
  329. #endif // !! AI_SCENE_COMBINER_H_INC