types.h 15 KB

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