Vector4 inline definitions, and rearrangement into multiple inl files
[gph-math] / src / vector.cc
1 #include "vector.h"
2 #include "matrix.h"
3
4 namespace gph {
5
6 Vector2::Vector2(const Vector3 &v)
7         : x(v.x), y(v.y)
8 {
9 }
10
11 // ---- Vector3 ----
12
13 Vector3::Vector3(const Vector4 &v)
14         : x(v.x), y(v.y), z(v.z)
15 {
16 }
17
18 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m)
19 {
20         float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0];
21         float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1];
22         float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2];
23         return Vector3(x, y, z);
24 }
25
26 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v)
27 {
28         float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
29         float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
30         float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
31         return Vector3(x, y, z);
32 }
33
34 // ---- Vector4 ----
35
36 Vector4::Vector4(const Vector3 &v)
37         : x(v.x), y(v.y), z(v.z), w(1.0f)
38 {
39 }
40
41 Vector4 operator *(const Vector4 &v, const Matrix4x4 &m)
42 {
43         float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0];
44         float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1];
45         float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2];
46         float w = v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3];
47         return Vector4(x, y, z, w);
48 }
49
50 Vector4 operator *(const Matrix4x4 &m, const Vector4 &v)
51 {
52         float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
53         float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
54         float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
55         float w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
56         return Vector4(x, y, z, w);
57 }
58
59
60 }       // namespace gph