Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkGeometry2D.h"
00020 #include <vtkTransform.h>
00021
00022
00023 mitk::Geometry2D::Geometry2D()
00024 : m_ScaleFactorMMPerUnitX( 1.0 ),
00025 m_ScaleFactorMMPerUnitY( 1.0 ),
00026 m_ReferenceGeometry( NULL )
00027 {
00028 }
00029
00030
00031 mitk::Geometry2D::~Geometry2D()
00032 {
00033 }
00034
00035
00036 void
00037 mitk::Geometry2D::SetIndexToWorldTransform(
00038 mitk::AffineTransform3D* transform)
00039 {
00040 Superclass::SetIndexToWorldTransform(transform);
00041
00042 m_ScaleFactorMMPerUnitX=GetExtentInMM(0)/GetExtent(0);
00043 m_ScaleFactorMMPerUnitY=GetExtentInMM(1)/GetExtent(1);
00044
00045 assert(m_ScaleFactorMMPerUnitX<ScalarTypeNumericTraits::infinity());
00046 assert(m_ScaleFactorMMPerUnitY<ScalarTypeNumericTraits::infinity());
00047 }
00048
00049
00050 void
00051 mitk::Geometry2D::SetExtentInMM(int direction, ScalarType extentInMM)
00052 {
00053 Superclass::SetExtentInMM(direction, extentInMM);
00054
00055 m_ScaleFactorMMPerUnitX=GetExtentInMM(0)/GetExtent(0);
00056 m_ScaleFactorMMPerUnitY=GetExtentInMM(1)/GetExtent(1);
00057
00058 assert(m_ScaleFactorMMPerUnitX<ScalarTypeNumericTraits::infinity());
00059 assert(m_ScaleFactorMMPerUnitY<ScalarTypeNumericTraits::infinity());
00060 }
00061
00062
00063 bool
00064 mitk::Geometry2D::Map(
00065 const mitk::Point3D &pt3d_mm, mitk::Point2D &pt2d_mm) const
00066 {
00067 assert(m_BoundingBox.IsNotNull());
00068
00069 Point3D pt3d_units;
00070 BackTransform(pt3d_mm, pt3d_units);
00071 pt2d_mm[0]=pt3d_units[0]*m_ScaleFactorMMPerUnitX;
00072 pt2d_mm[1]=pt3d_units[1]*m_ScaleFactorMMPerUnitY;
00073 pt3d_units[2]=0;
00074 return const_cast<BoundingBox*>(m_BoundingBox.GetPointer())->IsInside(pt3d_units);
00075 }
00076
00077
00078 void
00079 mitk::Geometry2D::Map(const mitk::Point2D &pt2d_mm, mitk::Point3D &pt3d_mm) const
00080 {
00081 Point3D pt3d_units;
00082 pt3d_units[0]=pt2d_mm[0]/m_ScaleFactorMMPerUnitX;
00083 pt3d_units[1]=pt2d_mm[1]/m_ScaleFactorMMPerUnitY;
00084 pt3d_units[2]=0;
00085 pt3d_mm = GetParametricTransform()->TransformPoint(pt3d_units);
00086 }
00087
00088
00089 void
00090 mitk::Geometry2D::IndexToWorld(
00091 const mitk::Point2D &, mitk::Point2D &) const
00092 {
00093 itkExceptionMacro(<< "No general transform possible (only affine) ==> no general" \
00094 " IndexToWorld(const mitk::Point2D &pt_mm, mitk::Point2D &pt_units)" \
00095 " possible. Has to be implemented in sub-class.");
00096 }
00097
00098
00099 void
00100 mitk::Geometry2D::WorldToIndex(
00101 const mitk::Point2D &, mitk::Point2D &) const
00102 {
00103 itkExceptionMacro(<< "No general back transform possible (only affine) ==> no general" \
00104 " WorldToIndex(const mitk::Point2D &pt_mm, mitk::Point2D &pt_units)" \
00105 " possible. Has to be implemented in sub-class.");
00106 }
00107
00108
00109 void
00110 mitk::Geometry2D::IndexToWorld(const mitk::Point2D &,
00111 const mitk::Vector2D &, mitk::Vector2D &) const
00112 {
00113 itkExceptionMacro(<< "No general transform possible (only affine) ==> no general" \
00114 " IndexToWorld(const mitk::Vector2D &vec_mm, mitk::Vector2D &vec_units)" \
00115 " possible. Has to be implemented in sub-class.");
00116 }
00117
00118
00119 void
00120 mitk::Geometry2D::WorldToIndex(const mitk::Point2D &,
00121 const mitk::Vector2D &, mitk::Vector2D &) const
00122 {
00123 itkExceptionMacro(<< "No general back transform possible (only affine) ==> no general" \
00124 " WorldToIndex(const mitk::Vector2D &vec_mm, mitk::Vector2D &vec_units)" \
00125 " possible. Has to be implemented in sub-class.");
00126 }
00127
00128 void
00129 mitk::Geometry2D::SetSizeInUnits(mitk::ScalarType width, mitk::ScalarType height)
00130 {
00131 ScalarType bounds[6]={0, width, 0, height, 0, 1};
00132 ScalarType extent, newextentInMM;
00133 if(GetExtent(0)>0)
00134 {
00135 extent = GetExtent(0);
00136 if(width>extent)
00137 newextentInMM = GetExtentInMM(0)/width*extent;
00138 else
00139 newextentInMM = GetExtentInMM(0)*extent/width;
00140 SetExtentInMM(0, newextentInMM);
00141 }
00142 if(GetExtent(1)>0)
00143 {
00144 extent = GetExtent(1);
00145 if(width>extent)
00146 newextentInMM = GetExtentInMM(1)/height*extent;
00147 else
00148 newextentInMM = GetExtentInMM(1)*extent/height;
00149 SetExtentInMM(1, newextentInMM);
00150 }
00151 SetBounds(bounds);
00152 }
00153
00154
00155 bool
00156 mitk::Geometry2D::Project(
00157 const mitk::Point3D &pt3d_mm, mitk::Point3D &projectedPt3d_mm) const
00158 {
00159 assert(m_BoundingBox.IsNotNull());
00160
00161 Point3D pt3d_units;
00162 BackTransform(pt3d_mm, pt3d_units);
00163 pt3d_units[2] = 0;
00164 projectedPt3d_mm = GetParametricTransform()->TransformPoint(pt3d_units);
00165 return const_cast<BoundingBox*>(m_BoundingBox.GetPointer())->IsInside(pt3d_units);
00166 }
00167
00168
00169 bool
00170 mitk::Geometry2D::Map(const mitk::Point3D & atPt3d_mm,
00171 const mitk::Vector3D &vec3d_mm, mitk::Vector2D &vec2d_mm) const
00172 {
00173 Point2D pt2d_mm_start, pt2d_mm_end;
00174 Point3D pt3d_mm_end;
00175 bool inside=Map(atPt3d_mm, pt2d_mm_start);
00176 pt3d_mm_end = atPt3d_mm+vec3d_mm;
00177 inside&=Map(pt3d_mm_end, pt2d_mm_end);
00178 vec2d_mm=pt2d_mm_end-pt2d_mm_start;
00179 return inside;
00180 }
00181
00182
00183 void
00184 mitk::Geometry2D::Map(const mitk::Point2D &,
00185 const mitk::Vector2D &, mitk::Vector3D &) const
00186 {
00187
00188 assert(false);
00189 }
00190
00191
00192 bool
00193 mitk::Geometry2D::Project(const mitk::Point3D & atPt3d_mm,
00194 const mitk::Vector3D &vec3d_mm, mitk::Vector3D &projectedVec3d_mm) const
00195 {
00196 assert(m_BoundingBox.IsNotNull());
00197
00198 Vector3D vec3d_units;
00199 BackTransform(atPt3d_mm, vec3d_mm, vec3d_units);
00200 vec3d_units[2] = 0;
00201 projectedVec3d_mm = GetParametricTransform()->TransformVector(vec3d_units);
00202
00203 Point3D pt3d_units;
00204 BackTransform(atPt3d_mm, pt3d_units);
00205 return const_cast<BoundingBox*>(m_BoundingBox.GetPointer())->IsInside(pt3d_units);
00206 }
00207
00208
00209 mitk::ScalarType
00210 mitk::Geometry2D::SignedDistance(const mitk::Point3D& pt3d_mm) const
00211 {
00212 Point3D projectedPoint;
00213 Project(pt3d_mm, projectedPoint);
00214 Vector3D direction = pt3d_mm-projectedPoint;
00215 ScalarType distance = direction.GetNorm();
00216
00217 if(IsAbove(pt3d_mm) == false)
00218 distance*=-1.0;
00219
00220 return distance;
00221 }
00222
00223 bool
00224 mitk::Geometry2D::IsAbove(const mitk::Point3D& pt3d_mm) const
00225 {
00226 Point3D pt3d_units;
00227 Geometry3D::WorldToIndex(pt3d_mm, pt3d_units);
00228 return (pt3d_units[2] > m_BoundingBox->GetBounds()[4]);
00229 }
00230
00231 mitk::AffineGeometryFrame3D::Pointer
00232 mitk::Geometry2D::Clone() const
00233 {
00234 Self::Pointer newGeometry = Self::New();
00235 newGeometry->Initialize();
00236 InitializeGeometry(newGeometry);
00237 return newGeometry.GetPointer();
00238 }
00239
00240
00241 void
00242 mitk::Geometry2D::InitializeGeometry(Self * newGeometry) const
00243 {
00244 Superclass::InitializeGeometry(newGeometry);
00245
00246 newGeometry->SetReferenceGeometry( m_ReferenceGeometry );
00247 }
00248
00249 void
00250 mitk::Geometry2D::PrintSelf(std::ostream& os, itk::Indent indent) const
00251 {
00252 Superclass::PrintSelf(os,indent);
00253 os << indent << " ScaleFactorMMPerUnitX: "
00254 << m_ScaleFactorMMPerUnitX << std::endl;
00255 os << indent << " ScaleFactorMMPerUnitY: "
00256 << m_ScaleFactorMMPerUnitY << std::endl;
00257 }
00258
00259 void
00260 mitk::Geometry2D::SetReferenceGeometry( mitk::Geometry3D *geometry )
00261 {
00262 m_ReferenceGeometry = geometry;
00263 }
00264
00265 mitk::Geometry3D *
00266 mitk::Geometry2D::GetReferenceGeometry() const
00267 {
00268 return m_ReferenceGeometry;
00269 }
00270
00271 bool
00272 mitk::Geometry2D::HasReferenceGeometry() const
00273 {
00274 return ( m_ReferenceGeometry != NULL );
00275 }