types.h 15 KB

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