SceneCombiner.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, 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/Defines.h>
  43. #include <assimp/ai_assert.h>
  44. #include <assimp/types.h>
  45. #include <stddef.h>
  46. #include <stdint.h>
  47. #include <list>
  48. #include <set>
  49. #include <vector>
  50. struct aiScene;
  51. struct aiNode;
  52. struct aiMaterial;
  53. struct aiTexture;
  54. struct aiCamera;
  55. struct aiLight;
  56. struct aiMetadata;
  57. struct aiBone;
  58. struct aiMesh;
  59. struct aiAnimMesh;
  60. struct aiAnimation;
  61. struct aiNodeAnim;
  62. struct aiMeshMorphAnim;
  63. namespace Assimp {
  64. // ---------------------------------------------------------------------------
  65. /** \brief Helper data structure for SceneCombiner.
  66. *
  67. * Describes to which node a scene must be attached to.
  68. */
  69. struct AttachmentInfo {
  70. AttachmentInfo() :
  71. scene(nullptr),
  72. attachToNode(nullptr) {}
  73. AttachmentInfo(aiScene *_scene, aiNode *_attachToNode) :
  74. scene(_scene), attachToNode(_attachToNode) {}
  75. aiScene *scene;
  76. aiNode *attachToNode;
  77. };
  78. // ---------------------------------------------------------------------------
  79. struct NodeAttachmentInfo {
  80. NodeAttachmentInfo() :
  81. node(nullptr),
  82. attachToNode(nullptr),
  83. resolved(false),
  84. src_idx(SIZE_MAX) {}
  85. NodeAttachmentInfo(aiNode *_scene, aiNode *_attachToNode, size_t idx) :
  86. node(_scene), attachToNode(_attachToNode), resolved(false), src_idx(idx) {}
  87. aiNode *node;
  88. aiNode *attachToNode;
  89. bool resolved;
  90. size_t src_idx;
  91. };
  92. // ---------------------------------------------------------------------------
  93. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES
  94. * Generate unique names for all named scene items
  95. */
  96. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES 0x1
  97. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES
  98. * Generate unique names for materials, too.
  99. * This is not absolutely required to pass the validation.
  100. */
  101. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES 0x2
  102. /** @def AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY
  103. * Use deep copies of duplicate scenes
  104. */
  105. #define AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY 0x4
  106. /** @def AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS
  107. * If attachment nodes are not found in the given master scene,
  108. * search the other imported scenes for them in an any order.
  109. */
  110. #define AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS 0x8
  111. /** @def AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY
  112. * Can be combined with AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES.
  113. * Unique names are generated, but only if this is absolutely
  114. * required to avoid name conflicts.
  115. */
  116. #define AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY 0x10
  117. typedef std::pair<aiBone *, unsigned int> BoneSrcIndex;
  118. // ---------------------------------------------------------------------------
  119. /** @brief Helper data structure for SceneCombiner::MergeBones.
  120. */
  121. struct BoneWithHash : public std::pair<uint32_t, aiString *> {
  122. std::vector<BoneSrcIndex> pSrcBones;
  123. };
  124. // ---------------------------------------------------------------------------
  125. /** @brief Utility for SceneCombiner
  126. */
  127. struct SceneHelper {
  128. SceneHelper() :
  129. scene(nullptr),
  130. idlen(0) {
  131. id[0] = 0;
  132. }
  133. explicit SceneHelper(aiScene *_scene) :
  134. scene(_scene), idlen(0) {
  135. id[0] = 0;
  136. }
  137. AI_FORCE_INLINE aiScene *operator->() const {
  138. return scene;
  139. }
  140. // scene we're working on
  141. aiScene *scene;
  142. // prefix to be added to all identifiers in the scene ...
  143. char id[32];
  144. // and its strlen()
  145. unsigned int idlen;
  146. // hash table to quickly check whether a name is contained in the scene
  147. std::set<unsigned int> hashes;
  148. };
  149. // ---------------------------------------------------------------------------
  150. /** \brief Static helper class providing various utilities to merge two
  151. * scenes. It is intended as internal utility and NOT for use by
  152. * applications.
  153. *
  154. * The class is currently being used by various postprocessing steps
  155. * and loaders (ie. LWS).
  156. */
  157. class ASSIMP_API SceneCombiner {
  158. // class cannot be instanced
  159. SceneCombiner() {
  160. // empty
  161. }
  162. ~SceneCombiner() {
  163. // empty
  164. }
  165. public:
  166. // -------------------------------------------------------------------
  167. /** Merges two or more scenes.
  168. *
  169. * @param dest Receives a pointer to the destination scene. If the
  170. * pointer doesn't point to nullptr when the function is called, the
  171. * existing scene is cleared and refilled.
  172. * @param src Non-empty list of scenes to be merged. The function
  173. * deletes the input scenes afterwards. There may be duplicate scenes.
  174. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  175. */
  176. static void MergeScenes(aiScene **dest, std::vector<aiScene *> &src,
  177. unsigned int flags = 0);
  178. // -------------------------------------------------------------------
  179. /** Merges two or more scenes and attaches all scenes to a specific
  180. * position in the node graph of the master scene.
  181. *
  182. * @param dest Receives a pointer to the destination scene. If the
  183. * pointer doesn't point to nullptr when the function is called, the
  184. * existing scene is cleared and refilled.
  185. * @param master Master scene. It will be deleted afterwards. All
  186. * other scenes will be inserted in its node graph.
  187. * @param src Non-empty list of scenes to be merged along with their
  188. * corresponding attachment points in the master scene. The function
  189. * deletes the input scenes afterwards. There may be duplicate scenes.
  190. * @param flags Combination of the AI_INT_MERGE_SCENE flags defined above
  191. */
  192. static void MergeScenes(aiScene **dest, aiScene *master,
  193. std::vector<AttachmentInfo> &src,
  194. unsigned int flags = 0);
  195. // -------------------------------------------------------------------
  196. /** Merges two or more meshes
  197. *
  198. * The meshes should have equal vertex formats. Only components
  199. * that are provided by ALL meshes will be present in the output mesh.
  200. * An exception is made for VColors - they are set to black. The
  201. * meshes should have the same material indices, too. The output
  202. * material index is always the material index of the first mesh.
  203. *
  204. * @param dest Destination mesh. Must be empty.
  205. * @param flags Currently no parameters
  206. * @param begin First mesh to be processed
  207. * @param end Points to the mesh after the last mesh to be processed
  208. */
  209. static void MergeMeshes(aiMesh **dest, unsigned int flags,
  210. std::vector<aiMesh *>::const_iterator begin,
  211. std::vector<aiMesh *>::const_iterator end);
  212. // -------------------------------------------------------------------
  213. /** Merges two or more bones
  214. *
  215. * @param out Mesh to receive the output bone list
  216. * @param flags Currently no parameters
  217. * @param begin First mesh to be processed
  218. * @param end Points to the mesh after the last mesh to be processed
  219. */
  220. static void MergeBones(aiMesh *out, std::vector<aiMesh *>::const_iterator it,
  221. std::vector<aiMesh *>::const_iterator end);
  222. // -------------------------------------------------------------------
  223. /** Merges two or more materials
  224. *
  225. * The materials should be complementary as much as possible. In case
  226. * of a property present in different materials, the first occurrence
  227. * is used.
  228. *
  229. * @param dest Destination material. Must be empty.
  230. * @param begin First material to be processed
  231. * @param end Points to the material after the last material to be processed
  232. */
  233. static void MergeMaterials(aiMaterial **dest,
  234. std::vector<aiMaterial *>::const_iterator begin,
  235. std::vector<aiMaterial *>::const_iterator end);
  236. // -------------------------------------------------------------------
  237. /** Builds a list of uniquely named bones in a mesh list
  238. *
  239. * @param asBones Receives the output list
  240. * @param it First mesh to be processed
  241. * @param end Last mesh to be processed
  242. */
  243. static void BuildUniqueBoneList(std::list<BoneWithHash> &asBones,
  244. std::vector<aiMesh *>::const_iterator it,
  245. std::vector<aiMesh *>::const_iterator end);
  246. // -------------------------------------------------------------------
  247. /** Add a name prefix to all nodes in a scene.
  248. *
  249. * @param Current node. This function is called recursively.
  250. * @param prefix Prefix to be added to all nodes
  251. * @param len STring length
  252. */
  253. static void AddNodePrefixes(aiNode *node, const char *prefix,
  254. unsigned int len);
  255. // -------------------------------------------------------------------
  256. /** Add an offset to all mesh indices in a node graph
  257. *
  258. * @param Current node. This function is called recursively.
  259. * @param offset Offset to be added to all mesh indices
  260. */
  261. static void OffsetNodeMeshIndices(aiNode *node, unsigned int offset);
  262. // -------------------------------------------------------------------
  263. /** Attach a list of node graphs to well-defined nodes in a master
  264. * graph. This is a helper for MergeScenes()
  265. *
  266. * @param master Master scene
  267. * @param srcList List of source scenes along with their attachment
  268. * points. If an attachment point is nullptr (or does not exist in
  269. * the master graph), a scene is attached to the root of the master
  270. * graph (as an additional child node)
  271. * @duplicates List of duplicates. If elem[n] == n the scene is not
  272. * a duplicate. Otherwise elem[n] links scene n to its first occurrence.
  273. */
  274. static void AttachToGraph(aiScene *master,
  275. std::vector<NodeAttachmentInfo> &srcList);
  276. static void AttachToGraph(aiNode *attach,
  277. std::vector<NodeAttachmentInfo> &srcList);
  278. // -------------------------------------------------------------------
  279. /** Get a deep copy of a scene
  280. *
  281. * @param dest Receives a pointer to the destination scene
  282. * @param src Source scene - remains unmodified.
  283. */
  284. static void CopyScene(aiScene **dest, const aiScene *source, bool allocate = true);
  285. // -------------------------------------------------------------------
  286. /** Get a flat copy of a scene
  287. *
  288. * Only the first hierarchy layer is copied. All pointer members of
  289. * aiScene are shared by source and destination scene. If the
  290. * pointer doesn't point to nullptr when the function is called, the
  291. * existing scene is cleared and refilled.
  292. * @param dest Receives a pointer to the destination scene
  293. * @param src Source scene - remains unmodified.
  294. */
  295. static void CopySceneFlat(aiScene **dest, const aiScene *source);
  296. // -------------------------------------------------------------------
  297. /** Get a deep copy of a mesh
  298. *
  299. * @param dest Receives a pointer to the destination mesh
  300. * @param src Source mesh - remains unmodified.
  301. */
  302. static void Copy(aiMesh **dest, const aiMesh *src);
  303. // similar to Copy():
  304. static void Copy(aiAnimMesh **dest, const aiAnimMesh *src);
  305. static void Copy(aiMaterial **dest, const aiMaterial *src);
  306. static void Copy(aiTexture **dest, const aiTexture *src);
  307. static void Copy(aiAnimation **dest, const aiAnimation *src);
  308. static void Copy(aiCamera **dest, const aiCamera *src);
  309. static void Copy(aiBone **dest, const aiBone *src);
  310. static void Copy(aiLight **dest, const aiLight *src);
  311. static void Copy(aiNodeAnim **dest, const aiNodeAnim *src);
  312. static void Copy(aiMeshMorphAnim **dest, const aiMeshMorphAnim *src);
  313. static void Copy(aiMetadata **dest, const aiMetadata *src);
  314. // recursive, of course
  315. static void Copy(aiNode **dest, const aiNode *src);
  316. private:
  317. // -------------------------------------------------------------------
  318. // Same as AddNodePrefixes, but with an additional check
  319. static void AddNodePrefixesChecked(aiNode *node, const char *prefix,
  320. unsigned int len,
  321. std::vector<SceneHelper> &input,
  322. unsigned int cur);
  323. // -------------------------------------------------------------------
  324. // Add node identifiers to a hashing set
  325. static void AddNodeHashes(aiNode *node, std::set<unsigned int> &hashes);
  326. // -------------------------------------------------------------------
  327. // Search for duplicate names
  328. static bool FindNameMatch(const aiString &name,
  329. std::vector<SceneHelper> &input, unsigned int cur);
  330. };
  331. } // namespace Assimp
  332. #endif // !! AI_SCENE_COMBINER_H_INC