| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkVector.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:49 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkVector_h |
| 18 |
|
#define __itkVector_h |
| 19 |
|
|
| 20 |
|
#include "itkFixedArray.h" |
| 21 |
|
#include "itkNumericTraits.h" |
| 22 |
|
#include "vnl/vnl_vector_ref.h" |
| 23 |
|
#include "itkIndent.h" |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
namespace itk |
| 27 |
|
{ |
| 28 |
|
|
| 29 |
|
/** \class Vector |
| 30 |
|
* \brief A templated class holding a n-Dimensional vector. |
| 31 |
|
* |
| 32 |
|
* Vector is a templated class that holds a single vector (i.e., an array |
| 33 |
|
* of values). Vector can be used as the data type held at each pixel in |
| 34 |
|
* an Image or at each vertex of an Mesh. The template parameter T can |
| 35 |
|
* be any data type that behaves like a primitive (or atomic) data type (int, |
| 36 |
|
* short, float, complex). The NVectorDimension defines the number of |
| 37 |
|
* components in the vector array. |
| 38 |
|
* |
| 39 |
|
* Vector is not a dynamically extendible array like std::vector. It is |
| 40 |
|
* intended to be used like a mathematical vector. |
| 41 |
|
* |
| 42 |
|
* If you wish a simpler pixel types, you can use Scalar, which represents |
| 43 |
|
* a single data value at a pixel. There is also the more complex type |
| 44 |
|
* ScalarVector, which supports (for a given pixel) a single scalar value |
| 45 |
|
* plus an array of vector values. (The scalar and vectors can be of |
| 46 |
|
* different data type.) |
| 47 |
|
* |
| 48 |
|
* \ingroup Geometry |
| 49 |
|
* \ingroup DataRepresentation |
| 50 |
|
* |
| 51 |
|
* \sa Image |
| 52 |
|
* \sa Mesh |
| 53 |
|
* \sa Point |
| 54 |
|
* \sa CovariantVector |
| 55 |
|
* \sa Matrix |
| 56 |
|
*/ |
| 57 |
|
template<class T, unsigned int NVectorDimension=3> |
| 58 |
|
class Vector : public FixedArray<T,NVectorDimension> |
| 59 |
|
{ |
| 60 |
|
public: |
| 61 |
|
/** Standard class typedefs. */ |
| 62 |
|
typedef Vector Self; |
| 63 |
TDA |
typedef FixedArray<T,NVectorDimension> Superclass; |
| 64 |
|
|
| 65 |
|
/** ValueType can be used to declare a variable that is the same type |
| 66 |
|
* as a data element held in an Vector. */ |
| 67 |
|
typedef T ValueType; |
| 68 |
TDA |
typedef typename NumericTraits< ValueType >::RealType RealValueType; |
| 69 |
|
|
| 70 |
|
/** Dimension of the vector space. */ |
| 71 |
|
itkStaticConstMacro(Dimension, unsigned int, NVectorDimension); |
| 72 |
|
|
| 73 |
|
/** I am a vector type. */ |
| 74 |
|
typedef Self VectorType; |
| 75 |
|
|
| 76 |
|
/** Component value type */ |
| 77 |
|
typedef T ComponentType; |
| 78 |
|
|
| 79 |
|
/** The Array type from which this vector is derived. */ |
| 80 |
|
typedef FixedArray<T, NVectorDimension> BaseArray; |
| 81 |
|
|
| 82 |
|
/** Get the dimension (size) of the vector. */ |
| 83 |
|
static unsigned int GetVectorDimension() |
| 84 |
|
{ return NVectorDimension; } |
| 85 |
|
|
| 86 |
|
/** Set a vnl_vector_ref referencing the same memory block. */ |
| 87 |
|
void SetVnlVector( const vnl_vector<T> & ); |
| 88 |
|
|
| 89 |
|
/** Get a vnl_vector_ref referencing the same memory block. */ |
| 90 |
|
vnl_vector_ref<T> GetVnlVector( void ); |
| 91 |
|
|
| 92 |
|
/** Get a vnl_vector with a copy of the internal memory block. */ |
| 93 |
|
vnl_vector<T> GetVnlVector( void ) const; |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
/** Set a vnl_vector_ref referencing the same memory block. |
| 97 |
|
* \deprecated Use SetVnlVector() instead. */ |
| 98 |
|
void Set_vnl_vector( const vnl_vector<T> & ); |
| 99 |
|
|
| 100 |
|
/** Get a vnl_vector_ref referencing the same memory block. |
| 101 |
|
* \deprecated Use GetVnlVector() instead. */ |
| 102 |
|
vnl_vector_ref<T> Get_vnl_vector( void ); |
| 103 |
|
|
| 104 |
|
/** Get a vnl_vector with a copy of the internal memory block. |
| 105 |
|
* \deprecated Use GetVnlVector() instead. */ |
| 106 |
|
vnl_vector<T> Get_vnl_vector( void ) const; |
| 107 |
|
|
| 108 |
|
/** Default constructor and copy constructors. */ |
| 109 |
|
Vector(): BaseArray() { } |
| 110 |
|
Vector(const ValueType& r); |
| 111 |
|
|
| 112 |
|
/** Pass-through constructor for the Array base class. */ |
| 113 |
|
template< class TVectorValueType > |
| 114 |
|
Vector(const Vector< TVectorValueType, NVectorDimension>& r): BaseArray(r) {} |
| 115 |
|
Vector(const ValueType r[Dimension]): BaseArray(r) {} |
| 116 |
|
|
| 117 |
|
/** Pass-through assignment operator for the Array base class. */ |
| 118 |
|
template< class TVectorValueType > |
| 119 |
|
Vector& operator= (const Vector< TVectorValueType, NVectorDimension> & r) |
| 120 |
|
{ |
| 121 |
|
BaseArray::operator=(r); |
| 122 |
|
return *this; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
Vector& operator= (const ValueType r[NVectorDimension]); |
| 126 |
|
|
| 127 |
|
/** Scalar operator*=. Scales elements by a scalar. */ |
| 128 |
|
const Self& operator*=(const ValueType &value); |
| 129 |
|
|
| 130 |
|
/** Scalar operator/=. Scales (divides) elements by a scalar. */ |
| 131 |
|
const Self& operator/=(const ValueType &value); |
| 132 |
|
|
| 133 |
|
/** Vector operator+=. Adds a vectors to the current vector. */ |
| 134 |
|
const Self& operator+=(const Self &vec); |
| 135 |
|
|
| 136 |
|
/** Vector operator-=. Subtracts a vector from a current vector. */ |
| 137 |
|
const Self& operator-=(const Self &vec); |
| 138 |
|
|
| 139 |
|
/** Vector negation. Negate all the elements of a vector. Return a new |
| 140 |
|
* vector */ |
| 141 |
|
Self operator-() const; |
| 142 |
|
|
| 143 |
|
/** Vector addition. Add two vectors. Return a new vector. */ |
| 144 |
|
Self operator+(const Self &vec) const; |
| 145 |
|
|
| 146 |
|
/** Vector subtraction. Subtract two vectors. Return a new vector. */ |
| 147 |
|
Self operator-(const Self &vec) const; |
| 148 |
|
|
| 149 |
|
/** Vector operator*. Performs the inner product of two vectors. |
| 150 |
|
* this is also known as the scalar product. */ |
| 151 |
|
ValueType operator*(const Self &vec) const; |
| 152 |
|
|
| 153 |
|
/** Scalar operator*. Scale the elements of a vector by a scalar. |
| 154 |
|
* Return a new vector. */ |
| 155 |
|
Self operator*(const ValueType& val) const; |
| 156 |
|
|
| 157 |
|
/** Scalar operator/. Scale (divide) the elements of a vector by a scalar. |
| 158 |
|
* Return a new vector. */ |
| 159 |
|
Self operator/(const ValueType& val) const; |
| 160 |
|
|
| 161 |
|
/** Operators == and != compare a vector component by component. All |
| 162 |
|
* components must be equal for two vectors to be equal. (Of course |
| 163 |
|
* compile-time constraints on the template parameters length and type |
| 164 |
|
* prevent comparisons between vectors of different type and length.) */ |
| 165 |
|
bool operator==(const Self& v) const |
| 166 |
|
{ return Superclass::operator==(v); } |
| 167 |
|
bool operator!=(const Self& v) const |
| 168 |
|
{ return !operator==(v); } |
| 169 |
|
|
| 170 |
|
/** Returns the Euclidean Norm of the vector */ |
| 171 |
|
RealValueType GetNorm( void ) const; |
| 172 |
|
|
| 173 |
|
/** Returns vector's Squared Euclidean Norm */ |
| 174 |
|
RealValueType GetSquaredNorm( void ) const; |
| 175 |
|
|
| 176 |
|
/** Returns the number of components in this vector type */ |
| 177 |
|
static unsigned int GetNumberOfComponents(){ return NVectorDimension;} |
| 178 |
|
|
| 179 |
|
/** Divides the vector componets by the vector norm */ |
| 180 |
|
void Normalize(void); |
| 181 |
|
|
| 182 |
|
void SetNthComponent(int c, const ComponentType& v) |
| 183 |
|
{ this->operator[](c) = v; } |
| 184 |
|
|
| 185 |
|
/** Copy from another Vector with a different representation type. |
| 186 |
|
* Casting is done with C-Like rules */ |
| 187 |
|
template < typename TCoordRepB > |
| 188 |
|
void CastFrom( const Vector<TCoordRepB,NVectorDimension> & pa ) |
| 189 |
IND |
**{ |
| 190 |
|
for(unsigned int i=0; i<NVectorDimension; i++ ) |
| 191 |
|
{ |
| 192 |
|
(*this)[i] = static_cast<T>( pa[i] ); |
| 193 |
|
} |
| 194 |
IND |
**} |
| 195 |
|
|
| 196 |
|
}; |
| 197 |
|
|
| 198 |
|
template< class T, unsigned int NVectorDimension > |
| 199 |
|
std::ostream& operator<<(std::ostream& os, |
| 200 |
|
const Vector<T,NVectorDimension> & v); |
| 201 |
|
|
| 202 |
|
template< class T, unsigned int NVectorDimension > |
| 203 |
|
std::istream& operator>>(std::istream& is, |
| 204 |
|
Vector<T,NVectorDimension> & v); |
| 205 |
|
|
| 206 |
|
ITKCommon_EXPORT Vector<double,3> CrossProduct( const Vector<double,3> &, |
| 207 |
|
const Vector<double,3> & ); |
| 208 |
|
|
| 209 |
|
ITKCommon_EXPORT Vector<float,3> CrossProduct( const Vector<float,3> &, |
| 210 |
|
const Vector<float,3> & ); |
| 211 |
|
|
| 212 |
|
ITKCommon_EXPORT Vector<int,3> CrossProduct( const Vector<int,3> &, |
| 213 |
|
const Vector<int,3> & ); |
| 214 |
|
|
| 215 |
|
} // end namespace itk |
| 216 |
|
|
| 217 |
|
|
| 218 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 219 |
|
#include "itkVector.txx" |
| 220 |
|
#endif |
| 221 |
|
|
| 222 |
|
|
| 223 |
|
#endif |
| 224 |
|
|