SimpleITK  
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 
34 #ifndef SWIG
35 
36 template <typename T, unsigned int NVectorDimension>
37 class Vector;
38 
39 class ProcessObject;
40 class Command;
41 class EventObject;
42 #endif
43 
44 namespace simple
45 {
46 
47 class Command;
48 
49 
55 {
56 public:
58 
63  ProcessObject();
64 
68  virtual ~ProcessObject();
69 
70  // Print ourselves out
71  virtual std::string
72  ToString() const;
73 
75  virtual std::string
76  GetName() const = 0;
77 
84  virtual void
85  DebugOn();
86  virtual void
87  DebugOff();
93  virtual bool
94  GetDebug() const;
95  virtual void
96  SetDebug(bool debugFlag);
104  static void
105  GlobalDefaultDebugOn();
106  static void
107  GlobalDefaultDebugOff();
111  static bool
112  GetGlobalDefaultDebug();
113  static void
114  SetGlobalDefaultDebug(bool debugFlag);
125  static void
126  GlobalWarningDisplayOn();
127  static void
128  GlobalWarningDisplayOff();
129  static void
130  SetGlobalWarningDisplay(bool flag);
131  static bool
132  GetGlobalWarningDisplay();
147  static double
148  GetGlobalDefaultCoordinateTolerance();
149  static void
150  SetGlobalDefaultCoordinateTolerance(double);
151 
152  static double
153  GetGlobalDefaultDirectionTolerance();
154  static void
155  SetGlobalDefaultDirectionTolerance(double);
182  static bool
183  SetGlobalDefaultThreader(const std::string & threader);
184  static std::string
185  GetGlobalDefaultThreader();
201  static void
202  SetGlobalDefaultNumberOfThreads(unsigned int n);
203  static unsigned int
204  GetGlobalDefaultNumberOfThreads();
221  virtual void
222  SetNumberOfThreads(unsigned int n);
223  virtual unsigned int
224  GetNumberOfThreads() const;
237  virtual void
238  SetNumberOfWorkUnits(unsigned int n);
239  virtual unsigned int
240  GetNumberOfWorkUnits() const;
270  virtual int
271  AddCommand(itk::simple::EventEnum event, itk::simple::Command & cmd);
272 
273 #ifndef SWIG
274 
279  virtual int
280  AddCommand(itk::simple::EventEnum event, const std::function<void()> & func);
281 #endif
282 
288  virtual void
289  RemoveAllCommands();
290 
292  virtual bool
293  HasCommand(itk::simple::EventEnum event) const;
294 
295 
305  virtual float
306  GetProgress() const;
307 
323  virtual void
324  Abort();
325 
326 protected:
327 #ifndef SWIG
328 
330  {
332  : m_Event(e)
333  , m_Command(c)
334  , m_ITKTag(std::numeric_limits<unsigned long>::max())
335  {}
338 
339  // set to max if currently not registered
340  unsigned long m_ITKTag;
341 
342  inline bool
343  operator==(const EventCommand & o) const
344  {
345  return m_Command == o.m_Command;
346  }
347  inline bool
348  operator<(const EventCommand & o) const
349  {
350  return m_Command < o.m_Command;
351  }
352  };
353 
354  // method called before filter update to set parameters and
355  // connect commands.
356  virtual void
357  PreUpdate(itk::ProcessObject * p);
358 
359  // overridable method to add a command, the return value is
360  // placed in the m_ITKTag of the EventCommand object.
361  virtual unsigned long
362  AddITKObserver(const itk::EventObject &, itk::Command *);
363 
364  // overridable method to remove a command
365  virtual void
366  RemoveITKObserver(EventCommand & e);
367 
368  // Create an ITK EventObject from the SimpleITK enumerated type.
369  static const itk::EventObject &
370  GetITKEventObject(EventEnum e);
371 
372  // returns the current active process, if no active process then
373  // an exception is throw.
374  virtual itk::ProcessObject *
375  GetActiveProcess();
376 
377  // overridable callback when the active process has completed
378  virtual void
379  OnActiveProcessDelete();
380 
381  friend class itk::simple::Command;
382  // method call by command when it's deleted, maintains internal
383  // references between command and process objects.
384  virtual void
385  onCommandDelete(const itk::simple::Command * cmd) noexcept;
386 #endif
387 
388 
389  template <class TImageType>
390  static typename TImageType::ConstPointer
391  CastImageToITK(const Image & img)
392  {
393  typename TImageType::ConstPointer itkImage = dynamic_cast<const TImageType *>(img.GetITKBase());
394 
395  if (itkImage.IsNull())
396  {
397  sitkExceptionMacro("Failure to convert SimpleITK image of dimension: "
398  << img.GetDimension() << " and pixel type: \"" << img.GetPixelIDTypeAsString()
399  << "\" to ITK image of dimension: " << TImageType::GetImageDimension() << " and pixel type: \""
401  }
402  return itkImage;
403  }
404 
405  template <class TImageType>
406  static Image
407  CastITKToImage(TImageType * img)
408  {
409  return Image(img);
410  }
411 
412 #ifndef SWIG
413  template <class TPixelType,
414  unsigned int VImageDimension,
415  unsigned int VLength,
416  template <typename, unsigned int>
417  class TVector>
418  static Image
419  CastITKToImage(itk::Image<TVector<TPixelType, VLength>, VImageDimension> * img)
420  {
421  // The implementation defined int sitkImageConvert.hxx needs
422  // to be manually included when this method is used.
423  auto out = GetVectorImageFromImage(img, true);
424 
425  return Image(out.GetPointer());
426  }
427 
428  template <unsigned int VImageDimension, unsigned int VLength, template <unsigned int> class TVector>
429  static Image
430  CastITKToImage(itk::Image<TVector<VLength>, VImageDimension> * img)
431  {
432  // The implementation defined int sitkImageConvert.hxx needs
433  // to be manually included when this method is used.
434  auto out = GetVectorImageFromImage(img, true);
435 
436  return Image(out.GetPointer());
437  }
438 
439 #endif
440 
448  template <typename T>
449  static std::ostream &
450  ToStringHelper(std::ostream & os, const T & v)
451  {
452  os << v;
453  return os;
454  }
455  static std::ostream &
456  ToStringHelper(std::ostream & os, const char & v);
457  static std::ostream &
458  ToStringHelper(std::ostream & os, const signed char & v);
459  static std::ostream &
460  ToStringHelper(std::ostream & os, const unsigned char & v);
463 private:
464  // Add command to active process object, the EventCommand's
465  // ITKTag must be unset as max or else an exception is
466  // thrown. The EventCommand's ITKTag is updated to the command
467  // registered to ITK's ProcessObject. It's assumed that there is
468  // an current active process
469  unsigned long
470  AddObserverToActiveProcessObject(EventCommand & e);
471 
472  // Remove the command from the active processes. Its is assumed
473  // that an active process exists. The tag is set to max after it
474  // is removed.
475  void
476  RemoveObserverFromActiveProcessObject(EventCommand & e);
477 
478  bool m_Debug;
479 
480  unsigned int m_NumberOfThreads;
481  unsigned int m_NumberOfWorkUnits;
482 
483  std::list<EventCommand> m_Commands;
484 
486 
487  //
489 };
490 
491 
492 } // namespace simple
493 } // namespace itk
494 #endif
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:76
itk::simple::ProcessObject::EventCommand::m_Command
Command * m_Command
Definition: sitkProcessObject.h:337
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
itk::simple::ProcessObject::m_Commands
std::list< EventCommand > m_Commands
Definition: sitkProcessObject.h:483
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::GetVectorImageFromImage
SITKCommon_HIDDEN itk::VectorImage< TPixelType, NImageDimension >::Pointer GetVectorImageFromImage(itk::Image< itk::Vector< TPixelType, NLength >, NImageDimension > *img, bool transferOwnership=false)
Utility methods to convert between itk image types efficiently by sharing the buffer between the inpu...
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:488
itk::simple::ProcessObject::EventCommand::m_ITKTag
unsigned long m_ITKTag
Definition: sitkProcessObject.h:340
itk::simple::Image::GetPixelIDTypeAsString
std::string GetPixelIDTypeAsString() const
itk::simple::ProcessObject::m_NumberOfThreads
unsigned int m_NumberOfThreads
Definition: sitkProcessObject.h:480
itk::simple::ProcessObject::m_Debug
bool m_Debug
Definition: sitkProcessObject.h:478
sitkEvent.h
itk::simple::ProcessObject::EventCommand::operator==
bool operator==(const EventCommand &o) const
Definition: sitkProcessObject.h:343
itk::simple::GetPixelIDValueAsString
const std::string SITKCommon_EXPORT GetPixelIDValueAsString(PixelIDValueType type)
itk::simple::ImageTypeToPixelIDValue
Definition: sitkPixelIDValues.h:43
itk::Command
itk::simple::Image::GetDimension
unsigned int GetDimension() const
itk::simple::ProcessObject::CastImageToITK
static TImageType::ConstPointer CastImageToITK(const Image &img)
Definition: sitkProcessObject.h:391
SITKCommon_EXPORT
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
itk::simple::NonCopyable
An inheritable class to disable copying of a class.
Definition: sitkNonCopyable.h:49
itk::Command
class ITK_FORWARD_EXPORT Command
itk::simple::ProcessObject::m_NumberOfWorkUnits
unsigned int m_NumberOfWorkUnits
Definition: sitkProcessObject.h:481
itk::simple::ProcessObject::m_ActiveProcess
itk::ProcessObject * m_ActiveProcess
Definition: sitkProcessObject.h:485
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(itk::Image< TVector< VLength >, VImageDimension > *img)
Definition: sitkProcessObject.h:430
sitkImageConvert.h
itk
itk::ProcessObject
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
itk::simple::ProcessObject::EventCommand::m_Event
EventEnum m_Event
Definition: sitkProcessObject.h:336
itk::simple::ProcessObject
Base class for SimpleITK classes based on ProcessObject.
Definition: sitkProcessObject.h:54
itk::simple::ProcessObject::EventCommand
Definition: sitkProcessObject.h:329
itk::Image
Definition: sitkPixelIDTypes.h:28
itk::EventObject
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(TImageType *img)
Definition: sitkProcessObject.h:407
sitkExceptionMacro
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:70
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
Definition: sitkProcessObject.h:419
itk::simple::ProcessObject::ToStringHelper
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
Definition: sitkProcessObject.h:450
itk::simple::ProcessObject::EventCommand::EventCommand
EventCommand(EventEnum e, Command *c)
Definition: sitkProcessObject.h:331