| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkSpatialOrientationAdapter.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 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
DEF |
#ifndef _itkSpatialOrientationAdapter_txx |
| 21 |
DEF |
#define _itkSpatialOrientationAdapter_txx |
| 22 |
|
#include "itkSpatialOrientationAdapter.h" |
| 23 |
|
#include "itkImageBase.h" |
| 24 |
|
|
| 25 |
|
namespace itk |
| 26 |
|
{ |
| 27 |
|
|
| 28 |
|
namespace Function |
| 29 |
|
{ |
| 30 |
|
|
| 31 |
|
inline unsigned Max3(float x, float y, float z) |
| 32 |
|
{ |
| 33 |
|
const double obliquityThresholdCosineValue = 0.001; |
| 34 |
|
|
| 35 |
|
double absX = vnl_math_abs(x); |
| 36 |
|
double absY = vnl_math_abs(y); |
| 37 |
|
double absZ = vnl_math_abs(z); |
| 38 |
|
|
| 39 |
LEN |
if ( ( absX > obliquityThresholdCosineValue ) && ( absX > absY ) && ( absX > absZ )) |
| 40 |
|
{ |
| 41 |
|
return 0; |
| 42 |
|
} |
| 43 |
LEN |
else if ( ( absY > obliquityThresholdCosineValue ) && ( absY > absX ) && ( absY > absZ ) ) |
| 44 |
|
{ |
| 45 |
|
return 1; |
| 46 |
|
} |
| 47 |
LEN |
else if ( ( absZ > obliquityThresholdCosineValue ) && ( absZ > absX ) && ( absZ > absY ) ) |
| 48 |
|
{ |
| 49 |
|
return 2; |
| 50 |
|
} |
| 51 |
|
// they must all be equal, so just say x |
| 52 |
|
return 0; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
inline int Sign(float x) |
| 56 |
|
{ |
| 57 |
|
if(x < 0) |
| 58 |
|
return -1; |
| 59 |
|
return 1; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
} // namespace Function |
| 63 |
|
|
| 64 |
|
template <int Dimension> |
| 65 |
|
typename SpatialOrientationAdapter<Dimension>::OrientationType |
| 66 |
|
SpatialOrientationAdapter<Dimension> |
| 67 |
LEN |
::FromDirectionCosines(const typename SpatialOrientationAdapter<Dimension>::DirectionType &Dir) |
| 68 |
|
{ |
| 69 |
|
// const typename ImageBase<VImageDimension>::DirectionType &direction = |
| 70 |
|
// this->GetDirection(); |
| 71 |
IND |
*int axes[9] = {0,0,0,0,0,0,0,0,0}; |
| 72 |
|
int dominant_axis; |
| 73 |
|
|
| 74 |
|
dominant_axis = Function::Max3(Dir[0][0],Dir[1][0],Dir[2][0]); |
| 75 |
|
axes[dominant_axis] = Function::Sign(Dir[dominant_axis][0]); |
| 76 |
|
dominant_axis = Function::Max3(Dir[0][1],Dir[1][1],Dir[2][1]); |
| 77 |
|
axes[dominant_axis+3] = Function::Sign(Dir[dominant_axis][1]); |
| 78 |
|
dominant_axis = Function::Max3(Dir[0][2],Dir[1][2],Dir[2][2]); |
| 79 |
|
axes[dominant_axis+6] = Function::Sign(Dir[dominant_axis][2]); |
| 80 |
|
|
| 81 |
LEN |
SpatialOrientation::CoordinateTerms terms[3] = {SpatialOrientation::ITK_COORDINATE_UNKNOWN,SpatialOrientation::ITK_COORDINATE_UNKNOWN,SpatialOrientation::ITK_COORDINATE_UNKNOWN}; |
| 82 |
|
|
| 83 |
|
for(unsigned i = 0; i < 3; i++) |
| 84 |
|
{ |
| 85 |
|
if(int(axes[(i*3)]) == 1) |
| 86 |
|
{ |
| 87 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Right; |
| 88 |
|
} |
| 89 |
|
else if(axes[(i*3)] == -1) |
| 90 |
|
{ |
| 91 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Left; |
| 92 |
|
} |
| 93 |
|
else if(axes[(i*3)+1] == 1) |
| 94 |
|
{ |
| 95 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Anterior; |
| 96 |
|
} |
| 97 |
|
else if(axes[(i*3)+1] == -1) |
| 98 |
|
{ |
| 99 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Posterior; |
| 100 |
|
} |
| 101 |
|
else if(axes[(i*3)+2] == 1) |
| 102 |
|
{ |
| 103 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Inferior; |
| 104 |
|
} |
| 105 |
|
else if(axes[(i*3)+2] == -1) |
| 106 |
|
{ |
| 107 |
|
terms[i] = SpatialOrientation::ITK_COORDINATE_Superior; |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
// |
| 111 |
|
// all terms must be defined, otherwise just punt |
| 112 |
LEN |
if(terms[0] == SpatialOrientation::ITK_COORDINATE_UNKNOWN || terms[1] == SpatialOrientation::ITK_COORDINATE_UNKNOWN || terms[2] == SpatialOrientation::ITK_COORDINATE_UNKNOWN) |
| 113 |
|
{ |
| 114 |
|
return SpatialOrientation::ITK_COORDINATE_ORIENTATION_RIP; |
| 115 |
|
} |
| 116 |
|
return static_cast<SpatialOrientation::ValidCoordinateOrientationFlags> |
| 117 |
IND |
****((terms[0] << |
| 118 |
|
SpatialOrientation::ITK_COORDINATE_PrimaryMinor) + |
| 119 |
|
(terms[1] << |
| 120 |
|
SpatialOrientation::ITK_COORDINATE_SecondaryMinor) + |
| 121 |
|
(terms[2] << |
| 122 |
|
SpatialOrientation::ITK_COORDINATE_TertiaryMinor)); |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
template <int Dimension> |
| 126 |
|
typename SpatialOrientationAdapter<Dimension>::DirectionType |
| 127 |
|
SpatialOrientationAdapter<Dimension> |
| 128 |
LEN |
::ToDirectionCosines(const typename SpatialOrientationAdapter<Dimension>::OrientationType &Or) |
| 129 |
|
{ |
| 130 |
|
typedef SpatialOrientation::CoordinateTerms CoordinateTerms; |
| 131 |
|
|
| 132 |
|
CoordinateTerms terms[3]; |
| 133 |
|
terms[0] = |
| 134 |
IND |
****static_cast<CoordinateTerms> |
| 135 |
IND |
****((Or >> SpatialOrientation::ITK_COORDINATE_PrimaryMinor) & 0xff); |
| 136 |
|
terms[1] = |
| 137 |
IND |
****static_cast<CoordinateTerms> |
| 138 |
IND |
****((Or >> SpatialOrientation::ITK_COORDINATE_SecondaryMinor) & 0xff); |
| 139 |
|
terms[2] = |
| 140 |
IND |
****static_cast<CoordinateTerms> |
| 141 |
IND |
****((Or >> SpatialOrientation::ITK_COORDINATE_TertiaryMinor) & 0xff); |
| 142 |
|
DirectionType direction; |
| 143 |
|
direction.Fill(0.0); |
| 144 |
|
for(unsigned int i = 0; i < DirectionType::ColumnDimensions; i++) |
| 145 |
|
{ |
| 146 |
|
switch(terms[i]) |
| 147 |
|
{ |
| 148 |
|
case SpatialOrientation::ITK_COORDINATE_Right: |
| 149 |
|
direction[0][i] = 1; |
| 150 |
|
break; |
| 151 |
|
case SpatialOrientation::ITK_COORDINATE_Left: |
| 152 |
|
direction[0][i] = -1; |
| 153 |
|
break; |
| 154 |
|
case SpatialOrientation::ITK_COORDINATE_Anterior: |
| 155 |
|
if ( DirectionType::RowDimensions > 1) direction[1][i] = 1; |
| 156 |
IND |
********break; |
| 157 |
|
case SpatialOrientation::ITK_COORDINATE_Posterior: |
| 158 |
IND |
********if ( DirectionType::RowDimensions > 1) direction[1][i] = -1; |
| 159 |
IND |
********break; |
| 160 |
|
case SpatialOrientation::ITK_COORDINATE_Inferior: |
| 161 |
IND |
********if ( DirectionType::RowDimensions > 2) direction[2][i] = 1; |
| 162 |
IND |
********break; |
| 163 |
|
case SpatialOrientation::ITK_COORDINATE_Superior: |
| 164 |
IND |
********if ( DirectionType::RowDimensions > 2) direction[2][i] = -1; |
| 165 |
IND |
********break; |
| 166 |
|
case SpatialOrientation::ITK_COORDINATE_UNKNOWN: |
| 167 |
|
default: |
| 168 |
IND |
********//TODO: Should there be a default? Throw an exception? |
| 169 |
IND |
********break; |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
//TODO: Should check that directions are orthoganal. |
| 173 |
|
return direction; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
#endif // _itkSpatialOrientationAdapter_txx |
| 179 |
|
|