SimpleITK  1.3.0.dev466
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("Failure to convert SimpleITK image of dimension: "
278  << img.GetDimension() << " and pixel type: \""
279  << img.GetPixelIDTypeAsString() << "\" to ITK image of dimension: "
280  << TImageType::GetImageDimension() << " and pixel type: \""
282  << "\"!" )
283  }
284  return itkImage;
285  }
286 
287  template< class TImageType >
288  static Image CastITKToImage( TImageType *img )
289  {
290  return Image(img);
291  }
292 
293 #ifndef SWIG
294  template< class TPixelType, unsigned int VImageDimension, unsigned int VLength,
295  template<typename, unsigned int> class TVector >
296  static Image CastITKToImage( itk::Image< TVector< TPixelType, VLength >, VImageDimension> *img )
297  {
298  typedef itk::VectorImage< TPixelType, VImageDimension > VectorImageType;
299 
300  size_t numberOfElements = img->GetBufferedRegion().GetNumberOfPixels();
301  typename VectorImageType::InternalPixelType* buffer = reinterpret_cast<typename VectorImageType::InternalPixelType*>( img->GetPixelContainer()->GetBufferPointer() );
302 
303  // Unlike an image of Vectors a VectorImage's container is a
304  // container of TPixelType, whos size is the image's number of
305  // pixels * number of pixels per component
306  numberOfElements *= VImageDimension;
307 
308  typename VectorImageType::Pointer out = VectorImageType::New();
309 
310  // Set the image's pixel container to import the pointer provided.
311  out->GetPixelContainer()->SetImportPointer(buffer, numberOfElements, true );
312  img->GetPixelContainer()->ContainerManageMemoryOff();
313  out->CopyInformation( img );
314  out->SetRegions( img->GetBufferedRegion() );
315 
316  return Image(out.GetPointer());
317  }
318 #endif
319 
327  template <typename T>
328  static std::ostream & ToStringHelper(std::ostream &os, const T &v)
329  {
330  os << v;
331  return os;
332  }
333  static std::ostream & ToStringHelper(std::ostream &os, const char &v);
334  static std::ostream & ToStringHelper(std::ostream &os, const signed char &v);
335  static std::ostream & ToStringHelper(std::ostream &os, const unsigned char &v);
338  private:
339 
340  // Add command to active process object, the EventCommand's
341  // ITKTag must be unset as max or else an exception is
342  // thrown. The EventCommand's ITKTag is updated to the command
343  // registered to ITK's ProcessObject. It's assumed that there is
344  // an current active process
345  unsigned long AddObserverToActiveProcessObject( EventCommand &e );
346 
347  // Remove the command from the active processes. Its is assumed
348  // that an active process exists. The tag is set to max after it
349  // is removed.
350  void RemoveObserverFromActiveProcessObject( EventCommand &e );
351 
352  bool m_Debug;
353 
354  unsigned int m_NumberOfThreads;
355 
356  std::list<EventCommand> m_Commands;
357 
359 
360  //
362  };
363 
364 
365  }
366 }
367 #endif
static std::ostream & ToStringHelper(std::ostream &os, const T &v)
itk::DataObject * GetITKBase(void)
STL namespace.
std::string GetPixelIDTypeAsString(void) const
const std::string SITKCommon_EXPORT GetPixelIDValueAsString(PixelIDValueType type)
EventEnum
Events which can be observed from ProcessObject.
Definition: sitkEvent.h:31
class ITK_FORWARD_EXPORT ProcessObject
static Image CastITKToImage(TImageType *img)
itk::ProcessObject * m_ActiveProcess
uint64_t GetNumberOfPixels(void) const
Get the number of pixels in the image.
An implementation of the Command design pattern for callback.
Definition: sitkCommand.h:44
void CopyInformation(const Image &srcImage)
Copy common meta-data from an image to this one.
std::list< EventCommand > m_Commands
static TImageType::ConstPointer CastImageToITK(const Image &img)
#define SITKCommon_EXPORT
Definition: sitkCommon.h:41
The Image class for SimpleITK.
Definition: sitkImage.h:78
#define SITK_NOEXCEPT
Definition: sitkMacro.h:68
static Image CastITKToImage(itk::Image< TVector< TPixelType, VLength >, VImageDimension > *img)
bool operator<(const EventCommand &o) const
bool operator==(const EventCommand &o) const
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:88
Base class for SimpleITK classes based on ProcessObject.
An inheratable class to disable copying of a class.
class ITK_FORWARD_EXPORT Command
unsigned int GetDimension(void) const