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  {
334 
335  // set to max if currently not registered
336  unsigned long m_ITKTag;
337 
338  inline bool
339  operator==(const EventCommand & o) const
340  {
341  return m_Command == o.m_Command;
342  }
343  inline bool
344  operator<(const EventCommand & o) const
345  {
346  return m_Command < o.m_Command;
347  }
348  };
349 
350  // method called before filter update to set parameters and
351  // connect commands.
352  virtual void
353  PreUpdate(itk::ProcessObject * p);
354 
355  // overridable method to add a command, the return value is
356  // placed in the m_ITKTag of the EventCommand object.
357  virtual unsigned long
358  AddITKObserver(const itk::EventObject &, itk::Command *);
359 
360  // overridable method to remove a command
361  virtual void
362  RemoveITKObserver(EventCommand & e);
363 
364  // Create an ITK EventObject from the SimpleITK enumerated type.
365  static const itk::EventObject &
366  GetITKEventObject(EventEnum e);
367 
368  // returns the current active process, if no active process then
369  // an exception is throw.
370  virtual itk::ProcessObject *
371  GetActiveProcess();
372 
373  // overridable callback when the active process has completed
374  virtual void
375  OnActiveProcessDelete();
376 
377  friend class itk::simple::Command;
378  // method call by command when it's deleted, maintains internal
379  // references between command and process objects.
380  virtual void
381  onCommandDelete(const itk::simple::Command * cmd) noexcept;
382 #endif
383 
384 
385  template <class TImageType>
386  static typename TImageType::ConstPointer
387  CastImageToITK(const Image & img)
388  {
389  typename TImageType::ConstPointer itkImage = dynamic_cast<const TImageType *>(img.GetITKBase());
390 
391  if (itkImage.IsNull())
392  {
393  sitkExceptionMacro("Failure to convert SimpleITK image of dimension: "
394  << img.GetDimension() << " and pixel type: \"" << img.GetPixelIDTypeAsString()
395  << "\" to ITK image of dimension: " << TImageType::GetImageDimension() << " and pixel type: \""
397  }
398  return itkImage;
399  }
400 
401  template <class TImageType>
402  static Image
403  CastITKToImage(TImageType * img)
404  {
405  return Image(img);
406  }
407 
408 #ifndef SWIG
409  template <class TPixelType,
410  unsigned int VImageDimension,
411  unsigned int VLength,
412  template <typename, unsigned int>
413  class TVector>
414  static Image
415  CastITKToImage(itk::Image<TVector<TPixelType, VLength>, VImageDimension> * img)
416  {
417  // The implementation defined int sitkImageConvert.hxx needs
418  // to be manually included when this method is used.
419  auto out = GetVectorImageFromImage(img, true);
420 
421  return Image(out.GetPointer());
422  }
423 
424  template <unsigned int VImageDimension, unsigned int VLength, template <unsigned int> class TVector>
425  static Image
426  CastITKToImage(itk::Image<TVector<VLength>, VImageDimension> * img)
427  {
428  // The implementation defined int sitkImageConvert.hxx needs
429  // to be manually included when this method is used.
430  auto out = GetVectorImageFromImage(img, true);
431 
432  return Image(out.GetPointer());
433  }
434 
435 #endif
436 
444  template <typename T>
445  static std::ostream &
446  ToStringHelper(std::ostream & os, const T & v)
447  {
448  os << v;
449  return os;
450  }
451  static std::ostream &
452  ToStringHelper(std::ostream & os, const char & v);
453  static std::ostream &
454  ToStringHelper(std::ostream & os, const signed char & v);
455  static std::ostream &
456  ToStringHelper(std::ostream & os, const unsigned char & v);
459 private:
460  // Add command to active process object, the EventCommand's
461  // ITKTag must be unset as max or else an exception is
462  // thrown. The EventCommand's ITKTag is updated to the command
463  // registered to ITK's ProcessObject. It's assumed that there is
464  // an current active process
465  unsigned long
466  AddObserverToActiveProcessObject(EventCommand & e);
467 
468  // Remove the command from the active processes. Its is assumed
469  // that an active process exists. The tag is set to max after it
470  // is removed.
471  void
472  RemoveObserverFromActiveProcessObject(EventCommand & e);
473 
474  bool m_Debug;
475 
476  unsigned int m_NumberOfThreads;
477  unsigned int m_NumberOfWorkUnits;
478 
479  std::list<EventCommand> m_Commands;
480 
482 
483  //
485 };
486 
487 
488 } // namespace simple
489 } // namespace itk
490 #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:333
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:479
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:484
itk::simple::ProcessObject::EventCommand::m_ITKTag
unsigned long m_ITKTag
Definition: sitkProcessObject.h:336
itk::simple::Image::GetPixelIDTypeAsString
std::string GetPixelIDTypeAsString() const
itk::simple::ProcessObject::m_NumberOfThreads
unsigned int m_NumberOfThreads
Definition: sitkProcessObject.h:476
itk::simple::ProcessObject::m_Debug
bool m_Debug
Definition: sitkProcessObject.h:474
sitkEvent.h
itk::simple::ProcessObject::EventCommand::operator==
bool operator==(const EventCommand &o) const
Definition: sitkProcessObject.h:339
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:387
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:477
itk::simple::ProcessObject::m_ActiveProcess
itk::ProcessObject * m_ActiveProcess
Definition: sitkProcessObject.h:481
itk::simple::ProcessObject::CastITKToImage
static Image CastITKToImage(itk::Image< TVector< VLength >, VImageDimension > *img)
Definition: sitkProcessObject.h:426
sitkImageConvert.h
itk
itk::ProcessObject
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
itk::simple::ProcessObject::EventCommand::m_Event
EventEnum m_Event
Definition: sitkProcessObject.h:332
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:403
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:415
itk::simple::ProcessObject::ToStringHelper
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
Definition: sitkProcessObject.h:446