1 | /* |
2 | --------------------------------------------------------------------------- |
3 | Open Asset Import Library (assimp) |
4 | --------------------------------------------------------------------------- |
5 | |
6 | Copyright (c) 2006-2019, assimp team |
7 | |
8 | |
9 | |
10 | All rights reserved. |
11 | |
12 | Redistribution and use of this software in source and binary forms, |
13 | with or without modification, are permitted provided that the following |
14 | conditions are met: |
15 | |
16 | * Redistributions of source code must retain the above |
17 | copyright notice, this list of conditions and the |
18 | following disclaimer. |
19 | |
20 | * Redistributions in binary form must reproduce the above |
21 | copyright notice, this list of conditions and the |
22 | following disclaimer in the documentation and/or other |
23 | materials provided with the distribution. |
24 | |
25 | * Neither the name of the assimp team, nor the names of its |
26 | contributors may be used to endorse or promote products |
27 | derived from this software without specific prior |
28 | written permission of the assimp team. |
29 | |
30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
33 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
34 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
35 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
36 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
37 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
38 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
40 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
41 | --------------------------------------------------------------------------- |
42 | */ |
43 | |
44 | /** @file types.h |
45 | * Basic data types and primitives, such as vectors or colors. |
46 | */ |
47 | #pragma once |
48 | #ifndef AI_TYPES_H_INC |
49 | #define AI_TYPES_H_INC |
50 | |
51 | // Some runtime headers |
52 | #include <sys/types.h> |
53 | #include <stddef.h> |
54 | #include <string.h> |
55 | #include <limits.h> |
56 | #include <stdint.h> |
57 | |
58 | // Our compile configuration |
59 | #include "defs.h" |
60 | |
61 | // Some types moved to separate header due to size of operators |
62 | #include "vector3.h" |
63 | #include "vector2.h" |
64 | #include "color4.h" |
65 | #include "matrix3x3.h" |
66 | #include "matrix4x4.h" |
67 | #include "quaternion.h" |
68 | |
69 | typedef int32_t ai_int32; |
70 | typedef uint32_t ai_uint32 ; |
71 | |
72 | #ifdef __cplusplus |
73 | #include <cstring> |
74 | #include <new> // for std::nothrow_t |
75 | #include <string> // for aiString::Set(const std::string&) |
76 | |
77 | |
78 | namespace Assimp { |
79 | //! @cond never |
80 | namespace Intern { |
81 | // -------------------------------------------------------------------- |
82 | /** @brief Internal helper class to utilize our internal new/delete |
83 | * routines for allocating object of this and derived classes. |
84 | * |
85 | * By doing this you can safely share class objects between Assimp |
86 | * and the application - it works even over DLL boundaries. A good |
87 | * example is the #IOSystem where the application allocates its custom |
88 | * #IOSystem, then calls #Importer::SetIOSystem(). When the Importer |
89 | * destructs, Assimp calls operator delete on the stored #IOSystem. |
90 | * If it lies on a different heap than Assimp is working with, |
91 | * the application is determined to crash. |
92 | */ |
93 | // -------------------------------------------------------------------- |
94 | #ifndef SWIG |
95 | struct ASSIMP_API AllocateFromAssimpHeap { |
96 | // http://www.gotw.ca/publications/mill15.htm |
97 | |
98 | // new/delete overload |
99 | void *operator new ( size_t num_bytes) /* throw( std::bad_alloc ) */; |
100 | void *operator new ( size_t num_bytes, const std::nothrow_t& ) throw(); |
101 | void operator delete ( void* data); |
102 | |
103 | // array new/delete overload |
104 | void *operator new[] ( size_t num_bytes) /* throw( std::bad_alloc ) */; |
105 | void *operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw(); |
106 | void operator delete[] ( void* data); |
107 | |
108 | }; // struct AllocateFromAssimpHeap |
109 | #endif |
110 | } // namespace Intern |
111 | //! @endcond |
112 | } // namespace Assimp |
113 | |
114 | extern "C" { |
115 | #endif |
116 | |
117 | /** Maximum dimension for strings, ASSIMP strings are zero terminated. */ |
118 | #ifdef __cplusplus |
119 | static const size_t MAXLEN = 1024; |
120 | #else |
121 | # define MAXLEN 1024 |
122 | #endif |
123 | |
124 | // ---------------------------------------------------------------------------------- |
125 | /** Represents a plane in a three-dimensional, euclidean space |
126 | */ |
127 | struct aiPlane { |
128 | #ifdef __cplusplus |
129 | aiPlane () AI_NO_EXCEPT : a(0.f), b(0.f), c(0.f), d(0.f) {} |
130 | aiPlane (ai_real _a, ai_real _b, ai_real _c, ai_real _d) |
131 | : a(_a), b(_b), c(_c), d(_d) {} |
132 | |
133 | aiPlane (const aiPlane& o) : a(o.a), b(o.b), c(o.c), d(o.d) {} |
134 | |
135 | #endif // !__cplusplus |
136 | |
137 | //! Plane equation |
138 | ai_real a,b,c,d; |
139 | }; // !struct aiPlane |
140 | |
141 | // ---------------------------------------------------------------------------------- |
142 | /** Represents a ray |
143 | */ |
144 | struct aiRay { |
145 | #ifdef __cplusplus |
146 | aiRay () AI_NO_EXCEPT {} |
147 | aiRay (const aiVector3D& _pos, const aiVector3D& _dir) |
148 | : pos(_pos), dir(_dir) {} |
149 | |
150 | aiRay (const aiRay& o) : pos (o.pos), dir (o.dir) {} |
151 | |
152 | #endif // !__cplusplus |
153 | |
154 | //! Position and direction of the ray |
155 | C_STRUCT aiVector3D pos, dir; |
156 | }; // !struct aiRay |
157 | |
158 | // ---------------------------------------------------------------------------------- |
159 | /** Represents a color in Red-Green-Blue space. |
160 | */ |
161 | struct aiColor3D |
162 | { |
163 | #ifdef __cplusplus |
164 | aiColor3D () AI_NO_EXCEPT : r(0.0f), g(0.0f), b(0.0f) {} |
165 | aiColor3D (ai_real _r, ai_real _g, ai_real _b) : r(_r), g(_g), b(_b) {} |
166 | explicit aiColor3D (ai_real _r) : r(_r), g(_r), b(_r) {} |
167 | aiColor3D (const aiColor3D& o) : r(o.r), g(o.g), b(o.b) {} |
168 | |
169 | aiColor3D &operator=(const aiColor3D &o) { |
170 | r = o.r; |
171 | g = o.g; |
172 | b = o.b; |
173 | return *this; |
174 | } |
175 | |
176 | /** Component-wise comparison */ |
177 | // TODO: add epsilon? |
178 | bool operator == (const aiColor3D& other) const |
179 | {return r == other.r && g == other.g && b == other.b;} |
180 | |
181 | /** Component-wise inverse comparison */ |
182 | // TODO: add epsilon? |
183 | bool operator != (const aiColor3D& other) const |
184 | {return r != other.r || g != other.g || b != other.b;} |
185 | |
186 | /** Component-wise comparison */ |
187 | // TODO: add epsilon? |
188 | bool operator < (const aiColor3D& other) const { |
189 | return r < other.r || ( r == other.r && (g < other.g || (g == other.g && b < other.b ) ) ); |
190 | } |
191 | |
192 | /** Component-wise addition */ |
193 | aiColor3D operator+(const aiColor3D& c) const { |
194 | return aiColor3D(r+c.r,g+c.g,b+c.b); |
195 | } |
196 | |
197 | /** Component-wise subtraction */ |
198 | aiColor3D operator-(const aiColor3D& c) const { |
199 | return aiColor3D(r-c.r,g-c.g,b-c.b); |
200 | } |
201 | |
202 | /** Component-wise multiplication */ |
203 | aiColor3D operator*(const aiColor3D& c) const { |
204 | return aiColor3D(r*c.r,g*c.g,b*c.b); |
205 | } |
206 | |
207 | /** Multiply with a scalar */ |
208 | aiColor3D operator*(ai_real f) const { |
209 | return aiColor3D(r*f,g*f,b*f); |
210 | } |
211 | |
212 | /** Access a specific color component */ |
213 | ai_real operator[](unsigned int i) const { |
214 | return *(&r + i); |
215 | } |
216 | |
217 | /** Access a specific color component */ |
218 | ai_real& operator[](unsigned int i) { |
219 | if ( 0 == i ) { |
220 | return r; |
221 | } else if ( 1 == i ) { |
222 | return g; |
223 | } else if ( 2 == i ) { |
224 | return b; |
225 | } |
226 | return r; |
227 | } |
228 | |
229 | /** Check whether a color is black */ |
230 | bool IsBlack() const { |
231 | static const ai_real epsilon = ai_real(10e-3); |
232 | return std::fabs( x: r ) < epsilon && std::fabs( x: g ) < epsilon && std::fabs( x: b ) < epsilon; |
233 | } |
234 | |
235 | #endif // !__cplusplus |
236 | |
237 | //! Red, green and blue color values |
238 | ai_real r, g, b; |
239 | }; // !struct aiColor3D |
240 | |
241 | // ---------------------------------------------------------------------------------- |
242 | /** Represents an UTF-8 string, zero byte terminated. |
243 | * |
244 | * The character set of an aiString is explicitly defined to be UTF-8. This Unicode |
245 | * transformation was chosen in the belief that most strings in 3d files are limited |
246 | * to ASCII, thus the character set needed to be strictly ASCII compatible. |
247 | * |
248 | * Most text file loaders provide proper Unicode input file handling, special unicode |
249 | * characters are correctly transcoded to UTF8 and are kept throughout the libraries' |
250 | * import pipeline. |
251 | * |
252 | * For most applications, it will be absolutely sufficient to interpret the |
253 | * aiString as ASCII data and work with it as one would work with a plain char*. |
254 | * Windows users in need of proper support for i.e asian characters can use the |
255 | * MultiByteToWideChar(), WideCharToMultiByte() WinAPI functionality to convert the |
256 | * UTF-8 strings to their working character set (i.e. MBCS, WideChar). |
257 | * |
258 | * We use this representation instead of std::string to be C-compatible. The |
259 | * (binary) length of such a string is limited to MAXLEN characters (including the |
260 | * the terminating zero). |
261 | */ |
262 | struct aiString |
263 | { |
264 | #ifdef __cplusplus |
265 | /** Default constructor, the string is set to have zero length */ |
266 | aiString() AI_NO_EXCEPT |
267 | : length( 0 ) { |
268 | data[0] = '\0'; |
269 | |
270 | #ifdef ASSIMP_BUILD_DEBUG |
271 | // Debug build: overwrite the string on its full length with ESC (27) |
272 | memset(s: data+1,c: 27,n: MAXLEN-1); |
273 | #endif |
274 | } |
275 | |
276 | /** Copy constructor */ |
277 | aiString(const aiString& rOther) |
278 | : length(rOther.length) |
279 | { |
280 | // Crop the string to the maximum length |
281 | length = length>=MAXLEN?MAXLEN-1:length; |
282 | memcpy( dest: data, src: rOther.data, n: length); |
283 | data[length] = '\0'; |
284 | } |
285 | |
286 | /** Constructor from std::string */ |
287 | explicit aiString(const std::string& pString) : |
288 | length( (ai_uint32) pString.length()) |
289 | { |
290 | length = length>=MAXLEN?MAXLEN-1:length; |
291 | memcpy( dest: data, src: pString.c_str(), n: length); |
292 | data[length] = '\0'; |
293 | } |
294 | |
295 | /** Copy a std::string to the aiString */ |
296 | void Set( const std::string& pString) { |
297 | if( pString.length() > MAXLEN - 1) { |
298 | return; |
299 | } |
300 | length = (ai_uint32)pString.length(); |
301 | memcpy( dest: data, src: pString.c_str(), n: length); |
302 | data[length] = 0; |
303 | } |
304 | |
305 | /** Copy a const char* to the aiString */ |
306 | void Set( const char* sz) { |
307 | const ai_int32 len = (ai_uint32) ::strlen(s: sz); |
308 | if( len > (ai_int32)MAXLEN - 1) { |
309 | return; |
310 | } |
311 | length = len; |
312 | memcpy( dest: data, src: sz, n: len); |
313 | data[len] = 0; |
314 | } |
315 | |
316 | |
317 | /** Assignment operator */ |
318 | aiString& operator = (const aiString &rOther) { |
319 | if (this == &rOther) { |
320 | return *this; |
321 | } |
322 | |
323 | length = rOther.length;; |
324 | memcpy( dest: data, src: rOther.data, n: length); |
325 | data[length] = '\0'; |
326 | return *this; |
327 | } |
328 | |
329 | |
330 | /** Assign a const char* to the string */ |
331 | aiString& operator = (const char* sz) { |
332 | Set(sz); |
333 | return *this; |
334 | } |
335 | |
336 | /** Assign a cstd::string to the string */ |
337 | aiString& operator = ( const std::string& pString) { |
338 | Set(pString); |
339 | return *this; |
340 | } |
341 | |
342 | /** Comparison operator */ |
343 | bool operator==(const aiString& other) const { |
344 | return (length == other.length && 0 == memcmp(s1: data,s2: other.data,n: length)); |
345 | } |
346 | |
347 | /** Inverse comparison operator */ |
348 | bool operator!=(const aiString& other) const { |
349 | return (length != other.length || 0 != memcmp(s1: data,s2: other.data,n: length)); |
350 | } |
351 | |
352 | /** Append a string to the string */ |
353 | void Append (const char* app) { |
354 | const ai_uint32 len = (ai_uint32) ::strlen(s: app); |
355 | if (!len) { |
356 | return; |
357 | } |
358 | if (length + len >= MAXLEN) { |
359 | return; |
360 | } |
361 | |
362 | memcpy(dest: &data[length],src: app,n: len+1); |
363 | length += len; |
364 | } |
365 | |
366 | /** Clear the string - reset its length to zero */ |
367 | void Clear () { |
368 | length = 0; |
369 | data[0] = '\0'; |
370 | |
371 | #ifdef ASSIMP_BUILD_DEBUG |
372 | // Debug build: overwrite the string on its full length with ESC (27) |
373 | memset(s: data+1,c: 27,n: MAXLEN-1); |
374 | #endif |
375 | } |
376 | |
377 | /** Returns a pointer to the underlying zero-terminated array of characters */ |
378 | const char* C_Str() const { |
379 | return data; |
380 | } |
381 | |
382 | #endif // !__cplusplus |
383 | |
384 | /** Binary length of the string excluding the terminal 0. This is NOT the |
385 | * logical length of strings containing UTF-8 multi-byte sequences! It's |
386 | * the number of bytes from the beginning of the string to its end.*/ |
387 | ai_uint32 length; |
388 | |
389 | /** String buffer. Size limit is MAXLEN */ |
390 | char data[MAXLEN]; |
391 | } ; // !struct aiString |
392 | |
393 | |
394 | // ---------------------------------------------------------------------------------- |
395 | /** Standard return type for some library functions. |
396 | * Rarely used, and if, mostly in the C API. |
397 | */ |
398 | typedef enum aiReturn |
399 | { |
400 | /** Indicates that a function was successful */ |
401 | aiReturn_SUCCESS = 0x0, |
402 | |
403 | /** Indicates that a function failed */ |
404 | aiReturn_FAILURE = -0x1, |
405 | |
406 | /** Indicates that not enough memory was available |
407 | * to perform the requested operation |
408 | */ |
409 | aiReturn_OUTOFMEMORY = -0x3, |
410 | |
411 | /** @cond never |
412 | * Force 32-bit size enum |
413 | */ |
414 | _AI_ENFORCE_ENUM_SIZE = 0x7fffffff |
415 | |
416 | /// @endcond |
417 | } aiReturn; // !enum aiReturn |
418 | |
419 | // just for backwards compatibility, don't use these constants anymore |
420 | #define AI_SUCCESS aiReturn_SUCCESS |
421 | #define AI_FAILURE aiReturn_FAILURE |
422 | #define AI_OUTOFMEMORY aiReturn_OUTOFMEMORY |
423 | |
424 | // ---------------------------------------------------------------------------------- |
425 | /** Seek origins (for the virtual file system API). |
426 | * Much cooler than using SEEK_SET, SEEK_CUR or SEEK_END. |
427 | */ |
428 | enum aiOrigin |
429 | { |
430 | /** Beginning of the file */ |
431 | aiOrigin_SET = 0x0, |
432 | |
433 | /** Current position of the file pointer */ |
434 | aiOrigin_CUR = 0x1, |
435 | |
436 | /** End of the file, offsets must be negative */ |
437 | aiOrigin_END = 0x2, |
438 | |
439 | /** @cond never |
440 | * Force 32-bit size enum |
441 | */ |
442 | _AI_ORIGIN_ENFORCE_ENUM_SIZE = 0x7fffffff |
443 | |
444 | /// @endcond |
445 | }; // !enum aiOrigin |
446 | |
447 | // ---------------------------------------------------------------------------------- |
448 | /** @brief Enumerates predefined log streaming destinations. |
449 | * Logging to these streams can be enabled with a single call to |
450 | * #LogStream::createDefaultStream. |
451 | */ |
452 | enum aiDefaultLogStream |
453 | { |
454 | /** Stream the log to a file */ |
455 | aiDefaultLogStream_FILE = 0x1, |
456 | |
457 | /** Stream the log to std::cout */ |
458 | aiDefaultLogStream_STDOUT = 0x2, |
459 | |
460 | /** Stream the log to std::cerr */ |
461 | aiDefaultLogStream_STDERR = 0x4, |
462 | |
463 | /** MSVC only: Stream the log the the debugger |
464 | * (this relies on OutputDebugString from the Win32 SDK) |
465 | */ |
466 | aiDefaultLogStream_DEBUGGER = 0x8, |
467 | |
468 | /** @cond never |
469 | * Force 32-bit size enum |
470 | */ |
471 | _AI_DLS_ENFORCE_ENUM_SIZE = 0x7fffffff |
472 | /// @endcond |
473 | }; // !enum aiDefaultLogStream |
474 | |
475 | // just for backwards compatibility, don't use these constants anymore |
476 | #define DLS_FILE aiDefaultLogStream_FILE |
477 | #define DLS_STDOUT aiDefaultLogStream_STDOUT |
478 | #define DLS_STDERR aiDefaultLogStream_STDERR |
479 | #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER |
480 | |
481 | // ---------------------------------------------------------------------------------- |
482 | /** Stores the memory requirements for different components (e.g. meshes, materials, |
483 | * animations) of an import. All sizes are in bytes. |
484 | * @see Importer::GetMemoryRequirements() |
485 | */ |
486 | struct aiMemoryInfo |
487 | { |
488 | #ifdef __cplusplus |
489 | |
490 | /** Default constructor */ |
491 | aiMemoryInfo() AI_NO_EXCEPT |
492 | : textures (0) |
493 | , materials (0) |
494 | , meshes (0) |
495 | , nodes (0) |
496 | , animations (0) |
497 | , cameras (0) |
498 | , lights (0) |
499 | , total (0) |
500 | {} |
501 | |
502 | #endif |
503 | |
504 | /** Storage allocated for texture data */ |
505 | unsigned int textures; |
506 | |
507 | /** Storage allocated for material data */ |
508 | unsigned int materials; |
509 | |
510 | /** Storage allocated for mesh data */ |
511 | unsigned int meshes; |
512 | |
513 | /** Storage allocated for node data */ |
514 | unsigned int nodes; |
515 | |
516 | /** Storage allocated for animation data */ |
517 | unsigned int animations; |
518 | |
519 | /** Storage allocated for camera data */ |
520 | unsigned int cameras; |
521 | |
522 | /** Storage allocated for light data */ |
523 | unsigned int lights; |
524 | |
525 | /** Total storage allocated for the full import. */ |
526 | unsigned int total; |
527 | }; // !struct aiMemoryInfo |
528 | |
529 | #ifdef __cplusplus |
530 | } |
531 | #endif //! __cplusplus |
532 | |
533 | // Include implementation files |
534 | #include "vector2.inl" |
535 | #include "vector3.inl" |
536 | #include "color4.inl" |
537 | #include "quaternion.inl" |
538 | #include "matrix3x3.inl" |
539 | #include "matrix4x4.inl" |
540 | |
541 | #endif // AI_TYPES_H_INC |
542 | |