rev |
line source |
eleni@0
|
1 #include <math.h>
|
eleni@0
|
2 #include <stdio.h>
|
eleni@0
|
3 #include "vector.h"
|
eleni@0
|
4 #include "matrix.h"
|
eleni@0
|
5
|
eleni@0
|
6 Vector3::Vector3() {
|
eleni@0
|
7 x = 0;
|
eleni@0
|
8 y = 0;
|
eleni@0
|
9 z = 0;
|
eleni@0
|
10 }
|
eleni@0
|
11
|
eleni@0
|
12 Vector3::Vector3(float x, float y, float z) {
|
eleni@0
|
13 this->x = x;
|
eleni@0
|
14 this->y = y;
|
eleni@0
|
15 this->z = z;
|
eleni@0
|
16 }
|
eleni@0
|
17
|
eleni@0
|
18 void Vector3::transform(const Matrix4x4 &tm) {
|
eleni@0
|
19 float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
|
eleni@0
|
20 float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
|
eleni@0
|
21 float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
|
eleni@0
|
22 x = x1;
|
eleni@0
|
23 y = y1;
|
eleni@0
|
24 z = z1;
|
eleni@0
|
25 }
|
eleni@0
|
26
|
eleni@0
|
27 void Vector3::printv() {
|
eleni@0
|
28 printf("%f\t%f\t%f\n", x, y, z);
|
eleni@0
|
29 }
|
eleni@0
|
30
|
eleni@0
|
31
|
eleni@0
|
32 bool operator < (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
33 return a.x < b.x && a.y < b.y && a.z < b.z;
|
eleni@0
|
34 }
|
eleni@0
|
35
|
eleni@0
|
36 bool operator > (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
37 return a.x > b.x && a.y > b.y && a.z > b.z;
|
eleni@0
|
38 }
|
eleni@0
|
39
|
eleni@0
|
40 bool operator == (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
41 return a.x == b.x && a.y == b.y && a.z == b.z;
|
eleni@0
|
42 }
|
eleni@0
|
43
|
eleni@0
|
44 Vector3 operator + (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
45 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
|
eleni@0
|
46 }
|
eleni@0
|
47
|
eleni@0
|
48 Vector3 operator - (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
49 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
|
eleni@0
|
50 }
|
eleni@0
|
51
|
eleni@0
|
52 Vector3 operator - (const Vector3 &a) {
|
eleni@0
|
53 return Vector3(-a.x, -a.y, -a.z);
|
eleni@0
|
54 }
|
eleni@0
|
55
|
eleni@0
|
56 Vector3 operator * (const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
57 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
|
eleni@0
|
58 }
|
eleni@0
|
59
|
eleni@0
|
60 Vector3 operator * (const Vector3 &a, float b) {
|
eleni@0
|
61 return Vector3(a.x*b, a.y*b, a.z*b);
|
eleni@0
|
62 }
|
eleni@0
|
63
|
eleni@0
|
64 Vector3 operator * (float b, const Vector3 &a) {
|
eleni@0
|
65 return Vector3(a.x*b, a.y*b, a.z*b);
|
eleni@0
|
66 }
|
eleni@0
|
67
|
eleni@0
|
68 Vector3 operator / (const Vector3 &a, float b) {
|
eleni@0
|
69 return Vector3(a.x / b, a.y / b, a.z / b);
|
eleni@0
|
70 }
|
eleni@0
|
71
|
eleni@0
|
72 const Vector3 &operator += (Vector3 &a, const Vector3 &b) {
|
eleni@0
|
73 a.x += b.x;
|
eleni@0
|
74 a.y += b.y;
|
eleni@0
|
75 a.z += b.z;
|
eleni@0
|
76 return a;
|
eleni@0
|
77 }
|
eleni@0
|
78
|
eleni@0
|
79 float length(const Vector3 &a) {
|
eleni@0
|
80 return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
|
eleni@0
|
81 }
|
eleni@0
|
82
|
eleni@0
|
83 float dot(const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
84 return a.x*b.x + a.y*b.y + a.z*b.z;
|
eleni@0
|
85 }
|
eleni@0
|
86
|
eleni@0
|
87 Vector3 cross(const Vector3 &a, const Vector3 &b) {
|
eleni@0
|
88 return Vector3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
|
eleni@0
|
89 }
|
eleni@0
|
90
|
eleni@0
|
91 Vector3 normalize(const Vector3 &vec) {
|
eleni@0
|
92 float mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
|
eleni@0
|
93 return vec / mag;
|
eleni@0
|
94 }
|
eleni@0
|
95
|
eleni@0
|
96 Vector3 reflect(const Vector3 &v, const Vector3 &n) {
|
eleni@0
|
97 return 2.0 * dot(v, n) * n - v;
|
eleni@0
|
98 }
|
eleni@0
|
99
|
eleni@0
|
100 //////
|
eleni@0
|
101 //
|
eleni@0
|
102 /////
|
eleni@0
|
103
|
eleni@0
|
104 Vector4::Vector4() {
|
eleni@0
|
105 x = 0;
|
eleni@0
|
106 y = 0;
|
eleni@0
|
107 z = 0;
|
eleni@0
|
108 w = 0;
|
eleni@0
|
109 }
|
eleni@0
|
110
|
eleni@0
|
111 Vector4::Vector4(float x, float y, float z, float w) {
|
eleni@0
|
112 this->x = x;
|
eleni@0
|
113 this->y = y;
|
eleni@0
|
114 this->z = z;
|
eleni@0
|
115 this->w = w;
|
eleni@0
|
116 }
|
eleni@0
|
117
|
eleni@0
|
118 void Vector4::transform(const Matrix4x4 &tm) {
|
eleni@0
|
119 float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
|
eleni@0
|
120 float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
|
eleni@0
|
121 float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
|
eleni@0
|
122 float w1 = tm.m[3][0]*x + tm.m[3][1]*y + tm.m[3][2]*z + tm.m[3][3];
|
eleni@0
|
123 x = x1;
|
eleni@0
|
124 y = y1;
|
eleni@0
|
125 z = z1;
|
eleni@0
|
126 w = w1;
|
eleni@0
|
127 }
|
eleni@0
|
128
|
eleni@0
|
129 void Vector4::printv() {
|
eleni@0
|
130 printf("%f\t%f\t%f\t%f\n", x, y, z, w);
|
eleni@0
|
131 }
|
eleni@0
|
132
|
eleni@0
|
133
|
eleni@0
|
134 bool operator < (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
135 return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w;
|
eleni@0
|
136 }
|
eleni@0
|
137
|
eleni@0
|
138 bool operator > (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
139 return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w;
|
eleni@0
|
140 }
|
eleni@0
|
141
|
eleni@0
|
142 bool operator == (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
143 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
|
eleni@0
|
144 }
|
eleni@0
|
145
|
eleni@0
|
146 Vector4 operator + (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
147 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
|
eleni@0
|
148 }
|
eleni@0
|
149
|
eleni@0
|
150 Vector4 operator - (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
151 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
|
eleni@0
|
152 }
|
eleni@0
|
153
|
eleni@0
|
154 Vector4 operator - (const Vector4 &a) {
|
eleni@0
|
155 return Vector4(-a.x, -a.y, -a.z, -a.w);
|
eleni@0
|
156 }
|
eleni@0
|
157
|
eleni@0
|
158 Vector4 operator * (const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
159 return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
|
eleni@0
|
160 }
|
eleni@0
|
161
|
eleni@0
|
162 Vector4 operator * (const Vector4 &a, float b) {
|
eleni@0
|
163 return Vector4(a.x*b, a.y*b, a.z*b, a.w*b);
|
eleni@0
|
164 }
|
eleni@0
|
165
|
eleni@0
|
166 Vector4 operator * (float b, const Vector4 &a) {
|
eleni@0
|
167 return Vector4(a.x * b, a.y * b, a.z * b, a.w * b);
|
eleni@0
|
168 }
|
eleni@0
|
169
|
eleni@0
|
170 Vector4 operator / (const Vector4 &a, float b) {
|
eleni@0
|
171 return Vector4(a.x / b, a.y / b, a.z / b, a.w / b);
|
eleni@0
|
172 }
|
eleni@0
|
173
|
eleni@0
|
174 const Vector4 &operator += (Vector4 &a, const Vector4 &b) {
|
eleni@0
|
175 a.x += b.x;
|
eleni@0
|
176 a.y += b.y;
|
eleni@0
|
177 a.z += b.z;
|
eleni@0
|
178 a.w += b.w;
|
eleni@0
|
179 return a;
|
eleni@0
|
180 }
|
eleni@0
|
181
|
eleni@0
|
182 float length(const Vector4 &a) {
|
eleni@0
|
183 return sqrt(a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w);
|
eleni@0
|
184 }
|
eleni@0
|
185
|
eleni@0
|
186 float dot(const Vector4 &a, const Vector4 &b) {
|
eleni@0
|
187 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
eleni@0
|
188 }
|
eleni@0
|
189
|
eleni@0
|
190 Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
|
eleni@0
|
191 float a = (v2.x * v3.y) - (v2.y * v3.x);
|
eleni@0
|
192 float b = (v2.x * v3.z) - (v2.z * v3.x);
|
eleni@0
|
193 float c = (v2.x * v3.w) - (v2.w * v3.x);
|
eleni@0
|
194 float d = (v2.y * v3.z) - (v2.z * v3.y);
|
eleni@0
|
195 float e = (v2.y * v3.w) - (v2.w * v3.y);
|
eleni@0
|
196 float f = (v2.z * v3.w) - (v2.w * v3.z);
|
eleni@0
|
197
|
eleni@0
|
198 Vector4 result;
|
eleni@0
|
199 result.x = (v1.y * f) - (v1.z * e) + (v1.w * d);
|
eleni@0
|
200 result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
|
eleni@0
|
201 result.z = (v1.x * e) - (v1.y * c) + (v1.w * a);
|
eleni@0
|
202 result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
|
eleni@0
|
203 return result;
|
eleni@0
|
204 }
|
eleni@0
|
205
|
eleni@0
|
206 Vector4 normalize(const Vector4 &vec) {
|
eleni@0
|
207 float mag = sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
|
eleni@0
|
208 return vec / mag;
|
eleni@0
|
209 }
|
eleni@0
|
210
|
eleni@0
|
211 Vector4 reflect(const Vector4 &v, const Vector4 &n) {
|
eleni@0
|
212 return 2.0 * dot(v, n) * n - v;
|
eleni@0
|
213 }
|