/*============================================================================= NOTE: THIS FILE IS A HANDMADE WRAPPER FOR THE ManagedITK PROJECT. Project: ManagedITK Program: Insight Segmentation & Registration Toolkit Module: itkManagedImageRegion.cxx Language: C++/CLI Author: Dan Mueller $Date: 2007-04-11 16:26:16 +1000 (Wed, 11 Apr 2007) $ $Revision: 128 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. Portions of this code are covered under the ITK and VTK copyright. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ #pragma once #pragma warning( disable : 4635 ) // Disable warnings about XML doc comments #ifndef __itkManagedImageRegion_cxx #define __itkManagedImageRegion_cxx // Include some managed files #include "itkManagedSize.cxx" #include "itkManagedIndex.cxx" #include "itkManagedContinuousIndex.cxx" // Use some managed namespaces #using #using using namespace System; using namespace System::IO; using namespace System::Reflection; using namespace System::ComponentModel; using namespace System::Diagnostics; namespace itk { /// ///This class is a managed replacement for itk::ImageRegion. /// /// ///ImageRegion is an class that represents some structured portion or ///piece of an Image. The ImageRegion is represented with an index and ///a size in each of the n-dimensions of the image. (The index is the ///corner of the image, the size is the lengths of the image in each of ///the topological directions.) /// public ref class itkImageRegion { private: itkSize^ m_Size; itkIndex^ m_Index; public: ///Default constructor. itkImageRegion ( ) { } ///Constructor taking an existing size and index. ///The size to set within the new image region. ///The index to set within the new image region. itkImageRegion ( itkSize^ size, itkIndex^ index ) { if (size->Dimension != index->Dimension) throw gcnew ArgumentException( "The size and index of the image region must have the same dimensionality." ); this->m_Size = size; this->m_Index = index; } ///Get the dimensionality of the region. property virtual unsigned int Dimension { unsigned int get() { return this->m_Size->Dimension; } } ///Get/set the size of the image region. property virtual itkSize^ Size { itkSize^ get() { return this->m_Size; } void set( itkSize^ size ) { this->m_Size = size; } } ///Get/set the index of the image region. property virtual itkIndex^ Index { itkIndex^ get() { return this->m_Index; } void set( itkIndex^ index ) { this->m_Index = index; } } ///Test if an index is inside the image region. ///True if inside, false otherwise. bool IsInside ( itkIndex^ index ) { for(unsigned int i=0; iLength; i++) { if ( index[i] < this->Index[i] ) return false; if ( index[i] >= this->Index[i] + this->Size[i] ) return false; } return true; } ///Test if a continuous index is inside the image region. ///True if inside, false otherwise. bool IsInside ( itkContinuousIndex^ cindex ) { for (unsigned int i=0; iLength; i++) { if ( cindex[i] < this->Index[i] ) return false; if ( cindex[i] >= (this->Index[i] + this->Size[i]) ) return false; } return true; } /// ///Pad an image region by the specified radius uniformly in all dimensions. /// void PadByRadius( System::Int32 radius ) { array^ radiusArray = gcnew array( this->Dimension ); for (unsigned int i=0; i < this->Dimension; ++i) radiusArray[i] = radius; this->PadByRadius( radiusArray ); } /// ///Pad an image region by the specified radius. /// void PadByRadius( itkSize^ radius ) { this->PadByRadius( radius->Data ); } /// ///Pad an image region by the specified radius. /// void PadByRadius( array^ radius ) { for (unsigned int i = 0; i < this->Dimension; i++) { m_Size[i] += 2 * radius[i]; m_Index[i] -= radius[i]; } } /// ///Crop a region by another region. If this region is outside of the crop, ///this method returns false and does not modify the region. Otherwise, ///this method returns true and the region ismodified to reflect the crop. /// bool Crop ( itkImageRegion^ region ) { System::Int32 crop; unsigned int i; bool cropPossible = (this->Dimension == region->Dimension); // Can we crop? for (i = 0; i < region->Dimension && cropPossible; i++) { // Is left edge of current region to the right of the right edge // of the region to crop with? (if so, we cannot crop) if ( m_Index[i] >= region->Index[i] + region->Size[i] ) cropPossible = false; // If right edge of the current region to the left of the left // edge of the region to crop with? (if so, we cannot crop) if ( m_Index[i] + m_Size[i] <= region->Index[i] ) cropPossible = false; } // If we cannot crop, return without changing anythin if (!cropPossible) return cropPossible; // We can crop, so crop for (i=0; i < region->Dimension; i++) { // First check the start index if (m_Index[i] < region->Index[i]) { // How much do we need to adjust crop = region->Index[i] - m_Index[i]; // Adjust the start index and the size of the current region m_Index[i] += crop; m_Size[i] -= crop; } // Now check the final size if ( m_Index[i] + m_Size[i] > region->Index[i] + region->Size[i] ) { // How much do we need to adjust crop = m_Index[i] + m_Size[i] - region->Index[i] - region->Size[i]; // Adjust the size m_Size[i] -= crop; } } return cropPossible; } ///Returns a string representation of the itkImageRegion. ///A string representation in the format "Index=[XXX,XXX,..] Size=[XXX,XXX,..]". virtual String^ ToString() override { return "Index=" + this->Index->ToString() + " Size=" + this->Size->ToString(); } }; // end ref class } // end namespace itk #endif