2
0

types.h 16 KB

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