5 #ifndef SPIDAR_VECTOR_HPP 6 #define SPIDAR_VECTOR_HPP 23 template <
class T,
size_t N>
28 static const size_t size = N;
35 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(0);
40 for (
size_t i=0; i<N; ++i) element[i] = other.
element[i];
45 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(value);
48 Vector(
const ValueType (&other)[N])
50 for (
size_t i=0; i<N; ++i) element[i] = other[i];
53 template <
class OtherType>
54 explicit Vector(
const OtherType& other)
56 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(other.element[i]);
59 explicit Vector(ValueType x, ValueType y, ValueType z)
61 static_assert(N==3,
"N is not 3");
63 this->x() =
static_cast<ValueType
>(x);
64 this->y() =
static_cast<ValueType
>(y);
65 this->z() =
static_cast<ValueType
>(z);
70 for (
size_t i=0; i<N; ++i) element[i] = other.
element[i];
74 ValueType& operator[](
size_t i)
79 const ValueType& operator[](
size_t i)
const 85 bool operator==(
const Vector& other)
const 87 for (
size_t i=0; i<N; ++i)
89 if (element[i] != other.
element[i])
return false;
95 bool operator!=(
const Vector& other)
const 97 if (*
this==other)
return false;
104 for (
size_t i=0; i<N; ++i) element[i] += other.
element[i];
111 for (
size_t i=0; i<N; ++i) element[i] -= other.
element[i];
116 Vector& operator*=(
double value)
118 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(value * element[i]);
123 Vector& operator/=(
double value)
125 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(element[i] / value);
132 for (
size_t i=0; i<N; ++i) element[i] = static_cast<ValueType>(0);
136 ValueType dot(
const Vector& other)
const 140 for (
size_t i=0; i<N; ++i)
142 temp += element[i] * other.
element[i];
150 static_assert(N==3,
"N is not 3");
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());
162 ValueType norm(
void)
const 164 return static_cast<ValueType
>(std::sqrt(static_cast<double>(dot(*
this))));
170 return *
this / norm();
176 static_assert(N==3,
"N is not 3");
181 const ValueType& x(
void)
const 183 static_assert(N==3,
"N is not 3");
191 static_assert(N==3,
"N is not 3");
196 const ValueType& y(
void)
const 198 static_assert(N==3,
"N is not 3");
206 static_assert(N==3,
"N is not 3");
211 const ValueType& z(
void)
const 213 static_assert(N==3,
"N is not 3");
221 template <
class T,
size_t N>
222 std::ostream& operator<<(std::ostream& os, const Vector<T,N>& v)
225 for (
size_t i=0; i<N; ++i)
228 if (i<N-1) os <<
" ";
237 template <
class T,
size_t N>
244 template <
class T,
size_t N>
251 template <
class T,
size_t N>
258 template <
class T,
size_t N>
265 template <
class T,
size_t N>
272 template <
class T,
size_t N>
276 typedef typename VectorType::ValueType
ValueType;
280 for (
size_t i=0; i<N; ++i)
282 v.element[i] =
static_cast<ValueType
>((1.0-t) * from.
element[i] + t * to.
element[i]);
290 #endif // SPIDAR_VECTOR_HPP
static const size_t size
要素数
ValueType element[N]
ベクトルの要素