An optical flow driven virtual keyboard.
[vkeyb] / src / vector.cc
1 /* 
2 vkeyb - camera motion detection virtual keyboard
3 Copyright (C) 2012 Eleni Maria Stea <elene.mst@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <math.h>
20 #include <stdio.h>
21 #include "vector.h"
22 #include "matrix.h"
23
24 Vector3::Vector3() {
25         x = 0;
26         y = 0;
27         z = 0;
28 }
29
30 Vector3::Vector3(double x, double y, double z) {
31         this->x = x;
32         this->y = y;
33         this->z = z;
34 }
35
36 void Vector3::transform(const Matrix4x4 &tm) {
37         double x1 = tm.matrix[0][0]*x + tm.matrix[0][1]*y + tm.matrix[0][2]*z + tm.matrix[0][3];
38         double y1 = tm.matrix[1][0]*x + tm.matrix[1][1]*y + tm.matrix[1][2]*z + tm.matrix[1][3];
39         double z1 = tm.matrix[2][0]*x + tm.matrix[2][1]*y + tm.matrix[2][2]*z + tm.matrix[2][3];
40         x = x1;
41         y = y1;
42         z = z1;
43 }
44
45 void Vector3::printv() {
46         printf("%f\t%f\t%f\n", x, y, z);
47 }
48
49
50 bool operator < (const Vector3 &a, const Vector3 &b) {
51         return a.x < b.x && a.y < b.y && a.z < b.z;
52 }
53
54 bool operator > (const Vector3 &a, const Vector3 &b) {
55         return a.x > b.x && a.y > b.y && a.z > b.z;
56 }
57
58 bool operator == (const Vector3 &a, const Vector3 &b) {
59         return a.x == b.x && a.y == b.y && a.z == b.z;
60 }
61
62 Vector3 operator + (const Vector3 &a, const Vector3 &b) {
63         return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
64 }
65
66 Vector3 operator - (const Vector3 &a, const Vector3 &b) {
67         return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
68 }
69
70 Vector3 operator - (const Vector3 &a) {
71         return Vector3(-a.x, -a.y, -a.z);
72 }
73
74 Vector3 operator * (const Vector3 &a, const Vector3 &b) {
75         return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
76 }
77
78 Vector3 operator * (const Vector3 &a, double b) {
79         return Vector3(a.x*b, a.y*b, a.z*b);
80 }
81
82 Vector3 operator * (double b, const Vector3 &a) {
83         return Vector3(a.x*b, a.y*b, a.z*b);
84 }
85
86 Vector3 operator / (const Vector3 &a, double b) {
87         return Vector3(a.x / b, a.y / b, a.z / b);
88 }
89
90 const Vector3 &operator += (Vector3 &a, const Vector3 &b) {
91         a.x += b.x;
92         a.y += b.y;
93         a.z += b.z;
94         return a;
95 }
96
97 double length(const Vector3 &a) {
98         return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
99 }
100
101 double dot(const Vector3 &a, const Vector3 &b) {
102         return a.x*b.x + a.y*b.y + a.z*b.z;
103 }
104
105 Vector3 cross(const Vector3 &a, const Vector3 &b) {
106         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);
107 }
108
109 Vector3 normalize(const Vector3 &vec) {
110         double mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
111         return vec / mag;
112 }
113
114 Vector3 reflect(const Vector3 &v, const Vector3 &n) {
115         return 2.0 * dot(v, n) * n - v;
116 }