]> Creatis software - clitk.git/blob - itk/clitkBoundingBoxUtils.txx
Utils for bounding box calculation (formerly in clitkSegmentationUtils)
[clitk.git] / itk / clitkBoundingBoxUtils.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 namespace clitk {
20
21   //--------------------------------------------------------------------
22   template<class ImageType>
23   void ComputeBBFromImageRegion(const ImageType * image, 
24                                 typename ImageType::RegionType region,
25                                 typename itk::BoundingBox<unsigned long, 
26                                 ImageType::ImageDimension>::Pointer bb) {
27     typedef typename ImageType::IndexType IndexType;
28     IndexType firstIndex;
29     IndexType lastIndex;
30     for(unsigned int i=0; i<image->GetImageDimension(); i++) {
31       firstIndex[i] = region.GetIndex()[i];
32       lastIndex[i] = firstIndex[i]+region.GetSize()[i];
33     }
34
35     typedef itk::BoundingBox<unsigned long, 
36                              ImageType::ImageDimension> BBType;
37     typedef typename BBType::PointType PointType;
38     PointType lastPoint;
39     PointType firstPoint;
40     image->TransformIndexToPhysicalPoint(firstIndex, firstPoint);
41     image->TransformIndexToPhysicalPoint(lastIndex, lastPoint);
42
43     bb->SetMaximum(lastPoint);
44     bb->SetMinimum(firstPoint);
45   }
46   //--------------------------------------------------------------------
47
48
49   //--------------------------------------------------------------------
50   template<int Dimension>
51   void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
52                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
53                              typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2) {
54
55     typedef itk::BoundingBox<unsigned long, Dimension> BBType;
56     typedef typename BBType::PointType PointType;
57     PointType lastPoint;
58     PointType firstPoint;
59
60     for(unsigned int i=0; i<Dimension; i++) {
61       firstPoint[i] = std::max(bbi1->GetMinimum()[i], 
62                                bbi2->GetMinimum()[i]);
63       lastPoint[i] = std::min(bbi1->GetMaximum()[i], 
64                               bbi2->GetMaximum()[i]);
65     }
66
67     bbo->SetMaximum(lastPoint);
68     bbo->SetMinimum(firstPoint);
69   }
70   //--------------------------------------------------------------------
71
72
73   //--------------------------------------------------------------------
74   template<class ImageType>
75   void ComputeRegionFromBB(const ImageType * image, 
76                            const typename itk::BoundingBox<unsigned long, 
77                                                            ImageType::ImageDimension>::Pointer bb, 
78                            typename ImageType::RegionType & region) {
79     // Types
80     typedef typename ImageType::IndexType  IndexType;
81     typedef typename ImageType::PointType  PointType;
82     typedef typename ImageType::RegionType RegionType;
83     typedef typename ImageType::SizeType   SizeType;
84
85     // Region starting point
86     IndexType regionStart;
87     PointType start = bb->GetMinimum();
88     image->TransformPhysicalPointToIndex(start, regionStart);
89     
90     // Region size
91     SizeType regionSize;
92     PointType maxs = bb->GetMaximum();
93     PointType mins = bb->GetMinimum();
94     for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
95       regionSize[i] = lrint((maxs[i] - mins[i])/image->GetSpacing()[i]);
96     }
97    
98     // Create region
99     region.SetIndex(regionStart);
100     region.SetSize(regionSize);
101   }
102   //--------------------------------------------------------------------
103
104
105 } // end of namespace
106