File: cmtkXformList.cxx

package info (click to toggle)
cmtk 3.3.1p2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,492 kB
  • sloc: cpp: 87,098; ansic: 23,347; sh: 3,896; xml: 1,551; perl: 707; makefile: 332
file content (169 lines) | stat: -rw-r--r-- 4,756 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
/*
//
//  Copyright 1997-2009 Torsten Rohlfing
//
//  Copyright 2004-2012, 2014 SRI International
//
//  This file is part of the Computational Morphometry Toolkit.
//
//  http://www.nitrc.org/projects/cmtk/
//
//  The Computational Morphometry Toolkit is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  The Computational Morphometry Toolkit is distributed in the hope that it
//  will be useful, but WITHOUT ANY WARRANTY; without even the implied
//  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with the Computational Morphometry Toolkit.  If not, see
//  <http://www.gnu.org/licenses/>.
//
//  $Revision: 5436 $
//
//  $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
//  $LastChangedBy: torstenrohlfing $
//
*/

#include "cmtkXformList.h"

void
cmtk::XformList::Add
( const Xform::SmartConstPtr& xform, const bool inverse, const Types::Coordinate globalScale  )
{
  this->push_back( XformListEntry::SmartConstPtr( new XformListEntry( xform, inverse, globalScale ) ) );
}

void
cmtk::XformList::AddToFront
( const Xform::SmartConstPtr& xform, const bool inverse, const Types::Coordinate globalScale  )
{
  this->push_front( XformListEntry::SmartConstPtr( new XformListEntry( xform, inverse, globalScale ) ) );
}

bool
cmtk::XformList::ApplyInPlace( Xform::SpaceVectorType& v ) const
{
  for ( const_iterator it = this->begin(); it != this->end(); ++it ) 
    {
    if ( (*it)->Inverse ) 
      {
      // is this an affine transformation that has an inverse?
      if ( (*it)->InverseAffineXform ) 
	{
	// apply inverse
	v = (*it)->InverseAffineXform->Apply( v );
	}
      else
	{
	// not affine: use approximate inverse
	if ( ! (*it)->m_Xform->ApplyInverse( v, v, this->m_Epsilon ) )
	  return false;
	} 
      } 
    else
      {
      // are we outside xform domain? then return failure.
      if ( !(*it)->m_Xform->InDomain( v ) ) return false;
      v = (*it)->m_Xform->Apply( v );
      }
    }
  return true;
}

bool
cmtk::XformList::GetJacobian
( const Xform::SpaceVectorType& v, Types::DataItem& jacobian, const bool correctGlobalScale ) const
{
  Xform::SpaceVectorType vv( v );

  jacobian = static_cast<Types::DataItem>( 1.0 );
  for ( const_iterator it = this->begin(); it != this->end(); ++it ) 
    {
    if ( (*it)->Inverse ) 
      {
      if ( correctGlobalScale )
	jacobian *= static_cast<Types::DataItem>( (*it)->GlobalScale );

      // is this an affine transformation that has an inverse?
      if ( (*it)->InverseAffineXform ) 
	{
	// apply inverse
	vv = (*it)->InverseAffineXform->Apply( vv );
	}
      else
	{
	// not affine: use approximate inverse
	if ( ! (*it)->m_Xform->ApplyInverse( vv, vv, this->m_Epsilon ) )
	  return false;
	}

      // compute Jacobian at destination and invert
      jacobian /= static_cast<Types::DataItem>( (*it)->m_Xform->GetJacobianDeterminant( vv ) );
      } 
    else 
      {
      // are we outside xform domain? then return failure.
      if ( !(*it)->m_Xform->InDomain( v ) ) return false;

      jacobian *= static_cast<Types::DataItem>( (*it)->m_Xform->GetJacobianDeterminant( vv ) );
      if ( correctGlobalScale )
	jacobian /= static_cast<Types::DataItem>( (*it)->GlobalScale );
      vv = (*it)->m_Xform->Apply( vv );
      }
    }
  return true;
}

bool
cmtk::XformList::AllAffine() const
{
  for ( const_iterator it = this->begin(); it != this->end(); ++it ) 
    {
    if ( !(*it)->IsAffine() )
      return false;
    }
  return true;
}

cmtk::XformList
cmtk::XformList::MakeAllAffine() const
{
  cmtk::XformList allAffine;

  for ( const_iterator it = this->begin(); it != this->end(); ++it ) 
    {
    allAffine.push_back( (*it)->CopyAsAffine() );
    }

  return allAffine;
}

std::string
cmtk::XformList::GetFixedImagePath() const
{
  const XformListEntry& first = **(this->begin());

  // if transformation is inverse, get original "moving" path instead.
  if ( first.Inverse )
    return first.m_Xform->GetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, "" );
  else
    return first.m_Xform->GetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, "" );
}

std::string
cmtk::XformList::GetMovingImagePath() const
{
  const XformListEntry& last = **(this->rbegin());
  
  // if transformation is inverse, get original "fixed" path instead.
  if ( last.Inverse )
    return last.m_Xform->GetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, "" );
  else
    return last.m_Xform->GetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, "" );
}