diff --git a/Code/Algorithms/Common/include/mapMaskBoundingBoxHelper.h b/Code/Algorithms/Common/include/mapMaskBoundingBoxHelper.h index c919034..4788fd8 100644 --- a/Code/Algorithms/Common/include/mapMaskBoundingBoxHelper.h +++ b/Code/Algorithms/Common/include/mapMaskBoundingBoxHelper.h @@ -1,140 +1,134 @@ // ----------------------------------------------------------------------- // MatchPoint - DKFZ translational registration framework // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See mapCopyright.txt or // http://www.dkfz.de/en/sidt/projects/MatchPoint/copyright.html // // 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. // //------------------------------------------------------------------------ /*! // @file // @version $Revision$ (last changed revision) // @date $Date$ (last change date) // @author $Author$ (last changed by) // Subversion HeadURL: $HeadURL$ */ #ifndef __MASK_BOUNDING_BOX_HELPER_H #define __MASK_BOUNDING_BOX_HELPER_H #include "itkIndent.h" #include "itkSpatialObject.h" #include "itkImageBase.h" #include "mapExceptionObjectMacros.h" namespace map { namespace algorithm { template class MaskBoundingBoxHelper { public: using Self = MaskBoundingBoxHelper; using MaskBaseType = ::itk::SpatialObject; using ImageBaseType = ::itk::ImageBase; using ImageRegionType = typename ImageBaseType::RegionType; using PointType = typename ImageBaseType::PointType; public: /*! @brief computes the image region for an given image (geometry) which guarantees to cover the bounding box of the passed mask. It is not guaranteed that the returned bounding box is within the largest possible or buffered region of the image. Thus using the returning region you can be sure that any inside point of the mask is within the region (and has a pixel correspondence, if it is also covered by the largest possible region). @pre mask must be a valid pointer @pre refImage must be a valid pointer @eguarante strong @param [in] mask Pointer to the mask object. @param [in] refImage Pointer to the image on which geomtry the bounding region should be computed @oaram [out] boundingRegion The computed bounding image region. @return Indicates if boundingRegion has a valid value. */ - static bool computeBoundingImageRegion(const MaskBaseType* mask, const ImageBaseType* refImage, + static bool computeBoundingImageRegion(MaskBaseType* mask, const ImageBaseType* refImage, ImageRegionType& boundingRegion) { ImageRegionType resultRegion; - bool result = false; if (!mask) { mapDefaultExceptionStaticMacro( << "Cannot compute bounding box. Mask pointer is Null."); } if (!refImage) { mapDefaultExceptionStaticMacro( << "Cannot compute bounding box. Reference image pointer is Null."); } - if (mask->ComputeBoundingBox()) + mask->Update(); + + const auto* spBBox = mask->GetMyBoundingBoxInWorldSpace(); + const auto cornerPoints = spBBox->ComputeCorners(); + + typename ImageBaseType::IndexType minIndex; + minIndex.Fill(::itk::NumericTraits::max()); + typename ImageBaseType::IndexType maxIndex; + maxIndex.Fill( + ::itk::NumericTraits::NonpositiveMin()); + + for (const auto& cornerPoint : cornerPoints) { - //there is really a bounding box - typename MaskBaseType::BoundingBoxType::Pointer spBBox = mask->GetBoundingBox(); - const typename MaskBaseType::BoundingBoxType::PointsContainer* cornerPoints = spBBox->GetCorners(); - - typename ImageBaseType::IndexType minIndex; - minIndex.Fill(::itk::NumericTraits::max()); - typename ImageBaseType::IndexType maxIndex; - maxIndex.Fill( - ::itk::NumericTraits::NonpositiveMin()); - - for (typename MaskBaseType::BoundingBoxType::PointsContainer::ConstIterator pos = - cornerPoints->Begin(); pos != cornerPoints->End(); ++pos) - { - typename ImageBaseType::IndexType index; - typename ImageBaseType::PointType point = pos.Value(); + typename ImageBaseType::IndexType index; - refImage->TransformPhysicalPointToIndex(point, index); + refImage->TransformPhysicalPointToIndex(cornerPoint, index); - for (unsigned int i = 0; i < ImageBaseType::GetImageDimension(); ++i) + for (unsigned int i = 0; i < ImageBaseType::GetImageDimension(); ++i) + { + if (index[i] < minIndex[i]) { - if (index[i] < minIndex[i]) - { - minIndex[i] = index[i]; - }; - - if (index[i] > maxIndex[i]) - { - maxIndex[i] = index[i]; - }; - } + minIndex[i] = index[i]; + }; + if (index[i] > maxIndex[i]) + { + maxIndex[i] = index[i]; + }; } - resultRegion.SetIndex(minIndex); - resultRegion.SetUpperIndex(maxIndex); - boundingRegion = resultRegion; - result = true; } - return result; + resultRegion.SetIndex(minIndex); + resultRegion.SetUpperIndex(maxIndex); + boundingRegion = resultRegion; + + return true; }; protected: /*! @brief virtual destructor */ virtual ~MaskBoundingBoxHelper() {}; MaskBoundingBoxHelper() {}; private: //No copy constructor allowed MaskBoundingBoxHelper(const Self& source); void operator=(const Self&); //purposely not implemented }; } // namespace algorithm } // namespace map #endif diff --git a/Code/Algorithms/Common/test/mapMaskBoundingBoxHelperTest.cpp b/Code/Algorithms/Common/test/mapMaskBoundingBoxHelperTest.cpp index 65080bb..0a51c4b 100644 --- a/Code/Algorithms/Common/test/mapMaskBoundingBoxHelperTest.cpp +++ b/Code/Algorithms/Common/test/mapMaskBoundingBoxHelperTest.cpp @@ -1,87 +1,87 @@ // ----------------------------------------------------------------------- // MatchPoint - DKFZ translational registration framework // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See mapCopyright.txt or // http://www.dkfz.de/en/sidt/projects/MatchPoint/copyright.html // // 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. // //------------------------------------------------------------------------ /*! // @file // @version $Revision$ (last changed revision) // @date $Date$ (last change date) // @author $Author$ (last changed by) // Subversion HeadURL: $HeadURL$ */ #if defined(_MSC_VER) #pragma warning ( disable : 4786 ) #endif #include "itkImage.h" #include "itkBoxSpatialObject.h" #include "mapMaskBoundingBoxHelper.h" #include "litCheckMacros.h" #include namespace map { namespace testing { int mapMaskBoundingBoxHelperTest(int, char* []) { PREPARE_DEFAULT_TEST_REPORTING; typedef ::itk::Image ImageType; ImageType::Pointer image = ImageType::New(); ImageType::DirectionType direction; direction[0][0] = 0.866; direction[1][0] = -0.5; direction[0][1] = 0.5; direction[1][1] = 0.866; image->SetDirection(direction); ImageType::PointType origin; origin.Fill(-20); image->SetOrigin(origin); ImageType::SizeType size; size.Fill(100); image->SetRegions(size); image->Allocate(); image->FillBuffer(0); typedef ::itk::BoxSpatialObject<2> MaskType; MaskType::Pointer mask = MaskType::New(); MaskType::SizeType maskSize; maskSize[0] = 10; maskSize[1] = 15; - mask->SetSize(maskSize); + mask->SetSizeInObjectSpace(maskSize); ImageType::RegionType region; CHECK_THROW(map::algorithm::MaskBoundingBoxHelper<2>::computeBoundingImageRegion(NULL, image, region)); CHECK_THROW(map::algorithm::MaskBoundingBoxHelper<2>::computeBoundingImageRegion(mask, NULL, region)); CHECK_THROW(map::algorithm::MaskBoundingBoxHelper<2>::computeBoundingImageRegion(NULL, NULL, region)); CHECK(map::algorithm::MaskBoundingBoxHelper<2>::computeBoundingImageRegion(mask, image, region)); CHECK_EQUAL(0, region.GetIndex(0)); CHECK_EQUAL(27, region.GetIndex(1)); CHECK_EQUAL(17, region.GetSize(0)); CHECK_EQUAL(19, region.GetSize(1)); RETURN_AND_REPORT_TEST_SUCCESS; } } //namespace testing } //namespace map