KWStyle - itkSymmetricSecondRankTensor.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkSymmetricSecondRankTensor.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:48 $
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 _itkSymmetricSecondRankTensor_txx
18 DEF #define _itkSymmetricSecondRankTensor_txx
19
20 #include "itkSymmetricSecondRankTensor.h"
21 #include "itkNumericTraitsTensorPixel.h"
22
23 namespace itk
24 {
25
26 /*
27 IND ** Assignment Operator
28 IND **/
29 template<class T,unsigned int NDimension>
30 SymmetricSecondRankTensor<T,NDimension>&
31 SymmetricSecondRankTensor<T,NDimension>
32 ::operator= (const Self& r)
33 {
34   BaseArray::operator=(r);
35   return *this;
36 }
37
38
39 /*
40 IND ** Assignment Operator from a scalar constant
41 IND **/
42 template<class T,unsigned int NDimension>
43 SymmetricSecondRankTensor<T,NDimension>&
44 SymmetricSecondRankTensor<T,NDimension>
45 ::operator= (const ComponentType & r)
46 {
47   BaseArray::operator=(r);
48   return *this;
49 }
50
51
52 /*
53 IND ** Assigment from a plain array
54 IND **/
55 template<class T,unsigned int NDimension>
56 SymmetricSecondRankTensor<T,NDimension>&
57 SymmetricSecondRankTensor<T,NDimension>
58 ::operator= (const ComponentArrayType r )
59 {
60   BaseArray::operator=(r);
61   return *this;
62 }
63
64
65   
66 /**
67  * Returns a temporary copy of a vector
68  */
69 template<class T,unsigned int NDimension>
70 SymmetricSecondRankTensor<T,NDimension> 
71 SymmetricSecondRankTensor<T,NDimension>
72 ::operator+(const Self & r) const
73 {
74   Self result;
75   for( unsigned int i=0; i<InternalDimension; i++) 
76     {
77     result[i] = (*this)[i] + r[i];
78     }
79   return result;
80 }
81
82
83 EML
84 EML
85 /**
86  * Returns a temporary copy of a vector
87  */
88 template<class T,unsigned int NDimension>
89 SymmetricSecondRankTensor<T,NDimension> 
90 SymmetricSecondRankTensor<T,NDimension>
91 ::operator-(const Self & r) const
92 {
93   Self result;
94   for( unsigned int i=0; i<InternalDimension; i++) 
95     {
96     result[i] = (*this)[i] - r[i];
97     }
98   return result;
99 }
100
101
102  
103 /**
104  * Performs addition in place
105  */
106 template<class T,unsigned int NDimension>
107 const SymmetricSecondRankTensor<T,NDimension> & 
108 SymmetricSecondRankTensor<T,NDimension>
109 ::operator+=(const Self & r) 
110 {
111   for( unsigned int i=0; i<InternalDimension; i++) 
112     {
113     (*this)[i] += r[i];
114     }
115   return *this;
116 }
117
118
119 EML
120  
121 /**
122  * Performs subtraction in place 
123  */
124 template<class T,unsigned int NDimension>
125 const SymmetricSecondRankTensor<T,NDimension> & 
126 SymmetricSecondRankTensor<T,NDimension>
127 ::operator-=(const Self & r)
128 {
129   for( unsigned int i=0; i<InternalDimension; i++) 
130     {
131     (*this)[i] -= r[i];
132     }
133   return *this;
134 }
135
136
137  
138 /**
139  * Performs multiplication by a scalar, in place
140  */
141 template<class T,unsigned int NDimension>
142 const SymmetricSecondRankTensor<T,NDimension> & 
143 SymmetricSecondRankTensor<T,NDimension>
144 ::operator*=(const RealValueType & r) 
145 {
146   for( unsigned int i=0; i<InternalDimension; i++) 
147     {
148     (*this)[i] *= r;
149     }
150   return *this;
151 }
152
153
154  
155 /**
156  * Performs division by a scalar, in place
157  */
158 template<class T,unsigned int NDimension>
159 const SymmetricSecondRankTensor<T,NDimension> & 
160 SymmetricSecondRankTensor<T,NDimension>
161 ::operator/=(const RealValueType & r) 
162 {
163   for( unsigned int i=0; i<InternalDimension; i++) 
164     {
165     (*this)[i] /= r;
166     }
167   return *this;
168 }
169
170
171 EML
172 EML
173 /**
174  * Performs multiplication with a scalar
175  */
176 template<class T,unsigned int NDimension>
177 SymmetricSecondRankTensor<T,NDimension> 
178 SymmetricSecondRankTensor<T,NDimension>
179 ::operator*(const RealValueType & r) const
180 {
181   Self result;
182   for( unsigned int i=0; i<InternalDimension; i++) 
183     {
184     result[i] = (*this)[i] * r;
185     }
186   return result;
187 }
188
189
190 /**
191  * Performs division by a scalar
192  */
193 template<class T,unsigned int NDimension>
194 SymmetricSecondRankTensor<T,NDimension> 
195 SymmetricSecondRankTensor<T,NDimension>
196 ::operator/(const RealValueType & r) const
197 {
198   Self result;
199   for( unsigned int i=0; i<InternalDimension; i++) 
200     {
201     result[i] = (*this)[i] / r;
202     }
203   return result;
204 }
205
206
207 /*
208  * Matrix notation access to elements
209  */
210 template<class T,unsigned int NDimension>
211 const typename SymmetricSecondRankTensor<T,NDimension>::ValueType &
212 SymmetricSecondRankTensor<T,NDimension>
213 ::operator()(unsigned int row, unsigned int col) const
214 {
215   unsigned int k; 
216
217   if( row < col ) 
218     {
219     k = row * Dimension + col - row * ( row + 1 ) / 2; 
220     }
221   else
222     {
223     k = col * Dimension + row - col * ( col + 1 ) / 2; 
224     }
225   
226
227   if( k >= InternalDimension )
228     {
229     k = 0;
230     }
231
232   return (*this)[k];
233 }
234
235
236 EML
237 /*
238 IND ** Matrix notation access to elements
239 IND **/
240 template<class T,unsigned int NDimension>
241 typename SymmetricSecondRankTensor<T,NDimension>::ValueType &
242 SymmetricSecondRankTensor<T,NDimension>
243 ::operator()(unsigned int row, unsigned int col)
244 {
245   unsigned int k; 
246
247   if( row < col ) 
248     {
249     k = row * Dimension + col - row * ( row + 1 ) / 2; 
250     }
251   else
252     {
253     k = col * Dimension + row - col * ( col + 1 ) / 2; 
254     }
255   
256
257   if( k >= InternalDimension )
258     {
259     k = 0;
260     }
261
262   return (*this)[k];
263 }
264
265
266 /*
267 IND ** Set the Tensor to an Identity.
268 IND ** Set ones in the diagonal and zeroes every where else.
269 IND **/
270 template<class T,unsigned int NDimension>
271 void 
272 SymmetricSecondRankTensor<T,NDimension>
273 ::SetIdentity() 
274 {
275   this->Fill(NumericTraits< T >::Zero);
276   for( unsigned int i=0; i < Dimension; i++)
277     {
278     (*this)(i,i) = NumericTraits< T >::One;
279     }
280 }
281
282
283 /*
284 IND ** Get the Trace
285 IND **/
286 template<class T,unsigned int NDimension>
287 typename SymmetricSecondRankTensor<T,NDimension>::AccumulateValueType
288 SymmetricSecondRankTensor<T,NDimension>
289 ::GetTrace() const
290 {
291   AccumulateValueType trace = NumericTraits< AccumulateValueType >::Zero;
292   unsigned int k = 0;
293   for(unsigned int i=0; i<Dimension; i++)
294     {
295     trace += (*this)[k];
296     k += (Dimension-i);
297     }
298   return trace;
299 }
300
301
302 /*
303  * Compute Eigen Values 
304  */
305 template<class T,unsigned int NDimension>
306 void
307 SymmetricSecondRankTensor<T,NDimension>
308 ::ComputeEigenValues( EigenValuesArrayType & eigenValues ) const 
309 {
310   
311 LEN   SymmetricEigenAnalysisType symmetricEigenSystem = SymmetricEigenAnalysisType( Dimension );
312
313   MatrixType tensorMatrix;
314   for( unsigned int row=0; row < Dimension; row++ )
315     {
316     for( unsigned int col=0; col < Dimension; col++ )
317       {
318       tensorMatrix[row][col] = (*this)(row,col);
319       }
320     }
321   
322   symmetricEigenSystem.ComputeEigenValues( tensorMatrix, eigenValues );
323   
324 }
325
326 /*
327  * Compute Eigen analysis, it returns an array with eigen values
328  * and a Matrix with eigen vectors
329  */
330 template<class T,unsigned int NDimension>
331 void
332 SymmetricSecondRankTensor<T,NDimension>
333 ::ComputeEigenAnalysis( EigenValuesArrayType & eigenValues,
334                         EigenVectorsMatrixType & eigenVectors ) const
335 {
336 LEN   SymmetricEigenAnalysisType symmetricEigenSystem = SymmetricEigenAnalysisType( Dimension );
337
338   MatrixType tensorMatrix;
339   for( unsigned int row=0; row < Dimension; row++ )
340     {
341     for( unsigned int col=0; col < Dimension; col++ )
342       {
343       tensorMatrix[row][col] = (*this)(row,col);
344       }
345     }
346   
347   symmetricEigenSystem.ComputeEigenValuesAndVectors( 
348                       tensorMatrix, eigenValues, eigenVectors );
349
350 }
351
352
353 /**
354  * Print content to an ostream
355  */
356 template<class T,unsigned int NDimension>
357 std::ostream &
358 operator<<(std::ostream& os,const SymmetricSecondRankTensor<T,NDimension> & c ) 
359 {
360   for(unsigned int i=0; i<c.GetNumberOfComponents(); i++)
361     {
362     os <<  static_cast<typename NumericTraits<T>::PrintType>(c[i]) << "  ";
363     }
364   return os;
365 }
366
367
368 /**
369  * Read content from an istream
370  */
371 template<class T,unsigned int NDimension>
372 std::istream &
373 operator>>(std::istream& is, SymmetricSecondRankTensor<T,NDimension> & dt ) 
374 {
375   for(unsigned int i=0; i < dt.GetNumberOfComponents(); i++)
376     {
377     is >> dt[i];
378     }
379   return is;
380 }
381
382 // end namespace itk
383
384 #endif
385

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