summaryrefslogtreecommitdiffstats
path: root/src/Vector.hpp
blob: 6389bbb09b7b6aee821c4db84211c1987feb9115 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once

#include <ostream>
#include <cmath>
#include <tuple>

#include <glm/vec3.hpp>

template<class T>
struct Vector3 {
	T x, y, z;

	Vector3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {}

	Vector3(const Vector3 &rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {}

    template<class U>
    Vector3(const Vector3<U> &rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {}

    Vector3(Vector3 &&rhs) = default;

	~Vector3() = default;

	double GetLength() const { return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); }

	operator glm::vec3() const {
		return glm::vec3(x, y, z);
	}

    glm::vec3 glm() const {
        return (glm::vec3)(*this);
    }

	void swap(Vector3 &rhs) noexcept {
		std::swap(x, rhs.x);
		std::swap(y, rhs.y);
		std::swap(z, rhs.z);
	}

	Vector3 &operator=(Vector3 rhs) noexcept {
		rhs.swap(*this);
		return *this;
	}

	Vector3 operator*(T rhs) const {
		return Vector3<T>(
				x * rhs,
				y * rhs,
				z * rhs
		);
	}

	Vector3 operator/(T rhs) const {
		return Vector3<T>(
				x / rhs,
				y / rhs,
				z / rhs
		);
	}

	Vector3 operator+(const Vector3 &rhs) const {
		return Vector3<T>(
				x + rhs.x,
				y + rhs.y,
				z + rhs.z
		);
	}

	Vector3 operator-(const Vector3 &rhs) const {
		return Vector3<T>(
				x - rhs.x,
				y - rhs.y,
				z - rhs.z
		);
	}

	Vector3 operator*(const Vector3 &rhs) const {
		return Vector3<T>(
				x * rhs.x,
				y * rhs.y,
				z * rhs.z
		);
	}

	Vector3 operator/(const Vector3 &rhs) const {
		return Vector3<T>(
				x / rhs.x,
				y / rhs.y,
				z / rhs.z
		);
	}

	bool operator==(const Vector3 &rhs) const {
		return (x == rhs.x && y == rhs.y && z == rhs.z);
	}

	bool operator!=(const Vector3 &rhs) const {
		return !(*this == rhs);
	}

	bool operator<(const Vector3 &rhs) const {
		return std::tie(x, y, z) < std::tie(rhs.x, rhs.y, rhs.z);
	}
    

	friend std::ostream &operator<<(std::ostream &os, const Vector3 &vector3) {
		os << vector3.x << ", " << vector3.y << ", " << vector3.z;
		return os;
	}
};

using VectorF = Vector3<double>;
using Vector = Vector3<signed long long>;