-
Notifications
You must be signed in to change notification settings - Fork 5
/
vec.h
124 lines (106 loc) · 2.96 KB
/
vec.h
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
114
115
116
117
118
119
120
121
122
123
124
#pragma once
////////////////////////////////////////////////////////////////////////////////
#include <array>
#include <iterator>
////////////////////////////////////////////////////////////////////////////////
// STL-friendly fixed size containers
typedef std::array<unsigned char, 2> Vec2b;
typedef std::array<int, 2> Vec2i;
typedef std::array<unsigned, 2> Vec2u;
typedef std::array<float, 2> Vec2f;
typedef std::array<double, 2> Vec2d;
typedef std::array<unsigned char, 3> Vec3b;
typedef std::array<int, 3> Vec3i;
typedef std::array<unsigned, 3> Vec3u;
typedef std::array<float, 3> Vec3f;
typedef std::array<double, 3> Vec3d;
typedef std::array<unsigned char, 4> Vec4b;
typedef std::array<int, 4> Vec4i;
typedef std::array<unsigned, 4> Vec4u;
typedef std::array<float, 4> Vec4f;
typedef std::array<double, 4> Vec4d;
////////////////////////////////////////////////////////////////////////////////
template <typename Scalar, size_t Rows>
inline std::ostream& operator<<(std::ostream &out, std::array<Scalar, Rows> v) {
out << "{";
if (!v.empty()) {
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<Scalar>(out, "; "));
out << v.back();
}
out << "}";
return out;
}
// -----------------------------------------------------------------------------
template<typename T, size_t N>
std::array<T, N> operator +(std::array<T, N> x, std::array<T, N> y) {
std::array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = x[i] + y[i];
}
return res;
}
template<typename T, size_t N>
std::array<T, N> operator -(std::array<T, N> x, std::array<T, N> y) {
std::array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = x[i] - y[i];
}
return res;
}
template<typename T, size_t N>
std::array<T, N> operator -(std::array<T, N> y) {
std::array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = - y[i];
}
return res;
}
template<typename T, size_t N>
std::array<T, N> operator /(std::array<T, N> x, T s) {
std::array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = x[i] / s;
}
return res;
}
template<typename T, size_t N>
std::array<T, N> operator *(T s, std::array<T, N> x) {
std::array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = s * x[i];
}
return res;
}
// -----------------------------------------------------------------------------
namespace Vec {
template<typename T, size_t N>
std::array<T, N> constant(T c) {
std::array<T, N> x;
x.fill(c);
return x;
}
template<typename T, size_t N>
T prod(std::array<T, N> x) {
T res(1);
for (size_t i = 0; i < N; ++i) {
res *= x[i];
}
return res;
}
template<typename T, size_t N>
T sqLength(std::array<T, N> x) {
T res(0);
for (size_t i = 0; i < N; ++i) {
res += x[i] * x[i];
}
return res;
}
template<typename T, size_t N>
T sqDistance(std::array<T, N> x, std::array<T, N> y) {
T res(0);
for (size_t i = 0; i < N; ++i) {
res += (x[i] - y[i]) * (x[i] - y[i]);
}
return res;
}
}