]> Creatis software - clitk.git/blob - itk/clitkBoundingBoxUtils.txx
Added more options to clitkImageUncertainty, default should be identical
[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 ComputeBBIntersection(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                              int dimension) {
74     typedef itk::BoundingBox<unsigned long, Dimension> BBType;
75     typedef typename BBType::PointType PointType;
76     PointType lastPoint;
77     PointType firstPoint;
78     firstPoint[dimension] = std::max(bbi1->GetMinimum()[dimension], bbi2->GetMinimum()[dimension]);
79     lastPoint[dimension] = std::min(bbi1->GetMaximum()[dimension], bbi2->GetMaximum()[dimension]);
80     bbo->SetMaximum(lastPoint);
81     bbo->SetMinimum(firstPoint);
82   }
83   //--------------------------------------------------------------------
84
85
86   //--------------------------------------------------------------------
87   template<int Dimension>
88   void ComputeBBUnion(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
89                       typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
90                       typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2) {
91     typedef itk::BoundingBox<unsigned long, Dimension> BBType;
92     typedef typename BBType::PointType PointType;
93     PointType lastPoint;
94     PointType firstPoint;
95     for(unsigned int i=0; i<Dimension; i++) {
96       firstPoint[i] = std::min(bbi1->GetMinimum()[i], bbi2->GetMinimum()[i]);
97       lastPoint[i] = std::max(bbi1->GetMaximum()[i], bbi2->GetMaximum()[i]);
98     }
99     bbo->SetMaximum(lastPoint);
100     bbo->SetMinimum(firstPoint);
101   }
102   //--------------------------------------------------------------------
103
104
105   //--------------------------------------------------------------------
106   template<class ImageType>
107   void ComputeRegionFromBB(const ImageType * image, 
108                            const typename itk::BoundingBox<unsigned long, 
109                                                            ImageType::ImageDimension>::Pointer bb, 
110                            typename ImageType::RegionType & region) {
111     // Types
112     typedef typename ImageType::IndexType  IndexType;
113     typedef typename ImageType::PointType  PointType;
114     typedef typename ImageType::RegionType RegionType;
115     typedef typename ImageType::SizeType   SizeType;
116
117     // Region starting point
118     IndexType regionStart;
119     PointType start = bb->GetMinimum();
120     image->TransformPhysicalPointToIndex(start, regionStart);
121     
122     // Region size
123     SizeType regionSize;
124     PointType maxs = bb->GetMaximum();
125     PointType mins = bb->GetMinimum();
126     for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
127       regionSize[i] = lrint((maxs[i] - mins[i])/image->GetSpacing()[i]);
128     }
129    
130     // Create region
131     region.SetIndex(regionStart);
132     region.SetSize(regionSize);
133   }
134   //--------------------------------------------------------------------
135
136
137 } // end of namespace
138