]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ShivaVG/src/shVectors.h
Update for OpenSceneGraph 3.3.2 API changes.
[simgear.git] / simgear / canvas / ShivaVG / src / shVectors.h
1 /*
2  * Copyright (c) 2007 Ivan Leben
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library in the file COPYING;
16  * if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #ifndef __SHVECTORS_H
22 #define __SHVECTORS_H
23
24 #include "shDefs.h"
25
26 /* Vector structures
27  *--------------------------------------------------------------*/
28 typedef struct
29 {
30   SHfloat x,y;
31 } SHVector2;
32
33 void SHVector2_ctor(SHVector2 *v);
34 void SHVector2_dtor(SHVector2 *v);
35
36 typedef struct
37 {
38   SHfloat x,y,z;
39 } SHVector3;
40
41 void SHVector3_ctor(SHVector3 *v);
42 void SHVector3_dtor(SHVector3 *v);
43
44 typedef struct
45 {
46   SHfloat x,y,z,w;
47 } SHVector4;
48
49 void SHVector4_ctor(SHVector4 *v);
50 void SHVector4_dtor(SHVector4 *v);
51
52 typedef struct
53 {
54   SHfloat x,y,w,h;
55 } SHRectangle;
56
57 void SHRectangle_ctor(SHRectangle *r);
58 void SHRectangle_dtor(SHRectangle *r);
59 void shRectangleSet(SHRectangle *r, SHfloat x,
60                     SHfloat y, SHfloat w, SHfloat h);
61
62 typedef struct
63 {
64   SHfloat m[3][3];
65 } SHMatrix3x3;
66
67 void SHMatrix3x3_ctor(SHMatrix3x3 *m);
68 void SHMatrix3x3_dtor(SHMatrix3x3 *m);
69
70 /*------------------------------------------------------------
71  * Vector Arrays
72  *------------------------------------------------------------*/
73
74 #define _ITEM_T SHVector2
75 #define _ARRAY_T SHVector2Array
76 #define _FUNC_T shVector2Array
77 #define _ARRAY_DECLARE
78 #include "shArrayBase.h"
79
80 /*--------------------------------------------------------
81  * Macros for typical vector operations. The only way to
82  * inline in C is to actually write a macro
83  *--------------------------------------------------------- */
84
85 #define SET2(v,xs,ys) { v.x=xs; v.y=ys; }
86 #define SET3(v,xs,ys,zs) { v.x=xs; v.y=ys; v.z=zs; }
87 #define SET4(v,xs,ys,zs,ws) { v.x=xs; v.y=ys; v.z=zs; v.w=ws; }
88
89 #define SET2V(v1,v2) { v1.x=v2.x; v1.y=v2.y; }
90 #define SET3V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; }
91 #define SET4V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; v1.w=v2.w; }
92
93 #define EQ2(v,xx,yy)       ( v.x==xx && v.y==yy )
94 #define EQ3(v,xx,yy,zz)    ( v.x==xx && v.y==yy && v.z==zz )
95 #define EQ4(v,xx,yy,zz,ww) ( v.x==xx && v.y==yy && v.z==zz && v.w==ww )
96
97 #define ISZERO2(v) ( v.x==0.0f && v.y==0.0f )
98 #define ISZERO3(v) ( v.x==0.0f && v.y==0.0f && v.z==0.0f)
99 #define ISZERO4(v) ( v.x==0.0f && v.y==0.0f && v.z==0.0f && v.w==0.0f )
100
101 #define EQ2V(v1,v2) ( v1.x==v2.x && v1.y==v2.y )
102 #define EQ3V(v1,v2) ( v1.x==v2.x && v1.y==v2.y && v1.z==v2.z )
103 #define EQ4V(v1,v2) ( v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w )
104
105 #define ADD2(v,xx,yy)       { v.x+=xx; v.y+=yy; }
106 #define ADD3(v,xx,yy,zz)    { v.x+=xx; v.y+=yy; v.z+=zz; }
107 #define ADD4(v,xx,yy,zz,ww) { v.x+=xx; v.y+=yy; v.z+=zz; v.w+=ww; }
108
109 #define ADD2V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; }
110 #define ADD3V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; }
111 #define ADD4V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; v1.w+=v2.w; }
112
113 #define SUB2(v,xx,yy)       { v.x-=xx; v.y-=yy; }
114 #define SUB3(v,xx,yy,zz)    { v.x-=xx; v.y-=yy; v.z-=zz; }
115 #define SUB4(v,xx,yy,zz,ww) { v.x-=xx; v.y-=yy; v.z-=zz; v.w-=v2.w; }
116
117 #define SUB2V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; }
118 #define SUB3V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; }
119 #define SUB4V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; v1.w-=v2.w; }
120
121 #define MUL2(v,f) { v.x*=f; v.y*=f; }
122 #define MUL3(v,f) { v.x*=f; v.y*=f; v.z*=z; }
123 #define MUL4(v,f) { v.x*=f; v.y*=f; v.z*=z; v.w*=w; }
124
125 #define DIV2(v,f) { v.x/=f; v.y/=f; }
126 #define DIV3(v,f) { v.x/=f; v.y/=f; v.z/=z; }
127 #define DIV4(v,f) { v.x/=f; v.y/=f; v.z/=z; v.w/=w; }
128
129 #define ABS2(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); }
130 #define ABS3(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); }
131 #define ABS4(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); v.w=SH_ABS(v.w); }
132
133 #define NORMSQ2(v) (v.x*v.x + v.y*v.y)
134 #define NORMSQ3(v) (v.x*v.x + v.y*v.y + v.z*v.z)
135 #define NORMSQ4(v) (v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w)
136
137 #define NORM2(v) SH_SQRT(NORMSQ2(v))
138 #define NORM3(v) SH_SQRT(NORMSQ3(v))
139 #define NORM4(v) SH_SQRT(NORMSQ4(v))
140
141 #define NORMALIZE2(v) { SHfloat n=NORM2(v); v.x/=n; v.y/=n; }
142 #define NORMALIZE3(v) { SHfloat n=NORM3(v); v.x/=n; v.y/=n; v.z/=n; }
143 #define NORMALIZE4(v) { SHfloat n=NORM4(v); v.x/=n; v.y/=n; v.z/=n; v.w/=w; }
144
145 #define DOT2(v1,v2) (v1.x*v2.x + v1.y*v2.y)
146 #define DOT3(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z)
147 #define DOT4(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w)
148
149 #define CROSS2(v1,v2) (v1.x*v2.y - v2.x*v1.y)
150
151 #define ANGLE2(v1,v2) (SH_ACOS( DOT2(v1,v2) / (NORM2(v1)*NORM2(v2)) ))
152 #define ANGLE2N(v1,v2) (SH_ACOS( DOT2(v1,v2) ))
153
154 #define OFFSET2V(v, o, s) { v.x += o.x*s; v.y += o.y*s; }
155 #define OFFSET3V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; }
156 #define OFFSET4V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; v.w += o.w*s; }
157
158 /*-----------------------------------------------------
159  * Macros for matrix operations
160  *-----------------------------------------------------*/
161
162 #define SETMAT(mat, m00, m01, m02, m10, m11, m12, m20, m21, m22) { \
163 mat.m[0][0] = m00; mat.m[0][1] = m01; mat.m[0][2] = m02; \
164   mat.m[1][0] = m10; mat.m[1][1] = m11; mat.m[1][2] = m12; \
165   mat.m[2][0] = m20; mat.m[2][1] = m21; mat.m[2][2] = m22; }
166
167 #define SETMATMAT(m1, m2) { \
168 int i,j; \
169   for(i=0;i<3;i++) \
170   for(j=0;j<3;j++) \
171     m1.m[i][j] = m2.m[i][j]; }
172
173 #define MULMATS(mat, s) { \
174 int i,j; \
175   for(i=0;i<3;i++) \
176   for(j=0;j<3;j++) \
177     mat.m[i][j] *= s; }
178
179 #define DIVMATS(mat, s) { \
180 int i,j; \
181   for(i=0;i<3;i++) \
182   for(j=0;j<3;j++) \
183     mat.m[i][j] /= s; }
184
185 #define MULMATMAT(m1, m2, mout) { \
186 int i,j; \
187   for(i=0;i<3;i++) \
188   for(j=0;j<3;j++) \
189     mout.m[i][j] = \
190       m1.m[i][0] * m2.m[0][j] + \
191       m1.m[i][1] * m2.m[1][j] + \
192       m1.m[i][2] * m2.m[2][j]; }
193
194 #define IDMAT(mat) SETMAT(mat, 1,0,0, 0,1,0, 0,0,1)
195
196 #define TRANSLATEMATL(mat, tx, ty) { \
197 SHMatrix3x3 trans,temp; \
198   SETMAT(trans, 1,0,tx, 0,1,ty, 0,0,1); \
199   MULMATMAT(trans, mat, temp); \
200   SETMATMAT(mat, temp); }
201
202 #define TRANSLATEMATR(mat, tx, ty) { \
203 SHMatrix3x3 trans,temp; \
204   SETMAT(trans, 1,0,tx, 0,1,ty, 0,0,1); \
205   MULMATMAT(mat, trans, temp); \
206   SETMATMAT(mat, temp); }
207
208 #define SCALEMATL(mat, sx, sy) { \
209 SHMatrix3x3 scale, temp; \
210   SETMAT(scale, sx,0,0, 0,sy,0, 0,0,1); \
211   MULMATMAT(scale, mat, temp); \
212   SETMATMAT(mat, temp); }
213
214 #define SCALEMATR(mat, sx, sy) { \
215 SHMatrix3x3 scale, temp; \
216   SETMAT(scale, sx,0,0, 0,sy,0, 0,0,1); \
217   MULMATMAT(mat, scale, temp); \
218   SETMATMAT(mat, temp); }
219
220 #define SHEARMATL(mat, shx, shy) {\
221 SHMatrix3x3 shear, temp;\
222   SETMAT(shear, 1,shx,0, shy,1,0, 0,0,1); \
223   MULMATMAT(shear, mat, temp); \
224   SETMATMAT(mat, temp); }
225
226 #define SHEARMATR(mat, shx, shy) {\
227 SHMatrix3x3 shear, temp;\
228   SETMAT(shear, 1,shx,0, shy,1,0, 0,0,1); \
229   MULMATMAT(mat, shear, temp); \
230   SETMATMAT(mat, temp); }
231
232 #define ROTATEMATL(mat, a) { \
233 SHfloat cosa=SH_COS(a), sina=SH_SIN(a); \
234   SHMatrix3x3 rot, temp; \
235   SETMAT(rot, cosa,-sina,0, sina,cosa,0, 0,0,1); \
236   MULMATMAT(rot, mat, temp); \
237   SETMATMAT(mat, temp); }
238
239 #define ROTATEMATR(mat, a) { \
240 SHfloat cosa=SH_COS(a), sina=SH_SIN(a); \
241   SHMatrix3x3 rot, temp; \
242   SETMAT(rot, cosa,-sina,0, sina,cosa,0, 0,0,1); \
243   MULMATMAT(mat, rot, temp); \
244   SETMATMAT(mat, temp); }
245
246 #define TRANSFORM2TO(v, mat, vout) { \
247 vout.x = v.x*mat.m[0][0] + v.y*mat.m[0][1] + 1*mat.m[0][2]; \
248   vout.y = v.x*mat.m[1][0] + v.y*mat.m[1][1] + 1*mat.m[1][2]; }
249
250 #define TRANSFORM2(v, mat) { \
251 SHVector2 temp; TRANSFORM2TO(v, mat, temp); v = temp; }
252
253 #define TRANSFORM2DIRTO(v, mat, vout) { \
254 vout.x = v.x*mat.m[0][0] + v.y*mat.m[0][1]; \
255   vout.y = v.x*mat.m[1][0] + v.y*mat.m[1][1]; }
256
257 #define TRANSFORM2DIR(v, mat) { \
258 SHVector2 temp; TRANSFORM2DIRTO(v, mat, temp); v = temp; }
259
260
261 /*--------------------------------------------------------
262  * Additional functions
263  *--------------------------------------------------------- */
264
265 void shMatrixToGL(SHMatrix3x3 *m, SHfloat mgl[16]);
266
267 SHint shInvertMatrix(SHMatrix3x3 *m, SHMatrix3x3 *mout);
268
269 SHfloat shVectorOrientation(SHVector2 *v);
270
271 int shLineLineXsection(SHVector2 *o1, SHVector2 *v1,
272                        SHVector2 *o2, SHVector2 *v2,
273                        SHVector2 *xsection);
274
275 #endif/* __SHVECTORS_H */