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"
24#include "sitkEvent.h"
25#include "sitkImage.h"
26#include "sitkImageConvert.h"
27
28#include <iostream>
29#include <list>
30
31namespace itk
32{
33
34#ifndef SWIG
35
36template <typename T, unsigned int NVectorDimension>
37class Vector;
38
39class ProcessObject;
40class Command;
41class EventObject;
42#endif
43
44namespace simple
45{
46
47class Command;
48
49
55{
56public:
58
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
86 virtual void
89
93 virtual bool
94 GetDebug() const;
95 virtual void
96 SetDebug(bool debugFlag);
98
104 static void
106 static void
109
111 static bool
113 static void
114 SetGlobalDefaultDebug(bool debugFlag);
116
125 static void
127 static void
129 static void
131 static bool
134
147 static double
149 static void
151
152 static double
154 static void
157
158
183 static bool
184 SetGlobalDefaultThreader(const std::string & threader);
185 static std::string
188
202 static void
204 static unsigned int
207
232 void
234 unsigned int
237
251 virtual void
252 SetNumberOfThreads(unsigned int n);
253 virtual unsigned int
256
267 virtual void
268 SetNumberOfWorkUnits(unsigned int n);
269 virtual unsigned int
272
273
300 virtual int
302
303#ifndef SWIG
309 virtual int
310 AddCommand(itk::simple::EventEnum event, const std::function<void()> & func);
311#endif
312
318 virtual void
320
322 virtual bool
324
325
335 virtual float
336 GetProgress() const;
337
353 virtual void
355
356protected:
357#ifndef SWIG
358
360 {
364
365 // set to max if currently not registered
366 unsigned long m_ITKTag;
367
368 inline bool
369 operator==(const EventCommand & o) const
370 {
371 return m_Command == o.m_Command;
372 }
373 inline bool
374 operator<(const EventCommand & o) const
375 {
376 return m_Command < o.m_Command;
377 }
378 };
379
380 // method called before filter update to set parameters and
381 // connect commands.
382 virtual void
384
385 // overridable method to add a command, the return value is
386 // placed in the m_ITKTag of the EventCommand object.
387 virtual unsigned long
389
390 // overridable method to remove a command
391 virtual void
393
394 // Create an ITK EventObject from the SimpleITK enumerated type.
395 static const itk::EventObject &
397
398 // returns the current active process, if no active process then
399 // an exception is throw.
400 virtual itk::ProcessObject *
402
403 // overridable callback when the active process has completed
404 virtual void
406
408 // method call by command when it's deleted, maintains internal
409 // references between command and process objects.
410 virtual void
412#endif
413
414
415 template <class TImageType>
416 static typename TImageType::ConstPointer
418 {
419 typename TImageType::ConstPointer itkImage = dynamic_cast<const TImageType *>(img.GetITKBase());
420
421 if (itkImage.IsNull())
422 {
423 sitkExceptionMacro("Failure to convert SimpleITK image of dimension: "
424 << img.GetDimension() << " and pixel type: \"" << img.GetPixelIDTypeAsString()
425 << "\" to ITK image of dimension: " << TImageType::GetImageDimension() << " and pixel type: \""
427 }
428 return itkImage;
429 }
430
431 template <class TImageType>
432 static Image
433 CastITKToImage(TImageType * img)
434 {
435 return Image(img);
436 }
437
438#ifndef SWIG
439 template <class TPixelType,
440 unsigned int VImageDimension,
441 unsigned int VLength,
442 template <typename, unsigned int> class TVector>
443 static Image
444 CastITKToImage(itk::Image<TVector<TPixelType, VLength>, VImageDimension> * img)
445 {
446 // The implementation defined int sitkImageConvert.hxx needs
447 // to be manually included when this method is used.
448 auto out = GetVectorImageFromImage(img, true);
449
450 return Image(out.GetPointer());
451 }
452
453 template <unsigned int VImageDimension, unsigned int VLength, template <unsigned int> class TVector>
454 static Image
455 CastITKToImage(itk::Image<TVector<VLength>, VImageDimension> * img)
456 {
457 // The implementation defined int sitkImageConvert.hxx needs
458 // to be manually included when this method is used.
459 auto out = GetVectorImageFromImage(img, true);
460
461 return Image(out.GetPointer());
462 }
463
464#endif
465
473 template <typename T>
474 static std::ostream &
475 ToStringHelper(std::ostream & os, const T & v)
476 {
477 os << v;
478 return os;
479 }
480 static std::ostream &
481 ToStringHelper(std::ostream & os, const char & v);
482 static std::ostream &
483 ToStringHelper(std::ostream & os, const signed char & v);
484 static std::ostream &
485 ToStringHelper(std::ostream & os, const unsigned char & v);
487
488private:
489 // Add command to active process object, the EventCommand's
490 // ITKTag must be unset as max or else an exception is
491 // thrown. The EventCommand's ITKTag is updated to the command
492 // registered to ITK's ProcessObject. It's assumed that there is
493 // an current active process
494 unsigned long
496
497 // Remove the command from the active processes. Its is assumed
498 // that an active process exists. The tag is set to max after it
499 // is removed.
500 void
502
504
507
508 std::list<EventCommand> m_Commands;
509
511
512 //
514};
515
516
517} // namespace simple
518} // namespace itk
519#endif
An implementation of the Command design pattern for callback.
Definition sitkCommand.h:45
The Image class for SimpleITK.
Definition sitkImage.h:77
unsigned int GetDimension() const
itk::DataObject * GetITKBase()
std::string GetPixelIDTypeAsString() const
unsigned long AddObserverToActiveProcessObject(EventCommand &e)
static void GlobalWarningDisplayOn()
void SetMaximumNumberOfThreads(unsigned int n)
virtual unsigned int GetNumberOfWorkUnits() const
static void SetGlobalDefaultDebug(bool debugFlag)
virtual int AddCommand(itk::simple::EventEnum event, itk::simple::Command &cmd)
Add a Command Object to observer the event.
static unsigned int GetGlobalDefaultNumberOfThreads()
Set/Get the default threader used for process objects.
virtual void PreUpdate(itk::ProcessObject *p)
static Image CastITKToImage(itk::Image< TVector< VLength >, VImageDimension > *img)
static void SetGlobalDefaultNumberOfThreads(unsigned int n)
virtual void RemoveITKObserver(EventCommand &e)
static std::ostream & ToStringHelper(std::ostream &os, const unsigned char &v)
virtual float GetProgress() const
An Active Measurement of the progress of execution.
static std::ostream & ToStringHelper(std::ostream &os, const char &v)
static void GlobalDefaultDebugOn()
static std::ostream & ToStringHelper(std::ostream &os, const signed char &v)
static double GetGlobalDefaultDirectionTolerance()
Access the global tolerance to determine congruent spaces.
virtual int AddCommand(itk::simple::EventEnum event, const std::function< void()> &func)
Directly add a callback to observe an event.
void RemoveObserverFromActiveProcessObject(EventCommand &e)
std::list< EventCommand > m_Commands
static void SetGlobalDefaultCoordinateTolerance(double)
Access the global tolerance to determine congruent spaces.
static TImageType::ConstPointer CastImageToITK(const Image &img)
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
static bool SetGlobalDefaultThreader(const std::string &threader)
Set/Get the default threader used for process objects.
itk::ProcessObject * m_ActiveProcess
static bool GetGlobalDefaultDebug()
virtual bool HasCommand(itk::simple::EventEnum event) const
Query of this object has any registered commands for event.
virtual void OnActiveProcessDelete()
virtual void SetDebug(bool debugFlag)
static bool GetGlobalWarningDisplay()
virtual unsigned int GetNumberOfThreads() const
static void SetGlobalWarningDisplay(bool flag)
virtual void onCommandDelete(const itk::simple::Command *cmd) noexcept
virtual void SetNumberOfThreads(unsigned int n)
static void GlobalWarningDisplayOff()
virtual std::string ToString() const
virtual void SetNumberOfWorkUnits(unsigned int n)
friend class itk::simple::Command
virtual void RemoveAllCommands()
Remove all registered commands.
static const itk::EventObject & GetITKEventObject(EventEnum e)
virtual unsigned long AddITKObserver(const itk::EventObject &, itk::Command *)
virtual std::string GetName() const =0
static void SetGlobalDefaultDirectionTolerance(double)
Access the global tolerance to determine congruent spaces.
virtual bool GetDebug() const
static void GlobalDefaultDebugOff()
static Image CastITKToImage(TImageType *img)
virtual itk::ProcessObject * GetActiveProcess()
static double GetGlobalDefaultCoordinateTolerance()
Access the global tolerance to determine congruent spaces.
static std::string GetGlobalDefaultThreader()
Set/Get the default threader used for process objects.
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
unsigned int GetMaximumNumberOfThreads() const
const std::string SITKCommon_EXPORT GetPixelIDValueAsString(PixelIDValueType type)
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...
EventEnum
Events which can be observed from ProcessObject.
Definition sitkEvent.h:32
#define SITKCommon_EXPORT
Definition sitkCommon.h:41
#define sitkExceptionMacro(x)
Definition sitkMacro.h:65
bool operator==(const EventCommand &o) const
bool operator<(const EventCommand &o) const
EventCommand(EventEnum e, Command *c)