types.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file types.h
  35. * Basic data types and primitives, such as vectors or colors.
  36. */
  37. #ifndef AI_TYPES_H_INC
  38. #define AI_TYPES_H_INC
  39. // Some runtime headers
  40. #include <sys/types.h>
  41. #include <memory.h>
  42. #include <math.h>
  43. #include <stddef.h>
  44. // Our compile configuration
  45. #include "defs.h"
  46. // Some types moved to separate header due to size of operators
  47. #include "vector3.h"
  48. #include "vector2.h"
  49. #include "color4.h"
  50. #include "matrix3x3.h"
  51. #include "matrix4x4.h"
  52. #include "quaternion.h"
  53. #ifdef __cplusplus
  54. #include <new> // for std::nothrow_t
  55. #include <string> // for aiString::Set(const std::string&)
  56. namespace Assimp {
  57. //! @cond never
  58. namespace Intern {
  59. // --------------------------------------------------------------------
  60. /** @brief Internal helper class to utilize our internal new/delete
  61. * routines for allocating object of this and derived classes.
  62. *
  63. * By doing this you can safely share class objects between Assimp
  64. * and the application - it works even over DLL boundaries. A good
  65. * example is the #IOSystem where the application allocates its custom
  66. * #IOSystem, then calls #Importer::SetIOSystem(). When the Importer
  67. * destructs, Assimp calls operator delete on the stored #IOSystem.
  68. * If it lies on a different heap than Assimp is working with,
  69. * the application is determined to crash.
  70. */
  71. // --------------------------------------------------------------------
  72. #ifndef SWIG
  73. struct ASSIMP_API AllocateFromAssimpHeap {
  74. // http://www.gotw.ca/publications/mill15.htm
  75. // new/delete overload
  76. void *operator new ( size_t num_bytes) /* throw( std::bad_alloc ) */;
  77. void *operator new ( size_t num_bytes, const std::nothrow_t& ) throw();
  78. void operator delete ( void* data);
  79. // array new/delete overload
  80. void *operator new[] ( size_t num_bytes) /* throw( std::bad_alloc ) */;
  81. void *operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw();
  82. void operator delete[] ( void* data);
  83. }; // struct AllocateFromAssimpHeap
  84. #endif
  85. } // namespace Intern
  86. //! @endcond
  87. } // namespace Assimp
  88. extern "C" {
  89. #endif
  90. /** Maximum dimension for strings, ASSIMP strings are zero terminated. */
  91. #ifdef __cplusplus
  92. const size_t MAXLEN = 1024;
  93. #else
  94. # define MAXLEN 1024
  95. #endif
  96. #include "./Compiler/pushpack1.h"
  97. // ----------------------------------------------------------------------------------
  98. /** Represents a plane in a three-dimensional, euclidean space
  99. */
  100. struct aiPlane
  101. {
  102. #ifdef __cplusplus
  103. aiPlane () : a(0.f), b(0.f), c(0.f), d(0.f) {}
  104. aiPlane (float _a, float _b, float _c, float _d)
  105. : a(_a), b(_b), c(_c), d(_d) {}
  106. aiPlane (const aiPlane& o) : a(o.a), b(o.b), c(o.c), d(o.d) {}
  107. #endif // !__cplusplus
  108. //! Plane equation
  109. float a,b,c,d;
  110. } PACK_STRUCT; // !struct aiPlane
  111. // ----------------------------------------------------------------------------------
  112. /** Represents a ray
  113. */
  114. struct aiRay
  115. {
  116. #ifdef __cplusplus
  117. aiRay () {}
  118. aiRay (const aiVector3D& _pos, const aiVector3D& _dir)
  119. : pos(_pos), dir(_dir) {}
  120. aiRay (const aiRay& o) : pos (o.pos), dir (o.dir) {}
  121. #endif // !__cplusplus
  122. //! Position and direction of the ray
  123. C_STRUCT aiVector3D pos, dir;
  124. } PACK_STRUCT; // !struct aiRay
  125. // ----------------------------------------------------------------------------------
  126. /** Represents a color in Red-Green-Blue space.
  127. */
  128. struct aiColor3D
  129. {
  130. #ifdef __cplusplus
  131. aiColor3D () : r(0.0f), g(0.0f), b(0.0f) {}
  132. aiColor3D (float _r, float _g, float _b) : r(_r), g(_g), b(_b) {}
  133. aiColor3D (float _r) : r(_r), g(_r), b(_r) {}
  134. aiColor3D (const aiColor3D& o) : r(o.r), g(o.g), b(o.b) {}
  135. /** Component-wise comparison */
  136. // TODO: add epsilon?
  137. bool operator == (const aiColor3D& other) const
  138. {return r == other.r && g == other.g && b == other.b;}
  139. /** Component-wise inverse comparison */
  140. // TODO: add epsilon?
  141. bool operator != (const aiColor3D& other) const
  142. {return r != other.r || g != other.g || b != other.b;}
  143. /** Component-wise addition */
  144. aiColor3D operator+(const aiColor3D& c) const {
  145. return aiColor3D(r+c.r,g+c.g,b+c.b);
  146. }
  147. /** Component-wise subtraction */
  148. aiColor3D operator-(const aiColor3D& c) const {
  149. return aiColor3D(r+c.r,g+c.g,b+c.b);
  150. }
  151. /** Component-wise multiplication */
  152. aiColor3D operator*(const aiColor3D& c) const {
  153. return aiColor3D(r*c.r,g*c.g,b*c.b);
  154. }
  155. /** Multiply with a scalar */
  156. aiColor3D operator*(float f) const {
  157. return aiColor3D(r*f,g*f,b*f);
  158. }
  159. /** Access a specific color component */
  160. float operator[](unsigned int i) const {
  161. return *(&r + i);
  162. }
  163. /** Access a specific color component */
  164. float& operator[](unsigned int i) {
  165. return *(&r + i);
  166. }
  167. /** Check whether a color is black */
  168. bool IsBlack() const {
  169. static const float epsilon = 10e-3f;
  170. return fabs( r ) < epsilon && fabs( g ) < epsilon && fabs( b ) < epsilon;
  171. }
  172. #endif // !__cplusplus
  173. //! Red, green and blue color values
  174. float r, g, b;
  175. } PACK_STRUCT; // !struct aiColor3D
  176. #include "./Compiler/poppack1.h"
  177. // ----------------------------------------------------------------------------------
  178. /** Represents an UTF-8 string, zero byte terminated.
  179. *
  180. * The character set of an aiString is explicitly defined to be UTF-8. This Unicode
  181. * transformation was chosen in the belief that most strings in 3d files are limited
  182. * to ASCII, thus the character set needed to be strictly ASCII compatible.
  183. *
  184. * Most text file loaders provide proper Unicode input file handling, special unicode
  185. * characters are correctly transcoded to UTF8 and are kept throughout the libraries'
  186. * import pipeline.
  187. *
  188. * For most applications, it will be absolutely sufficient to interpret the
  189. * aiString as ASCII data and work with it as one would work with a plain char*.
  190. * Windows users in need of proper support for i.e asian characters can use the
  191. * #MultiByteToWideChar(), #WideCharToMultiByte() WinAPI functionality to convert the
  192. * UTF-8 strings to their working character set (i.e. MBCS, WideChar).
  193. *
  194. * We use this representation instead of std::string to be C-compatible. The
  195. * (binary) length of such a string is limited to MAXLEN characters (including the
  196. * the terminating zero).
  197. */
  198. struct aiString
  199. {
  200. #ifdef __cplusplus
  201. /** Default constructor, the string is set to have zero length */
  202. aiString() :
  203. length(0)
  204. {
  205. data[0] = '\0';
  206. #ifdef _DEBUG
  207. // Debug build: overwrite the string on its full length with ESC (27)
  208. memset(data+1,27,MAXLEN-1);
  209. #endif
  210. }
  211. /** Copy constructor */
  212. aiString(const aiString& rOther) :
  213. length(rOther.length)
  214. {
  215. // Crop the string to the maximum length
  216. length = length>=MAXLEN?MAXLEN-1:length;
  217. memcpy( data, rOther.data, length);
  218. data[length] = '\0';
  219. }
  220. /** Constructor from std::string */
  221. explicit aiString(const std::string& pString) :
  222. length(pString.length())
  223. {
  224. length = length>=MAXLEN?MAXLEN-1:length;
  225. memcpy( data, pString.c_str(), length);
  226. data[length] = '\0';
  227. }
  228. /** Copy a std::string to the aiString */
  229. void Set( const std::string& pString) {
  230. if( pString.length() > MAXLEN - 1) {
  231. return;
  232. }
  233. length = pString.length();
  234. memcpy( data, pString.c_str(), length);
  235. data[length] = 0;
  236. }
  237. /** Copy a const char* to the aiString */
  238. void Set( const char* sz) {
  239. const size_t len = ::strlen(sz);
  240. if( len > MAXLEN - 1) {
  241. return;
  242. }
  243. length = len;
  244. memcpy( data, sz, len);
  245. data[len] = 0;
  246. }
  247. /** Assign a const char* to the string */
  248. aiString& operator = (const char* sz) {
  249. Set(sz);
  250. return *this;
  251. }
  252. /** Assign a cstd::string to the string */
  253. aiString& operator = ( const std::string& pString) {
  254. Set(pString);
  255. return *this;
  256. }
  257. /** Comparison operator */
  258. bool operator==(const aiString& other) const {
  259. return (length == other.length && 0 == memcmp(data,other.data,length));
  260. }
  261. /** Inverse comparison operator */
  262. bool operator!=(const aiString& other) const {
  263. return (length != other.length || 0 != memcmp(data,other.data,length));
  264. }
  265. /** Append a string to the string */
  266. void Append (const char* app) {
  267. const size_t len = strlen(app);
  268. if (!len) {
  269. return;
  270. }
  271. if (length + len >= MAXLEN) {
  272. return;
  273. }
  274. memcpy(&data[length],app,len+1);
  275. length += len;
  276. }
  277. /** Clear the string - reset its length to zero */
  278. void Clear () {
  279. length = 0;
  280. data[0] = '\0';
  281. #ifdef _DEBUG
  282. // Debug build: overwrite the string on its full length with ESC (27)
  283. memset(data+1,27,MAXLEN-1);
  284. #endif
  285. }
  286. /** Returns a pointer to the underlying zero-terminated array of characters */
  287. const char* C_Str() const {
  288. return data;
  289. }
  290. #endif // !__cplusplus
  291. /** Binary length of the string excluding the terminal 0. This is NOT the
  292. * logical length of strings containing UTF-8 multibyte sequences! It's
  293. * the number of bytes from the beginning of the string to its end.*/
  294. size_t length;
  295. /** String buffer. Size limit is MAXLEN */
  296. char data[MAXLEN];
  297. } ; // !struct aiString
  298. // ----------------------------------------------------------------------------------
  299. /** Standard return type for some library functions.
  300. * Rarely used, and if, mostly in the C API.
  301. */
  302. enum aiReturn
  303. {
  304. /** Indicates that a function was successful */
  305. aiReturn_SUCCESS = 0x0,
  306. /** Indicates that a function failed */
  307. aiReturn_FAILURE = -0x1,
  308. /** Indicates that not enough memory was available
  309. * to perform the requested operation
  310. */
  311. aiReturn_OUTOFMEMORY = -0x3,
  312. /** @cond never
  313. * Force 32-bit size enum
  314. */
  315. _AI_ENFORCE_ENUM_SIZE = 0x7fffffff
  316. }; // !enum aiReturn
  317. // just for backwards compatibility, don't use these constants anymore
  318. #define AI_SUCCESS aiReturn_SUCCESS
  319. #define AI_FAILURE aiReturn_FAILURE
  320. #define AI_OUTOFMEMORY aiReturn_OUTOFMEMORY
  321. // ----------------------------------------------------------------------------------
  322. /** Seek origins (for the virtual file system API).
  323. * Much cooler than using SEEK_SET, SEEK_CUR or SEEK_END.
  324. */
  325. enum aiOrigin
  326. {
  327. /** Beginning of the file */
  328. aiOrigin_SET = 0x0,
  329. /** Current position of the file pointer */
  330. aiOrigin_CUR = 0x1,
  331. /** End of the file, offsets must be negative */
  332. aiOrigin_END = 0x2,
  333. /** @cond never
  334. * Force 32-bit size enum
  335. */
  336. _AI_ORIGIN_ENFORCE_ENUM_SIZE = 0x7fffffff
  337. }; // !enum aiOrigin
  338. // ----------------------------------------------------------------------------------
  339. /** @brief Enumerates predefined log streaming destinations.
  340. * Logging to these streams can be enabled with a single call to
  341. * #LogStream::createDefaultStream or #aiAttachPredefinedLogStream(),
  342. * respectively.
  343. */
  344. enum aiDefaultLogStream
  345. {
  346. /** Stream the log to a file */
  347. aiDefaultLogStream_FILE = 0x1,
  348. /** Stream the log to std::cout */
  349. aiDefaultLogStream_STDOUT = 0x2,
  350. /** Stream the log to std::cerr */
  351. aiDefaultLogStream_STDERR = 0x4,
  352. /** MSVC only: Stream the log the the debugger
  353. * (this relies on OutputDebugString from the Win32 SDK)
  354. */
  355. aiDefaultLogStream_DEBUGGER = 0x8,
  356. /** @cond never
  357. * Force 32-bit size enum
  358. */
  359. _AI_DLS_ENFORCE_ENUM_SIZE = 0x7fffffff
  360. }; // !enum aiDefaultLogStream
  361. // just for backwards compatibility, don't use these constants anymore
  362. #define DLS_FILE aiDefaultLogStream_FILE
  363. #define DLS_STDOUT aiDefaultLogStream_STDOUT
  364. #define DLS_STDERR aiDefaultLogStream_STDERR
  365. #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER
  366. // ----------------------------------------------------------------------------------
  367. /** Stores the memory requirements for different components (e.g. meshes, materials,
  368. * animations) of an import. All sizes are in bytes.
  369. * @see Importer::GetMemoryRequirements()
  370. */
  371. struct aiMemoryInfo
  372. {
  373. #ifdef __cplusplus
  374. /** Default constructor */
  375. aiMemoryInfo()
  376. : textures (0)
  377. , materials (0)
  378. , meshes (0)
  379. , nodes (0)
  380. , animations (0)
  381. , cameras (0)
  382. , lights (0)
  383. , total (0)
  384. {}
  385. #endif
  386. /** Storage allocated for texture data */
  387. unsigned int textures;
  388. /** Storage allocated for material data */
  389. unsigned int materials;
  390. /** Storage allocated for mesh data */
  391. unsigned int meshes;
  392. /** Storage allocated for node data */
  393. unsigned int nodes;
  394. /** Storage allocated for animation data */
  395. unsigned int animations;
  396. /** Storage allocated for camera data */
  397. unsigned int cameras;
  398. /** Storage allocated for light data */
  399. unsigned int lights;
  400. /** Total storage allocated for the full import. */
  401. unsigned int total;
  402. }; // !struct aiMemoryInfo
  403. #ifdef __cplusplus
  404. }
  405. #endif //! __cplusplus
  406. // Include implementation files
  407. #include "vector2.inl"
  408. #include "vector3.inl"
  409. #include "color4.inl"
  410. #include "quaternion.inl"
  411. #include "matrix3x3.inl"
  412. #include "matrix4x4.inl"
  413. #endif