fixed copying of library links in install target
[gph-math] / src / vector.cc
1 /*
2 gph-math - math library for graphics programs
3 Copyright (C) 2016 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software. Feel free to use, modify, and/or redistribute
6 it under the terms of the MIT/X11 license. See LICENSE for details.
7 If you intend to redistribute parts of the code without the LICENSE file
8 replace this paragraph with the full contents of the LICENSE file.
9 */
10 #include "vector.h"
11 #include "matrix.h"
12
13 namespace gph {
14
15 Vector2::Vector2(const Vector3 &v)
16         : x(v.x), y(v.y)
17 {
18 }
19
20 // ---- Vector3 ----
21
22 Vector3::Vector3(const Vector4 &v)
23         : x(v.x), y(v.y), z(v.z)
24 {
25 }
26
27 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m)
28 {
29         float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0];
30         float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1];
31         float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2];
32         return Vector3(x, y, z);
33 }
34
35 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v)
36 {
37         float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
38         float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
39         float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
40         return Vector3(x, y, z);
41 }
42
43 // ---- Vector4 ----
44
45 Vector4::Vector4(const Vector3 &v)
46         : x(v.x), y(v.y), z(v.z), w(1.0f)
47 {
48 }
49
50 Vector4 operator *(const Vector4 &v, const Matrix4x4 &m)
51 {
52         float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0];
53         float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1];
54         float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2];
55         float w = v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3];
56         return Vector4(x, y, z, w);
57 }
58
59 Vector4 operator *(const Matrix4x4 &m, const Vector4 &v)
60 {
61         float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
62         float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
63         float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
64         float w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
65         return Vector4(x, y, z, w);
66 }
67
68
69 }       // namespace gph