Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SceneCombiner.h 13 KiB

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