SimpleITK  1.1.0
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) SITK_NOEXCEPT;
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 
349  unsigned int m_NumberOfThreads;
350 
351  std::list<EventCommand> m_Commands;
352 
354 
355  //
357  };
358 
359 
360  }
361 }
362 #endif
itk::simple::Image
The main Image class for SimpleITK.
Definition: sitkImage.h:54
itk::ImageBase::GetBufferedRegion
virtual const RegionType & GetBufferedRegion() const
itk::simple::ProcessObject::Self
ProcessObject Self
Definition: sitkProcessObject.h:54
itk::simple::ProcessObject::EventCommand::m_Command
Command * m_Command
Definition: sitkProcessObject.h:230
itk::simple::ProcessObject::m_Commands
std::list< EventCommand > m_Commands
Definition: sitkProcessObject.h:351
SITK_NOEXCEPT
#define SITK_NOEXCEPT
Definition: sitkMacro.h:68
sitkTemplateFunctions.h
sitkNonCopyable.h
itk::simple::Image::GetITKBase
itk::DataObject * GetITKBase(void)
sitkCommon.h
itk::simple::EventEnum
EventEnum
Events which can be observed from ProcessObject.
Definition: sitkEvent.h:31
itk::VectorImage
Definition: sitkPixelIDTypes.h:26
sitkImage.h
itk::simple::Command
An implementation of the Command design pattern for callback.
Definition: sitkCommand.h:44
itk::simple::ProcessObject::m_ProgressMeasurement
float m_ProgressMeasurement
Definition: sitkProcessObject.h:356
itk::simple::ProcessObject::EventCommand::m_ITKTag
unsigned long m_ITKTag
Definition: sitkProcessObject.h:233
itk::simple::ProcessObject::m_NumberOfThreads
unsigned int m_NumberOfThreads
Definition: sitkProcessObject.h:349
itk::simple::ProcessObject::m_Debug
bool m_Debug
Definition: sitkProcessObject.h:347
sitkEvent.h
itk::simple::ProcessObject::EventCommand::operator==
bool operator==(const EventCommand &o) const
Definition: sitkProcessObject.h:235
itk::Command
itk::simple::ProcessObject::CastImageToITK
static TImageType::ConstPointer CastImageToITK(const Image &img)
Definition: sitkProcessObject.h:270
SITKCommon_EXPORT
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
itk::simple::NonCopyable
An inheratable class to disable copying of a class.
Definition: sitkNonCopyable.h:51
itk::Command
class ITK_FORWARD_EXPORT Command
itk::simple::ProcessObject::m_ActiveProcess
itk::ProcessObject * m_ActiveProcess
Definition: sitkProcessObject.h:353
itk
itk::ProcessObject
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
itk::simple::ProcessObject::EventCommand::m_Event
EventEnum m_Event
Definition: sitkProcessObject.h:229
itk::simple::ProcessObject
Base class for SimpleITK classes based on ProcessObject.
Definition: sitkProcessObject.h:50
itk::simple::ProcessObject::EventCommand
Definition: sitkProcessObject.h:224
itk::Image
Definition: sitkPixelIDTypes.h:25
itk::EventObject
itk::simple::ProcessObject::EventCommand::operator<
bool operator<(const EventCommand &o) const
Definition: sitkProcessObject.h:237
itk::ImageRegion::GetNumberOfPixels
SizeValueType GetNumberOfPixels() const
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(TImageType *img)
Definition: sitkProcessObject.h:283
sitkExceptionMacro
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:88
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
Definition: sitkProcessObject.h:291
itk::simple::ProcessObject::ToStringHelper
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
Definition: sitkProcessObject.h:323
itk::simple::ProcessObject::EventCommand::EventCommand
EventCommand(EventEnum e, Command *c)
Definition: sitkProcessObject.h:226