[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / chipmunk2d / include / chipmunk / chipmunk_types.h
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2  * 
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21
22 #ifndef CHIPMUNK_TYPES_H
23 #define CHIPMUNK_TYPES_H
24
25 #include <stdint.h>
26 #include <float.h>
27 #include <math.h>
28
29 #ifdef __APPLE__
30    #include "TargetConditionals.h"
31 #endif
32
33 // Use CGTypes by default on iOS and Mac.
34 // Also enables usage of doubles on 64 bit.
35 // Performance is usually very comparable when the CPU cache is well utilised.
36 #if (TARGET_OS_IPHONE || TARGET_OS_MAC) && (!defined CP_USE_CGTYPES)
37         #define CP_USE_CGTYPES 1
38 #endif
39
40 #if CP_USE_CGTYPES
41         #if TARGET_OS_IPHONE
42                 #include <CoreGraphics/CGGeometry.h>
43                 #include <CoreGraphics/CGAffineTransform.h>
44         #elif TARGET_OS_MAC
45                 #include <ApplicationServices/ApplicationServices.h>
46         #endif
47         
48         #if defined(__LP64__) && __LP64__
49                 #define CP_USE_DOUBLES 1
50         #else
51                 #define CP_USE_DOUBLES 0
52         #endif
53 #endif
54
55 #ifndef CP_USE_DOUBLES
56   #if !__arm__
57           // Use doubles by default for higher precision (but not for 32 bit ARM).
58           #define CP_USE_DOUBLES 1
59   #endif
60 #endif
61
62 /// @defgroup basicTypes Basic Types
63 /// Most of these types can be configured at compile time.
64 /// @{
65
66 #if CP_USE_DOUBLES
67 /// Chipmunk's floating point type.
68 /// Can be reconfigured at compile time.
69         typedef double cpFloat;
70         #define cpfsqrt sqrt
71         #define cpfsin sin
72         #define cpfcos cos
73         #define cpfacos acos
74         #define cpfatan2 atan2
75         #define cpfmod fmod
76         #define cpfexp exp
77         #define cpfpow pow
78         #define cpffloor floor
79         #define cpfceil ceil
80         #define CPFLOAT_MIN DBL_MIN
81 #else
82         typedef float cpFloat;
83         #define cpfsqrt sqrtf
84         #define cpfsin sinf
85         #define cpfcos cosf
86         #define cpfacos acosf
87         #define cpfatan2 atan2f
88         #define cpfmod fmodf
89         #define cpfexp expf
90         #define cpfpow powf
91         #define cpffloor floorf
92         #define cpfceil ceilf
93         #define CPFLOAT_MIN FLT_MIN
94 #endif
95
96 #ifndef INFINITY
97         #ifdef _MSC_VER
98                 union MSVC_EVIL_FLOAT_HACK
99                 {
100                         unsigned __int8 Bytes[4];
101                         float Value;
102                 };
103                 static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
104                 #define INFINITY (INFINITY_HACK.Value)
105         #endif
106         
107         #ifdef __GNUC__
108                 #define INFINITY (__builtin_inf())
109         #endif
110         
111         #ifndef INFINITY
112                 #define INFINITY (1e1000)
113         #endif
114 #endif
115
116
117 #define CP_PI ((cpFloat)3.14159265358979323846264338327950288)
118
119
120 /// Return the max of two cpFloats.
121 static inline cpFloat cpfmax(cpFloat a, cpFloat b)
122 {
123         return (a > b) ? a : b;
124 }
125
126 /// Return the min of two cpFloats.
127 static inline cpFloat cpfmin(cpFloat a, cpFloat b)
128 {
129         return (a < b) ? a : b;
130 }
131
132 /// Return the absolute value of a cpFloat.
133 static inline cpFloat cpfabs(cpFloat f)
134 {
135         return (f < 0) ? -f : f;
136 }
137
138 /// Clamp @c f to be between @c min and @c max.
139 static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
140 {
141         return cpfmin(cpfmax(f, min), max);
142 }
143
144 /// Clamp @c f to be between 0 and 1.
145 static inline cpFloat cpfclamp01(cpFloat f)
146 {
147         return cpfmax(0.0f, cpfmin(f, 1.0f));
148 }
149
150
151
152 /// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent.
153 static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
154 {
155         return f1*(1.0f - t) + f2*t;
156 }
157
158 /// Linearly interpolate from @c f1 to @c f2 by no more than @c d.
159 static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
160 {
161         return f1 + cpfclamp(f2 - f1, -d, d);
162 }
163
164 /// Hash value type.
165 #ifdef CP_HASH_VALUE_TYPE
166         typedef CP_HASH_VALUE_TYPE cpHashValue;
167 #else
168         typedef uintptr_t cpHashValue;
169 #endif
170
171 /// Type used internally to cache colliding object info for cpCollideShapes().
172 /// Should be at least 32 bits.
173 typedef uint32_t cpCollisionID;
174
175 // Oh C, how we love to define our own boolean types to get compiler compatibility
176 /// Chipmunk's boolean type.
177 #ifdef CP_BOOL_TYPE
178         typedef CP_BOOL_TYPE cpBool;
179 #else
180         typedef unsigned char cpBool;
181 #endif
182
183 #ifndef cpTrue
184 /// true value.
185         #define cpTrue 1
186 #endif
187
188 #ifndef cpFalse
189 /// false value.
190         #define cpFalse 0
191 #endif
192
193 #ifdef CP_DATA_POINTER_TYPE
194         typedef CP_DATA_POINTER_TYPE cpDataPointer;
195 #else
196 /// Type used for user data pointers.
197         typedef void * cpDataPointer;
198 #endif
199
200 #ifdef CP_COLLISION_TYPE_TYPE
201         typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
202 #else
203 /// Type used for cpSpace.collision_type.
204         typedef uintptr_t cpCollisionType;
205 #endif
206
207 #ifdef CP_GROUP_TYPE
208         typedef CP_GROUP_TYPE cpGroup;
209 #else
210 /// Type used for cpShape.group.
211         typedef uintptr_t cpGroup;
212 #endif
213
214 #ifdef CP_BITMASK_TYPE
215         typedef CP_BITMASK_TYPE cpBitmask;
216 #else
217 /// Type used for cpShapeFilter category and mask.
218         typedef unsigned int cpBitmask;
219 #endif
220
221 #ifdef CP_TIMESTAMP_TYPE
222         typedef CP_TIMESTAMP_TYPE cpTimestamp;
223 #else
224 /// Type used for various timestamps in Chipmunk.
225         typedef unsigned int cpTimestamp;
226 #endif
227
228 #ifndef CP_NO_GROUP
229 /// Value for cpShape.group signifying that a shape is in no group.
230         #define CP_NO_GROUP ((cpGroup)0)
231 #endif
232
233 #ifndef CP_ALL_CATEGORIES
234 /// Value for cpShape.layers signifying that a shape is in every layer.
235         #define CP_ALL_CATEGORIES (~(cpBitmask)0)
236 #endif
237
238 #ifndef CP_WILDCARD_COLLISION_TYPE
239 /// cpCollisionType value internally reserved for hashing wildcard handlers.
240         #define CP_WILDCARD_COLLISION_TYPE (~(cpCollisionType)0)
241 #endif
242
243 /// @}
244
245 // CGPoints are structurally the same, and allow
246 // easy interoperability with other Cocoa libraries
247 #if CP_USE_CGTYPES
248         typedef CGPoint cpVect;
249 #else
250 /// Chipmunk's 2D vector type.
251 /// @addtogroup cpVect
252         typedef struct cpVect{cpFloat x,y;} cpVect;
253 #endif
254
255 #if CP_USE_CGTYPES
256         typedef CGAffineTransform cpTransform;
257 #else
258         /// Column major affine transform.
259         typedef struct cpTransform {
260                 cpFloat a, b, c, d, tx, ty;
261         } cpTransform;
262 #endif
263
264 // NUKE
265 typedef struct cpMat2x2 {
266         // Row major [[a, b][c d]]
267         cpFloat a, b, c, d;
268 } cpMat2x2;
269
270 #endif