SimpleITK  2.0.0
sitkProcessObject.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright NumFOCUS
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 #include "sitkImageConvert.h"
27 
28 #include <iostream>
29 #include <list>
30 
31 namespace itk {
32 
33 #ifndef SWIG
34 
35  template< typename T, unsigned int NVectorDimension > class Vector;
36 
37  class ProcessObject;
38  class Command;
39  class EventObject;
40 #endif
41 
42  namespace simple {
43 
44  class Command;
45 
46 
52  protected NonCopyable
53  {
54  public:
56 
61  ProcessObject();
62 
66  virtual ~ProcessObject();
67 
68  // Print ourselves out
69  virtual std::string ToString() const;
70 
72  virtual std::string GetName() const = 0;
73 
80  virtual void DebugOn();
81  virtual void DebugOff();
87  virtual bool GetDebug() const;
88  virtual void SetDebug(bool debugFlag);
96  static void GlobalDefaultDebugOn();
97  static void GlobalDefaultDebugOff();
101  static bool GetGlobalDefaultDebug();
102  static void SetGlobalDefaultDebug(bool debugFlag);
113  static void GlobalWarningDisplayOn();
114  static void GlobalWarningDisplayOff();
115  static void SetGlobalWarningDisplay(bool flag);
116  static bool GetGlobalWarningDisplay();
131  static double GetGlobalDefaultCoordinateTolerance();
132  static void SetGlobalDefaultCoordinateTolerance(double );
133 
134  static double GetGlobalDefaultDirectionTolerance();
135  static void SetGlobalDefaultDirectionTolerance(double);
162  static bool SetGlobalDefaultThreader(const std::string &threader);
163  static std::string GetGlobalDefaultThreader();
179  static void SetGlobalDefaultNumberOfThreads(unsigned int n);
180  static unsigned int GetGlobalDefaultNumberOfThreads();
197  virtual void SetNumberOfThreads(unsigned int n);
198  virtual unsigned int GetNumberOfThreads() const;
211  virtual void SetNumberOfWorkUnits(unsigned int n);
212  virtual unsigned int GetNumberOfWorkUnits() const;
242  virtual int AddCommand(itk::simple::EventEnum event, itk::simple::Command &cmd);
243 
244  #ifndef SWIG
245 
250  virtual int AddCommand(itk::simple::EventEnum event, const std::function<void()> &func);
251  #endif
252 
258  virtual void RemoveAllCommands();
259 
261  virtual bool HasCommand( itk::simple::EventEnum event ) const;
262 
263 
273  virtual float GetProgress( ) const;
274 
290  virtual void Abort();
291 
292  protected:
293 
294  #ifndef SWIG
295 
297  {
299  : m_Event(e), m_Command(c), m_ITKTag(std::numeric_limits<unsigned long>::max())
300  {}
303 
304  // set to max if currently not registered
305  unsigned long m_ITKTag;
306 
307  inline bool operator==(const EventCommand &o) const
308  { return m_Command == o.m_Command; }
309  inline bool operator<(const EventCommand &o) const
310  { return m_Command < o.m_Command; }
311  };
312 
313  // method called before filter update to set parameters and
314  // connect commands.
315  virtual void PreUpdate( itk::ProcessObject *p );
316 
317  // overridable method to add a command, the return value is
318  // placed in the m_ITKTag of the EventCommand object.
319  virtual unsigned long AddITKObserver(const itk::EventObject &, itk::Command *);
320 
321  // overridable method to remove a command
322  virtual void RemoveITKObserver( EventCommand &e );
323 
324  // Create an ITK EventObject from the SimpleITK enumerated type.
325  static const itk::EventObject &GetITKEventObject(EventEnum e);
326 
327  // returns the current active process, if no active process then
328  // an exception is throw.
329  virtual itk::ProcessObject *GetActiveProcess( );
330 
331  // overidable callback when the active process has completed
332  virtual void OnActiveProcessDelete( );
333 
334  friend class itk::simple::Command;
335  // method call by command when it's deleted, maintains internal
336  // references between command and process objects.
337  virtual void onCommandDelete(const itk::simple::Command *cmd) noexcept;
338  #endif
339 
340 
341  template< class TImageType >
342  static typename TImageType::ConstPointer CastImageToITK( const Image &img )
343  {
344  typename TImageType::ConstPointer itkImage =
345  dynamic_cast < const TImageType* > ( img.GetITKBase() );
346 
347  if ( itkImage.IsNull() )
348  {
349  sitkExceptionMacro("Failure to convert SimpleITK image of dimension: "
350  << img.GetDimension() << " and pixel type: \""
351  << img.GetPixelIDTypeAsString() << "\" to ITK image of dimension: "
352  << TImageType::GetImageDimension() << " and pixel type: \""
354  << "\"!" )
355  }
356  return itkImage;
357  }
358 
359  template< class TImageType >
360  static Image CastITKToImage( TImageType *img )
361  {
362  return Image(img);
363  }
364 
365 #ifndef SWIG
366  template< class TPixelType, unsigned int VImageDimension, unsigned int VLength,
367  template<typename, unsigned int> class TVector >
368  static Image CastITKToImage( itk::Image< TVector< TPixelType, VLength >, VImageDimension> *img )
369  {
370  // The implementation defined int sitkImageConvert.hxx needs
371  // to be manually included when this method is used.
372  auto out = GetVectorImageFromImage(img, true);
373 
374  return Image(out.GetPointer());
375  }
376 #endif
377 
385  template <typename T>
386  static std::ostream & ToStringHelper(std::ostream &os, const T &v)
387  {
388  os << v;
389  return os;
390  }
391  static std::ostream & ToStringHelper(std::ostream &os, const char &v);
392  static std::ostream & ToStringHelper(std::ostream &os, const signed char &v);
393  static std::ostream & ToStringHelper(std::ostream &os, const unsigned char &v);
396  private:
397 
398  // Add command to active process object, the EventCommand's
399  // ITKTag must be unset as max or else an exception is
400  // thrown. The EventCommand's ITKTag is updated to the command
401  // registered to ITK's ProcessObject. It's assumed that there is
402  // an current active process
403  unsigned long AddObserverToActiveProcessObject( EventCommand &e );
404 
405  // Remove the command from the active processes. Its is assumed
406  // that an active process exists. The tag is set to max after it
407  // is removed.
408  void RemoveObserverFromActiveProcessObject( EventCommand &e );
409 
410  bool m_Debug;
411 
412  unsigned int m_NumberOfThreads;
413  unsigned int m_NumberOfWorkUnits;
414 
415  std::list<EventCommand> m_Commands;
416 
418 
419  //
421  };
422 
423 
424  }
425 }
426 #endif
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:75
itk::simple::ProcessObject::EventCommand::m_Command
Command * m_Command
Definition: sitkProcessObject.h:302
itk::simple::ProcessObject::m_Commands
std::list< EventCommand > m_Commands
Definition: sitkProcessObject.h:415
sitkTemplateFunctions.h
sitkNonCopyable.h
sitkCommon.h
itk::simple::EventEnum
EventEnum
Events which can be observed from ProcessObject.
Definition: sitkEvent.h:31
itk::simple::Image::GetITKBase
itk::DataObject * GetITKBase()
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:420
itk::simple::ProcessObject::EventCommand::m_ITKTag
unsigned long m_ITKTag
Definition: sitkProcessObject.h:305
itk::simple::Image::GetPixelIDTypeAsString
std::string GetPixelIDTypeAsString() const
itk::simple::ProcessObject::m_NumberOfThreads
unsigned int m_NumberOfThreads
Definition: sitkProcessObject.h:412
itk::simple::ProcessObject::m_Debug
bool m_Debug
Definition: sitkProcessObject.h:410
sitkEvent.h
itk::simple::ProcessObject::EventCommand::operator==
bool operator==(const EventCommand &o) const
Definition: sitkProcessObject.h:307
itk::simple::GetPixelIDValueAsString
const std::string SITKCommon_EXPORT GetPixelIDValueAsString(PixelIDValueType type)
itk::simple::ImageTypeToPixelIDValue
Definition: sitkPixelIDValues.h:41
itk::Command
itk::simple::Image::GetDimension
unsigned int GetDimension() const
itk::simple::ProcessObject::CastImageToITK
static TImageType::ConstPointer CastImageToITK(const Image &img)
Definition: sitkProcessObject.h:342
SITKCommon_EXPORT
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
itk::simple::NonCopyable
An inheritable class to disable copying of a class.
Definition: sitkNonCopyable.h:51
itk::Command
class ITK_FORWARD_EXPORT Command
itk::simple::ProcessObject::m_NumberOfWorkUnits
unsigned int m_NumberOfWorkUnits
Definition: sitkProcessObject.h:413
itk::simple::ProcessObject::m_ActiveProcess
itk::ProcessObject * m_ActiveProcess
Definition: sitkProcessObject.h:417
sitkImageConvert.h
itk
itk::ProcessObject
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
itk::simple::ProcessObject::EventCommand::m_Event
EventEnum m_Event
Definition: sitkProcessObject.h:301
itk::simple::ProcessObject
Base class for SimpleITK classes based on ProcessObject.
Definition: sitkProcessObject.h:51
itk::simple::GetVectorImageFromImage
SITKCommon_HIDDEN itk::VectorImage< TPixelType, NImageDimension >::Pointer GetVectorImageFromImage(itk::Image< itk::Vector< TPixelType, NLength >, NImageDimension > *img, bool transferOwnership=false)
itk::simple::ProcessObject::EventCommand
Definition: sitkProcessObject.h:296
itk::Image
Definition: sitkPixelIDTypes.h:26
itk::EventObject
itk::simple::ProcessObject::EventCommand::operator<
bool operator<(const EventCommand &o) const
Definition: sitkProcessObject.h:309
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(TImageType *img)
Definition: sitkProcessObject.h:360
sitkExceptionMacro
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:71
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
Definition: sitkProcessObject.h:368
itk::simple::ProcessObject::ToStringHelper
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
Definition: sitkProcessObject.h:386
itk::simple::ProcessObject::EventCommand::EventCommand
EventCommand(EventEnum e, Command *c)
Definition: sitkProcessObject.h:298