1 /*
2 * MyMath.h
3 * nlivarot
4 *
5 * Created by fred on Wed Jun 18 2003.
6 *
7 */
9 #ifndef my_math
10 #define my_math
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
16 #ifdef HAVE_INTTYPES_H
17 # include <inttypes.h>
18 #else
19 # ifdef HAVE_STDINT_H
20 # include <stdint.h>
21 # endif
22 #endif
24 #ifdef HAVE_STDLIB_H
25 # include <stdlib.h>
26 #endif
28 typedef struct vec2
29 {
30 double x, y;
31 } vec2;
34 typedef struct mat2
35 {
36 double xx, xy, yx, yy;
37 } mat2;
40 typedef struct vec2d
41 {
42 double x, y;
43 } vec2d;
46 typedef struct mat2d
47 {
48 double xx, xy, yx, yy;
49 } mat2d;
52 #define RotCCW(a) {\
53 double t = (a).x;\
54 (a).x = (a).y;\
55 (a).y = -t;\
56 }
58 #define RotCCWTo(a,d) {\
59 (d).x = (a).y;\
60 (d).y = -(a).x;\
61 }
63 #define RotCW(a) {\
64 double t = (a).x;\
65 (a).x = -(a).y;\
66 (a).y = t;\
67 }
69 #define RotCWTo(a,d) {\
70 (d).x = -(a).y;\
71 (d).y = (a).x;\
72 }
74 #define Normalize(a) { \
75 double _le = (a).x*(a).x+(a).y*(a).y; \
76 if ( _le > 0.0001 ) { \
77 _le = 1.0 / sqrt(_le); \
78 (a).x *= _le; \
79 (a).y *= _le; \
80 } \
81 }
83 #define L_VEC_Set(a,u,v) { \
84 a.x = u; \
85 a.y = v; \
86 }
89 #define L_VEC_Length(a,l) { \
90 l = sqrt(a.x*a.x+a.y*a.y); \
91 }
93 #define L_VEC_Add(a,b,r) { \
94 r.x = a.x+b.x; \
95 r.y = a.y+b.y; \
96 }
98 #define L_VEC_Sub(a,b,r) { \
99 r.x = a.x-b.x; \
100 r.y = a.y-b.y; \
101 }
103 #define L_VEC_Mul(a,b,r) { \
104 r.x = a.x*b.x; \
105 r.y = a.y*b.y; \
106 }
108 #define L_VEC_Div(a,b,r) { \
109 r.x = a.x/b.x; \
110 r.y = a.y/b.y; \
111 }
113 #define L_VEC_AddMul(a,b,c,r) { \
114 r.x = a.x+b.x*c.x; \
115 r.y = a.y+b.y*c.y; \
116 }
118 #define L_VEC_SubMul(a,b,c,r) { \
119 r.x = a.x-b.x*c.x; \
120 r.y = a.y-b.y*c.y; \
121 }
124 #define L_VEC_MulC(a,b,r) { \
125 r.x = a.x*(b); \
126 r.y = a.y*(b); \
127 }
129 #define L_VEC_DivC(a,b,r) { \
130 r.x = a.x/(b); \
131 r.y = a.y/(b); \
132 }
134 #define L_VEC_AddMulC(a,b,c,r) { \
135 r.x = a.x+b.x*c; \
136 r.y = a.y+b.y*c; \
137 }
139 #define L_VEC_SubMulC(a,b,c,r) { \
140 r.x = a.x-b.x*c; \
141 r.y = a.y-b.y*c; \
142 }
144 #define L_VEC_Cmp(a,b) ((fabs(a.y-b.y)<0.0000001)? \
145 ((fabs(a.x-b.x)<0.0000001)?0:((a.x > b.x)?1:-1)): \
146 ((a.y > b.y)?1:-1))
148 #define L_VAL_Cmp(a,b) ((fabs(a-b)<0.0000001)?0:((a>b)?1:-1))
150 #define L_VEC_Normalize(d) { \
151 double l=sqrt(d.x*d.x+d.y*d.y); \
152 if ( l < 0.00000001 ) { \
153 d.x=d.y=0; \
154 } else { \
155 d.x/=l; \
156 d.y/=l; \
157 } \
158 }
160 #define L_VEC_Distance(a,b,d) { \
161 double dx = a.x-b.x; \
162 double dy = a.y-b.y; \
163 d = sqrt(dx*dx + dy*dy); \
164 }
166 #define L_VEC_Neg(d) { \
167 d.x=d.x; d.y=-d.y; \
168 }
170 #define L_VEC_RotCW(d) { \
171 double t=d.x; d.x=d.y; d.y=-t; \
172 } \
174 #define L_VEC_RotCCW(d) { \
175 double t=d.x; d.x=-d.y; d.y=t; \
176 }
178 #define L_VAL_Zero(a) ((fabs(a)<0.00000001)?0:((a>0)?1:-1))
180 #define L_VEC_Cross(a,b,r) { \
181 r = a.x*b.x+a.y*b.y; \
182 }
184 #define L_VEC_Dot(a,b,r) { \
185 r = a.x*b.y-a.y*b.x; \
186 }
189 #define L_MAT(m,a,b) { \
190 c[0][0].Set(ica.x); c[0][1].Set(icb.x); c[1][0].Set(ica.y); c[1][1].Set(icb.y); \
191 }
193 #define L_MAT_Set(m,a00,a10,a01,a11) {m.xx = a00; m.xy = a01; m.yx = a10; m.yy = a11;}
195 #define L_MAT_SetC(m,a,b) {m.xx = a.x; m.xy = b.x; m.yx = a.y; m.yy = b.y;}
197 #define L_MAT_SetL(m,a,b) {m.xx = a.x; m.xy = a.y;m.yx = b.x; m.yy = b.y;}
199 #define L_MAT_Init(m) {m.xx=m.xy=m.yx=m.yy=0;}
201 #define L_MAT_Col(m,no,r) { \
202 if ( no == 0 ) { \
203 r.x = m.xx; \
204 r.y = m.yx; \
205 } \
206 if ( no == 0 ) { \
207 r.x = m.xy; \
208 r.y = m.yy; \
209 } \
210 }
212 #define L_MAT_Row(m,no,r) { \
213 if ( no == 0 ) { \
214 r.x = m.xx; \
215 r.y = m.xy; \
216 } \
217 if ( no == 0 ) { \
218 r.x = m.yx; \
219 r.y = m.yy; \
220 } \
221 }
223 #define L_MAT_Det(m,d) {d=m.xx*m.yy-m.xy*m.yx;}
225 #define L_MAT_Neg(m) {m.xx=-m.xx; m.xy=-m.xy; m.yx=-m.yx; m.yy=-m.yy;}
227 #define L_MAT_Trs(m) {double t=m.xy; m.xy=m.yx; m.yx=t;}
229 #define L_MAT_Inv(m) { \
230 double d; \
231 L_MAT_Det(m,d); \
232 m.yx =- m.yx; \
233 m.xy =- m.xy; \
234 double t=m.xx;m.xx=m.yy;m.yy=t; \
235 double inv_d = 1.0/d; \
236 m.xx *= inv_d; \
237 m.xy *= inv_d; \
238 m.yx *= inv_d; \
239 m.yy *= inv_d; \
240 }
242 #define L_MAT_Cof(m) { \
243 m.yx =- m.yx; \
244 m.xy =- m.xy; \
245 double t=m.xx; m.xx=m.yy; m.yy=t; \
246 }
248 #define L_MAT_Add(u,v,m) { \
249 m.xx=u.xx+v.xx; m.xy=u.xy+v.xy; m.yx=u.yx+v.yx; m.yy=u.yy+v.yy; \
250 }
252 #define L_MAT_Sub(u,v,m) { \
253 m.xx=u.xx-v.xx; m.xy=u.xy-v.xy; m.yx=u.yx-v.yx; m.yy=u.yy-v.yy; \
254 }
256 #define L_MAT_Mul(u,v,m) { \
257 mat2d r; \
258 r.xx = u.xx*v.xx+u.xy*v.yx; \
259 r.yx = u.yx*v.xx+u.yy*y.yx; \
260 r.xy = u.xx*v.xy+u.xy*v.yy; \
261 r.yy = u.yx*v.xy+u.yy*v.yy; \
262 m=r; \
263 }
265 #define L_MAT_MulC(u,v,m) { \
266 m.xx=u.xx*v; m.xy=u.xy*v; m.yx=u.yx*v; m.yy=u.yy*v; \
267 }
269 #define L_MAT_DivC(u,v,m) { \
270 double iv = 1.0/v; \
271 m.xx = u.xx*iv; m.xy=u.xy*iv; m.yx=u.yx*iv; m.yy=u.yy*iv; \
272 }
274 #define L_MAT_MulV(m,v,r) { \
275 vec2d t; \
276 t.x = m.xx*v.x+m.xy*v.y; \
277 t.y = m.yx*v.x+m.yy*v.y; \
278 r=t; \
279 }
281 #define L_MAT_TMulV(m,v,r) { \
282 vec2d t; \
283 t.x = m.xx*v.x+m.yx*v.y; \
284 t.y = m.xy*v.x+m.yy*v.y; \
285 r=t; \
286 }
290 #endif