Orfeo Toolbox  3.16
itkBoundingBox.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkBoundingBox.txx,v $
5  Language: C++
6  Date: $Date: 2009-04-05 10:56:39 $
7  Version: $Revision: 1.40 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  Portions of this code are covered under the VTK copyright.
13  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14 
15  This software is distributed WITHOUT ANY WARRANTY; without even
16  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  PURPOSE. See the above copyright notices for more information.
18 
19 =========================================================================*/
20 #ifndef __itkBoundingBox_txx
21 #define __itkBoundingBox_txx
22 #include "itkBoundingBox.h"
23 #include "itkNumericTraits.h"
24 #include "itkPoint.h"
25 
26 namespace itk
27 {
28 
32 template <typename TPointIdentifier, int VPointDimension,
33  typename TCoordRep, typename TPointsContainer>
34 void
36 ::PrintSelf(std::ostream& os, Indent indent) const
37 {
38  Superclass::PrintSelf(os, indent);
39 
40  os << indent << "Bounding Box: ( ";
41  for (unsigned int i=0; i<PointDimension; i++)
42  {
43  os << m_Bounds[2*i] << "," << m_Bounds[2*i+1] << " ";
44  }
45  os << " )" << std::endl;
46 }
47 
51 template <typename TPointIdentifier, int VPointDimension,
52  typename TCoordRep, typename TPointsContainer>
53 void
56 {
57  itkDebugMacro("setting Points container to " << points);
58  if(m_PointsContainer.GetPointer() != points)
59  {
60  m_PointsContainer = points;
61  this->Modified();
62  }
63 }
64 
66 template <typename TPointIdentifier, int VPointDimension,
67  typename TCoordRep, typename TPointsContainer>
68 const typename BoundingBox<TPointIdentifier , VPointDimension, TCoordRep,
69  TPointsContainer >::PointsContainer *
71 ::GetPoints(void) const
72 {
73  itkDebugMacro("returning Points container of " << m_PointsContainer );
74 
75  return m_PointsContainer.GetPointer();
76 }
77 
79 template <typename TPointIdentifier, int VPointDimension,
80  typename TCoordRep, typename TPointsContainer>
81 const typename BoundingBox<TPointIdentifier , VPointDimension, TCoordRep,
82  TPointsContainer >::PointsContainer *
85 {
86  m_CornersContainer->clear();
87 
88  PointType center = this->GetCenter();
89  PointType radius;
90 
91  for (unsigned int i=0; i<VPointDimension; i++)
92  {
93  radius[i] = m_Bounds[2*i+1]-center[i];
94  }
95 
96  for(unsigned int j=0;j<vcl_pow(2.0,(double)VPointDimension);j++)
97  {
98  PointType pnt;
99  for(unsigned int i=0; i<VPointDimension;i++)
100  {
101  pnt[i]=center[i]+vcl_pow(-1.0,((double)(j/(int(vcl_pow(2.0,(double)i))))))
102  *radius[i];
103  }
104 
105  // Push back is not defined so we insert at the end of the list
106  m_CornersContainer->InsertElement(m_CornersContainer->Size(),pnt);
107 
108  }
109 
110  return m_CornersContainer.GetPointer();
111 }
112 
114 template <typename TPointIdentifier, int VPointDimension,
115  typename TCoordRep, typename TPointsContainer>
117 ::BoundingBox(): m_PointsContainer(NULL)
118 {
119  m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
120  m_CornersContainer = PointsContainer::New();
121 }
122 
123 template <typename TPointIdentifier, int VPointDimension,
124  typename TCoordRep, typename TPointsContainer>
127 {
128 }
129 
130 template <typename TPointIdentifier, int VPointDimension,
131  typename TCoordRep, typename TPointsContainer>
132 bool
135 {
136  if ( !m_PointsContainer )
137  {
138  if ( this->GetMTime() > m_BoundsMTime )
139  {
140  m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
141  m_BoundsMTime.Modified();
142  }
143  return false;
144  }
145 
146  if ( this->GetMTime() > m_BoundsMTime )
147  {
148  //iterate over points determining min/max
149  //start by initializing the values
150  if(m_PointsContainer->Size() < 1)
151  {
152  m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
153  m_BoundsMTime.Modified();
154  return false;
155  }
156 
157  PointsContainerConstIterator ci = m_PointsContainer->Begin();
158  Point< TCoordRep, VPointDimension> point = ci->Value(); //point value
159  for (unsigned int i=0; i < PointDimension; i++)
160  {
161  m_Bounds[2*i ] = point[i];
162  m_Bounds[2*i+1] = point[i];
163  }
164  ++ci;
165 
166  //use a const iterator to grab the points and compute
167  //the bounding box.
168  while( ci != m_PointsContainer->End() )
169  {
170  point = ci->Value(); //point value
171  for (unsigned int i=0; i<PointDimension; i++)
172  {
173  if ( point[i] < m_Bounds[2*i] )
174  {
175  m_Bounds[2*i] = point[i];
176  }
177  if ( point[i] > m_Bounds[2*i+1] )
178  {
179  m_Bounds[2*i+1] = point[i];
180  }
181  }
182  ++ci;
183  }//for all points in container
184 
185  m_BoundsMTime.Modified();
186  }
187 
188  return true;
189 }
190 
191 
192 template <typename TPointIdentifier, int VPointDimension,
193  typename TCoordRep, typename TPointsContainer>
194 typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,
195  TPointsContainer>::PointType
197 ::GetCenter(void) const
198 {
199  this->ComputeBoundingBox();
200 
201  PointType center;
202  for (unsigned int i=0; i<PointDimension; i++)
203  {
204  center[i] = (m_Bounds[2*i] + m_Bounds[2*i+1]) / 2.0;
205  }
206 
207  return center;
208 }
209 
210 template <typename TPointIdentifier, int VPointDimension,
211  typename TCoordRep, typename TPointsContainer>
212 typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,
213  TPointsContainer>::PointType
215 ::GetMinimum(void) const
216 {
217  this->ComputeBoundingBox();
218 
219  PointType minimum;
220  for (unsigned int i=0; i<PointDimension; i++)
221  {
222  minimum[i] = m_Bounds[2*i];
223  }
224 
225  return minimum;
226 }
227 
228 template <typename TPointIdentifier, int VPointDimension,
229  typename TCoordRep, typename TPointsContainer>
230 void
232 ::SetMinimum(const PointType & point)
233 {
234  for (unsigned int i=0; i<PointDimension; i++)
235  {
236  m_Bounds[2*i] = point[i];
237  }
238 
239  m_BoundsMTime.Modified();
240 }
241 
242 template <typename TPointIdentifier, int VPointDimension,
243  typename TCoordRep, typename TPointsContainer>
244 typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,
245  TPointsContainer>::PointType
247 ::GetMaximum(void) const
248 {
249  this->ComputeBoundingBox();
250 
251  PointType maximum;
252  for (unsigned int i=0; i<PointDimension; i++)
253  {
254  maximum[i] = m_Bounds[2*i+1];
255  }
256 
257  return maximum;
258 }
259 
260 template <typename TPointIdentifier, int VPointDimension,
261  typename TCoordRep, typename TPointsContainer>
262 void
264 ::SetMaximum(const PointType & point)
265 {
266  for (unsigned int i=0; i<PointDimension; i++)
267  {
268  m_Bounds[2*i+1] = point[i];
269  }
270 
271  m_BoundsMTime.Modified();
272 }
273 
274 template <typename TPointIdentifier, int VPointDimension,
275  typename TCoordRep, typename TPointsContainer>
276 void
278 ::ConsiderPoint( const PointType & point )
279 {
280  bool changed = false;
281  for (unsigned int i=0; i<PointDimension; i++)
282  {
283  if ( point[i] < m_Bounds[2*i] )
284  {
285  m_Bounds[2*i] = point[i];
286  changed = true;
287  }
288  if ( point[i] > m_Bounds[2*i+1] )
289  {
290  m_Bounds[2*i+1] = point[i];
291  changed = true;
292  }
293  }
294 
295  if(changed)
296  {
297  m_BoundsMTime.Modified();
298  }
299 }
300 
301 template <typename TPointIdentifier, int VPointDimension,
302  typename TCoordRep, typename TPointsContainer>
303 typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,
304  TPointsContainer>::AccumulateType
307 {
308  typename NumericTraits<CoordRepType>::AccumulateType
309  dist2 = NumericTraits<CoordRepType>::Zero;
310 
311  if ( this->ComputeBoundingBox() )
312  {
313  for (unsigned int i=0; i<PointDimension; i++)
314  {
315  dist2 += (m_Bounds[2*i]-m_Bounds[2*i+1]) *
316  (m_Bounds[2*i]-m_Bounds[2*i+1]);
317  }
318  }
319 
320  return dist2;
321 }
322 
323 
324 template <typename TPointIdentifier, int VPointDimension,
325  typename TCoordRep, typename TPointsContainer>
326 bool
328 ::IsInside( const PointType & point ) const
329 {
330  unsigned int j = 0;
331  unsigned int i = 0;
332  while( i<PointDimension )
333  {
334  if( point[i] < m_Bounds[j++] )
335  {
336  return false;
337  }
338  if( point[i] > m_Bounds[j++] )
339  {
340  return false;
341  }
342  i++;
343  }
344  return true;
345 }
346 
347 template <typename TPointIdentifier, int VPointDimension,
348  typename TCoordRep, typename TPointsContainer>
349 unsigned long
351 ::GetMTime( void ) const
352 {
353  unsigned long latestTime = Object::GetMTime();
354  if( m_PointsContainer )
355  {
356  if( latestTime < m_PointsContainer->GetMTime() )
357  {
358  latestTime = m_PointsContainer->GetMTime();
359  }
360  }
361  return latestTime;
362 }
363 
364 template <typename TPointIdentifier, int VPointDimension,
365  typename TCoordRep, typename TPointsContainer>
366 typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,
367  TPointsContainer>::Pointer
369 ::DeepCopy( void ) const
370 {
371  Pointer clone = Self::New();
372 
373  // Connect the same points container into the clone
374  clone->SetPoints( this->m_PointsContainer );
375 
376  // Copy the corners into the clone.
377  clone->m_CornersContainer->clear();
378 
379  PointsContainerConstIterator itr = this->m_CornersContainer->Begin();
380  PointsContainerConstIterator end = this->m_CornersContainer->End();
381 
382  clone->m_CornersContainer->Reserve( this->m_CornersContainer->Size() );
383  PointsContainerIterator dest = clone->m_CornersContainer->Begin();
384 
385  while( itr != end )
386  {
387  dest.Value() = itr.Value();
388  ++itr;
389  }
390 
391  // Copy the bounds into the clone
392  for( unsigned int i=0; i < 2*PointDimension; i++ )
393  {
394  clone->m_Bounds[i] = this->m_Bounds[i];
395  }
396 
397  return clone;
398 }
399 
400 
401 } // end namespace itk
402 
403 #endif

Generated at Sat Feb 2 2013 23:30:23 for Orfeo Toolbox with doxygen 1.8.1.1