SPIDAR API Library  0x16033101
Space Interface Device for Artificial Reality
Vector.hpp
Go to the documentation of this file.
1 
5 #ifndef SPIDAR_VECTOR_HPP
6 #define SPIDAR_VECTOR_HPP
7 
8 #include <cmath>
9 #include <string>
10 
11 namespace Spidar
12 {
13 namespace Math
14 {
23 template <class T, size_t N>
24 struct Vector
25 {
26  typedef T ValueType;
27 
28  static const size_t size = N;
29 
30  ValueType element[N];
31 
32  // コンストラクタ
33  Vector(void)
34  {
35  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(0);
36  }
37 
38  Vector(const Vector& other)
39  {
40  for (size_t i=0; i<N; ++i) element[i] = other.element[i];
41  }
42 
43  explicit Vector(int value)
44  {
45  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(value);
46  }
47 
48  Vector(const ValueType (&other)[N])
49  {
50  for (size_t i=0; i<N; ++i) element[i] = other[i];
51  }
52 
53  template <class OtherType>
54  explicit Vector(const OtherType& other)
55  {
56  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(other.element[i]);
57  }
58 
59  explicit Vector(ValueType x, ValueType y, ValueType z)
60  {
61  static_assert(N==3, "N is not 3");
62 
63  this->x() = static_cast<ValueType>(x);
64  this->y() = static_cast<ValueType>(y);
65  this->z() = static_cast<ValueType>(z);
66  }
67 
68  Vector& operator=(const Vector& other)
69  {
70  for (size_t i=0; i<N; ++i) element[i] = other.element[i];
71  return *this;
72  }
73 
74  ValueType& operator[](size_t i)
75  {
76  return element[i];
77  }
78 
79  const ValueType& operator[](size_t i) const
80  {
81  return element[i];
82  }
83 
84  //
85  bool operator==(const Vector& other) const
86  {
87  for (size_t i=0; i<N; ++i)
88  {
89  if (element[i] != other.element[i]) return false;
90  }
91  return true;
92  }
93 
94  //
95  bool operator!=(const Vector& other) const
96  {
97  if (*this==other) return false;
98  else return true;
99  }
100 
101  //
102  Vector& operator+=(const Vector& other)
103  {
104  for (size_t i=0; i<N; ++i) element[i] += other.element[i];
105  return *this;
106  }
107 
108  //
109  Vector& operator-=(const Vector& other)
110  {
111  for (size_t i=0; i<N; ++i) element[i] -= other.element[i];
112  return *this;
113  }
114 
115  //
116  Vector& operator*=(double value)
117  {
118  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(value * element[i]);
119  return *this;
120  }
121 
122  //
123  Vector& operator/=(double value)
124  {
125  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(element[i] / value);
126  return *this;
127  }
128 
129  //
130  void clear(void)
131  {
132  for (size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(0);
133  }
134 
135  //
136  ValueType dot(const Vector& other) const
137  {
138  ValueType temp = 0;
139 
140  for (size_t i=0; i<N; ++i)
141  {
142  temp += element[i] * other.element[i];
143  }
144  return temp;
145  }
146 
147  //
148  Vector cross(const Vector& other) const
149  {
150  static_assert(N==3, "N is not 3");
151 
152  Vector temp;
153 
154  temp.x() = static_cast<ValueType>(y() * other.z() - z() * other.y());
155  temp.y() = static_cast<ValueType>(z() * other.x() - x() * other.z());
156  temp.z() = static_cast<ValueType>(x() * other.y() - y() * other.x());
157 
158  return temp;
159  }
160 
161  //
162  ValueType norm(void) const
163  {
164  return static_cast<ValueType>(std::sqrt(static_cast<double>(dot(*this))));
165  }
166 
167  //
168  Vector unit(void) const
169  {
170  return *this / norm();
171  }
172 
173  //
174  ValueType& x(void)
175  {
176  static_assert(N==3, "N is not 3");
177 
178  return element[0];
179  }
180 
181  const ValueType& x(void) const
182  {
183  static_assert(N==3, "N is not 3");
184 
185  return element[0];
186  }
187 
188  //
189  ValueType& y(void)
190  {
191  static_assert(N==3, "N is not 3");
192 
193  return element[1];
194  }
195 
196  const ValueType& y(void) const
197  {
198  static_assert(N==3, "N is not 3");
199 
200  return element[1];
201  }
202 
203  //
204  ValueType& z(void)
205  {
206  static_assert(N==3, "N is not 3");
207 
208  return element[2];
209  }
210 
211  const ValueType& z(void) const
212  {
213  static_assert(N==3, "N is not 3");
214 
215  return element[2];
216  }
217 
218 }; // end of struct Vector.
219 
220 //
221 template <class T, size_t N>
222 std::ostream& operator<<(std::ostream& os, const Vector<T,N>& v)
223 {
224  os << "(";
225  for (size_t i=0; i<N; ++i)
226  {
227  os << v.element[i];
228  if (i<N-1) os << " ";
229  }
230 
231  os << ")";
232 
233  return os;
234 }
235 
236 //
237 template <class T, size_t N>
238 const Vector<T,N> operator+(Vector<T,N> lhs, const Vector<T,N>& rhs)
239 {
240  return lhs += rhs;
241 }
242 
243 //
244 template <class T, size_t N>
245 const Vector<T,N> operator-(Vector<T,N> lhs, const Vector<T,N>& rhs)
246 {
247  return lhs -= rhs;
248 }
249 
250 //
251 template <class T, size_t N>
252 const Vector<T,N> operator*(Vector<T,N> lhs, double rhs)
253 {
254  return lhs *= rhs;
255 }
256 
257 //
258 template <class T, size_t N>
259 const Vector<T,N> operator*(double lhs, Vector<T,N> rhs)
260 {
261  return rhs *= lhs;
262 }
263 
264 //
265 template <class T, size_t N>
266 const Vector<T,N> operator/(Vector<T,N> lhs, double rhs)
267 {
268  return lhs /= rhs;
269 }
270 
271 //
272 template <class T, size_t N>
273 const Vector<T,N> lerp(const Vector<T,N>& from, const Vector<T,N>& to, double t)
274 {
275  typedef Vector<T,N> VectorType;
276  typedef typename VectorType::ValueType ValueType;
277 
278  VectorType v;
279 
280  for (size_t i=0; i<N; ++i)
281  {
282  v.element[i] = static_cast<ValueType>((1.0-t) * from.element[i] + t * to.element[i]);
283  }
284  return v;
285 }
286 
287 } // end of namespace Math.
288 } // end of namespace Spidar.
289 
290 #endif // SPIDAR_VECTOR_HPP
291 
292 // end of file.
T ValueType
要素の型
Definition: Vector.hpp:26
SPIDARライブラリのルート名前空間です.
ベクトルクラスです.
Definition: Vector.hpp:24
static const size_t size
要素数
Definition: Vector.hpp:28
ValueType element[N]
ベクトルの要素
Definition: Vector.hpp:30