KWStyle - itkCovariantVector.txx
 
Matrix View
Description

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

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