BaseProcess.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Base class of all import post processing steps */
  34. #ifndef AI_BASEPROCESS_H_INC
  35. #define AI_BASEPROCESS_H_INC
  36. #include <map>
  37. #include "../include/aiTypes.h"
  38. #include "GenericProperty.h"
  39. struct aiScene;
  40. namespace Assimp {
  41. class Importer;
  42. // ---------------------------------------------------------------------------
  43. /** Helper class to allow post-processing steps to interact with each other.
  44. *
  45. * The class maintains a simple property list that can be used by pp-steps
  46. * to provide additional information to other steps. This is primarily
  47. * intended for cross-step optimizations.
  48. */
  49. class ASSIMP_API SharedPostProcessInfo
  50. {
  51. public:
  52. struct Base
  53. {
  54. virtual ~Base()
  55. {}
  56. };
  57. template <typename T>
  58. struct THeapData : public Base
  59. {
  60. THeapData(T* in)
  61. : data (in)
  62. {}
  63. ~THeapData()
  64. {
  65. delete data;
  66. }
  67. T* data;
  68. };
  69. template <typename T>
  70. struct TStaticData : public Base
  71. {
  72. TStaticData(T in)
  73. : data (in)
  74. {}
  75. ~TStaticData()
  76. {}
  77. T data;
  78. };
  79. typedef unsigned int KeyType;
  80. typedef std::map<KeyType, Base*> PropertyMap;
  81. public:
  82. ~SharedPostProcessInfo()
  83. {
  84. Clean();
  85. }
  86. void Clean()
  87. {
  88. // invoke the virtual destructor for all stored properties
  89. for (PropertyMap::iterator it = pmap.begin(), end = pmap.end();
  90. it != end; ++it)
  91. {
  92. delete (*it).second;
  93. }
  94. pmap.clear();
  95. }
  96. template <typename T>
  97. inline void AddProperty( const char* name, T* in )
  98. {
  99. AddProperty(name,(Base*)new THeapData<T>(in));
  100. }
  101. template <typename T>
  102. inline void AddProperty( const char* name, T in )
  103. {
  104. AddProperty(name,(Base*)new TStaticData<T>(in));
  105. }
  106. template <typename T>
  107. inline bool GetProperty( const char* name, T*& out ) const
  108. {
  109. THeapData<T>* t;
  110. GetProperty(name,(Base*&)t);
  111. if(!t)
  112. {
  113. out = NULL;
  114. return false;
  115. }
  116. out = t->data;
  117. return true;
  118. }
  119. template <typename T>
  120. inline bool GetProperty( const char* name, T& out ) const
  121. {
  122. TStaticData<T>* t;
  123. GetProperty(name,(Base*&)t);
  124. if(!t)return false;
  125. out = t->data;
  126. return true;
  127. }
  128. inline void RemoveProperty( const char* name)
  129. {
  130. SetGenericPropertyPtr<Base>(pmap,name,NULL);
  131. }
  132. private:
  133. inline void AddProperty( const char* name, Base* data)
  134. {
  135. SetGenericPropertyPtr<Base>(pmap,name,data);
  136. }
  137. inline void GetProperty( const char* name, Base*& data) const
  138. {
  139. data = GetGenericProperty<Base*>(pmap,name,NULL);
  140. }
  141. PropertyMap pmap;
  142. };
  143. #define AI_SPP_SPATIAL_SORT "$Spat"
  144. // ---------------------------------------------------------------------------
  145. /** The BaseProcess defines a common interface for all post processing steps.
  146. * A post processing step is run after a successful import if the caller
  147. * specified the corresponding flag when calling ReadFile().
  148. * Enum #aiPostProcessSteps defines which flags are available.
  149. * After a successful import the Importer iterates over its internal array
  150. * of processes and calls IsActive() on each process to evaluate if the step
  151. * should be executed. If the function returns true, the class' Execute()
  152. * function is called subsequently.
  153. */
  154. class ASSIMP_API BaseProcess
  155. {
  156. friend class Importer;
  157. public:
  158. /** Constructor to be privately used by Importer */
  159. BaseProcess();
  160. /** Destructor, private as well */
  161. virtual ~BaseProcess();
  162. public:
  163. // -------------------------------------------------------------------
  164. /** Returns whether the processing step is present in the given flag.
  165. * @param pFlags The processing flags the importer was called with. A
  166. * bitwise combination of #aiPostProcessSteps.
  167. * @return true if the process is present in this flag fields,
  168. * false if not.
  169. */
  170. virtual bool IsActive( unsigned int pFlags) const = 0;
  171. // -------------------------------------------------------------------
  172. /** Executes the post processing step on the given imported data.
  173. * The function deletes the scene if the postprocess step fails (
  174. * the object pointer will be set to NULL).
  175. * @param pImp Importer instance (pImp->mScene must be valid)
  176. */
  177. void ExecuteOnScene( Importer* pImp);
  178. // -------------------------------------------------------------------
  179. /** Called prior to ExecuteOnScene().
  180. * The function is a request to the process to update its configuration
  181. * basing on the Importer's configuration property list.
  182. */
  183. virtual void SetupProperties(const Importer* pImp);
  184. // -------------------------------------------------------------------
  185. /** Executes the post processing step on the given imported data.
  186. * A process should throw an ImportErrorException* if it fails.
  187. * This method must be implemented by deriving classes.
  188. * @param pScene The imported data to work at.
  189. */
  190. virtual void Execute( aiScene* pScene) = 0;
  191. // -------------------------------------------------------------------
  192. /** Assign a new SharedPostProcessInfo to the step. This object
  193. * allows multiple postprocess steps to share data.
  194. * @param sh May be NULL
  195. */
  196. inline void SetSharedData(SharedPostProcessInfo* sh)
  197. {
  198. shared = sh;
  199. }
  200. // -------------------------------------------------------------------
  201. /** Get the shared data that is assigned to the step.
  202. */
  203. inline SharedPostProcessInfo* GetSharedData()
  204. {
  205. return shared;
  206. }
  207. protected:
  208. /** See the doc of #SharedPostProcessInfo for more details
  209. */
  210. SharedPostProcessInfo* shared;
  211. };
  212. } // end of namespace Assimp
  213. #endif // AI_BASEPROCESS_H_INC