]> Creatis software - clitk.git/blob - itk/clitkBoundingBoxUtils.txx
Add ComputeBBUnion
[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     typedef itk::BoundingBox<unsigned long, Dimension> BBType;
55     typedef typename BBType::PointType PointType;
56     PointType lastPoint;
57     PointType firstPoint;
58     for(unsigned int i=0; i<Dimension; i++) {
59       firstPoint[i] = std::max(bbi1->GetMinimum()[i], bbi2->GetMinimum()[i]);
60       lastPoint[i] = std::min(bbi1->GetMaximum()[i], bbi2->GetMaximum()[i]);
61     }
62     bbo->SetMaximum(lastPoint);
63     bbo->SetMinimum(firstPoint);
64   }
65   //--------------------------------------------------------------------
66
67
68   ///--------------------------------------------------------------------
69   template<int Dimension>
70   void ComputeBBUnion(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
71                       typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
72                       typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2) {
73     typedef itk::BoundingBox<unsigned long, Dimension> BBType;
74     typedef typename BBType::PointType PointType;
75     PointType lastPoint;
76     PointType firstPoint;
77     for(unsigned int i=0; i<Dimension; i++) {
78       firstPoint[i] = std::min(bbi1->GetMinimum()[i], bbi2->GetMinimum()[i]);
79       lastPoint[i] = std::max(bbi1->GetMaximum()[i], bbi2->GetMaximum()[i]);
80     }
81     bbo->SetMaximum(lastPoint);
82     bbo->SetMinimum(firstPoint);
83   }
84   //--------------------------------------------------------------------
85
86
87   //--------------------------------------------------------------------
88   template<class ImageType>
89   void ComputeRegionFromBB(const ImageType * image, 
90                            const typename itk::BoundingBox<unsigned long, 
91                                                            ImageType::ImageDimension>::Pointer bb, 
92                            typename ImageType::RegionType & region) {
93     // Types
94     typedef typename ImageType::IndexType  IndexType;
95     typedef typename ImageType::PointType  PointType;
96     typedef typename ImageType::RegionType RegionType;
97     typedef typename ImageType::SizeType   SizeType;
98
99     // Region starting point
100     IndexType regionStart;
101     PointType start = bb->GetMinimum();
102     image->TransformPhysicalPointToIndex(start, regionStart);
103     
104     // Region size
105     SizeType regionSize;
106     PointType maxs = bb->GetMaximum();
107     PointType mins = bb->GetMinimum();
108     for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
109       regionSize[i] = lrint((maxs[i] - mins[i])/image->GetSpacing()[i]);
110     }
111    
112     // Create region
113     region.SetIndex(regionStart);
114     region.SetSize(regionSize);
115   }
116   //--------------------------------------------------------------------
117
118
119 } // end of namespace
120