miniassimp

view include/miniassimp/types.h @ 0:879c81d94345

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Jan 2019 18:19:26 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2018, assimp team
10 All rights reserved.
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:
16 * Redistributions of source code must retain the above
17 copyright notice, this list of conditions and the
18 following disclaimer.
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.
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.
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 */
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
51 // Some runtime headers
52 #include <sys/types.h>
53 #include <stddef.h>
54 #include <string.h>
55 #include <limits.h>
57 // Our compile configuration
58 #include "defs.h"
60 // Some types moved to separate header due to size of operators
61 #include "vector3.h"
62 #include "vector2.h"
63 #include "color4.h"
64 #include "matrix3x3.h"
65 #include "matrix4x4.h"
66 #include "quaternion.h"
68 #ifdef __cplusplus
69 #include <cstring>
70 #include <new> // for std::nothrow_t
71 #include <string> // for aiString::Set(const std::string&)
73 namespace Assimp {
74 //! @cond never
75 namespace Intern {
76 // --------------------------------------------------------------------
77 /** @brief Internal helper class to utilize our internal new/delete
78 * routines for allocating object of this and derived classes.
79 *
80 * By doing this you can safely share class objects between Assimp
81 * and the application - it works even over DLL boundaries. A good
82 * example is the #IOSystem where the application allocates its custom
83 * #IOSystem, then calls #Importer::SetIOSystem(). When the Importer
84 * destructs, Assimp calls operator delete on the stored #IOSystem.
85 * If it lies on a different heap than Assimp is working with,
86 * the application is determined to crash.
87 */
88 // --------------------------------------------------------------------
89 #ifndef SWIG
90 struct ASSIMP_API AllocateFromAssimpHeap {
91 // http://www.gotw.ca/publications/mill15.htm
93 // new/delete overload
94 void *operator new ( size_t num_bytes) /* throw( std::bad_alloc ) */;
95 void *operator new ( size_t num_bytes, const std::nothrow_t& ) throw();
96 void operator delete ( void* data);
98 // array 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);
103 }; // struct AllocateFromAssimpHeap
104 #endif
105 } // namespace Intern
106 //! @endcond
107 } // namespace Assimp
109 extern "C" {
110 #endif
112 /** Maximum dimension for strings, ASSIMP strings are zero terminated. */
113 #ifdef __cplusplus
114 static const size_t MAXLEN = 1024;
115 #else
116 # define MAXLEN 1024
117 #endif
119 // ----------------------------------------------------------------------------------
120 /** Represents a plane in a three-dimensional, euclidean space
121 */
122 struct aiPlane {
123 #ifdef __cplusplus
124 aiPlane () AI_NO_EXCEPT : a(0.f), b(0.f), c(0.f), d(0.f) {}
125 aiPlane (ai_real _a, ai_real _b, ai_real _c, ai_real _d)
126 : a(_a), b(_b), c(_c), d(_d) {}
128 aiPlane (const aiPlane& o) : a(o.a), b(o.b), c(o.c), d(o.d) {}
130 #endif // !__cplusplus
132 //! Plane equation
133 ai_real a,b,c,d;
134 }; // !struct aiPlane
136 // ----------------------------------------------------------------------------------
137 /** Represents a ray
138 */
139 struct aiRay {
140 #ifdef __cplusplus
141 aiRay () AI_NO_EXCEPT {}
142 aiRay (const aiVector3D& _pos, const aiVector3D& _dir)
143 : pos(_pos), dir(_dir) {}
145 aiRay (const aiRay& o) : pos (o.pos), dir (o.dir) {}
147 #endif // !__cplusplus
149 //! Position and direction of the ray
150 C_STRUCT aiVector3D pos, dir;
151 }; // !struct aiRay
153 // ----------------------------------------------------------------------------------
154 /** Represents a color in Red-Green-Blue space.
155 */
156 struct aiColor3D
157 {
158 #ifdef __cplusplus
159 aiColor3D () AI_NO_EXCEPT : r(0.0f), g(0.0f), b(0.0f) {}
160 aiColor3D (ai_real _r, ai_real _g, ai_real _b) : r(_r), g(_g), b(_b) {}
161 explicit aiColor3D (ai_real _r) : r(_r), g(_r), b(_r) {}
162 aiColor3D (const aiColor3D& o) : r(o.r), g(o.g), b(o.b) {}
164 /** Component-wise comparison */
165 // TODO: add epsilon?
166 bool operator == (const aiColor3D& other) const
167 {return r == other.r && g == other.g && b == other.b;}
169 /** Component-wise inverse comparison */
170 // TODO: add epsilon?
171 bool operator != (const aiColor3D& other) const
172 {return r != other.r || g != other.g || b != other.b;}
174 /** Component-wise comparison */
175 // TODO: add epsilon?
176 bool operator < (const aiColor3D& other) const {
177 return r < other.r || ( r == other.r && (g < other.g || (g == other.g && b < other.b ) ) );
178 }
180 /** Component-wise addition */
181 aiColor3D operator+(const aiColor3D& c) const {
182 return aiColor3D(r+c.r,g+c.g,b+c.b);
183 }
185 /** Component-wise subtraction */
186 aiColor3D operator-(const aiColor3D& c) const {
187 return aiColor3D(r-c.r,g-c.g,b-c.b);
188 }
190 /** Component-wise multiplication */
191 aiColor3D operator*(const aiColor3D& c) const {
192 return aiColor3D(r*c.r,g*c.g,b*c.b);
193 }
195 /** Multiply with a scalar */
196 aiColor3D operator*(ai_real f) const {
197 return aiColor3D(r*f,g*f,b*f);
198 }
200 /** Access a specific color component */
201 ai_real operator[](unsigned int i) const {
202 return *(&r + i);
203 }
205 /** Access a specific color component */
206 ai_real& operator[](unsigned int i) {
207 if ( 0 == i ) {
208 return r;
209 } else if ( 1 == i ) {
210 return g;
211 } else if ( 2 == i ) {
212 return b;
213 }
214 return r;
215 }
217 /** Check whether a color is black */
218 bool IsBlack() const {
219 static const ai_real epsilon = ai_real(10e-3);
220 return std::fabs( r ) < epsilon && std::fabs( g ) < epsilon && std::fabs( b ) < epsilon;
221 }
223 #endif // !__cplusplus
225 //! Red, green and blue color values
226 ai_real r, g, b;
227 }; // !struct aiColor3D
229 // ----------------------------------------------------------------------------------
230 /** Represents an UTF-8 string, zero byte terminated.
231 *
232 * The character set of an aiString is explicitly defined to be UTF-8. This Unicode
233 * transformation was chosen in the belief that most strings in 3d files are limited
234 * to ASCII, thus the character set needed to be strictly ASCII compatible.
235 *
236 * Most text file loaders provide proper Unicode input file handling, special unicode
237 * characters are correctly transcoded to UTF8 and are kept throughout the libraries'
238 * import pipeline.
239 *
240 * For most applications, it will be absolutely sufficient to interpret the
241 * aiString as ASCII data and work with it as one would work with a plain char*.
242 * Windows users in need of proper support for i.e asian characters can use the
243 * MultiByteToWideChar(), WideCharToMultiByte() WinAPI functionality to convert the
244 * UTF-8 strings to their working character set (i.e. MBCS, WideChar).
245 *
246 * We use this representation instead of std::string to be C-compatible. The
247 * (binary) length of such a string is limited to MAXLEN characters (including the
248 * the terminating zero).
249 */
250 struct aiString
251 {
252 #ifdef __cplusplus
253 /** Default constructor, the string is set to have zero length */
254 aiString() AI_NO_EXCEPT
255 : length( 0 ) {
256 data[0] = '\0';
258 #ifdef ASSIMP_BUILD_DEBUG
259 // Debug build: overwrite the string on its full length with ESC (27)
260 memset(data+1,27,MAXLEN-1);
261 #endif
262 }
264 /** Copy constructor */
265 aiString(const aiString& rOther) :
266 length(rOther.length)
267 {
268 // Crop the string to the maximum length
269 length = length>=MAXLEN?MAXLEN-1:length;
270 memcpy( data, rOther.data, length);
271 data[length] = '\0';
272 }
274 /** Constructor from std::string */
275 explicit aiString(const std::string& pString) :
276 length(pString.length())
277 {
278 length = length>=MAXLEN?MAXLEN-1:length;
279 memcpy( data, pString.c_str(), length);
280 data[length] = '\0';
281 }
283 /** Copy a std::string to the aiString */
284 void Set( const std::string& pString) {
285 if( pString.length() > MAXLEN - 1) {
286 return;
287 }
288 length = pString.length();
289 memcpy( data, pString.c_str(), length);
290 data[length] = 0;
291 }
293 /** Copy a const char* to the aiString */
294 void Set( const char* sz) {
295 const size_t len = ::strlen(sz);
296 if( len > MAXLEN - 1) {
297 return;
298 }
299 length = len;
300 memcpy( data, sz, len);
301 data[len] = 0;
302 }
305 /** Assignment operator */
306 aiString& operator = (const aiString &rOther) {
307 if (this == &rOther) {
308 return *this;
309 }
311 length = rOther.length;;
312 memcpy( data, rOther.data, length);
313 data[length] = '\0';
314 return *this;
315 }
318 /** Assign a const char* to the string */
319 aiString& operator = (const char* sz) {
320 Set(sz);
321 return *this;
322 }
324 /** Assign a cstd::string to the string */
325 aiString& operator = ( const std::string& pString) {
326 Set(pString);
327 return *this;
328 }
330 /** Comparison operator */
331 bool operator==(const aiString& other) const {
332 return (length == other.length && 0 == memcmp(data,other.data,length));
333 }
335 /** Inverse comparison operator */
336 bool operator!=(const aiString& other) const {
337 return (length != other.length || 0 != memcmp(data,other.data,length));
338 }
340 /** Append a string to the string */
341 void Append (const char* app) {
342 const size_t len = ::strlen(app);
343 if (!len) {
344 return;
345 }
346 if (length + len >= MAXLEN) {
347 return;
348 }
350 memcpy(&data[length],app,len+1);
351 length += len;
352 }
354 /** Clear the string - reset its length to zero */
355 void Clear () {
356 length = 0;
357 data[0] = '\0';
359 #ifdef ASSIMP_BUILD_DEBUG
360 // Debug build: overwrite the string on its full length with ESC (27)
361 memset(data+1,27,MAXLEN-1);
362 #endif
363 }
365 /** Returns a pointer to the underlying zero-terminated array of characters */
366 const char* C_Str() const {
367 return data;
368 }
370 #endif // !__cplusplus
372 /** Binary length of the string excluding the terminal 0. This is NOT the
373 * logical length of strings containing UTF-8 multi-byte sequences! It's
374 * the number of bytes from the beginning of the string to its end.*/
375 size_t length;
377 /** String buffer. Size limit is MAXLEN */
378 char data[MAXLEN];
379 } ; // !struct aiString
382 // ----------------------------------------------------------------------------------
383 /** Standard return type for some library functions.
384 * Rarely used, and if, mostly in the C API.
385 */
386 typedef enum aiReturn
387 {
388 /** Indicates that a function was successful */
389 aiReturn_SUCCESS = 0x0,
391 /** Indicates that a function failed */
392 aiReturn_FAILURE = -0x1,
394 /** Indicates that not enough memory was available
395 * to perform the requested operation
396 */
397 aiReturn_OUTOFMEMORY = -0x3,
399 /** @cond never
400 * Force 32-bit size enum
401 */
402 _AI_ENFORCE_ENUM_SIZE = 0x7fffffff
404 /// @endcond
405 } aiReturn; // !enum aiReturn
407 // just for backwards compatibility, don't use these constants anymore
408 #define AI_SUCCESS aiReturn_SUCCESS
409 #define AI_FAILURE aiReturn_FAILURE
410 #define AI_OUTOFMEMORY aiReturn_OUTOFMEMORY
412 // ----------------------------------------------------------------------------------
413 /** Seek origins (for the virtual file system API).
414 * Much cooler than using SEEK_SET, SEEK_CUR or SEEK_END.
415 */
416 enum aiOrigin
417 {
418 /** Beginning of the file */
419 aiOrigin_SET = 0x0,
421 /** Current position of the file pointer */
422 aiOrigin_CUR = 0x1,
424 /** End of the file, offsets must be negative */
425 aiOrigin_END = 0x2,
427 /** @cond never
428 * Force 32-bit size enum
429 */
430 _AI_ORIGIN_ENFORCE_ENUM_SIZE = 0x7fffffff
432 /// @endcond
433 }; // !enum aiOrigin
435 // ----------------------------------------------------------------------------------
436 /** @brief Enumerates predefined log streaming destinations.
437 * Logging to these streams can be enabled with a single call to
438 * #LogStream::createDefaultStream.
439 */
440 enum aiDefaultLogStream
441 {
442 /** Stream the log to a file */
443 aiDefaultLogStream_FILE = 0x1,
445 /** Stream the log to std::cout */
446 aiDefaultLogStream_STDOUT = 0x2,
448 /** Stream the log to std::cerr */
449 aiDefaultLogStream_STDERR = 0x4,
451 /** MSVC only: Stream the log the the debugger
452 * (this relies on OutputDebugString from the Win32 SDK)
453 */
454 aiDefaultLogStream_DEBUGGER = 0x8,
456 /** @cond never
457 * Force 32-bit size enum
458 */
459 _AI_DLS_ENFORCE_ENUM_SIZE = 0x7fffffff
460 /// @endcond
461 }; // !enum aiDefaultLogStream
463 // just for backwards compatibility, don't use these constants anymore
464 #define DLS_FILE aiDefaultLogStream_FILE
465 #define DLS_STDOUT aiDefaultLogStream_STDOUT
466 #define DLS_STDERR aiDefaultLogStream_STDERR
467 #define DLS_DEBUGGER aiDefaultLogStream_DEBUGGER
469 // ----------------------------------------------------------------------------------
470 /** Stores the memory requirements for different components (e.g. meshes, materials,
471 * animations) of an import. All sizes are in bytes.
472 * @see Importer::GetMemoryRequirements()
473 */
474 struct aiMemoryInfo
475 {
476 #ifdef __cplusplus
478 /** Default constructor */
479 aiMemoryInfo() AI_NO_EXCEPT
480 : textures (0)
481 , materials (0)
482 , meshes (0)
483 , nodes (0)
484 , animations (0)
485 , cameras (0)
486 , lights (0)
487 , total (0)
488 {}
490 #endif
492 /** Storage allocated for texture data */
493 unsigned int textures;
495 /** Storage allocated for material data */
496 unsigned int materials;
498 /** Storage allocated for mesh data */
499 unsigned int meshes;
501 /** Storage allocated for node data */
502 unsigned int nodes;
504 /** Storage allocated for animation data */
505 unsigned int animations;
507 /** Storage allocated for camera data */
508 unsigned int cameras;
510 /** Storage allocated for light data */
511 unsigned int lights;
513 /** Total storage allocated for the full import. */
514 unsigned int total;
515 }; // !struct aiMemoryInfo
517 #ifdef __cplusplus
518 }
519 #endif //! __cplusplus
521 // Include implementation files
522 #include "vector2.inl"
523 #include "vector3.inl"
524 #include "color4.inl"
525 #include "quaternion.inl"
526 #include "matrix3x3.inl"
527 #include "matrix4x4.inl"
529 #endif // AI_TYPES_H_INC