KWStyle - itkVector.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkVector.txx.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 DEF #ifndef _itkVector_txx
18 DEF #define _itkVector_txx
19
20 #include "itkVector.h" 
21 #include "itkNumericTraits.h" 
22 #include <vnl/vnl_math.h>
23 #include "vnl/vnl_vector.h"
24 #include "itkObject.h"
25 #include "itkNumericTraitsVectorPixel.h"
26
27 namespace itk
28 {
29
30
31 /**
32  * Constructor to initialize entire vector to one value.
33  */
34 template<class T, unsigned int TVectorDimension>
35 Vector<T, TVectorDimension>
36 ::Vector(const ValueType& r)
37 {
38 LEN   for(typename BaseArray::Iterator i = BaseArray::Begin(); i != BaseArray::End(); ++i)
39     {
40     *i = r;
41     }
42 }
43
44
45 template<class T, unsigned int TVectorDimension>
46 Vector<T, TVectorDimension>&
47 Vector<T, TVectorDimension>
48 ::operator= (const ValueType r[TVectorDimension])
49 {
50   BaseArray::operator=(r);
51   return *this;
52 }
53
54
55 /**
56  *
57  */
58 template<class T, unsigned int TVectorDimension>
59 const Vector<T, TVectorDimension> &
60 Vector<T, TVectorDimension>
61 ::operator*=( const ValueType & value )
62 {
63   for( unsigned int i=0; i<TVectorDimension; i++) 
64     {
65     (*this)[i] *= value;
66     }
67   return *this;
68 }
69
70   
71 /**
72  *
73  */
74 template<class T, unsigned int TVectorDimension>
75 const Vector<T, TVectorDimension> &
76 Vector<T, TVectorDimension>
77 ::operator/=( const ValueType & value )
78 {
79   for( unsigned int i=0; i<TVectorDimension; i++) 
80     {
81     (*this)[i] /= value;
82     }
83   return *this;
84 }
85
86
87 /**
88  *
89  */
90 template<class T, unsigned int TVectorDimension>
91 const typename Vector<T, TVectorDimension>::Self &
92 Vector<T, TVectorDimension>
93 ::operator+=( const Self & vec )
94 {
95   for( unsigned int i=0; i<TVectorDimension; i++) 
96     {
97     (*this)[i] += vec[i];
98     }
99   return *this;
100 }
101
102  
103 /**
104  *
105  */
106 template<class T, unsigned int TVectorDimension>
107 const typename Vector<T, TVectorDimension>::Self &
108 Vector<T, TVectorDimension>
109 ::operator-=( const Self & vec )
110 {
111   for( unsigned int i=0; i<TVectorDimension; i++) 
112     {
113     (*this)[i] -= vec[i];
114     }
115   return *this;
116 }
117
118  
119 /**
120  * Returns a temporary copy of a vector
121  */
122 template<class T, unsigned int TVectorDimension>
123 Vector<T, TVectorDimension> 
124 Vector<T, TVectorDimension>
125 ::operator-() const
126 {
127   Self result;
128   for( unsigned int i=0; i<TVectorDimension; i++) 
129     {
130     result[i] = -(*this)[i];
131     }
132   return result;
133 }
134
135
136 EML
137 /**
138  * Returns a temporary copy of a vector
139  */
140 template<class T, unsigned int TVectorDimension>
141 Vector<T, TVectorDimension> 
142 Vector<T, TVectorDimension>
143 ::operator+( const Self & vec ) const
144 {
145   Self result;
146   for( unsigned int i=0; i<TVectorDimension; i++) 
147     {
148     result[i] = (*this)[i] + vec[i];
149     }
150   return result;
151 }
152
153
154 EML
155 /**
156  * Returns a temporary copy of a vector
157  */
158 template<class T, unsigned int TVectorDimension>
159 Vector<T, TVectorDimension> 
160 Vector<T, TVectorDimension>
161 ::operator-( const Self & vec )  const
162 {
163   Self result;
164   for( unsigned int i=0; i<TVectorDimension; i++) 
165     {
166     result[i] = (*this)[i] - vec[i];
167     }
168   return result;
169 }
170
171
172 EML
173 /**
174  * Multiply components by a constant
175  */
176 template<class T, unsigned int TVectorDimension>
177 Vector<T, TVectorDimension> 
178 Vector<T, TVectorDimension>
179 ::operator*( const ValueType & value ) const
180 {
181   Self result;
182   for( unsigned int i=0; i<TVectorDimension; i++) 
183     {
184     result[i] = (*this)[i] * value;
185     }
186   return result;
187 }
188
189  
190 /**
191  * Returns vector's Squared Euclidean Norm
192  */
193 template<class T, unsigned int TVectorDimension>
194 typename Vector<T, TVectorDimension>::RealValueType
195 Vector<T, TVectorDimension>
196 ::GetSquaredNorm( void ) const
197 {
198 LEN   typename NumericTraits<RealValueType>::AccumulateType sum = NumericTraits<T>::Zero;
199   for( unsigned int i=0; i<TVectorDimension; i++) 
200     {
201     const RealValueType value = (*this)[i];
202     sum += value * value;
203     }
204   return sum;
205 }
206
207
208 EML
209 /**
210  * Returns vector's Euclidean Norm
211  */
212 template<class T, unsigned int TVectorDimension>
213 typename Vector<T, TVectorDimension>::RealValueType
214 Vector<T, TVectorDimension>
215 ::GetNorm( void ) const
216 {
217   return RealValueType( sqrt( double(this->GetSquaredNorm()) )); 
218 }
219
220
221 /**
222  * Divide vector's components by vector's norm
223  */
224 template<class T, unsigned int TVectorDimension>
225 void
226 Vector<T, TVectorDimension>
227 ::Normalize( void ) 
228 {
229   const RealValueType norm = this->GetNorm();
230   for( unsigned int i=0; i<TVectorDimension; i++) 
231     {
232     (*this)[i] /= norm;
233     }
234 }
235
236
237 EML
238 EML
239 /**
240  * Returns a temporary copy of a vector
241  */
242 template<class T, unsigned int TVectorDimension>
243 Vector<T, TVectorDimension> 
244 Vector<T, TVectorDimension>
245 ::operator/( const ValueType & value ) const
246 {
247   Self result;
248   for( unsigned int i=0; i<TVectorDimension; i++) 
249     {
250     result[i] = (*this)[i] / value;
251     }
252   return result;
253 }
254
255
256   
257 /**
258  * Set a vnl_vector
259  */
260 template<class T, unsigned int TVectorDimension>
261 void
262 Vector<T, TVectorDimension>
263 ::Set_vnl_vector( const vnl_vector<T> & v)
264 {
265   for(unsigned int i=0;i<v.size();i++) 
266     {
267     (*this)[i] = v(i);
268     } 
269 }
270  
271
272 /**
273  * Return a vnl_vector_ref
274  */
275 template<class T, unsigned int TVectorDimension>
276 vnl_vector_ref< T >
277 Vector<T, TVectorDimension>
278 ::Get_vnl_vector( void ) 
279 {
280   return vnl_vector_ref< T >( TVectorDimension, this->GetDataPointer() );
281 }
282  
283
284 /**
285  * Return a vnl_vector const
286  */
287 template<class T, unsigned int TVectorDimension>
288 vnl_vector< T >
289 Vector<T, TVectorDimension>
290 ::Get_vnl_vector( void ) const 
291 {
292   // Return a vector_ref<>.  This will be automatically converted to a
293   // vnl_vector<>.  We have to use a const_cast<> which would normally
294   // be prohibited in a const method, but it is safe to do here
295   // because the cast to vnl_vector<> will ultimately copy the data.
296   return vnl_vector_ref<T>( TVectorDimension,
297                             const_cast<T*>(this->GetDataPointer()));
298 }
299  
300 /**
301  * Set a vnl_vector
302  */
303 template<class T, unsigned int TVectorDimension>
304 void
305 Vector<T, TVectorDimension>
306 ::SetVnlVector( const vnl_vector<T> & v)
307 {
308   for(unsigned int i=0;i<v.size();i++) 
309     {
310     (*this)[i] = v(i);
311     } 
312 }
313  
314
315 /**
316  * Return a vnl_vector_ref
317  */
318 template<class T, unsigned int TVectorDimension>
319 vnl_vector_ref< T >
320 Vector<T, TVectorDimension>
321 ::GetVnlVector( void ) 
322 {
323   return vnl_vector_ref< T >( TVectorDimension, this->GetDataPointer() );
324 }
325  
326
327 /**
328  * Return a vnl_vector const
329  */
330 template<class T, unsigned int TVectorDimension>
331 vnl_vector< T >
332 Vector<T, TVectorDimension>
333 ::GetVnlVector( void ) const 
334 {
335   // Return a vector_ref<>.  This will be automatically converted to a
336   // vnl_vector<>.  We have to use a const_cast<> which would normally
337   // be prohibited in a const method, but it is safe to do here
338   // because the cast to vnl_vector<> will ultimately copy the data.
339   return vnl_vector_ref<T>( TVectorDimension,
340                             const_cast<T*>(this->GetDataPointer()));
341 }
342
343 /**
344  * Print content to an ostream
345  */
346 template<class T, unsigned int TVectorDimension>
347 std::ostream &
348 operator<<(std::ostream& os,const Vector<T,TVectorDimension> & vct ) 
349 {
350   os << "[";
351   if ( TVectorDimension == 1)
352     {
353     os << vct[0];
354     }
355   else
356     {
357     for( unsigned int i=0; i+1<TVectorDimension; i++)
358       {
359       os <<  vct[i] << ", ";
360       }
361     os << vct[TVectorDimension-1];
362     }
363   os << "]";
364   return os;
365 }
366
367
368 /**
369  * Read content from an istream
370  */
371 template<class T, unsigned int TVectorDimension>
372 std::istream &
373 operator>>(std::istream& is, Vector<T,TVectorDimension> & vct ) 
374 {
375   for( unsigned int i=0; i<TVectorDimension; i++)
376     {
377     is >>  vct[i];
378     }
379   return is;
380 }
381
382
383 /**
384  *
385  */
386 template<class T, unsigned int TVectorDimension>
387 typename Vector<T, TVectorDimension>::ValueType
388 Vector<T, TVectorDimension>
389 ::operator*( const Self & other ) const
390 {
391   typename NumericTraits<T>::AccumulateType value = NumericTraits<T>::Zero;
392   for( unsigned int i=0; i<TVectorDimension; i++) 
393     {
394     value += (*this)[i] * other[i];
395     }
396   return value;
397 }
398
399
400 EML
401 EML
402 // end namespace itk
403
404
405 #endif
406

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:52PM
© Kitware Inc.