6#ifndef _POINT_VECTOR_H_
7#define _POINT_VECTOR_H_
19 template <
typename T, std::
size_t N>
22 typedef std::array<T, N> Base;
23 typedef std::size_t Size;
29 using Base::operator[];
40 for (
auto v : L )
if ( i < N ) (*this)[ i++ ] = v;
70 for ( Size i = 0; i < N; i++ ) (*
this)[ i ] = *vals++;
73 operator T*() {
return data(); }
75 operator const T*()
const {
return data(); }
77 void selfDisplay( std::ostream& out )
const
80 for ( Size i = 0; i < N; i++ )
81 out << (*
this)[ i ] << ( ( i < N-1 ) ?
',' :
')' );
84 Self& operator+=(
const Self& other )
86 for ( Size i = 0; i < N; ++i ) (*
this)[ i ] += other[ i ];
89 Self& operator-=(
const Self& other )
91 for ( Size i = 0; i < N; ++i ) (*
this)[ i ] -= other[ i ];
94 Self& operator*=( T val )
96 for ( Size i = 0; i < N; ++i ) (*
this)[ i ] *= val;
99 Self& operator/=( T val )
101 for ( Size i = 0; i < N; ++i ) (*
this)[ i ] /= val;
109 for ( Size i = 0; i < N; ++i ) result += (*
this)[ i ] * other[ i ];
116 return Self( (*
this)[1]*other[2] - (*
this)[2]*other[1],
117 (*
this)[2]*other[0] - (*
this)[0]*other[2],
118 (*
this)[0]*other[1] - (*
this)[1]*other[0] );
121 Self operator+(
const Self& other )
const
123 Self result( *
this );
128 Self operator-(
const Self& other )
const
130 Self result( *
this );
137 return sqrt(
dot( *
this ) );
145 template <
typename T, std::
size_t N>
146 std::ostream& operator<<( std::ostream& out,
const PointVector<T,N>& PV )
148 PV.selfDisplay( out );
152 template <
typename T, std::
size_t N>
153 PointVector<T,N> operator*( T val,
const PointVector<T,N>& PV )
155 typedef typename PointVector<T,N>::Size Size;
156 PointVector<T,N> result( PV );
157 for ( Size i = 0; i < N; ++i ) result[ i ] *= val;
161 template <
typename T, std::
size_t N>
162 PointVector<T,N> operator*(
const PointVector<T,N>& PV, T val )
164 typedef typename PointVector<T,N>::Size Size;
165 PointVector<T,N> result( PV );
166 for ( Size i = 0; i < N; ++i ) result[ i ] *= val;
170 template <
typename T, std::
size_t N>
171 PointVector<T,N> operator/( T val,
const PointVector<T,N>& PV )
173 typedef typename PointVector<T,N>::Size Size;
174 PointVector<T,N> result( PV );
175 for ( Size i = 0; i < N; ++i ) result[ i ] = val / result[ i ];
179 template <
typename T, std::
size_t N>
180 PointVector<T,N> operator/(
const PointVector<T,N>& PV, T val )
182 typedef typename PointVector<T,N>::Size Size;
183 PointVector<T,N> result( PV );
184 for ( Size i = 0; i < N; ++i ) result[ i ] /= val;
188 template <
typename T, std::
size_t N>
190 T distance2(
const PointVector<T,N>& p1,
const PointVector<T,N>& p2 )
192 PointVector<T,N> tmp = p2 - p1;
193 return tmp.dot( tmp );
196 template <
typename T, std::
size_t N>
198 T distance(
const PointVector<T,N>& p1,
const PointVector<T,N>& p2 )
200 return sqrt( distance2( p1, p2 ) );
float Real
the type for representing a real number.
PointVector< Real, 3 > Point3
The type for representing a 3d vector;.
PointVector< Real, 3 > Vector3
The type for representing a 3d point.
T dot(const Self &other) const
dot product (produit scalaire).
Self cross(const Self &other) const
cross product (produit vectoriel).