KWStyle - itkBoundingBox.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkBoundingBox.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:33 $
7   Version:   $Revision: 1.4 $
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 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 DEF #ifndef _itkBoundingBox_txx
21 DEF #define _itkBoundingBox_txx
22 #include "itkBoundingBox.h"
23 #include "itkNumericTraits.h"
24 #include "itkPoint.h"
25
26 namespace itk
27 {
28
29 /**
30  * Print out the bounding box.
31  */
32 template <typename TPointIdentifier, int VPointDimension,
33           typename TCoordRep, typename TPointsContainer>
34 void
35 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
36 ::PrintSelf(std::ostream& os, Indent indent) const
37 {
38   Superclass::PrintSelf(os, indent);
39
40 SEM   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
48 /**
49  * Access routine to set the points container.
50  */
51 template <typename TPointIdentifier, int VPointDimension,
52           typename TCoordRep, typename TPointsContainer>
53 void
54 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
55 ::SetPoints(const PointsContainer* points)
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
65 /** Access routine to get the points container. */
66 template <typename TPointIdentifier, int VPointDimension,
67           typename TCoordRep, typename TPointsContainer>
68 LEN const typename BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >::PointsContainer *
69 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
70 ::GetPoints(void) const
71 {
72   itkDebugMacro("returning Points container of " << m_PointsContainer );
73
74   return m_PointsContainer.GetPointer();
75 }
76
77 /** Compute and get the corners of the bounding box */
78 template <typename TPointIdentifier, int VPointDimension,
79           typename TCoordRep, typename TPointsContainer>
80 LEN const typename BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >::PointsContainer *
81 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
82 ::GetCorners(void)
83 {
84   m_CornersContainer->clear();
85
86   PointType center = this->GetCenter();
87   PointType radius;
88
89   for (unsigned int i=0; i<VPointDimension; i++)
90     {
91     radius[i] = m_Bounds[2*i+1]-center[i];
92     }
93     
94   for(unsigned int j=0;j<pow(2.0,(double)VPointDimension);j++)
95     {       
96     PointType pnt;
97     for(unsigned int i=0; i<VPointDimension;i++)
98       {
99 LEN       pnt[i]=center[i]+pow(-1.0,((double)(j/(int(pow(2.0,(double)i))))))*radius[i];
100       }
101     m_CornersContainer->push_back(pnt);   
102     }
103
104   return m_CornersContainer.GetPointer();
105 }
106
107 /******************************************************************************
108 IND ** PROTECTED METHOD DEFINITIONS
109 IND ******************************************************************************/
110 template <typename TPointIdentifier, int VPointDimension,
111           typename TCoordRep, typename TPointsContainer>
112 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
113 ::BoundingBox():
114 IND **m_PointsContainer(NULL)
115 {
116   m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
117   m_CornersContainer = PointsContainer::New();
118 }
119
120 template <typename TPointIdentifier, int VPointDimension,
121           typename TCoordRep, typename TPointsContainer>
122 BoundingBox<TPointIdentifier , VPointDimension, TCoordRep, TPointsContainer >
123 ::~BoundingBox()
124 {
125 }
126
127 template <typename TPointIdentifier, int VPointDimension,
128           typename TCoordRep, typename TPointsContainer>
129 bool  
130 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
131 ::ComputeBoundingBox(void) const
132 {
133   if ( !m_PointsContainer )
134     {
135     if ( this->GetMTime() > m_BoundsMTime )
136       {
137       m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
138       m_BoundsMTime.Modified();
139       }
140     return false;
141     }
142
143   if ( this->GetMTime() > m_BoundsMTime )
144     {
145     //iterate over points determining min/max
146     //start by initializing the values
147     if(m_PointsContainer->Size() < 1)
148       {
149       m_Bounds.Fill( NumericTraits< CoordRepType >::Zero );
150       m_BoundsMTime.Modified();
151       return false;
152       }
153
154     PointsContainerConstIterator ci = m_PointsContainer->Begin();
155     Point< TCoordRep, VPointDimension>   point = ci->Value();     //point value
156     for (unsigned int i=0; i < PointDimension; i++)
157       {
158       m_Bounds[2*i  ] = point[i];
159       m_Bounds[2*i+1] = point[i];
160       }
161     ++ci;
162     
163     //use a const iterator to grab the points and compute
164     //the bounding box.
165     while( ci != m_PointsContainer->End() )
166       {
167       point = ci->Value();     //point value
168       for (unsigned int i=0; i<PointDimension; i++)
169         {
170         if ( point[i] < m_Bounds[2*i] )
171           {
172           m_Bounds[2*i] = point[i];
173           }
174         if ( point[i] > m_Bounds[2*i+1] )
175           {
176           m_Bounds[2*i+1] = point[i];
177           }
178         }
179       ++ci;
180       }//for all points in container
181
182     m_BoundsMTime.Modified();
183     }
184
185   return true;
186 }
187
188
189 template <typename TPointIdentifier, int VPointDimension,
190           typename TCoordRep, typename TPointsContainer>
191 LEN typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>::PointType 
192 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
193 ::GetCenter(void) const
194 {
195   this->ComputeBoundingBox();
196
197   PointType center;
198   for (unsigned int i=0; i<PointDimension; i++)
199     {
200     center[i] = (m_Bounds[2*i] + m_Bounds[2*i+1]) / 2.0;
201     }
202
203   return center;
204 }
205
206 template <typename TPointIdentifier, int VPointDimension,
207           typename TCoordRep, typename TPointsContainer>
208 LEN typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>::PointType 
209 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
210 ::GetMinimum(void) const
211 {
212   this->ComputeBoundingBox();
213
214   PointType minimum;
215   for (unsigned int i=0; i<PointDimension; i++)
216     {
217     minimum[i] = m_Bounds[2*i];
218     }
219
220   return minimum;
221 }
222
223 template <typename TPointIdentifier, int VPointDimension,
224           typename TCoordRep, typename TPointsContainer>
225 void
226 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
227 ::SetMinimum(const PointType & point)
228 {
229   for (unsigned int i=0; i<PointDimension; i++)
230     {
231     m_Bounds[2*i] = point[i];
232     }
233
234   m_BoundsMTime.Modified();
235 }
236
237 template <typename TPointIdentifier, int VPointDimension,
238           typename TCoordRep, typename TPointsContainer>
239 LEN typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>::PointType 
240 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
241 ::GetMaximum(void) const
242 {
243   this->ComputeBoundingBox();
244
245   PointType maximum;
246   for (unsigned int i=0; i<PointDimension; i++)
247     {
248     maximum[i] = m_Bounds[2*i+1];
249     }
250
251   return maximum;
252 }
253
254 template <typename TPointIdentifier, int VPointDimension,
255           typename TCoordRep, typename TPointsContainer>
256 void
257 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
258 ::SetMaximum(const PointType & point)
259 {
260   for (unsigned int i=0; i<PointDimension; i++)
261     {
262     m_Bounds[2*i+1] = point[i];
263     }
264
265   m_BoundsMTime.Modified();
266 }
267
268 template <typename TPointIdentifier, int VPointDimension,
269           typename TCoordRep, typename TPointsContainer>
270 void
271 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
272 ::ConsiderPoint( const PointType & point )
273 {
274   bool changed = false;
275   for (unsigned int i=0; i<PointDimension; i++)
276     {
277     if ( point[i] < m_Bounds[2*i] )
278       {
279       m_Bounds[2*i] = point[i];
280       changed = true;
281       }
282     if ( point[i] > m_Bounds[2*i+1] )
283       {
284       m_Bounds[2*i+1] = point[i];
285       changed = true;
286       }
287     }
288
289   if(changed)
290     {
291     m_BoundsMTime.Modified();
292     }
293 }
294
295 template <typename TPointIdentifier, int VPointDimension,
296           typename TCoordRep, typename TPointsContainer>
297 LEN typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>::AccumulateType 
298 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
299 ::GetDiagonalLength2(void) const
300 {
301   typename NumericTraits<CoordRepType>::AccumulateType
302 IND ****dist2 = NumericTraits<CoordRepType>::Zero;
303
304   if ( this->ComputeBoundingBox() )
305     {
306     for (unsigned int i=0; i<PointDimension; i++)
307       {
308       dist2 += (m_Bounds[2*i]-m_Bounds[2*i+1]) * 
309         (m_Bounds[2*i]-m_Bounds[2*i+1]);
310       }
311     }
312
313   return dist2;
314 }
315
316
317 EML
318 EML
319 EML
320 template <typename TPointIdentifier, int VPointDimension,
321           typename TCoordRep, typename TPointsContainer>
322 bool
323 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
324 ::IsInside( const PointType & point ) const
325 {
326   unsigned int j = 0; 
327   unsigned int i = 0;
328   while( i<PointDimension )
329     {
330     if( point[i] < m_Bounds[j++] )
331       {
332       return false;
333       }
334     if( point[i] > m_Bounds[j++] )
335       {
336       return false;
337       }
338     i++;
339     }
340   return true;
341 }
342
343 template <typename TPointIdentifier, int VPointDimension,
344           typename TCoordRep, typename TPointsContainer>
345 unsigned long
346 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
347 ::GetMTime( void ) const
348 {
349   unsigned long latestTime = Object::GetMTime(); 
350   if( m_PointsContainer )
351     {
352     if( latestTime < m_PointsContainer->GetMTime() )
353       {
354       latestTime = m_PointsContainer->GetMTime();
355       }
356     }
357   return latestTime;
358 }
359
360 template <typename TPointIdentifier, int VPointDimension,
361           typename TCoordRep, typename TPointsContainer>
362 LEN typename BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>::Pointer 
363 BoundingBox<TPointIdentifier,VPointDimension,TCoordRep,TPointsContainer>
364 ::DeepCopy( void ) const
365 {
366   Pointer clone = Self::New();
367
368   // Connect the same points container into the clone
369   clone->SetPoints( this->m_PointsContainer );
370   
371   // Copy the corners into the clone.
372   clone->m_CornersContainer->clear();
373   
374   PointsContainerConstIterator itr = this->m_CornersContainer->Begin();
375   PointsContainerConstIterator end = this->m_CornersContainer->End();
376   
377   clone->m_CornersContainer->Reserve( this->m_CornersContainer->Size() );
378   PointsContainerIterator dest = clone->m_CornersContainer->Begin();
379     
380   while( itr != end )
381     {
382     dest.Value() = itr.Value();
383     ++itr;
384     }
385
386   // Copy the bounds into the clone
387   for( unsigned int i=0; i < 2*PointDimension; i++ )
388     {
389     clone->m_Bounds[i] = this->m_Bounds[i];
390     }
391
392   return clone;
393 }
394
395
396 // end namespace itk
397
398 #endif
399

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:00PM
© Kitware Inc.