aiTypes.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 aiTypes.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 <memory.h>
  42. #include <math.h>
  43. #include <stddef.h>
  44. // Our compile configuration
  45. #include "aiDefines.h"
  46. // Some types moved to separate header due to size of operators
  47. #include "aiVector3D.h"
  48. #include "aiVector2D.h"
  49. #include "aiMatrix3x3.h"
  50. #include "aiMatrix4x4.h"
  51. #include "aiQuaternion.h"
  52. #ifdef __cplusplus
  53. # include <string> // for aiString::Set(const std::string&)
  54. namespace Assimp {
  55. namespace Intern {
  56. // --------------------------------------------------------------------
  57. /** @brief Internal helper class to utilize our internal new/delete
  58. * routines for allocating object of this and derived classes.
  59. *
  60. * By doing this you can safely share class objects between Assimp
  61. * and the application - it works even over DLL boundaries. A good
  62. * example is the #IOSystem where the application allocates its custom
  63. * #IOSystem, then calls #Importer::SetIOSystem(). When the Importer
  64. * destructs, Assimp calls operator delete on the stored #IOSystem.
  65. * If it lies on a different heap than Assimp is working with,
  66. * the application is determined to crash.
  67. */
  68. // --------------------------------------------------------------------
  69. struct ASSIMP_API AllocateFromAssimpHeap {
  70. // new/delete overload
  71. void *operator new ( size_t num_bytes);
  72. void operator delete ( void* data);
  73. // array new/delete overload
  74. void *operator new[] ( size_t num_bytes);
  75. void operator delete[] ( void* data);
  76. }; //! struct AllocateFromAssimpHeap
  77. } //! namespace Intern
  78. } //! namespace Assimp
  79. extern "C" {
  80. #endif
  81. /** Maximum dimension for strings, ASSIMP strings are zero terminated. */
  82. #ifdef __cplusplus
  83. const size_t MAXLEN = 1024;
  84. #else
  85. # define MAXLEN 1024
  86. #endif
  87. #include "./Compiler/pushpack1.h"
  88. // ----------------------------------------------------------------------------------
  89. /** Represents a plane in a three-dimensional, euclidean space
  90. */
  91. struct aiPlane
  92. {
  93. #ifdef __cplusplus
  94. aiPlane () : a(0.f), b(0.f), c(0.f), d(0.f) {}
  95. aiPlane (float _a, float _b, float _c, float _d)
  96. : a(_a), b(_b), c(_c), d(_d) {}
  97. aiPlane (const aiPlane& o) : a(o.a), b(o.b), c(o.c), d(o.d) {}
  98. #endif // !__cplusplus
  99. //! Plane equation
  100. float a,b,c,d;
  101. } PACK_STRUCT; // !struct aiPlane
  102. // ----------------------------------------------------------------------------------
  103. /** Represents a ray
  104. */
  105. struct aiRay
  106. {
  107. #ifdef __cplusplus
  108. aiRay () {}
  109. aiRay (const aiVector3D& _pos, const aiVector3D& _dir)
  110. : pos(_pos), dir(_dir) {}
  111. aiRay (const aiRay& o) : pos (o.pos), dir (o.dir) {}
  112. #endif // !__cplusplus
  113. //! Position and direction of the ray
  114. C_STRUCT aiVector3D pos, dir;
  115. } PACK_STRUCT; // !struct aiRay
  116. // ----------------------------------------------------------------------------------
  117. /** Represents a color in Red-Green-Blue space.
  118. */
  119. struct aiColor3D
  120. {
  121. #ifdef __cplusplus
  122. aiColor3D () : r(0.0f), g(0.0f), b(0.0f) {}
  123. aiColor3D (float _r, float _g, float _b) : r(_r), g(_g), b(_b) {}
  124. aiColor3D (const aiColor3D& o) : r(o.r), g(o.g), b(o.b) {}
  125. /** Component-wise comparison */
  126. // TODO: add epsilon?
  127. bool operator == (const aiColor3D& other) const
  128. {return r == other.r && g == other.g && b == other.b;}
  129. /** Component-wise inverse comparison */
  130. // TODO: add epsilon?
  131. bool operator != (const aiColor3D& other) const
  132. {return r != other.r || g != other.g || b != other.b;}
  133. /** Component-wise addition */
  134. aiColor3D operator+(const aiColor3D& c) const {
  135. return aiColor3D(r+c.r,g+c.g,b+c.b);
  136. }
  137. /** Component-wise subtraction */
  138. aiColor3D operator-(const aiColor3D& c) const {
  139. return aiColor3D(r+c.r,g+c.g,b+c.b);
  140. }
  141. /** Component-wise multiplication */
  142. aiColor3D operator*(const aiColor3D& c) const {
  143. return aiColor3D(r*c.r,g*c.g,b*c.b);
  144. }
  145. /** Multiply with a scalar */
  146. aiColor3D operator*(float f) const {
  147. return aiColor3D(r*f,g*f,b*f);
  148. }
  149. /** Access a specific color component */
  150. float operator[](unsigned int i) const {
  151. return *(&r + i);
  152. }
  153. /** Access a specific color component */
  154. float& operator[](unsigned int i) {
  155. return *(&r + i);
  156. }
  157. /** Check whether a color is black */
  158. bool IsBlack() const {
  159. static const float epsilon = 10e-3f;
  160. return fabs( r ) < epsilon && fabs( g ) < epsilon && fabs( b ) < epsilon;
  161. }
  162. #endif // !__cplusplus
  163. //! Red, green and blue color values
  164. float r, g, b;
  165. } PACK_STRUCT; // !struct aiColor3D
  166. // ----------------------------------------------------------------------------------
  167. /** Represents a color in Red-Green-Blue space including an
  168. * alpha component.
  169. */
  170. struct aiColor4D
  171. {
  172. #ifdef __cplusplus
  173. aiColor4D () : r(0.0f), g(0.0f), b(0.0f), a(0.0f) {}
  174. aiColor4D (float _r, float _g, float _b, float _a)
  175. : r(_r), g(_g), b(_b), a(_a) {}
  176. aiColor4D (const aiColor4D& o)
  177. : r(o.r), g(o.g), b(o.b), a(o.a) {}
  178. /** Component-wise comparison */
  179. // TODO: add epsilon?
  180. bool operator == (const aiColor4D& other) const {
  181. return r == other.r && g == other.g && b == other.b && a == other.a;
  182. }
  183. /** Component-wise inverse comparison */
  184. // TODO: add epsilon?
  185. bool operator != (const aiColor4D& other) const {
  186. return r != other.r || g != other.g || b != other.b || a != other.a;
  187. }
  188. /** Access a specific color component */
  189. inline float operator[](unsigned int i) const {
  190. return *(&r + i);
  191. }
  192. /** Access a specific color component */
  193. inline float& operator[](unsigned int i) {
  194. return *(&r + i);
  195. }
  196. /** Check whether a color is black */
  197. inline bool IsBlack() const
  198. {
  199. // The alpha component doesn't care here. black is black.
  200. static const float epsilon = 10e-3f;
  201. return fabs( r ) < epsilon && fabs( g ) < epsilon && fabs( b ) < epsilon;
  202. }
  203. #endif // !__cplusplus
  204. //! Red, green, blue and alpha color values
  205. float r, g, b, a;
  206. } PACK_STRUCT; // !struct aiColor4D
  207. #include "./Compiler/poppack1.h"
  208. // ----------------------------------------------------------------------------------
  209. /** Represents an UTF-8 string, zero byte terminated.
  210. *
  211. * The character set of an aiString is explicitly defined to be UTF-8. This Unicode
  212. * transformation was chosen in the belief that most strings in 3d files are limited
  213. * to the ASCII characters, thus the character set needed to be ASCII compatible.
  214. *
  215. * Most text file loaders provide proper Unicode input file handling, special unicode
  216. * characters are correctly transcoded to UTF8 and are kept throughout the libraries'
  217. * import pipeline.
  218. *
  219. * For most applications, it will be absolutely sufficient to interpret the
  220. * aiString as ASCII data and work with it as one would work with a plain char*.
  221. * Windows users in need of proper support for i.e asian characters can use the
  222. * #MultiByteToWideChar(), #WideCharToMultiByte() WinAPI functionality to convert the
  223. * UTF-8 strings to their working character set (i.e. MBCS, WideChar).
  224. *
  225. * We use this representation instead of std::string to be C-compatible. The
  226. * (binary) length of such a string is limited to MAXLEN characters (excluding the 0).
  227. */
  228. struct aiString
  229. {
  230. #ifdef __cplusplus
  231. /** Default constructor, the string is set to have zero length */
  232. aiString() :
  233. length(0)
  234. {
  235. data[0] = '\0';
  236. #ifdef _DEBUG
  237. // Debug build: overwrite the string on its full length with ESC (27)
  238. memset(data+1,27,MAXLEN-1);
  239. #endif
  240. }
  241. /** Copy constructor */
  242. aiString(const aiString& rOther) :
  243. length(rOther.length)
  244. {
  245. memcpy( data, rOther.data, rOther.length);
  246. data[length] = '\0';
  247. }
  248. /** Constructor from std::string */
  249. aiString(const std::string& pString) :
  250. length(pString.length())
  251. {
  252. memcpy( data, pString.c_str(), length);
  253. data[length] = '\0';
  254. }
  255. /** Copy a std::string to the aiString */
  256. void Set( const std::string& pString) {
  257. if( pString.length() > MAXLEN - 1) {
  258. return;
  259. }
  260. length = pString.length();
  261. ::memcpy( data, pString.c_str(), length);
  262. data[length] = 0;
  263. }
  264. /** Copy a const char* to the aiString */
  265. void Set( const char* sz) {
  266. const size_t len = ::strlen(sz);
  267. if( len > MAXLEN - 1) {
  268. return;
  269. }
  270. length = len;
  271. ::memcpy( data, sz, len);
  272. data[len] = 0;
  273. }
  274. /** Assign a const char* to the string */
  275. aiString& operator = (const char* sz) {
  276. Set(sz);
  277. return *this;
  278. }
  279. /** Assign a cstd::string to the string */
  280. aiString& operator = ( const std::string& pString) {
  281. Set(pString);
  282. return *this;
  283. }
  284. /** Comparison operator */
  285. bool operator==(const aiString& other) const {
  286. return (length == other.length && 0 == strcmp(this->data,other.data));
  287. }
  288. /** Inverse comparison operator */
  289. bool operator!=(const aiString& other) const {
  290. return (length != other.length || 0 != ::strcmp(this->data,other.data));
  291. }
  292. /** Append a string to the string */
  293. void Append (const char* app) {
  294. const size_t len = ::strlen(app);
  295. if (!len) {
  296. return;
  297. }
  298. if (length + len >= MAXLEN) {
  299. return;
  300. }
  301. memcpy(&data[length],app,len+1);
  302. length += len;
  303. }
  304. /** Clear the string - reset its length to zero */
  305. void Clear () {
  306. length = 0;
  307. data[0] = '\0';
  308. #ifdef _DEBUG
  309. // Debug build: overwrite the string on its full length with ESC (27)
  310. memset(data+1,27,MAXLEN-1);
  311. #endif
  312. }
  313. #endif // !__cplusplus
  314. /** Binary length of the string excluding the terminal 0. This is NOT the
  315. * logical length of strings containing UTF-8 multibyte sequences! It's
  316. * the number of bytes from the beginning of the string to its end.*/
  317. size_t length;
  318. /** String buffer. Size limit is MAXLEN */
  319. char data[MAXLEN];
  320. } ; // !struct aiString
  321. // ----------------------------------------------------------------------------------
  322. /** Standard return type for some library functions.
  323. * Rarely used, and if, mostly in the C API.
  324. */
  325. enum aiReturn
  326. {
  327. /** Indicates that a function was successful */
  328. aiReturn_SUCCESS = 0x0,
  329. /** Indicates that a function failed */
  330. aiReturn_FAILURE = -0x1,
  331. /** Indicates that not enough memory was available
  332. * to perform the requested operation
  333. */
  334. aiReturn_OUTOFMEMORY = -0x3,
  335. /** @cond never
  336. * Force 32-bit size enum
  337. */
  338. _AI_ENFORCE_ENUM_SIZE = 0x7fffffff
  339. }; // !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. }; // !enum aiOrigin
  361. // ----------------------------------------------------------------------------------
  362. /** @brief Enumerates predefined log streaming destinations.
  363. * Logging to these streams can be enabled with a single call to
  364. * #LogStream::createDefaultStream or #aiAttachPredefinedLogStream(),
  365. * respectively.
  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. }; // !enum aiDefaultLogStream
  384. // just for backwards compatibility, don't use these constants anymore
  385. #define DLS_FILE aiDefaultLogStream_FILE
  386. #define DLS_STDOUT aiDefaultLogStream_STDOUT
  387. #define DLS_STDERR aiDefaultLogStream_STDERR
  388. #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER
  389. // ----------------------------------------------------------------------------------
  390. /** Stores the memory requirements for different components (e.g. meshes, materials,
  391. * animations) of an import. All sizes are in bytes.
  392. * @see Importer::GetMemoryRequirements()
  393. */
  394. struct aiMemoryInfo
  395. {
  396. #ifdef __cplusplus
  397. /** Default constructor */
  398. aiMemoryInfo()
  399. : textures (0)
  400. , materials (0)
  401. , meshes (0)
  402. , nodes (0)
  403. , animations (0)
  404. , cameras (0)
  405. , lights (0)
  406. , total (0)
  407. {}
  408. #endif
  409. /** Storage allocated for texture data */
  410. unsigned int textures;
  411. /** Storage allocated for material data */
  412. unsigned int materials;
  413. /** Storage allocated for mesh data */
  414. unsigned int meshes;
  415. /** Storage allocated for node data */
  416. unsigned int nodes;
  417. /** Storage allocated for animation data */
  418. unsigned int animations;
  419. /** Storage allocated for camera data */
  420. unsigned int cameras;
  421. /** Storage allocated for light data */
  422. unsigned int lights;
  423. /** Total storage allocated for the full import. */
  424. unsigned int total;
  425. }; // !struct aiMemoryInfo
  426. #ifdef __cplusplus
  427. }
  428. #endif //! __cplusplus
  429. // Include implementations
  430. #include "aiVector3D.inl"
  431. #include "aiMatrix3x3.inl"
  432. #include "aiMatrix4x4.inl"
  433. #endif //!! include guard