types.h 16 KB

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