File: itkSymmetricSecondRankTensorTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (505 lines) | stat: -rw-r--r-- 14,937 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include <iostream>

#include "itkMath.h"
#include "itkSymmetricSecondRankTensor.h"
#include "itkImageRegionIterator.h"

int itkSymmetricSecondRankTensorTest(int, char* [] )
{
  // Test it all

  float val[6] = {1.8f, 0.2f, 0.5f, 3.4f, 2.0f, 1.2f};

  typedef itk::SymmetricSecondRankTensor<float,3>         Float3DTensorType;
  typedef itk::SymmetricSecondRankTensor<unsigned char,3> Uchar3DTensorType;

  Float3DTensorType pixel(val);

  unsigned char pixelInit0[6] = {255, 255, 255,128,34,17};
  unsigned char pixelInit1[6] = {255, 255, 244,19,23,29};

  Uchar3DTensorType pixelArray[2];
  pixelArray[0] = pixelInit0;
  pixelArray[1] = pixelInit1;

  std::cout << "sizeof(pixel) = " << sizeof (pixel) << std::endl;
  if (sizeof(pixel) != 6 * sizeof(Float3DTensorType::ComponentType))
    {
    std::cerr << "ERROR: sizeof(pixel) == " << sizeof(pixel) << " but is should be " << 6 * sizeof(Float3DTensorType::ComponentType) << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "pixel.GetNumberOfComponents = " << pixel.GetNumberOfComponents() << std::endl;
  std::cout << "pixel.GetNthComponent()" << std::endl;
  for (unsigned int i = 0; i < pixel.GetNumberOfComponents(); i++)
    {
    std::cout << "\tpixel[" << i << "] = " << pixel.GetNthComponent(i) << std::endl;
    }

  pixel(0,0) = 11.0;
  pixel(0,1) = 21.0;
  pixel(0,2) = 15.0;
  pixel(1,0) = 11.0;
  pixel(1,1) = 31.0;
  pixel(1,2) = 10.0;
  pixel(2,0) = 11.0; // these three last element will overwrite its symmetric counterparts
  pixel(2,1) = 41.0;
  pixel(2,2) = 14.0;

  std::cout << "testing the pixel(i,j) APID" << std::endl;
  for (unsigned int i = 0; i < pixel.GetNumberOfComponents(); i++)
    {
    std::cout << "\tpixel[" << i << "] = " << pixel.GetNthComponent(i) << std::endl;
    }

  std::cout << "pixel[0] = 111; pixel[1] = 222; pixel[2] = 333;" << std::endl;
  std::cout << "pixel[3] = 444; pixel[4] = 555; pixel[5] = 666;" << std::endl;

  pixel[0] = 111; pixel[1] = 222; pixel[2] = 333;
  pixel[3] = 444; pixel[4] = 555; pixel[5] = 666;

  for (unsigned int i = 0; i < pixel.GetNumberOfComponents(); i++)
    {
    std::cout << "\tpixel[" << i << "] = " << pixel.GetNthComponent(i) << std::endl;
    }

  std::cout << "std::cout << pixel << std::endl;" << std::endl;
  std::cout << "\t" << pixel << std::endl;

  for (unsigned int j = 0; j < 2; j++)
    {
    std::cout << "pixelArray["<< j << "].GetNumberOfComponents = " << pixelArray[j].GetNumberOfComponents() << std::endl;
    std::cout << "pixelArray[" << j << "].GetNthComponent()" << std::endl;
    for (unsigned int i = 0; i < pixelArray[j].GetNumberOfComponents(); i++)
      {
      std::cout << "\tpixelArray[" << j << "].GetNthComponent(" << i << ") = " << static_cast<int>(pixelArray[j].GetNthComponent(i)) << std::endl;
      }
    }

  std::cout << "Testing arithmetic methods" << std::endl;
  Float3DTensorType pa;
  Float3DTensorType pb;

  pa[0] = 1.25;
  pa[1] = 3.25;
  pa[2] = 5.25;
  pa[3] = 1.25;
  pa[4] = 3.25;
  pa[5] = 5.25;

  pb[0] = 1.55;
  pb[1] = 3.55;
  pb[2] = 5.55;
  pb[3] = 1.55;
  pb[4] = 3.55;
  pb[5] = 5.55;

  Float3DTensorType pc;

  pc = pa + pb;
  std::cout << "addition = " << pc << std::endl;

  pc = pa - pb;
  std::cout << "subtraction = " << pc << std::endl;

  pc += pb;
  std::cout << "in-place addition = " << pc << std::endl;

  pc -= pb;
  std::cout << "in-place subtraction = " << pc << std::endl;

  pc = pa * 3.2;
  std::cout << "product by scalar = " << pc << std::endl;

  /** Create an Image of tensors  */
  typedef Float3DTensorType           PixelType;
  typedef itk::Image< PixelType, 3 >  ImageType;

  ImageType::Pointer dti = ImageType::New();

  ImageType::SizeType  size;
  ImageType::IndexType start;
  ImageType::RegionType region;

  size[0] = 128;
  size[1] = 128;
  size[2] = 128;

  start[0] = 0;
  start[1] = 0;
  start[2] = 0;

  region.SetIndex( start );
  region.SetSize( size );

  dti->SetRegions( region );
  dti->Allocate();

  ImageType::SpacingType spacing;
  spacing[0] = 0.5;
  spacing[1] = 0.5;
  spacing[2] = 1.5;

  ImageType::PointType origin;
  origin[0] = 25.5;
  origin[1] = 25.5;
  origin[2] = 27.5;

  dti->SetOrigin( origin );
  dti->SetSpacing( spacing );

  PixelType tensor;

  tensor[0] = 1.2;
  tensor[1] = 2.2;
  tensor[2] = 3.2;
  tensor[3] = 4.2;
  tensor[4] = 5.2;
  tensor[5] = 6.2;

  dti->FillBuffer( tensor );

  typedef itk::ImageRegionIterator< ImageType > IteratorType;

  IteratorType it( dti, region );
  it.GoToBegin();

  while( !it.IsAtEnd() )
    {
    it.Set( tensor );
    ++it;
    }

  // Test Eigen values computation
  {
    typedef itk::SymmetricSecondRankTensor<double,3>         Double3DTensorType;

    Double3DTensorType tensor3D;

    double v[3];
    v[0] = 19.0;
    v[1] = 23.0;
    v[2] = 29.0;

    tensor3D(0,0) = v[0];
    tensor3D(0,1) =  0.0;
    tensor3D(0,2) =  0.0;
    tensor3D(1,0) =  0.0; // overrides (0,1)
    tensor3D(1,1) = v[1];
    tensor3D(1,2) =  0.0;
    tensor3D(2,0) =  0.0; // overrides (0,2)
    tensor3D(2,1) =  0.0; // overrides (1,2)
    tensor3D(2,2) = v[2];

    std::cout << "SymmetricTensor = " << std::endl;
    std::cout << tensor3D << std::endl;

    Double3DTensorType::EigenValuesArrayType     eigenValues;
    Double3DTensorType::EigenValuesArrayType     eigenValues2;
    Double3DTensorType::EigenVectorsMatrixType   eigenVectors;

    tensor3D.ComputeEigenAnalysis( eigenValues, eigenVectors );
    tensor3D.ComputeEigenValues( eigenValues2 );

    std::cout << "EigenValues = " << std::endl;
    std::cout << eigenValues << std::endl;

    std::cout << "EigenVectors = " << std::endl;
    std::cout << eigenVectors << std::endl;

    const double tolerance = 1e-4;

    {
      Double3DTensorType::EigenValuesArrayType     expectedValues;
      expectedValues[0] = v[0];
      expectedValues[1] = v[1];
      expectedValues[2] = v[2];

      for(unsigned int i=0; i<3; i++)
        {
        if( std::fabs( expectedValues[i] - eigenValues[i] ) > tolerance )
          {
          std::cerr << "EigenAnalysis computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues << std::endl;
          return EXIT_FAILURE;
          }
        }

      for(unsigned int j=0; j<3; j++)
        {
        if( std::fabs( expectedValues[j] - eigenValues2[j] ) > tolerance )
          {
          std::cerr << "EigenValues computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues2 << std::endl;
          return EXIT_FAILURE;
          }
        }

    }

    // Now let's do something more involved...
    tensor3D(0,0) =  7.0;
    tensor3D(0,1) =  0.0;
    tensor3D(0,2) =  3.0;
    tensor3D(1,0) =  0.0; // overrides (0,1)
    tensor3D(1,1) =  0.0;
    tensor3D(1,2) =  0.0;
    tensor3D(2,0) =  3.0; // overrides (0,2)
    tensor3D(2,1) =  0.0; // overrides (1,2)
    tensor3D(2,2) =  7.0;

    std::cout << "SymmetricTensor = " << std::endl;
    std::cout << tensor3D << std::endl;

    tensor3D.ComputeEigenAnalysis( eigenValues, eigenVectors );
    tensor3D.ComputeEigenValues( eigenValues2 );

    std::cout << "EigenValues = " << std::endl;
    std::cout << eigenValues << std::endl;

    std::cout << "EigenVectors = " << std::endl;
    std::cout << eigenVectors << std::endl;

    {
      Double3DTensorType::EigenValuesArrayType     expectedValues;
      expectedValues[0] =  0.0;
      expectedValues[1] =  4.0;
      expectedValues[2] = 10.0;

      for(unsigned int i=0; i<3; i++)
        {
        if( std::fabs( expectedValues[i] - eigenValues[i] ) > tolerance )
          {
          std::cerr << "EigenAnalysis computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues << std::endl;
          return EXIT_FAILURE;
          }
        }

      for(unsigned int j=0; j<3; j++)
        {
        if( std::fabs( expectedValues[j] - eigenValues2[j] ) > tolerance )
          {
          std::cerr << "EigenValues computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues2 << std::endl;
          return EXIT_FAILURE;
          }
        }


    }

    // Now let's do one where we know the rotation...
    tensor3D(0,0) =  9.0;
    tensor3D(0,1) =  0.0;
    tensor3D(0,2) =  7.0;
    tensor3D(1,0) =  0.0; // overrides (0,1)
    tensor3D(1,1) =  0.0;
    tensor3D(1,2) =  0.0;
    tensor3D(2,0) =  7.0; // overrides (0,2)
    tensor3D(2,1) =  0.0; // overrides (1,2)
    tensor3D(2,2) =  3.0;

    std::cout << "SymmetricTensor = " << std::endl;
    std::cout << tensor3D << std::endl;

    tensor3D.ComputeEigenAnalysis( eigenValues, eigenVectors );
    tensor3D.ComputeEigenValues( eigenValues2 );

    std::cout << "EigenValues = " << std::endl;
    std::cout << eigenValues << std::endl;

    std::cout << "EigenVectors = " << std::endl;
    std::cout << eigenVectors << std::endl;

    {
      Double3DTensorType::EigenValuesArrayType     expectedValues;
      expectedValues[0] = -1.61577;
      expectedValues[1] =  0.00000;
      expectedValues[2] = 13.61580;

      for(unsigned int i=0; i<3; i++)
        {
        if( std::fabs( expectedValues[i] - eigenValues[i] ) > tolerance )
          {
          std::cerr << "Eigenvalue computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues << std::endl;
          return EXIT_FAILURE;
          }
        }

      for(unsigned int j=0; j<3; j++)
        {
        if( std::fabs( expectedValues[j] - eigenValues2[j] ) > tolerance )
          {
          std::cerr << "EigenValues computation failed" << std::endl;
          std::cerr << "expectedValues = " << expectedValues << std::endl;
          std::cerr << "eigenValues    = " << eigenValues2 << std::endl;
          return EXIT_FAILURE;
          }
        }

    }

  } // end of Test Eigen values computation


  // Test GetTrace() method
  {

    typedef itk::SymmetricSecondRankTensor<double,3>         Double3DTensorType;
    typedef Double3DTensorType::AccumulateValueType          AccumulateValueType;

    Double3DTensorType tensor3D;

    tensor3D(0,0) =  19.0;
    tensor3D(0,1) =   0.0;
    tensor3D(0,2) =   0.0;
    tensor3D(1,0) =   0.0; // overrides (0,1)
    tensor3D(1,1) =  23.0;
    tensor3D(1,2) =   0.0;
    tensor3D(2,0) =   7.0; // overrides (0,2)
    tensor3D(2,1) =   0.0; // overrides (1,2)
    tensor3D(2,2) =  29.0;

    AccumulateValueType expectedTrace =
              itk::NumericTraits< AccumulateValueType >::ZeroValue();

    expectedTrace += tensor3D(0,0);
    expectedTrace += tensor3D(1,1);
    expectedTrace += tensor3D(2,2);

    const double tolerance = 1e-4;

    AccumulateValueType computedTrace = tensor3D.GetTrace();
    if( std::fabs( computedTrace - expectedTrace ) > tolerance )
      {
      std::cerr << "Error computing the Trace" << std::endl;
      std::cerr << "Expected trace = " << expectedTrace << std::endl;
      std::cerr << "Computed trace = " << computedTrace << std::endl;
      return EXIT_FAILURE;
      }

  } // end of Test GetTrace() method


  // Test Matrix * SymmetricSecondRankTensor function
  {

    typedef itk::SymmetricSecondRankTensor<double,3>   Double3DTensorType;
    typedef itk::Matrix<double, 3, 3>                  Double3DMatrixType;

    Double3DTensorType tensor3D;

    tensor3D(0,0) =  19.0;
    tensor3D(0,1) =   0.0;
    tensor3D(0,2) =   0.0;
    tensor3D(1,0) =   0.0; // overrides (0,1)
    tensor3D(1,1) =  23.0;
    tensor3D(1,2) =   0.0;
    tensor3D(2,0) =   7.0; // overrides (0,2)
    tensor3D(2,1) =   0.0; // overrides (1,2)
    tensor3D(2,2) =  29.0;

    Double3DMatrixType matrix3D;
    matrix3D.Fill(1.0);
    std::vector<double> ans;
    ans.push_back(26);
    ans.push_back(23);
    ans.push_back(36);

    Double3DMatrixType result1 = tensor3D.PreMultiply( matrix3D );
    std::cout << result1 << std::endl;
    for (unsigned int ii = 0; ii < 3; ++ii)
    {
      if (itk::Math::NotAlmostEquals(result1[0][ii], ans[ii]))
      {
        std::cout << "PreMultiply FAILED" << std::endl;
        return EXIT_FAILURE;
      }
    }

    Double3DMatrixType result2 = tensor3D.PostMultiply( matrix3D );
    std::cout << result2 << std::endl;
    for (unsigned int ii = 0; ii < 3; ++ii)
    {
      if (itk::Math::NotAlmostEquals(result2[ii][0], ans[ii]))
      {
        std::cout << "PostMultiply FAILED" << std::endl;
        return EXIT_FAILURE;
      }
    }

    Double3DTensorType result3 = tensor3D.Rotate( matrix3D );
    std::cout << result3 << std::endl;
    for (unsigned int ii = 0; ii < 6; ++ii)
    {
      if (itk::Math::NotAlmostEquals(result3[ii], 85))
      {
        std::cout << "Rotate FAILED" << std::endl;
        return EXIT_FAILURE;
      }
    }

  } // end of Matrix * SymmetricSecondRankTensor test

  //Test casting constructors
  {
    typedef itk::SymmetricSecondRankTensor<int,3>     Int3DTensorType;

    Int3DTensorType intTensor;
    intTensor[0] =   1;
    intTensor[1] =  -2;
    intTensor[2] =   3;
    intTensor[3] =   4;
    intTensor[4] =   5;
    intTensor[5] =   6;

    //Test constructors
    Float3DTensorType floatTensor(intTensor);

    //test Assignment
    Float3DTensorType floatTensor2 = intTensor;

    //Test casting
    Float3DTensorType floatTensor3 = static_cast<Float3DTensorType>(intTensor);

    //Check that all floatTensors have are the same
    float precision = 1e-6;
    for (unsigned int i=0;i<Float3DTensorType::InternalDimension;++i)
    {
      float intVal = static_cast<float>(intTensor[i]);
      if ( (floatTensor[i] - intVal) > precision ||
          (floatTensor2[i] - intVal) > precision ||
          (floatTensor3[i] - intVal) > precision )
      {
        std::cerr << "Error failed casting/templated Constructor Test" << std::endl;
        return EXIT_FAILURE;
      }
    }
  }

  return EXIT_SUCCESS;
}