SceneCombiner.h 14 KB

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