File: ImageWrapperBase.cxx

package info (click to toggle)
itksnap 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,132 kB
  • sloc: cpp: 91,089; ansic: 1,994; sh: 327; makefile: 16
file content (97 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (3)
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
#include "ImageWrapperBase.h"
#include "itkImageBase.h"
#include "IRISException.h"




vnl_matrix_fixed<double, 4, 4>
ImageWrapperBase
::ConstructNiftiSform(vnl_matrix<double> m_dir,
                      vnl_vector<double> v_origin,
                      vnl_vector<double> v_spacing)
{
  // Set the NIFTI/RAS transform
  vnl_matrix<double> m_ras_matrix;
  vnl_diag_matrix<double> m_scale, m_lps_to_ras;
  vnl_vector<double> v_ras_offset;

  // Compute the matrix
  m_scale.set(v_spacing);
  m_lps_to_ras.set(vnl_vector<double>(3, 1.0));
  m_lps_to_ras[0] = -1;
  m_lps_to_ras[1] = -1;
  m_ras_matrix = m_lps_to_ras * m_dir * m_scale;

  // Compute the vector
  v_ras_offset = m_lps_to_ras * v_origin;

  // Create the larger matrix
  vnl_vector<double> vcol(4, 1.0);
  vcol.update(v_ras_offset);

  vnl_matrix_fixed<double,4,4> m_sform;
  m_sform.set_identity();
  m_sform.update(m_ras_matrix);
  m_sform.set_column(3, vcol);
  return m_sform;
}

vnl_matrix_fixed<double, 4, 4>
ImageWrapperBase
::ConstructVTKtoNiftiTransform(vnl_matrix<double> m_dir,
                               vnl_vector<double> v_origin,
                               vnl_vector<double> v_spacing)
{
  vnl_matrix_fixed<double,4,4> vox2nii = ConstructNiftiSform(m_dir, v_origin, v_spacing);
  vnl_matrix_fixed<double,4,4> vtk2vox;
  vtk2vox.set_identity();
  for(size_t i = 0; i < 3; i++)
    {
    vtk2vox(i,i) = 1.0 / v_spacing[i];
    vtk2vox(i,3) = - v_origin[i] / v_spacing[i];
    }
  return vox2nii * vtk2vox;
}


ScalarRepresentationIterator
::ScalarRepresentationIterator(const VectorImageWrapperBase *wrapper)
  : m_Depth(NUMBER_OF_SCALAR_REPS, 1)
{
  assert(wrapper->GetNumberOfComponents() > 0);

  m_Depth[SCALAR_REP_COMPONENT] = wrapper->GetNumberOfComponents();
  m_Current = SCALAR_REP_COMPONENT;
  m_Index = 0;
}

ScalarRepresentationIterator &ScalarRepresentationIterator::operator ++()
{
  // Once at the end stay at the end
  if(m_Current == NUMBER_OF_SCALAR_REPS)
    return *this;

  // If there is room to grow in current rep, do it
  if(m_Index + 1 < m_Depth[(int) m_Current])
    {
    m_Index++;
    }
  else
    {
    m_Current++;
    m_Index = 0;
    }

  return *this;
}

bool ScalarRepresentationIterator::IsAtEnd() const
{
  return m_Current == NUMBER_OF_SCALAR_REPS;
}