types.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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 constexpr size_t AI_MAXLEN = 1024;
  101. #else
  102. #define AI_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(float _r, float _g, float _b) :
  139. r(_r), g(_g), b(_b) {}
  140. explicit aiColor3D(float _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. float operator[](unsigned int i) const {
  179. return *(&r + i);
  180. }
  181. /** Access a specific color component */
  182. float &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 float epsilon = float(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. float r, g, b;
  200. }; // !struct aiColor3D
  201. // ----------------------------------------------------------------------------------
  202. /**
  203. * @brief Represents an UTF-8 string, zero byte terminated.
  204. *
  205. * The character set of an aiString is explicitly defined to be UTF-8. This Unicode
  206. * transformation was chosen in the belief that most strings in 3d files are limited
  207. * to ASCII, thus the character set needed to be strictly ASCII compatible.
  208. *
  209. * Most text file loaders provide proper Unicode input file handling, special unicode
  210. * characters are correctly transcoded to UTF8 and are kept throughout the libraries'
  211. * import pipeline.
  212. *
  213. * For most applications, it will be absolutely sufficient to interpret the
  214. * aiString as ASCII data and work with it as one would work with a plain char*.
  215. * Windows users in need of proper support for i.e asian characters can use the
  216. * MultiByteToWideChar(), WideCharToMultiByte() WinAPI functionality to convert the
  217. * UTF-8 strings to their working character set (i.e. MBCS, WideChar).
  218. *
  219. * We use this representation instead of std::string to be C-compatible. The
  220. * (binary) length of such a string is limited to AI_MAXLEN characters (including the
  221. * the terminating zero).
  222. */
  223. struct aiString {
  224. #ifdef __cplusplus
  225. /** Default constructor, the string is set to have zero length */
  226. aiString() AI_NO_EXCEPT :
  227. length(0), data{'\0'} {
  228. #ifdef ASSIMP_BUILD_DEBUG
  229. // Debug build: overwrite the string on its full length with ESC (27)
  230. memset(data + 1, 27, AI_MAXLEN - 1);
  231. #endif
  232. }
  233. /** Copy constructor */
  234. aiString(const aiString &rOther) :
  235. length(rOther.length), data{'\0'} {
  236. // Crop the string to the maximum length
  237. length = length >= AI_MAXLEN ? AI_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()), data{'\0'} {
  244. length = length >= AI_MAXLEN ? AI_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() > AI_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, size_t maxlen) {
  259. if (sz == nullptr) {
  260. return;
  261. }
  262. size_t len = 0;
  263. for (size_t i=0; i<maxlen; ++i) {
  264. if (sz[i] == '\0') {
  265. break;
  266. }
  267. ++len;
  268. }
  269. if (len > AI_MAXLEN - 1) {
  270. len = AI_MAXLEN - 1;
  271. }
  272. length = static_cast<uint32_t>(len);
  273. memcpy(data, sz, len);
  274. data[len] = 0;
  275. }
  276. /** Assignment operator */
  277. aiString &operator=(const aiString &rOther) {
  278. if (this == &rOther) {
  279. return *this;
  280. }
  281. length = rOther.length;
  282. if (length > (AI_MAXLEN - 1)) {
  283. length = static_cast<ai_int32>(AI_MAXLEN - 1);
  284. }
  285. memcpy(data, rOther.data, length);
  286. data[length] = '\0';
  287. return *this;
  288. }
  289. /** Assign a const char* to the string */
  290. aiString &operator=(const char *sz) {
  291. Set(sz);
  292. return *this;
  293. }
  294. /** Assign a cstd::string to the string */
  295. aiString &operator=(const std::string &pString) {
  296. Set(pString);
  297. return *this;
  298. }
  299. /** Comparison operator */
  300. bool operator==(const aiString &other) const {
  301. if (length == other.length) {
  302. return memcmp(data, other.data, length) == 0;
  303. }
  304. return false;
  305. }
  306. /** Inverse comparison operator */
  307. bool operator!=(const aiString &other) const {
  308. return !(*this == other);
  309. }
  310. /** Append a string to the string */
  311. void Append(const char *app) {
  312. const ai_uint32 len = static_cast<ai_uint32>(::strlen(app));
  313. if (!len) {
  314. return;
  315. }
  316. if (length + len >= AI_MAXLEN) {
  317. return;
  318. }
  319. memcpy(&data[length], app, len + 1);
  320. length += len;
  321. }
  322. /** Clear the string - reset its length to zero */
  323. void Clear() {
  324. length = 0;
  325. data[0] = '\0';
  326. #ifdef ASSIMP_BUILD_DEBUG
  327. // Debug build: overwrite the string on its full length with ESC (27)
  328. memset(data + 1, 27, AI_MAXLEN - 1);
  329. #endif
  330. }
  331. /** Returns a pointer to the underlying zero-terminated array of characters */
  332. const char *C_Str() const {
  333. return data;
  334. }
  335. /**
  336. * @brief Will return true, if the string is empty.
  337. * @return true if the string is empty, false if not
  338. */
  339. bool Empty() const {
  340. return length == 0;
  341. }
  342. #endif // !__cplusplus
  343. /** Binary length of the string excluding the terminal 0. This is NOT the
  344. * logical length of strings containing UTF-8 multi-byte sequences! It's
  345. * the number of bytes from the beginning of the string to its end.*/
  346. ai_uint32 length;
  347. /** String buffer. Size limit is AI_MAXLEN */
  348. char data[AI_MAXLEN];
  349. }; // !struct aiString
  350. // ----------------------------------------------------------------------------------
  351. /** Standard return type for some library functions.
  352. * Rarely used, and if, mostly in the C API.
  353. */
  354. typedef enum aiReturn {
  355. /** Indicates that a function was successful */
  356. aiReturn_SUCCESS = 0x0,
  357. /** Indicates that a function failed */
  358. aiReturn_FAILURE = -0x1,
  359. /** Indicates that not enough memory was available
  360. * to perform the requested operation
  361. */
  362. aiReturn_OUTOFMEMORY = -0x3,
  363. /** @cond never
  364. * Force 32-bit size enum
  365. */
  366. _AI_ENFORCE_ENUM_SIZE = 0x7fffffff
  367. /// @endcond
  368. } aiReturn; // !enum aiReturn
  369. // just for backwards compatibility, don't use these constants anymore
  370. #define AI_SUCCESS aiReturn_SUCCESS
  371. #define AI_FAILURE aiReturn_FAILURE
  372. #define AI_OUTOFMEMORY aiReturn_OUTOFMEMORY
  373. // ----------------------------------------------------------------------------------
  374. /** Seek origins (for the virtual file system API).
  375. * Much cooler than using SEEK_SET, SEEK_CUR or SEEK_END.
  376. */
  377. enum aiOrigin {
  378. /** Beginning of the file */
  379. aiOrigin_SET = 0x0,
  380. /** Current position of the file pointer */
  381. aiOrigin_CUR = 0x1,
  382. /** End of the file, offsets must be negative */
  383. aiOrigin_END = 0x2,
  384. /** @cond never
  385. * Force 32-bit size enum
  386. */
  387. _AI_ORIGIN_ENFORCE_ENUM_SIZE = 0x7fffffff
  388. /// @endcond
  389. }; // !enum aiOrigin
  390. // ----------------------------------------------------------------------------------
  391. /** @brief Enumerates predefined log streaming destinations.
  392. * Logging to these streams can be enabled with a single call to
  393. * #LogStream::createDefaultStream.
  394. */
  395. enum aiDefaultLogStream {
  396. /** Stream the log to a file */
  397. aiDefaultLogStream_FILE = 0x1,
  398. /** Stream the log to std::cout */
  399. aiDefaultLogStream_STDOUT = 0x2,
  400. /** Stream the log to std::cerr */
  401. aiDefaultLogStream_STDERR = 0x4,
  402. /** MSVC only: Stream the log the the debugger
  403. * (this relies on OutputDebugString from the Win32 SDK)
  404. */
  405. aiDefaultLogStream_DEBUGGER = 0x8,
  406. /** @cond never
  407. * Force 32-bit size enum
  408. */
  409. _AI_DLS_ENFORCE_ENUM_SIZE = 0x7fffffff
  410. /// @endcond
  411. }; // !enum aiDefaultLogStream
  412. // just for backwards compatibility, don't use these constants anymore
  413. #define DLS_FILE aiDefaultLogStream_FILE
  414. #define DLS_STDOUT aiDefaultLogStream_STDOUT
  415. #define DLS_STDERR aiDefaultLogStream_STDERR
  416. #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER
  417. // ----------------------------------------------------------------------------------
  418. /** Stores the memory requirements for different components (e.g. meshes, materials,
  419. * animations) of an import. All sizes are in bytes.
  420. * @see Importer::GetMemoryRequirements()
  421. */
  422. struct aiMemoryInfo {
  423. #ifdef __cplusplus
  424. /** Default constructor */
  425. aiMemoryInfo() AI_NO_EXCEPT
  426. : textures(0),
  427. materials(0),
  428. meshes(0),
  429. nodes(0),
  430. animations(0),
  431. cameras(0),
  432. lights(0),
  433. total(0) {}
  434. #endif
  435. /** Storage allocated for texture data */
  436. unsigned int textures;
  437. /** Storage allocated for material data */
  438. unsigned int materials;
  439. /** Storage allocated for mesh data */
  440. unsigned int meshes;
  441. /** Storage allocated for node data */
  442. unsigned int nodes;
  443. /** Storage allocated for animation data */
  444. unsigned int animations;
  445. /** Storage allocated for camera data */
  446. unsigned int cameras;
  447. /** Storage allocated for light data */
  448. unsigned int lights;
  449. /** Total storage allocated for the full import. */
  450. unsigned int total;
  451. }; // !struct aiMemoryInfo
  452. /**
  453. * @brief Type to store a in-memory data buffer.
  454. */
  455. struct aiBuffer {
  456. const char *data; ///< Begin poiner
  457. const char *end; ///< End pointer
  458. #ifdef __cplusplus
  459. /// @brief The class constructor.
  460. aiBuffer() :
  461. data(nullptr), end(nullptr) {}
  462. /// @brief The class destructor.
  463. ~aiBuffer() = default;
  464. #endif //! __cplusplus
  465. };
  466. #ifdef __cplusplus
  467. }
  468. #endif //! __cplusplus
  469. // Include implementation files
  470. #include "vector2.inl"
  471. #include "vector3.inl"
  472. #include "color4.inl"
  473. #include "matrix3x3.inl"
  474. #include "matrix4x4.inl"
  475. #include "quaternion.inl"
  476. #endif // AI_TYPES_H_INC