2 vkeyb - camera motion detection virtual keyboard
3 Copyright (C) 2012 Eleni Maria Stea <elene.mst@gmail.com>
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.
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.
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/>.
30 Vector3::Vector3(double x, double y, double z) {
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];
45 void Vector3::printv() {
46 printf("%f\t%f\t%f\n", x, y, z);
50 bool operator < (const Vector3 &a, const Vector3 &b) {
51 return a.x < b.x && a.y < b.y && a.z < b.z;
54 bool operator > (const Vector3 &a, const Vector3 &b) {
55 return a.x > b.x && a.y > b.y && a.z > b.z;
58 bool operator == (const Vector3 &a, const Vector3 &b) {
59 return a.x == b.x && a.y == b.y && a.z == b.z;
62 Vector3 operator + (const Vector3 &a, const Vector3 &b) {
63 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
66 Vector3 operator - (const Vector3 &a, const Vector3 &b) {
67 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
70 Vector3 operator - (const Vector3 &a) {
71 return Vector3(-a.x, -a.y, -a.z);
74 Vector3 operator * (const Vector3 &a, const Vector3 &b) {
75 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
78 Vector3 operator * (const Vector3 &a, double b) {
79 return Vector3(a.x*b, a.y*b, a.z*b);
82 Vector3 operator * (double b, const Vector3 &a) {
83 return Vector3(a.x*b, a.y*b, a.z*b);
86 Vector3 operator / (const Vector3 &a, double b) {
87 return Vector3(a.x / b, a.y / b, a.z / b);
90 const Vector3 &operator += (Vector3 &a, const Vector3 &b) {
97 double length(const Vector3 &a) {
98 return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
101 double dot(const Vector3 &a, const Vector3 &b) {
102 return a.x*b.x + a.y*b.y + a.z*b.z;
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);
109 Vector3 normalize(const Vector3 &vec) {
110 double mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
114 Vector3 reflect(const Vector3 &v, const Vector3 &n) {
115 return 2.0 * dot(v, n) * n - v;