]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ShivaVG/src/shVectors.c
ShivaVG: check for zero before dividing.
[simgear.git] / simgear / canvas / ShivaVG / src / shVectors.c
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 #include "shVectors.h"
22
23 #define _ITEM_T SHVector2
24 #define _ARRAY_T SHVector2Array
25 #define _FUNC_T shVector2Array
26 #define _COMPARE_T(v1,v2) EQ2V(v1,v2)
27 #define _ARRAY_DEFINE
28 #include "shArrayBase.h"
29
30 void SHVector2_ctor(SHVector2 *v) {
31   v->x=0.0f; v->y=0.0f;
32 }
33
34 void SHVector2_dtor(SHVector2 *v) {
35 }
36
37 void SHVector3_ctor(SHVector3 *v) {
38   v->x=0.0f; v->y=0.0f; v->z=0.0f;
39 }
40
41 void SHVector3_dtor(SHVector3 *v) {
42 }
43
44 void SHVector4_ctor(SHVector4 *v) {
45   v->x=0.0f; v->y=0.0f; v->z=0.0f; v->w=0.0f;
46 }
47
48 void SHVector4_dtor(SHVector4 *v) {
49 }
50
51 void SHRectangle_ctor(SHRectangle *r) {
52   r->x=0.0f; r->y=0.0f; r->w=0.0f; r->h=0.0f;
53 }
54
55 void SHRectangle_dtor(SHRectangle *r) {
56 }
57
58 void shRectangleSet(SHRectangle *r, SHfloat x,
59                     SHfloat y, SHfloat w, SHfloat h)
60 {  
61   r->x=x; r->y=y; r->w=w; r->h=h;
62 }
63
64 void SHMatrix3x3_ctor(SHMatrix3x3 *mt)
65 {
66   IDMAT((*mt));
67 }
68
69 void SHMatrix3x3_dtor(SHMatrix3x3 *mt)
70 {
71 }
72
73 void shMatrixToGL(SHMatrix3x3 *m, SHfloat mgl[16])
74 {
75   /* When 2D vectors are specified OpenGL defaults Z to 0.0f so we
76      have to shift the third column of our 3x3 matrix to right */
77   mgl[0] = m->m[0][0]; mgl[4] = m->m[0][1]; mgl[8]  = 0.0f; mgl[12] = m->m[0][2];
78   mgl[1] = m->m[1][0]; mgl[5] = m->m[1][1]; mgl[9]  = 0.0f; mgl[13] = m->m[1][2];
79   mgl[2] = m->m[2][0]; mgl[6] = m->m[2][1]; mgl[10] = 1.0f; mgl[14] = m->m[2][1];
80   mgl[3] = 0.0f;       mgl[7] = 0.0f;       mgl[11] = 0.0f; mgl[15] = 1.0f;
81 }
82
83 int shInvertMatrix(SHMatrix3x3 *m, SHMatrix3x3 *mout)
84 {
85   /* Calculate determinant */
86   SHfloat D0 = m->m[1][1]*m->m[2][2] - m->m[2][1]*m->m[1][2];
87   SHfloat D1 = m->m[2][0]*m->m[1][2] - m->m[1][0]*m->m[2][2];
88   SHfloat D2 = m->m[1][0]*m->m[2][1] - m->m[2][0]*m->m[1][1]; 
89   SHfloat D = m->m[0][0]*D0 + m->m[0][1]*D1 + m->m[0][2]*D2;
90   
91   /* Check if singular */
92   if( D == 0.0f ) return 0;
93   D = 1.0f / D;
94   
95   /* Calculate inverse */
96   mout->m[0][0] = D * D0;
97   mout->m[1][0] = D * D1;
98   mout->m[2][0] = D * D2;
99   mout->m[0][1] = D * (m->m[2][1]*m->m[0][2] - m->m[0][1]*m->m[2][2]);
100   mout->m[1][1] = D * (m->m[0][0]*m->m[2][2] - m->m[2][0]*m->m[0][2]);
101   mout->m[2][1] = D * (m->m[2][0]*m->m[0][1] - m->m[0][0]*m->m[2][1]);
102   mout->m[0][2] = D * (m->m[0][1]*m->m[1][2] - m->m[1][1]*m->m[0][2]);
103   mout->m[1][2] = D * (m->m[1][0]*m->m[0][2] - m->m[0][0]*m->m[1][2]);
104   mout->m[2][2] = D * (m->m[0][0]*m->m[1][1] - m->m[1][0]*m->m[0][1]);
105   
106   return 1;
107 }
108
109 SHfloat shVectorOrientation(SHVector2 *v) {
110   SHfloat norm = (SHfloat)NORM2((*v));
111   SHfloat cosa = v->x/norm;
112   SHfloat sina = v->y/norm;
113   return (SHfloat)(sina>=0 ? SH_ACOS(cosa) : 2*PI-SH_ACOS(cosa));
114 }
115
116 int shLineLineXsection(SHVector2 *o1, SHVector2 *v1,
117                        SHVector2 *o2, SHVector2 *v2,
118                        SHVector2 *xsection)
119 {
120   SHfloat rightU = o2->x - o1->x;
121   SHfloat rightD = o2->y - o1->y;
122   
123   SHfloat D  = v1->x  * (-v2->y) - v1->y   * (-v2->x);
124   SHfloat DX = rightU * (-v2->y) - rightD * (-v2->x);
125 /*SHfloat DY = v1.x   * rightD  - v1.y   * rightU;*/
126   
127   SHfloat t1;
128   
129   if (D == 0.0f)
130     return 0;
131
132   t1 = DX / D;
133
134   xsection->x = o1->x + t1*v1->x;
135   xsection->y = o1->y + t1*v1->y;
136   return 1;
137 }