Orfeo Toolbox  3.16
itkSceneSpatialObject.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkSceneSpatialObject.txx,v $
5  Language: C++
6  Date: $Date: 2007-01-28 19:24:56 $
7  Version: $Revision: 1.14 $
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  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkSceneSpatialObject_txx
18 #define __itkSceneSpatialObject_txx
19 
20 #include "itkSceneSpatialObject.h"
21 #include <algorithm>
22 
23 namespace itk
24 {
25 
27 template <unsigned int TSpaceDimension>
30 {
31 }
32 
34 template <unsigned int TSpaceDimension>
37 {
38 }
39 
41 template <unsigned int TSpaceDimension>
42 void
45 {
46  m_Objects.push_back( pointer );
47  this->Modified();
48 }
49 
51 template <unsigned int TSpaceDimension>
52 void
55 {
56  typename ObjectListType::iterator it;
57  it = std::find(m_Objects.begin(),m_Objects.end(),pointer);
58 
59  if( it != m_Objects.end() )
60  {
61  if( *it == pointer )
62  {
63  m_Objects.erase( it );
64  this->Modified();
65  }
66  }
67  else
68  {
69  //throw an exception object to let user know that
70  // he tried to remove an object
71  // which is not in the list of the children.
72  }
73 }
74 
75 
77 template <unsigned int TSpaceDimension>
78 unsigned long
80 ::GetMTime( void ) const
81 {
82  typename ObjectListType::const_iterator it = m_Objects.begin();
83  typename ObjectListType::const_iterator itEnd = m_Objects.end();
84 
85  unsigned long latestTime = Superclass::GetMTime();
86  unsigned long localTime;
87  while(it!=itEnd)
88  {
89  localTime = (*it)->GetMTime();
90  if( localTime > latestTime )
91  {
92  latestTime = localTime;
93  }
94  it++;
95  }
96  return latestTime;
97 }
98 
100 template <unsigned int TSpaceDimension>
103 ::GetObjects( unsigned int depth, char * name )
104 {
105  ObjectListType * newList = new ObjectListType;
106 
107  typename ObjectListType::const_iterator it = m_Objects.begin();
108  typename ObjectListType::const_iterator itEnd = m_Objects.end();
109 
110  while(it != itEnd)
111  {
112  if(name == NULL || strstr(typeid(**it).name(), name))
113  {
114  newList->push_back(*it);
115  }
116  if(depth>0)
117  {
119  ChildListType;
120  ChildListType * childList =
121  dynamic_cast<SpatialObject<TSpaceDimension> *>((*it).GetPointer())->
122  GetChildren(depth-1, name);
123  typename ChildListType::const_iterator cIt = childList->begin();
124  typename ChildListType::const_iterator cItEnd = childList->end();
125 
126  while(cIt != cItEnd)
127  {
128  newList->push_back(dynamic_cast< ObjectType * >((*cIt).GetPointer()));
129  cIt++;
130  }
131 
132  delete childList;
133  }
134  it++;
135  }
136 
137  return newList;
138 }
139 
141 template <unsigned int TSpaceDimension>
142 void
145 {
146  m_Objects = children;
147 }
148 
150 template <unsigned int TSpaceDimension>
151 unsigned int
153 ::GetNumberOfObjects( unsigned int depth, char * name )
154 {
155  typename ObjectListType::const_iterator it = m_Objects.begin();
156  typename ObjectListType::const_iterator itEnd = m_Objects.end();
157 
158  unsigned int cnt = 0;
159  while(it != itEnd)
160  {
161  if(name == NULL || strstr(typeid(**it).name(), name))
162  {
163  cnt++;
164  }
165  it++;
166  }
167 
168  it = m_Objects.begin();
169  itEnd = m_Objects.end();
170  if( depth > 0 )
171  {
172  while(it != itEnd)
173  {
174  cnt +=
175  (dynamic_cast<SpatialObject<TSpaceDimension> * >((*it).GetPointer()))->
176  GetNumberOfChildren( depth-1, name );
177  it++;
178  }
179  }
180 
181  return cnt;
182 }
183 
185 template <unsigned int TSpaceDimension>
186 void
188 ::PrintSelf( std::ostream& os, Indent indent ) const
189 {
190  os << indent << "Number of objects: "
191  << m_Objects.size() << std::endl;
192  os << indent << "List of objects: ";
193 
194  typename ObjectListType::const_iterator it = m_Objects.begin();
195  typename ObjectListType::const_iterator itEnd = m_Objects.end();
196 
197  while(it != itEnd)
198  {
199  os << "[" << (*it) << "] ";
200  it++;
201  }
202  os << std::endl;
203 
204  Superclass::PrintSelf(os, indent);
205 }
206 
209 template <unsigned int TSpaceDimension>
213 {
214  typename ObjectListType::iterator it = m_Objects.begin();
215  typename ObjectListType::iterator itEnd = m_Objects.end();
216 
217  typedef typename SpatialObjectType::ChildrenListType ChildListType;
218  ChildListType * cList;
219  typename ChildListType::iterator cIt;
220  typename ChildListType::iterator cItEnd;
221 
222  while( it != itEnd )
223  {
224  if( (*it)->GetId() == Id )
225  {
226  return *it;
227  }
228  else
229  {
230  //cList = (dynamic_cast<SpatialObject<TSpaceDimension> *>(*it))->
231  // GetChildren(SpatialObjectType::MaximumDepth);
232  cList = (*it)->GetChildren(SpatialObjectType::MaximumDepth);
233  cIt = cList->begin();
234  cItEnd = cList->end();
235  while(cIt != cItEnd)
236  {
237  if( (*cIt)->GetId() == Id )
238  {
240  tmp = *cIt;
241  delete cList;
242  return tmp;
243  }
244  cIt++;
245  }
246 
247  delete cList;
248  }
249 
250  it++;
251  }
252 
253  return NULL;
254 }
255 template <unsigned int TSpaceDimension>
256 bool
259 {
260  typename ObjectListType::iterator it = m_Objects.begin();
261  typename ObjectListType::iterator oldIt;
262  typename ObjectListType::iterator itEnd = m_Objects.end();
263 
264  bool ret = true;
265  while( it != itEnd)
266  {
267  int pID = (*it)->GetParentId();
268  if(pID >= 0)
269  {
271  static_cast<SpatialObject<TSpaceDimension> * >
272  (this->GetObjectById(pID));
273  if(pObj == NULL)
274  {
275  ret = false;
276  it++;
277  }
278  else
279  {
281  ((*it).GetPointer()));
282  oldIt = it;
283  it++;
284  m_Objects.erase( oldIt );
285  }
286  }
287  else
288  {
289  it++;
290  }
291  }
292 
293  return ret;
294 }
295 
296 
298 template <unsigned int TSpaceDimension>
299 bool
302 {
303  typename ObjectListType::iterator it = m_Objects.begin();
304  typename ObjectListType::iterator itEnd = m_Objects.end();
305 
306  bool ret = true;
307  while( it != itEnd)
308  {
309  // For every object in the scene we check the ID validity
310  typename ObjectType::ChildrenListType* children = (*it)->GetChildren();
311  typename ObjectType::ChildrenListType::const_iterator
312  itChild = children->begin();
313 
314  while( itChild != children->end())
315  {
316  if( (*itChild)->HasParent())
317  {
318  if((*itChild)->GetParent()->GetId()<0)
319  {
320  delete children;
321  return false;
322  }
323  }
324  itChild++;
325  }
326  delete children;
327  it++;
328  }
329  return ret;
330 }
331 
332 template <unsigned int TSpaceDimension>
333 void
336 {
337  typename ObjectListType::iterator it = m_Objects.begin();
338  typename ObjectListType::iterator itEnd = m_Objects.end();
339 
340  while( it != itEnd)
341  {
342  // For every object in the scene we check the ID validity
343  typename ObjectType::ChildrenListType* children = (*it)->GetChildren();
344  typename ObjectType::ChildrenListType::iterator itChild = children->begin();
345 
346  while( itChild != children->end())
347  {
348  if( (*itChild)->HasParent())
349  {
350  if((*itChild)->GetParent()->GetId()<0)
351  {
352  (*itChild)->GetParent()->SetId(this->GetNextAvailableId());
353  }
354  }
355  itChild++;
356  }
357  delete children;
358  it++;
359  }
360 }
361 
362 
364 template <unsigned int TSpaceDimension>
365 int
368 {
369  int Id = 0;
370 
371  typename ObjectListType::iterator it = m_Objects.begin();
372  typename ObjectListType::iterator itEnd = m_Objects.end();
373 
374  while( it != itEnd)
375  {
376  typename ObjectType::ChildrenListType* children = (*it)->GetChildren();
377  typename ObjectType::ChildrenListType::iterator
378  itChild = children->begin();
379 
380  while( itChild != children->end())
381  {
382  if((*itChild)->GetId() >= Id)
383  {
384  Id = (*itChild)->GetId()+1;
385  }
386  itChild++;
387  }
388  delete children;
389  it++;
390  }
391  return Id;
392 }
393 
395 template <unsigned int TSpaceDimension>
396 void
399 {
400  m_Objects.clear();
401  this->Modified();
402 }
403 
404 
405 } // end of namespace itk
406 
407 #endif

Generated at Sun Feb 3 2013 00:05:20 for Orfeo Toolbox with doxygen 1.8.1.1