00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkVector_h
00018 #define __itkVector_h
00019
00020 #include "itkFixedArray.h"
00021
00022 #include "itkNumericTraits.h"
00023 #include <vnl/vnl_vector_ref.h>
00024
00025
00026 namespace itk
00027 {
00028
00057 template<class T, unsigned int NVectorDimension=3>
00058 class Vector : public FixedArray<T,NVectorDimension>
00059 {
00060 public:
00062 typedef Vector Self;
00063 typedef FixedArray<T,NVectorDimension> Superclass;
00064
00067 typedef T ValueType;
00068 typedef typename NumericTraits< ValueType >::RealType RealValueType;
00069
00071 itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
00072
00074 typedef Self VectorType;
00075
00077 typedef T ComponentType;
00078
00080 typedef FixedArray<T, NVectorDimension> BaseArray;
00081
00083 static unsigned int GetVectorDimension()
00084 { return NVectorDimension; }
00085
00087 void SetVnlVector( const vnl_vector<T> & );
00088
00090 vnl_vector_ref<T> GetVnlVector( void );
00091
00093 vnl_vector<T> GetVnlVector( void ) const;
00094
00095
00098 void Set_vnl_vector( const vnl_vector<T> & );
00099
00102 vnl_vector_ref<T> Get_vnl_vector( void );
00103
00106 vnl_vector<T> Get_vnl_vector( void ) const;
00107
00109 Vector(): BaseArray() { }
00110 Vector(const ValueType& r);
00111
00113 template< class TVectorValueType >
00114 Vector(const Vector< TVectorValueType, NVectorDimension>& r): BaseArray(r) {}
00115 Vector(const ValueType r[Dimension]): BaseArray(r) {}
00117
00119 template< class TVectorValueType >
00120 Vector& operator= (const Vector< TVectorValueType, NVectorDimension> & r)
00121 {
00122 BaseArray::operator=(r);
00123 return *this;
00124 }
00126
00127 Vector& operator= (const ValueType r[NVectorDimension]);
00128
00130 template< class Tt > inline const Self& operator*=(const Tt &value)
00131 {
00132 for( unsigned int i=0; i<NVectorDimension; i++)
00133 {
00134 (*this)[i] = static_cast< ValueType >((*this)[i] * value);
00135 }
00136 return *this;
00137 }
00139
00141 template< class Tt > inline const Self& operator/=(const Tt &value)
00142 {
00143 for( unsigned int i=0; i<NVectorDimension; i++)
00144 {
00145 (*this)[i] = static_cast< ValueType >((*this)[i] / value);
00146 }
00147 return *this;
00148 }
00150
00152 const Self& operator+=(const Self &vec);
00153
00155 const Self& operator-=(const Self &vec);
00156
00159 Self operator-() const;
00160
00162 Self operator+(const Self &vec) const;
00163
00165 Self operator-(const Self &vec) const;
00166
00169 ValueType operator*(const Self &vec) const;
00170
00173 inline Self operator*(const ValueType& value) const
00174 {
00175 Self result;
00176 for( unsigned int i=0; i<NVectorDimension; i++)
00177 {
00178 result[i] = static_cast< ValueType >((*this)[i] * value);
00179 }
00180 return result;
00181 }
00183
00186 template< class Tt > inline Self operator/(const Tt& value) const
00187 {
00188 Self result;
00189 for( unsigned int i=0; i<NVectorDimension; i++)
00190 {
00191 result[i] = static_cast< ValueType >((*this)[i] / value);
00192 }
00193 return result;
00194 }
00196
00201 bool operator==(const Self& v) const
00202 { return Superclass::operator==(v); }
00203 bool operator!=(const Self& v) const
00204 { return !operator==(v); }
00206
00208 RealValueType GetNorm( void ) const;
00209
00211 RealValueType GetSquaredNorm( void ) const;
00212
00214 static unsigned int GetNumberOfComponents(){ return NVectorDimension;}
00215
00217 void Normalize(void);
00218
00219 void SetNthComponent(int c, const ComponentType& v)
00220 { this->operator[](c) = v; }
00221
00224 template < typename TCoordRepB >
00225 void CastFrom( const Vector<TCoordRepB,NVectorDimension> & pa )
00226 {
00227 for(unsigned int i=0; i<NVectorDimension; i++ )
00228 {
00229 (*this)[i] = static_cast<T>( pa[i] );
00230 }
00231 }
00232 };
00234
00237 template< class T, unsigned int NVectorDimension >
00238 inline
00239 Vector<T,NVectorDimension>
00240 operator*(const T &scalar, const Vector<T,NVectorDimension> & v)
00241 {
00242 return v * scalar;
00243 }
00244
00245 template< class T, unsigned int NVectorDimension >
00246 std::ostream& operator<<(std::ostream& os,
00247 const Vector<T,NVectorDimension> & v);
00248
00249 template< class T, unsigned int NVectorDimension >
00250 std::istream& operator>>(std::istream& is,
00251 Vector<T,NVectorDimension> & v);
00252
00253 ITKCommon_EXPORT Vector<double,3> CrossProduct( const Vector<double,3> &,
00254 const Vector<double,3> & );
00255
00256 ITKCommon_EXPORT Vector<float,3> CrossProduct( const Vector<float,3> &,
00257 const Vector<float,3> & );
00258
00259 ITKCommon_EXPORT Vector<int,3> CrossProduct( const Vector<int,3> &,
00260 const Vector<int,3> & );
00261
00262 }
00263
00264
00265
00266 #define ITK_TEMPLATE_Vector(_, EXPORT, x, y) namespace itk { \
00267 _(2(class EXPORT Vector< ITK_TEMPLATE_2 x >)) \
00268 _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00269 const Vector< ITK_TEMPLATE_2 x >&))) \
00270 _(1(EXPORT std::istream& operator>>(std::istream&, \
00271 Vector< ITK_TEMPLATE_2 x >&))) \
00272 namespace Templates { typedef Vector< ITK_TEMPLATE_2 x > Vector##y; } \
00273 }
00274
00275 #if ITK_TEMPLATE_EXPLICIT
00276 # include "Templates/itkVector+-.h"
00277 #endif
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 #include "itkNumericTraitsVectorPixel.h"
00288
00289 #if ITK_TEMPLATE_TXX
00290 # include "itkVector.txx"
00291 #endif
00292
00293
00294 #endif
00295