SimpleITK  1.0.1
sitkProcessObject.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 #ifndef sitkProcessObject_h
19 #define sitkProcessObject_h
20 
21 #include "sitkCommon.h"
22 #include "sitkNonCopyable.h"
23 #include "sitkTemplateFunctions.h"
24 #include "sitkEvent.h"
25 #include "sitkImage.h"
26 
27 #include <iostream>
28 #include <list>
29 
30 namespace itk {
31 
32 #ifndef SWIG
33 
34  template< typename T, unsigned int NVectorDimension > class Vector;
35 
36  class ProcessObject;
37  class Command;
38  class EventObject;
39 #endif
40 
41  namespace simple {
42 
43  class Command;
44 
45 
51  protected NonCopyable
52  {
53  public:
55 
60  ProcessObject();
61 
65  virtual ~ProcessObject();
66 
67  // Print ourselves out
68  virtual std::string ToString() const;
69 
71  virtual std::string GetName() const = 0;
72 
79  virtual void DebugOn();
80  virtual void DebugOff();
86  virtual bool GetDebug() const;
87  virtual void SetDebug(bool debugFlag);
95  static void GlobalDefaultDebugOn();
96  static void GlobalDefaultDebugOff();
100  static bool GetGlobalDefaultDebug();
101  static void SetGlobalDefaultDebug(bool debugFlag);
112  static void GlobalWarningDisplayOn();
113  static void GlobalWarningDisplayOff();
114  static void SetGlobalWarningDisplay(bool flag);
115  static bool GetGlobalWarningDisplay();
122  static void SetGlobalDefaultNumberOfThreads(unsigned int n);
123  static unsigned int GetGlobalDefaultNumberOfThreads();
138  static double GetGlobalDefaultCoordinateTolerance();
139  static void SetGlobalDefaultCoordinateTolerance(double );
140 
141  static double GetGlobalDefaultDirectionTolerance();
142  static void SetGlobalDefaultDirectionTolerance(double);
149  virtual void SetNumberOfThreads(unsigned int n);
150  virtual unsigned int GetNumberOfThreads() const;
179  virtual int AddCommand(itk::simple::EventEnum event, itk::simple::Command &cmd);
180 
186  virtual void RemoveAllCommands();
187 
189  virtual bool HasCommand( itk::simple::EventEnum event ) const;
190 
191 
201  virtual float GetProgress( ) const;
202 
218  virtual void Abort();
219 
220  protected:
221 
222  #ifndef SWIG
223 
225  {
227  : m_Event(e), m_Command(c), m_ITKTag(std::numeric_limits<unsigned long>::max())
228  {}
231 
232  // set to max if currently not registered
233  unsigned long m_ITKTag;
234 
235  inline bool operator==(const EventCommand &o) const
236  { return m_Command == o.m_Command; }
237  inline bool operator<(const EventCommand &o) const
238  { return m_Command < o.m_Command; }
239  };
240 
241  // method called before filter update to set parameters and
242  // connect commands.
243  virtual void PreUpdate( itk::ProcessObject *p );
244 
245  // overridable method to add a command, the return value is
246  // placed in the m_ITKTag of the EventCommand object.
247  virtual unsigned long AddITKObserver(const itk::EventObject &, itk::Command *);
248 
249  // overridable method to remove a command
250  virtual void RemoveITKObserver( EventCommand &e );
251 
252  // Create an ITK EventObject from the SimpleITK enumerated type.
253  static const itk::EventObject &GetITKEventObject(EventEnum e);
254 
255  // returns the current active process, if no active process then
256  // an exception is throw.
257  virtual itk::ProcessObject *GetActiveProcess( );
258 
259  // overidable callback when the active process has completed
260  virtual void OnActiveProcessDelete( );
261 
262  friend class itk::simple::Command;
263  // method call by command when it's deleted, maintains internal
264  // references between command and process objects.
265  virtual void onCommandDelete(const itk::simple::Command *cmd) throw();
266  #endif
267 
268 
269  template< class TImageType >
270  static typename TImageType::ConstPointer CastImageToITK( const Image &img )
271  {
272  typename TImageType::ConstPointer itkImage =
273  dynamic_cast < const TImageType* > ( img.GetITKBase() );
274 
275  if ( itkImage.IsNull() )
276  {
277  sitkExceptionMacro( "Unexpected template dispatch error!" );
278  }
279  return itkImage;
280  }
281 
282  template< class TImageType >
283  static Image CastITKToImage( TImageType *img )
284  {
285  return Image(img);
286  }
287 
288 #ifndef SWIG
289  template< class TPixelType, unsigned int VImageDimension, unsigned int VLength,
290  template<typename, unsigned int> class TVector >
291  static Image CastITKToImage( itk::Image< TVector< TPixelType, VLength >, VImageDimension> *img )
292  {
293  typedef itk::VectorImage< TPixelType, VImageDimension > VectorImageType;
294 
295  size_t numberOfElements = img->GetBufferedRegion().GetNumberOfPixels();
296  typename VectorImageType::InternalPixelType* buffer = reinterpret_cast<typename VectorImageType::InternalPixelType*>( img->GetPixelContainer()->GetBufferPointer() );
297 
298  // Unlike an image of Vectors a VectorImage's container is a
299  // container of TPixelType, whos size is the image's number of
300  // pixels * number of pixels per component
301  numberOfElements *= VImageDimension;
302 
303  typename VectorImageType::Pointer out = VectorImageType::New();
304 
305  // Set the image's pixel container to import the pointer provided.
306  out->GetPixelContainer()->SetImportPointer(buffer, numberOfElements, true );
307  img->GetPixelContainer()->ContainerManageMemoryOff();
308  out->CopyInformation( img );
309  out->SetRegions( img->GetBufferedRegion() );
310 
311  return Image(out.GetPointer());
312  }
313 #endif
314 
322  template <typename T>
323  static std::ostream & ToStringHelper(std::ostream &os, const T &v)
324  {
325  os << v;
326  return os;
327  }
328  static std::ostream & ToStringHelper(std::ostream &os, const char &v);
329  static std::ostream & ToStringHelper(std::ostream &os, const signed char &v);
330  static std::ostream & ToStringHelper(std::ostream &os, const unsigned char &v);
333  private:
334 
335  // Add command to active process object, the EventCommand's
336  // ITKTag must be unset as max or else an exception is
337  // thrown. The EventCommand's ITKTag is updated to the command
338  // registered to ITK's ProcessObject. It's assumed that there is
339  // an current active process
340  unsigned long AddObserverToActiveProcessObject( EventCommand &e );
341 
342  // Remove the command from the active processes. Its is assumed
343  // that an active process exists. The tag is set to max after it
344  // is removed.
345  void RemoveObserverFromActiveProcessObject( EventCommand &e );
346 
347  bool m_Debug;
348  unsigned int m_NumberOfThreads;
349 
350  std::list<EventCommand> m_Commands;
351 
353 
354  //
356  };
357 
358 
359  }
360 }
361 #endif
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
itk::DataObject * GetITKBase(void)
STL namespace.
SizeValueType GetNumberOfPixels() const
EventEnum
Events which can be observed from ProcessObject.
Definition: sitkEvent.h:31
class ITK_FORWARD_EXPORT ProcessObject
static Image CastITKToImage(TImageType *img)
itk::ProcessObject * m_ActiveProcess
An implementation of the Command design pattern for callback.
Definition: sitkCommand.h:44
std::list< EventCommand > m_Commands
static TImageType::ConstPointer CastImageToITK(const Image &img)
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
The main Image class for SimpleITK.
Definition: sitkImage.h:54
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
bool operator<(const EventCommand &o) const
bool operator==(const EventCommand &o) const
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:89
virtual const RegionType & GetBufferedRegion() const
Base class for SimpleITK classes based on ProcessObject.
An inheratable class to disable copying of a class.
class ITK_FORWARD_EXPORT Command