volmetrics
view src/vector.cc @ 15:a93c8aa85e05
changed win title :p
author | Eleni Maria Stea <elene.mst@gmail.com> |
---|---|
date | Mon, 03 Feb 2014 01:30:47 +0200 |
parents | |
children |
line source
1 #include <math.h>
2 #include <stdio.h>
3 #include "vector.h"
4 #include "matrix.h"
6 Vector3::Vector3() {
7 x = 0;
8 y = 0;
9 z = 0;
10 }
12 Vector3::Vector3(float x, float y, float z) {
13 this->x = x;
14 this->y = y;
15 this->z = z;
16 }
18 void Vector3::transform(const Matrix4x4 &tm) {
19 float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
20 float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
21 float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
22 x = x1;
23 y = y1;
24 z = z1;
25 }
27 void Vector3::printv() {
28 printf("%f\t%f\t%f\n", x, y, z);
29 }
32 bool operator < (const Vector3 &a, const Vector3 &b) {
33 return a.x < b.x && a.y < b.y && a.z < b.z;
34 }
36 bool operator > (const Vector3 &a, const Vector3 &b) {
37 return a.x > b.x && a.y > b.y && a.z > b.z;
38 }
40 bool operator == (const Vector3 &a, const Vector3 &b) {
41 return a.x == b.x && a.y == b.y && a.z == b.z;
42 }
44 Vector3 operator + (const Vector3 &a, const Vector3 &b) {
45 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
46 }
48 Vector3 operator - (const Vector3 &a, const Vector3 &b) {
49 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
50 }
52 Vector3 operator - (const Vector3 &a) {
53 return Vector3(-a.x, -a.y, -a.z);
54 }
56 Vector3 operator * (const Vector3 &a, const Vector3 &b) {
57 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
58 }
60 Vector3 operator * (const Vector3 &a, float b) {
61 return Vector3(a.x*b, a.y*b, a.z*b);
62 }
64 Vector3 operator * (float b, const Vector3 &a) {
65 return Vector3(a.x*b, a.y*b, a.z*b);
66 }
68 Vector3 operator / (const Vector3 &a, float b) {
69 return Vector3(a.x / b, a.y / b, a.z / b);
70 }
72 const Vector3 &operator += (Vector3 &a, const Vector3 &b) {
73 a.x += b.x;
74 a.y += b.y;
75 a.z += b.z;
76 return a;
77 }
79 float length(const Vector3 &a) {
80 return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
81 }
83 float dot(const Vector3 &a, const Vector3 &b) {
84 return a.x*b.x + a.y*b.y + a.z*b.z;
85 }
87 Vector3 cross(const Vector3 &a, const Vector3 &b) {
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);
89 }
91 Vector3 normalize(const Vector3 &vec) {
92 float mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
93 return vec / mag;
94 }
96 Vector3 reflect(const Vector3 &v, const Vector3 &n) {
97 return 2.0 * dot(v, n) * n - v;
98 }
100 //////
101 //
102 /////
104 Vector4::Vector4() {
105 x = 0;
106 y = 0;
107 z = 0;
108 w = 0;
109 }
111 Vector4::Vector4(float x, float y, float z, float w) {
112 this->x = x;
113 this->y = y;
114 this->z = z;
115 this->w = w;
116 }
118 void Vector4::transform(const Matrix4x4 &tm) {
119 float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
120 float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
121 float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
122 float w1 = tm.m[3][0]*x + tm.m[3][1]*y + tm.m[3][2]*z + tm.m[3][3];
123 x = x1;
124 y = y1;
125 z = z1;
126 w = w1;
127 }
129 void Vector4::printv() {
130 printf("%f\t%f\t%f\t%f\n", x, y, z, w);
131 }
134 bool operator < (const Vector4 &a, const Vector4 &b) {
135 return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w;
136 }
138 bool operator > (const Vector4 &a, const Vector4 &b) {
139 return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w;
140 }
142 bool operator == (const Vector4 &a, const Vector4 &b) {
143 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
144 }
146 Vector4 operator + (const Vector4 &a, const Vector4 &b) {
147 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
148 }
150 Vector4 operator - (const Vector4 &a, const Vector4 &b) {
151 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
152 }
154 Vector4 operator - (const Vector4 &a) {
155 return Vector4(-a.x, -a.y, -a.z, -a.w);
156 }
158 Vector4 operator * (const Vector4 &a, const Vector4 &b) {
159 return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
160 }
162 Vector4 operator * (const Vector4 &a, float b) {
163 return Vector4(a.x*b, a.y*b, a.z*b, a.w*b);
164 }
166 Vector4 operator * (float b, const Vector4 &a) {
167 return Vector4(a.x * b, a.y * b, a.z * b, a.w * b);
168 }
170 Vector4 operator / (const Vector4 &a, float b) {
171 return Vector4(a.x / b, a.y / b, a.z / b, a.w / b);
172 }
174 const Vector4 &operator += (Vector4 &a, const Vector4 &b) {
175 a.x += b.x;
176 a.y += b.y;
177 a.z += b.z;
178 a.w += b.w;
179 return a;
180 }
182 float length(const Vector4 &a) {
183 return sqrt(a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w);
184 }
186 float dot(const Vector4 &a, const Vector4 &b) {
187 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
188 }
190 Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
191 float a = (v2.x * v3.y) - (v2.y * v3.x);
192 float b = (v2.x * v3.z) - (v2.z * v3.x);
193 float c = (v2.x * v3.w) - (v2.w * v3.x);
194 float d = (v2.y * v3.z) - (v2.z * v3.y);
195 float e = (v2.y * v3.w) - (v2.w * v3.y);
196 float f = (v2.z * v3.w) - (v2.w * v3.z);
198 Vector4 result;
199 result.x = (v1.y * f) - (v1.z * e) + (v1.w * d);
200 result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
201 result.z = (v1.x * e) - (v1.y * c) + (v1.w * a);
202 result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
203 return result;
204 }
206 Vector4 normalize(const Vector4 &vec) {
207 float mag = sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
208 return vec / mag;
209 }
211 Vector4 reflect(const Vector4 &v, const Vector4 &n) {
212 return 2.0 * dot(v, n) * n - v;
213 }