types.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 <memory.h>
  42. #include <math.h>
  43. #include <stddef.h>
  44. #include <string.h>
  45. #include <limits.h>
  46. // Our compile configuration
  47. #include "defs.h"
  48. // Some types moved to separate header due to size of operators
  49. #include "vector3.h"
  50. #include "vector2.h"
  51. #include "color4.h"
  52. #include "matrix3x3.h"
  53. #include "matrix4x4.h"
  54. #include "quaternion.h"
  55. #ifdef __cplusplus
  56. #include <cstring>
  57. #include <new> // for std::nothrow_t
  58. #include <string> // for aiString::Set(const std::string&)
  59. namespace Assimp {
  60. //! @cond never
  61. namespace Intern {
  62. // --------------------------------------------------------------------
  63. /** @brief Internal helper class to utilize our internal new/delete
  64. * routines for allocating object of this and derived classes.
  65. *
  66. * By doing this you can safely share class objects between Assimp
  67. * and the application - it works even over DLL boundaries. A good
  68. * example is the #IOSystem where the application allocates its custom
  69. * #IOSystem, then calls #Importer::SetIOSystem(). When the Importer
  70. * destructs, Assimp calls operator delete on the stored #IOSystem.
  71. * If it lies on a different heap than Assimp is working with,
  72. * the application is determined to crash.
  73. */
  74. // --------------------------------------------------------------------
  75. #ifndef SWIG
  76. struct ASSIMP_API AllocateFromAssimpHeap {
  77. // http://www.gotw.ca/publications/mill15.htm
  78. // new/delete overload
  79. void *operator new ( size_t num_bytes) /* throw( std::bad_alloc ) */;
  80. void *operator new ( size_t num_bytes, const std::nothrow_t& ) throw();
  81. void operator delete ( void* data);
  82. // array new/delete overload
  83. void *operator new[] ( size_t num_bytes) /* throw( std::bad_alloc ) */;
  84. void *operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw();
  85. void operator delete[] ( void* data);
  86. }; // struct AllocateFromAssimpHeap
  87. #endif
  88. } // namespace Intern
  89. //! @endcond
  90. } // namespace Assimp
  91. extern "C" {
  92. #endif
  93. /** Maximum dimension for strings, ASSIMP strings are zero terminated. */
  94. #ifdef __cplusplus
  95. const size_t MAXLEN = 1024;
  96. #else
  97. # define MAXLEN 1024
  98. #endif
  99. #include "./Compiler/pushpack1.h"
  100. // ----------------------------------------------------------------------------------
  101. /** Represents a plane in a three-dimensional, euclidean space
  102. */
  103. struct aiPlane
  104. {
  105. #ifdef __cplusplus
  106. aiPlane () : a(0.f), b(0.f), c(0.f), d(0.f) {}
  107. aiPlane (float _a, float _b, float _c, float _d)
  108. : a(_a), b(_b), c(_c), d(_d) {}
  109. aiPlane (const aiPlane& o) : a(o.a), b(o.b), c(o.c), d(o.d) {}
  110. #endif // !__cplusplus
  111. //! Plane equation
  112. float a,b,c,d;
  113. } PACK_STRUCT; // !struct aiPlane
  114. // ----------------------------------------------------------------------------------
  115. /** Represents a ray
  116. */
  117. struct aiRay
  118. {
  119. #ifdef __cplusplus
  120. aiRay () {}
  121. aiRay (const aiVector3D& _pos, const aiVector3D& _dir)
  122. : pos(_pos), dir(_dir) {}
  123. aiRay (const aiRay& o) : pos (o.pos), dir (o.dir) {}
  124. #endif // !__cplusplus
  125. //! Position and direction of the ray
  126. C_STRUCT aiVector3D pos, dir;
  127. } PACK_STRUCT; // !struct aiRay
  128. // ----------------------------------------------------------------------------------
  129. /** Represents a color in Red-Green-Blue space.
  130. */
  131. struct aiColor3D
  132. {
  133. #ifdef __cplusplus
  134. aiColor3D () : r(0.0f), g(0.0f), b(0.0f) {}
  135. aiColor3D (float _r, float _g, float _b) : r(_r), g(_g), b(_b) {}
  136. aiColor3D (float _r) : r(_r), g(_r), b(_r) {}
  137. aiColor3D (const aiColor3D& o) : r(o.r), g(o.g), b(o.b) {}
  138. /** Component-wise comparison */
  139. // TODO: add epsilon?
  140. bool operator == (const aiColor3D& other) const
  141. {return r == other.r && g == other.g && b == other.b;}
  142. /** Component-wise inverse comparison */
  143. // TODO: add epsilon?
  144. bool operator != (const aiColor3D& other) const
  145. {return r != other.r || g != other.g || b != other.b;}
  146. /** Component-wise comparison */
  147. // TODO: add epsilon?
  148. bool operator < (const aiColor3D& other) const {
  149. return r < other.r || (
  150. r == other.r && (g < other.g ||
  151. (g == other.g && b < other.b)
  152. )
  153. );
  154. }
  155. /** Component-wise addition */
  156. aiColor3D operator+(const aiColor3D& c) const {
  157. return aiColor3D(r+c.r,g+c.g,b+c.b);
  158. }
  159. /** Component-wise subtraction */
  160. aiColor3D operator-(const aiColor3D& c) const {
  161. return aiColor3D(r-c.r,g-c.g,b-c.b);
  162. }
  163. /** Component-wise multiplication */
  164. aiColor3D operator*(const aiColor3D& c) const {
  165. return aiColor3D(r*c.r,g*c.g,b*c.b);
  166. }
  167. /** Multiply with a scalar */
  168. aiColor3D operator*(float f) const {
  169. return aiColor3D(r*f,g*f,b*f);
  170. }
  171. /** Access a specific color component */
  172. float operator[](unsigned int i) const {
  173. return *(&r + i);
  174. }
  175. /** Access a specific color component */
  176. float& operator[](unsigned int i) {
  177. return *(&r + i);
  178. }
  179. /** Check whether a color is black */
  180. bool IsBlack() const {
  181. static const float epsilon = 10e-3f;
  182. return fabs( r ) < epsilon && fabs( g ) < epsilon && fabs( b ) < epsilon;
  183. }
  184. #endif // !__cplusplus
  185. //! Red, green and blue color values
  186. float r, g, b;
  187. } PACK_STRUCT; // !struct aiColor3D
  188. #include "./Compiler/poppack1.h"
  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. /** Assign a const char* to the string */
  260. aiString& operator = (const char* sz) {
  261. Set(sz);
  262. return *this;
  263. }
  264. /** Assign a cstd::string to the string */
  265. aiString& operator = ( const std::string& pString) {
  266. Set(pString);
  267. return *this;
  268. }
  269. /** Comparison operator */
  270. bool operator==(const aiString& other) const {
  271. return (length == other.length && 0 == memcmp(data,other.data,length));
  272. }
  273. /** Inverse comparison operator */
  274. bool operator!=(const aiString& other) const {
  275. return (length != other.length || 0 != memcmp(data,other.data,length));
  276. }
  277. /** Append a string to the string */
  278. void Append (const char* app) {
  279. const size_t len = ::strlen(app);
  280. if (!len) {
  281. return;
  282. }
  283. if (length + len >= MAXLEN) {
  284. return;
  285. }
  286. memcpy(&data[length],app,len+1);
  287. length += len;
  288. }
  289. /** Clear the string - reset its length to zero */
  290. void Clear () {
  291. length = 0;
  292. data[0] = '\0';
  293. #ifdef ASSIMP_BUILD_DEBUG
  294. // Debug build: overwrite the string on its full length with ESC (27)
  295. memset(data+1,27,MAXLEN-1);
  296. #endif
  297. }
  298. /** Returns a pointer to the underlying zero-terminated array of characters */
  299. const char* C_Str() const {
  300. return data;
  301. }
  302. #endif // !__cplusplus
  303. /** Binary length of the string excluding the terminal 0. This is NOT the
  304. * logical length of strings containing UTF-8 multibyte sequences! It's
  305. * the number of bytes from the beginning of the string to its end.*/
  306. size_t length;
  307. /** String buffer. Size limit is MAXLEN */
  308. char data[MAXLEN];
  309. } ; // !struct aiString
  310. // ----------------------------------------------------------------------------------
  311. /** Standard return type for some library functions.
  312. * Rarely used, and if, mostly in the C API.
  313. */
  314. typedef enum aiReturn
  315. {
  316. /** Indicates that a function was successful */
  317. aiReturn_SUCCESS = 0x0,
  318. /** Indicates that a function failed */
  319. aiReturn_FAILURE = -0x1,
  320. /** Indicates that not enough memory was available
  321. * to perform the requested operation
  322. */
  323. aiReturn_OUTOFMEMORY = -0x3,
  324. /** @cond never
  325. * Force 32-bit size enum
  326. */
  327. _AI_ENFORCE_ENUM_SIZE = 0x7fffffff
  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. }; // !enum aiOrigin
  350. // ----------------------------------------------------------------------------------
  351. /** @brief Enumerates predefined log streaming destinations.
  352. * Logging to these streams can be enabled with a single call to
  353. * #LogStream::createDefaultStream or #aiAttachPredefinedLogStream(),
  354. * respectively.
  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. }; // !enum aiDefaultLogStream
  373. // just for backwards compatibility, don't use these constants anymore
  374. #define DLS_FILE aiDefaultLogStream_FILE
  375. #define DLS_STDOUT aiDefaultLogStream_STDOUT
  376. #define DLS_STDERR aiDefaultLogStream_STDERR
  377. #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER
  378. // ----------------------------------------------------------------------------------
  379. /** Stores the memory requirements for different components (e.g. meshes, materials,
  380. * animations) of an import. All sizes are in bytes.
  381. * @see Importer::GetMemoryRequirements()
  382. */
  383. struct aiMemoryInfo
  384. {
  385. #ifdef __cplusplus
  386. /** Default constructor */
  387. aiMemoryInfo()
  388. : textures (0)
  389. , materials (0)
  390. , meshes (0)
  391. , nodes (0)
  392. , animations (0)
  393. , cameras (0)
  394. , lights (0)
  395. , total (0)
  396. {}
  397. #endif
  398. /** Storage allocated for texture data */
  399. unsigned int textures;
  400. /** Storage allocated for material data */
  401. unsigned int materials;
  402. /** Storage allocated for mesh data */
  403. unsigned int meshes;
  404. /** Storage allocated for node data */
  405. unsigned int nodes;
  406. /** Storage allocated for animation data */
  407. unsigned int animations;
  408. /** Storage allocated for camera data */
  409. unsigned int cameras;
  410. /** Storage allocated for light data */
  411. unsigned int lights;
  412. /** Total storage allocated for the full import. */
  413. unsigned int total;
  414. }; // !struct aiMemoryInfo
  415. #ifdef __cplusplus
  416. }
  417. #endif //! __cplusplus
  418. // Include implementation files
  419. #include "vector2.inl"
  420. #include "vector3.inl"
  421. #include "color4.inl"
  422. #include "quaternion.inl"
  423. #include "matrix3x3.inl"
  424. #include "matrix4x4.inl"
  425. #endif