ITK  5.2.0
Insight Toolkit
itkProcessObject.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 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkProcessObject_h
29 #define itkProcessObject_h
30 
31 #include "itkDataObject.h"
32 #include "itkObjectFactory.h"
33 #include "itkNumericTraits.h"
34 #include "itkThreadSupport.h"
35 #include <vector>
36 #include <map>
37 #include <set>
38 #include <algorithm>
39 #include <thread>
40 
41 namespace itk
42 {
43 
44 class MultiThreaderBase;
45 
138 class ITKCommon_EXPORT ProcessObject : public Object
139 {
140 public:
141  ITK_DISALLOW_COPY_AND_MOVE(ProcessObject);
142 
148 
150  itkTypeMacro(ProcessObject, Object);
151 
154 
156  // using ConstDataObjectPointerArray = std::vector< const DataObject * >;
157 
159  using DataObjectPointerArray = std::vector<DataObjectPointer>;
160 
162 
164  using NameArray = std::vector<DataObjectIdentifierType>;
165 
168 
174  NameArray
175  GetInputNames() const;
176 
178  NameArray
179  GetRequiredInputNames() const;
180 
187  GetInputs();
188 
190  bool
191  HasInput(const DataObjectIdentifierType & key) const;
192 
193  using DataObjectPointerArraySizeType = DataObjectPointerArray::size_type;
194 
206  GetNumberOfInputs() const;
207 
210  GetNumberOfOutputs() const;
211 
217  NameArray
218  GetOutputNames() const;
219 
226  GetOutputs();
227 
229  bool
230  HasOutput(const DataObjectIdentifierType & key) const;
231 
234  GetIndexedInputs();
235 
250  GetNumberOfIndexedInputs() const;
251 
262  GetNumberOfValidRequiredInputs() const;
263 
266  GetIndexedOutputs();
267 
270  GetNumberOfIndexedOutputs() const;
271 
287  virtual DataObjectPointer
288  MakeOutput(DataObjectPointerArraySizeType idx);
289 
295  itkSetMacro(AbortGenerateData, bool);
296 
298  itkGetConstReferenceMacro(AbortGenerateData, bool);
299 
301  itkBooleanMacro(AbortGenerateData);
302 
311 #if !defined(ITK_LEGACY_REMOVE)
312  void
313  SetProgress(float progress)
314  {
315  m_Progress = progressFloatToFixed(progress);
316  }
317 #endif
318 
324  virtual float
325  GetProgress() const
326  {
327  return progressFixedToFloat(m_Progress);
328  }
329 
339  void
340  UpdateProgress(float progress);
341 
349  void
350  IncrementProgress(float increment);
351 
373  virtual void
374  Update();
375 
385  virtual void
386  UpdateLargestPossibleRegion();
387 
403  virtual void
404  UpdateOutputInformation();
405 
408  virtual void
409  PropagateRequestedRegion(DataObject * output);
410 
412  virtual void
413  UpdateOutputData(DataObject * output);
414 
424  virtual void
426  {}
427 
434  virtual void
435  ResetPipeline();
436 
453  virtual DataObjectPointer
454  MakeOutput(const DataObjectIdentifierType &);
455 
461  virtual void
462  SetReleaseDataFlag(bool val);
463  virtual bool
464  GetReleaseDataFlag() const;
465  void
467  {
468  this->SetReleaseDataFlag(true);
469  }
470  void
472  {
473  this->SetReleaseDataFlag(false);
474  }
476 
489  itkSetMacro(ReleaseDataBeforeUpdateFlag, bool);
490  itkGetConstReferenceMacro(ReleaseDataBeforeUpdateFlag, bool);
491  itkBooleanMacro(ReleaseDataBeforeUpdateFlag);
493 
495  itkSetClampMacro(NumberOfWorkUnits, ThreadIdType, 1, ITK_MAX_THREADS);
496  itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
498 
499 #if !defined(ITK_LEGACY_REMOVE) || defined(ITKV4_COMPATIBILITY)
500  itkLegacyMacro(void SetNumberOfThreads(ThreadIdType count)) { this->SetNumberOfWorkUnits(count); }
501 
502  itkLegacyMacro(ThreadIdType GetNumberOfThreads() const) { return this->GetNumberOfWorkUnits(); }
503 #endif // !ITK_LEGACY_REMOVE
504 
506  MultiThreaderType *
508  {
509  return m_MultiThreader;
510  }
511 
513  void
514  SetMultiThreader(MultiThreaderType * threader);
515 
522  virtual void
523  PrepareOutputs();
524 
525 protected:
526  ProcessObject();
527  ~ProcessObject() override;
528 
529  void
530  PrintSelf(std::ostream & os, Indent indent) const override;
531 
532  //
533  // Input Methods
534  //
535 
540  DataObject *
541  GetInput(const DataObjectIdentifierType & key);
542  const DataObject *
543  GetInput(const DataObjectIdentifierType & key) const;
544 
547  DataObject *
549  {
550  return idx < m_IndexedInputs.size() ? m_IndexedInputs[idx]->second.GetPointer() : nullptr;
551  }
552  const DataObject *
554  {
555  return idx < m_IndexedInputs.size() ? m_IndexedInputs[idx]->second.GetPointer() : nullptr;
556  }
558 
565  virtual void
566  SetInput(const DataObjectIdentifierType & key, DataObject * input);
567  virtual void
568  SetNthInput(DataObjectPointerArraySizeType idx, DataObject * input);
569 
571  virtual void
572  AddInput(DataObject * input);
573 
582  virtual void
583  PushBackInput(const DataObject * input);
584  virtual void
585  PopBackInput();
586  virtual void
587  PushFrontInput(const DataObject * input);
588  virtual void
589  PopFrontInput();
590 
597  virtual void
598  RemoveInput(const DataObjectIdentifierType & key);
599  virtual void RemoveInput(DataObjectPointerArraySizeType);
600 
602  DataObject *
604  {
605  return m_IndexedInputs[0]->second;
606  }
607  const DataObject *
609  {
610  return m_IndexedInputs[0]->second;
611  }
613 
615  virtual void
616  SetPrimaryInputName(const DataObjectIdentifierType & key);
617  virtual const char *
619  {
620  return this->m_IndexedInputs[0]->first.c_str();
621  }
623 
625  virtual void
626  SetPrimaryInput(DataObject * object);
627 
634  void
635  SetNumberOfIndexedInputs(DataObjectPointerArraySizeType num);
636 
646  virtual void SetNumberOfRequiredInputs(DataObjectPointerArraySizeType);
647  itkGetConstReferenceMacro(NumberOfRequiredInputs, DataObjectPointerArraySizeType);
648 
649 
654  bool
655  RemoveRequiredInputName(const DataObjectIdentifierType &);
656 
658  bool
659  IsRequiredInputName(const DataObjectIdentifierType &) const;
660 
665  void
666  SetRequiredInputNames(const NameArray &);
667 
678  bool
679  AddRequiredInputName(const DataObjectIdentifierType &);
680  bool
681  AddRequiredInputName(const DataObjectIdentifierType &, DataObjectPointerArraySizeType idx);
682 
693  void
694  AddOptionalInputName(const DataObjectIdentifierType &);
695  void
696  AddOptionalInputName(const DataObjectIdentifierType &, DataObjectPointerArraySizeType idx);
697 
698 
699  //
700  // Output Methods
701  //
702 
704  DataObject *
705  GetOutput(const DataObjectIdentifierType & key);
706  const DataObject *
707  GetOutput(const DataObjectIdentifierType & key) const;
709 
711  virtual void
712  SetPrimaryOutputName(const DataObjectIdentifierType & key);
713  virtual const char *
715  {
716  return this->m_IndexedOutputs[0]->first.c_str();
717  }
719 
721  DataObject *
722  GetOutput(DataObjectPointerArraySizeType i);
723  const DataObject *
724  GetOutput(DataObjectPointerArraySizeType i) const;
726 
728  virtual void
729  SetOutput(const DataObjectIdentifierType & name, DataObject * output);
730 
732  virtual void
733  RemoveOutput(const DataObjectIdentifierType & key);
734 
736  DataObject *
738  {
739  return m_IndexedOutputs[0]->second;
740  }
741  const DataObject *
743  {
744  return m_IndexedOutputs[0]->second;
745  }
747 
749  virtual void
750  SetPrimaryOutput(DataObject * object);
751 
754  virtual void
755  SetNthOutput(DataObjectPointerArraySizeType idx, DataObject * output);
756 
757  virtual void
758  AddOutput(DataObject * output);
759 
760  virtual void
761  RemoveOutput(DataObjectPointerArraySizeType idx);
762 
763  itkSetMacro(NumberOfRequiredOutputs, DataObjectPointerArraySizeType);
764  itkGetConstReferenceMacro(NumberOfRequiredOutputs, DataObjectPointerArraySizeType);
765 
767  void
768  SetNumberOfIndexedOutputs(DataObjectPointerArraySizeType num);
769 
770 
771  DataObjectIdentifierType
772  MakeNameFromInputIndex(DataObjectPointerArraySizeType idx) const;
773  DataObjectIdentifierType
774  MakeNameFromOutputIndex(DataObjectPointerArraySizeType idx) const;
775  DataObjectPointerArraySizeType
776  MakeIndexFromInputName(const DataObjectIdentifierType & name) const;
777  DataObjectPointerArraySizeType
778  MakeIndexFromOutputName(const DataObjectIdentifierType & name) const;
779  bool
780  IsIndexedInputName(const DataObjectIdentifierType &) const;
781  bool
782  IsIndexedOutputName(const DataObjectIdentifierType &) const;
783 
784  //
785  // Pipeline Methods
786  //
787 
799  virtual void
800  VerifyPreconditions() ITKv5_CONST;
801 
812  virtual void
813  VerifyInputInformation() ITKv5_CONST;
814 
828  virtual void
829  GenerateInputRequestedRegion();
830 
842  virtual void
843  GenerateOutputRequestedRegion(DataObject * output);
844 
855  virtual void
856  GenerateOutputInformation();
857 
859  virtual void
860  GenerateData()
861  {}
862 
867  virtual void
868  PropagateResetPipeline();
869 
881  virtual void
882  ReleaseInputs();
883 
892  virtual void
893  CacheInputReleaseDataFlags();
894 
898  virtual void
899  RestoreInputReleaseDataFlags();
900 
905  itkGetConstMacro(ThreaderUpdateProgress, bool);
906  itkBooleanMacro(ThreaderUpdateProgress);
907  virtual void
908  SetThreaderUpdateProgress(bool arg);
910 
911 
915  static inline constexpr float
917  {
918  return double(fixed) / double(std::numeric_limits<uint32_t>::max());
919  };
921 
926  static inline uint32_t
928  {
929  if (f <= 0.0f)
930  {
931  return 0;
932  }
933  if (f >= 1.0f)
934  {
935  return std::numeric_limits<uint32_t>::max();
936  }
937  double temp = static_cast<double>(f) * std::numeric_limits<uint32_t>::max();
938  return static_cast<uint32_t>(temp);
939  };
941 
947  bool m_Updating;
948 
951 
952 private:
955  MakeIndexFromName(const DataObjectIdentifierType &) const;
956 
958  using DataObjectPointerMap = std::map<DataObjectIdentifierType, DataObjectPointer>;
959 
960 
964 
965  std::vector<DataObjectPointerMap::iterator> m_IndexedInputs;
966  std::vector<DataObjectPointerMap::iterator> m_IndexedOutputs;
967 
969  std::map<DataObjectIdentifierType, bool> m_CachedInputReleaseDataFlags;
970 
973 
975  using NameSet = std::set<DataObjectIdentifierType>;
976 
979 
982  std::atomic<uint32_t> m_Progress;
983 
984 
985  std::thread::id m_UpdateThreadID;
986 
991 
992  bool m_ThreaderUpdateProgress{ true };
993 
996 
998  friend class DataObject;
999 
1003 
1004  friend class DataObjectIterator;
1007 
1008  friend class TestProcessObject;
1009 };
1010 } // end namespace itk
1011 
1012 #endif
itk::ProcessObject::m_ReleaseDataBeforeUpdateFlag
bool m_ReleaseDataBeforeUpdateFlag
Definition: itkProcessObject.h:995
itk::MultiThreaderBase
A class for performing multithreaded execution.
Definition: itkMultiThreaderBase.h:105
itkObjectFactory.h
itk::ProcessObject::m_IndexedOutputs
std::vector< DataObjectPointerMap::iterator > m_IndexedOutputs
Definition: itkProcessObject.h:966
itk::ProcessObject::DataObjectPointerArray
std::vector< DataObjectPointer > DataObjectPointerArray
Definition: itkProcessObject.h:159
itk::InputDataObjectConstIterator
A forward iterator over inputs of a ProcessObject.
Definition: itkInputDataObjectConstIterator.h:30
itk::ProcessObject::NameArray
std::vector< DataObjectIdentifierType > NameArray
Definition: itkProcessObject.h:164
itk::ProcessObject::GetPrimaryInput
const DataObject * GetPrimaryInput() const
Definition: itkProcessObject.h:608
itk::ProcessObject::m_OutputInformationMTime
TimeStamp m_OutputInformationMTime
Definition: itkProcessObject.h:950
itk::ProcessObject::DataObjectIdentifierType
DataObject::DataObjectIdentifierType DataObjectIdentifierType
Definition: itkProcessObject.h:161
itk::ProcessObject::GetInput
DataObject * GetInput(DataObjectPointerArraySizeType idx)
Definition: itkProcessObject.h:548
itk::ProcessObject::m_MultiThreader
itk::SmartPointer< MultiThreaderType > m_MultiThreader
Definition: itkProcessObject.h:989
itk::DataObjectIterator
A forward iterator over the DataObject of a ProcessObject.
Definition: itkDataObjectIterator.h:30
itk::ProcessObject::m_UpdateThreadID
std::thread::id m_UpdateThreadID
Definition: itkProcessObject.h:985
itk::ProcessObject::progressFloatToFixed
static uint32_t progressFloatToFixed(float f)
Definition: itkProcessObject.h:927
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ProcessObject::m_NumberOfRequiredInputs
DataObjectPointerArraySizeType m_NumberOfRequiredInputs
Definition: itkProcessObject.h:971
itk::ProcessObject::DataObjectPointerArraySizeType
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
Definition: itkProcessObject.h:193
itkDataObject.h
itk::ProcessObject::GetProgress
virtual float GetProgress() const
Get the execution progress of a process object.
Definition: itkProcessObject.h:325
itk::ProcessObject::m_IndexedInputs
std::vector< DataObjectPointerMap::iterator > m_IndexedInputs
Definition: itkProcessObject.h:965
itk::ProcessObject::m_Outputs
DataObjectPointerMap m_Outputs
Definition: itkProcessObject.h:963
itk::ProcessObject::DataObjectPointerMap
std::map< DataObjectIdentifierType, DataObjectPointer > DataObjectPointerMap
Definition: itkProcessObject.h:958
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
itk::ITK_MAX_THREADS
constexpr std::vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:74
itk::ProcessObject::NameSet
std::set< DataObjectIdentifierType > NameSet
Definition: itkProcessObject.h:975
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itkThreadSupport.h
itk::ProcessObject::m_AbortGenerateData
bool m_AbortGenerateData
Definition: itkProcessObject.h:981
itk::InputDataObjectIterator
A forward iterator over inputs of a ProcessObject.
Definition: itkInputDataObjectIterator.h:30
itk::ProcessObject::ReleaseDataFlagOn
void ReleaseDataFlagOn()
Definition: itkProcessObject.h:466
itk::TimeStamp
Generate a unique, increasing time value.
Definition: itkTimeStamp.h:60
itk::ProcessObject::GetPrimaryOutput
DataObject * GetPrimaryOutput()
Definition: itkProcessObject.h:737
itk::DataObject::DataObjectIdentifierType
std::string DataObjectIdentifierType
Definition: itkDataObject.h:304
itk::DataObjectConstIterator
A forward iterator over the DataObject of a ProcessObject.
Definition: itkDataObjectConstIterator.h:30
itk::ProcessObject::m_CachedInputReleaseDataFlags
std::map< DataObjectIdentifierType, bool > m_CachedInputReleaseDataFlags
Definition: itkProcessObject.h:969
itk::ProcessObject::GetInput
const DataObject * GetInput(DataObjectPointerArraySizeType idx) const
Definition: itkProcessObject.h:553
itk::ProcessObject::ReleaseDataFlagOff
void ReleaseDataFlagOff()
Definition: itkProcessObject.h:471
itk::OutputDataObjectConstIterator
A forward iterator over outputs of a ProcessObject.
Definition: itkOutputDataObjectConstIterator.h:30
itk::ProcessObject::GetPrimaryOutput
const DataObject * GetPrimaryOutput() const
Definition: itkProcessObject.h:742
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject::EnlargeOutputRequestedRegion
virtual void EnlargeOutputRequestedRegion(DataObject *)
Definition: itkProcessObject.h:425
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::OutputDataObjectIterator
A forward iterator over outputs of a ProcessObject.
Definition: itkOutputDataObjectIterator.h:30
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itkNumericTraits.h
itk::ProcessObject::m_Progress
std::atomic< uint32_t > m_Progress
Definition: itkProcessObject.h:982
itk::uint32_t
::uint32_t uint32_t
Definition: itkIntTypes.h:33
itk::ProcessObject::m_RequiredInputNames
NameSet m_RequiredInputNames
Definition: itkProcessObject.h:978
itk::ProcessObject::GetMultiThreader
MultiThreaderType * GetMultiThreader() const
Definition: itkProcessObject.h:507
itk::ProcessObject::m_NumberOfWorkUnits
ThreadIdType m_NumberOfWorkUnits
Definition: itkProcessObject.h:990
itk::ProcessObject::GetPrimaryInputName
virtual const char * GetPrimaryInputName() const
Definition: itkProcessObject.h:618
itk::ProcessObject::progressFixedToFloat
static constexpr float progressFixedToFloat(uint32_t fixed)
Definition: itkProcessObject.h:916
itk::ProcessObject::m_NumberOfRequiredOutputs
DataObjectPointerArraySizeType m_NumberOfRequiredOutputs
Definition: itkProcessObject.h:972
itk::DataObject::Pointer
SmartPointer< Self > Pointer
Definition: itkDataObject.h:301
itk::ProcessObject::GetPrimaryInput
DataObject * GetPrimaryInput()
Definition: itkProcessObject.h:603
itk::ProcessObject::m_Inputs
DataObjectPointerMap m_Inputs
Definition: itkProcessObject.h:962
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::ProcessObject::GetPrimaryOutputName
virtual const char * GetPrimaryOutputName() const
Definition: itkProcessObject.h:714