types.h 16 KB

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