ITK  6.0.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  * https://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 "itkIntTypes.h"
36 #include <vector>
37 #include <map>
38 #include <set>
39 #include <algorithm>
40 #include <thread>
41 
42 namespace itk
43 {
44 
45 class MultiThreaderBase;
46 
139 class ITKCommon_EXPORT ProcessObject : public Object
140 {
141 public:
142  ITK_DISALLOW_COPY_AND_MOVE(ProcessObject);
143 
149 
151  itkOverrideGetNameOfClassMacro(ProcessObject);
152 
155 
157  // using ConstDataObjectPointerArray = std::vector< const DataObject * >;
158 
160  using DataObjectPointerArray = std::vector<DataObjectPointer>;
161 
163 
165  using NameArray = std::vector<DataObjectIdentifierType>;
166 
169 
175  NameArray
176  GetInputNames() const;
177 
179  NameArray
180  GetRequiredInputNames() const;
181 
188  GetInputs();
189 
191  bool
192  HasInput(const DataObjectIdentifierType & key) const;
193 
194  using DataObjectPointerArraySizeType = DataObjectPointerArray::size_type;
195 
207  GetNumberOfInputs() const;
208 
211  GetNumberOfOutputs() const;
212 
218  NameArray
219  GetOutputNames() const;
220 
227  GetOutputs();
228 
230  bool
231  HasOutput(const DataObjectIdentifierType & key) const;
232 
235  GetIndexedInputs();
236 
251  GetNumberOfIndexedInputs() const;
252 
263  GetNumberOfValidRequiredInputs() const;
264 
267  GetIndexedOutputs();
268 
271  GetNumberOfIndexedOutputs() const;
272 
288  virtual DataObjectPointer
289  MakeOutput(DataObjectPointerArraySizeType idx);
290 
296  itkSetMacro(AbortGenerateData, bool);
297 
299  itkGetConstReferenceMacro(AbortGenerateData, bool);
300 
302  itkBooleanMacro(AbortGenerateData);
303 
312 #if !defined(ITK_LEGACY_REMOVE)
313  void
314  SetProgress(float progress)
315  {
316  m_Progress = progressFloatToFixed(progress);
317  }
318 #endif
319 
325  virtual float
326  GetProgress() const
327  {
328  return progressFixedToFloat(m_Progress);
329  }
330 
340  void
341  UpdateProgress(float progress);
342 
350  void
351  IncrementProgress(float increment);
352 
374  virtual void
375  Update();
376 
386  virtual void
387  UpdateLargestPossibleRegion();
388 
404  virtual void
405  UpdateOutputInformation();
406 
409  virtual void
410  PropagateRequestedRegion(DataObject * output);
411 
413  virtual void
414  UpdateOutputData(DataObject * output);
415 
425  virtual void
427  {}
428 
435  virtual void
436  ResetPipeline();
437 
454  virtual DataObjectPointer
455  MakeOutput(const DataObjectIdentifierType &);
456 
462  virtual void
463  SetReleaseDataFlag(bool val);
464  virtual bool
465  GetReleaseDataFlag() const;
466  void
468  {
469  this->SetReleaseDataFlag(true);
470  }
471  void
473  {
474  this->SetReleaseDataFlag(false);
475  }
490  itkSetMacro(ReleaseDataBeforeUpdateFlag, bool);
491  itkGetConstReferenceMacro(ReleaseDataBeforeUpdateFlag, bool);
492  itkBooleanMacro(ReleaseDataBeforeUpdateFlag);
496  itkSetClampMacro(NumberOfWorkUnits, ThreadIdType, 1, ITK_MAX_THREADS);
497  itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
501  MultiThreaderType *
503  {
504  return m_MultiThreader;
505  }
506 
508  void
509  SetMultiThreader(MultiThreaderType * threader);
510 
517  virtual void
518  PrepareOutputs();
519 
520 protected:
521  ProcessObject();
522  ~ProcessObject() override;
523 
524  void
525  PrintSelf(std::ostream & os, Indent indent) const override;
526 
527  //
528  // Input Methods
529  //
530 
535  DataObject *
536  GetInput(const DataObjectIdentifierType & key);
537  const DataObject *
538  GetInput(const DataObjectIdentifierType & key) const;
539 
542  DataObject *
544  {
545  return idx < m_IndexedInputs.size() ? m_IndexedInputs[idx]->second.GetPointer() : nullptr;
546  }
547  const DataObject *
549  {
550  return idx < m_IndexedInputs.size() ? m_IndexedInputs[idx]->second.GetPointer() : nullptr;
551  }
560  virtual void
561  SetInput(const DataObjectIdentifierType & key, DataObject * input);
562  virtual void
563  SetNthInput(DataObjectPointerArraySizeType idx, DataObject * input);
564 
566  virtual void
567  AddInput(DataObject * input);
568 
577  virtual void
578  PushBackInput(const DataObject * input);
579  virtual void
580  PopBackInput();
581  virtual void
582  PushFrontInput(const DataObject * input);
583  virtual void
584  PopFrontInput();
585 
592  virtual void
593  RemoveInput(const DataObjectIdentifierType & key);
594  virtual void RemoveInput(DataObjectPointerArraySizeType);
595 
597  DataObject *
599  {
600  return m_IndexedInputs[0]->second;
601  }
602  const DataObject *
604  {
605  return m_IndexedInputs[0]->second;
606  }
610  virtual void
611  SetPrimaryInputName(const DataObjectIdentifierType & key);
612  virtual const char *
614  {
615  return this->m_IndexedInputs[0]->first.c_str();
616  }
620  virtual void
621  SetPrimaryInput(DataObject * object);
622 
629  void
630  SetNumberOfIndexedInputs(DataObjectPointerArraySizeType num);
631 
641  virtual void SetNumberOfRequiredInputs(DataObjectPointerArraySizeType);
642  itkGetConstReferenceMacro(NumberOfRequiredInputs, DataObjectPointerArraySizeType);
643 
644 
649  bool
650  RemoveRequiredInputName(const DataObjectIdentifierType &);
651 
653  bool
654  IsRequiredInputName(const DataObjectIdentifierType &) const;
655 
660  void
661  SetRequiredInputNames(const NameArray &);
662 
673  bool
674  AddRequiredInputName(const DataObjectIdentifierType &);
675  bool
676  AddRequiredInputName(const DataObjectIdentifierType &, DataObjectPointerArraySizeType idx);
677 
688  void
689  AddOptionalInputName(const DataObjectIdentifierType &);
690  void
691  AddOptionalInputName(const DataObjectIdentifierType &, DataObjectPointerArraySizeType idx);
692 
693 
694  //
695  // Output Methods
696  //
697 
699  DataObject *
700  GetOutput(const DataObjectIdentifierType & key);
701  const DataObject *
702  GetOutput(const DataObjectIdentifierType & key) const;
706  virtual void
707  SetPrimaryOutputName(const DataObjectIdentifierType & key);
708  virtual const char *
710  {
711  return this->m_IndexedOutputs[0]->first.c_str();
712  }
716  DataObject *
717  GetOutput(DataObjectPointerArraySizeType i);
718  const DataObject *
719  GetOutput(DataObjectPointerArraySizeType i) const;
723  virtual void
724  SetOutput(const DataObjectIdentifierType & name, DataObject * output);
725 
727  virtual void
728  RemoveOutput(const DataObjectIdentifierType & key);
729 
731  DataObject *
733  {
734  return m_IndexedOutputs[0]->second;
735  }
736  const DataObject *
738  {
739  return m_IndexedOutputs[0]->second;
740  }
744  virtual void
745  SetPrimaryOutput(DataObject * object);
746 
749  virtual void
750  SetNthOutput(DataObjectPointerArraySizeType idx, DataObject * output);
751 
752  virtual void
753  AddOutput(DataObject * output);
754 
755  virtual void
756  RemoveOutput(DataObjectPointerArraySizeType idx);
757 
758  itkSetMacro(NumberOfRequiredOutputs, DataObjectPointerArraySizeType);
759  itkGetConstReferenceMacro(NumberOfRequiredOutputs, DataObjectPointerArraySizeType);
760 
762  void
763  SetNumberOfIndexedOutputs(DataObjectPointerArraySizeType num);
764 
765 
766  DataObjectIdentifierType
767  MakeNameFromInputIndex(DataObjectPointerArraySizeType idx) const;
768  DataObjectIdentifierType
769  MakeNameFromOutputIndex(DataObjectPointerArraySizeType idx) const;
770  DataObjectPointerArraySizeType
771  MakeIndexFromInputName(const DataObjectIdentifierType & name) const;
772  DataObjectPointerArraySizeType
773  MakeIndexFromOutputName(const DataObjectIdentifierType & name) const;
774  bool
775  IsIndexedInputName(const DataObjectIdentifierType &) const;
776  bool
777  IsIndexedOutputName(const DataObjectIdentifierType &) const;
778 
779  //
780  // Pipeline Methods
781  //
782 
794  virtual void
795  VerifyPreconditions() const;
796 
807  virtual void
808  VerifyInputInformation() const;
809 
823  virtual void
824  GenerateInputRequestedRegion();
825 
837  virtual void
838  GenerateOutputRequestedRegion(DataObject * output);
839 
850  virtual void
851  GenerateOutputInformation();
852 
854  virtual void
856  {}
857 
862  virtual void
863  PropagateResetPipeline();
864 
876  virtual void
877  ReleaseInputs();
878 
887  virtual void
888  CacheInputReleaseDataFlags();
889 
893  virtual void
894  RestoreInputReleaseDataFlags();
895 
900  itkGetConstMacro(ThreaderUpdateProgress, bool);
901  itkBooleanMacro(ThreaderUpdateProgress);
902  virtual void
903  SetThreaderUpdateProgress(bool arg);
910  static inline constexpr float
911  progressFixedToFloat(uint32_t fixed)
912  {
913  return static_cast<double>(fixed) / static_cast<double>(std::numeric_limits<uint32_t>::max());
914  }
915 
920  static inline uint32_t
922  {
923  if (f <= 0.0f)
924  {
925  return 0;
926  }
927  if (f >= 1.0f)
928  {
929  return std::numeric_limits<uint32_t>::max();
930  }
931  const double temp = static_cast<double>(f) * std::numeric_limits<uint32_t>::max();
932  return static_cast<uint32_t>(temp);
933  }
938  template <typename TSourceObject>
939  static void
940  MakeRequiredOutputs(TSourceObject & sourceObject, const DataObjectPointerArraySizeType numberOfRequiredOutputs)
941  {
942  sourceObject.ProcessObject::SetNumberOfRequiredOutputs(numberOfRequiredOutputs);
943 
944  for (unsigned int i{}; i < numberOfRequiredOutputs; ++i)
945  {
946  sourceObject.ProcessObject::SetNthOutput(i, sourceObject.TSourceObject::MakeOutput(i));
947  }
948  }
949 
955  bool m_Updating{};
956 
958  TimeStamp m_OutputInformationMTime{};
959 
960 private:
961  DataObjectIdentifierType MakeNameFromIndex(DataObjectPointerArraySizeType) const;
962  DataObjectPointerArraySizeType
963  MakeIndexFromName(const DataObjectIdentifierType &) const;
964 
966  using DataObjectPointerMap = std::map<DataObjectIdentifierType, DataObjectPointer>;
967 
968 
971  DataObjectPointerMap m_Outputs{};
972 
973  std::vector<DataObjectPointerMap::iterator> m_IndexedInputs{};
974  std::vector<DataObjectPointerMap::iterator> m_IndexedOutputs{};
975 
977  std::map<DataObjectIdentifierType, bool> m_CachedInputReleaseDataFlags{};
978 
979  DataObjectPointerArraySizeType m_NumberOfRequiredInputs{};
980  DataObjectPointerArraySizeType m_NumberOfRequiredOutputs{};
981 
983  using NameSet = std::set<DataObjectIdentifierType>;
984 
986  NameSet m_RequiredInputNames{};
987 
989  bool m_AbortGenerateData{};
990  std::atomic<uint32_t> m_Progress{};
991 
992 
993  std::thread::id m_UpdateThreadID{};
994 
998  ThreadIdType m_NumberOfWorkUnits{};
999 
1000  bool m_ThreaderUpdateProgress{ true };
1001 
1003  bool m_ReleaseDataBeforeUpdateFlag{};
1004 
1006  friend class DataObject;
1007 
1008  friend class ProgressReporter;
1010 
1014 
1015  friend class DataObjectIterator;
1018 
1019  friend class TestProcessObject;
1020 };
1021 } // end namespace itk
1022 
1023 #endif
itk::ProcessObject::GenerateData
virtual void GenerateData()
Definition: itkProcessObject.h:855
itk::MultiThreaderBase
A class for performing multithreaded execution.
Definition: itkMultiThreaderBase.h:107
itkObjectFactory.h
itk::ProcessObject::DataObjectPointerArray
std::vector< DataObjectPointer > DataObjectPointerArray
Definition: itkProcessObject.h:160
itk::InputDataObjectConstIterator
A forward iterator over inputs of a ProcessObject.
Definition: itkInputDataObjectConstIterator.h:30
itk::ProcessObject::NameArray
std::vector< DataObjectIdentifierType > NameArray
Definition: itkProcessObject.h:165
itk::ProcessObject::GetPrimaryInput
const DataObject * GetPrimaryInput() const
Definition: itkProcessObject.h:603
itk::ProcessObject::DataObjectIdentifierType
DataObject::DataObjectIdentifierType DataObjectIdentifierType
Definition: itkProcessObject.h:162
itk::ProcessObject::GetInput
DataObject * GetInput(DataObjectPointerArraySizeType idx)
Definition: itkProcessObject.h:543
itk::ProcessObject::m_MultiThreader
itk::SmartPointer< MultiThreaderType > m_MultiThreader
Definition: itkProcessObject.h:997
itk::DataObjectIterator
A forward iterator over the DataObject of a ProcessObject.
Definition: itkDataObjectIterator.h:30
itk::ProcessObject::progressFloatToFixed
static uint32_t progressFloatToFixed(float f)
Definition: itkProcessObject.h:921
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ProcessObject::DataObjectPointerArraySizeType
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
Definition: itkProcessObject.h:194
itk::ITK_MAX_THREADS
constexpr vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:69
itkDataObject.h
itk::ProcessObject::GetProgress
virtual float GetProgress() const
Get the execution progress of a process object.
Definition: itkProcessObject.h:326
itk::ProcessObject::MakeRequiredOutputs
static void MakeRequiredOutputs(TSourceObject &sourceObject, const DataObjectPointerArraySizeType numberOfRequiredOutputs)
Definition: itkProcessObject.h:940
itk::ProcessObject::DataObjectPointerMap
std::map< DataObjectIdentifierType, DataObjectPointer > DataObjectPointerMap
Definition: itkProcessObject.h:966
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:102
itk::ProcessObject::NameSet
std::set< DataObjectIdentifierType > NameSet
Definition: itkProcessObject.h:983
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itkThreadSupport.h
itk::InputDataObjectIterator
A forward iterator over inputs of a ProcessObject.
Definition: itkInputDataObjectIterator.h:30
itk::ProcessObject::ReleaseDataFlagOn
void ReleaseDataFlagOn()
Definition: itkProcessObject.h:467
itk::TimeStamp
Generate a unique, increasing time value.
Definition: itkTimeStamp.h:60
itk::ProcessObject::GetPrimaryOutput
DataObject * GetPrimaryOutput()
Definition: itkProcessObject.h:732
itkIntTypes.h
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::GetInput
const DataObject * GetInput(DataObjectPointerArraySizeType idx) const
Definition: itkProcessObject.h:548
itk::ProcessObject::ReleaseDataFlagOff
void ReleaseDataFlagOff()
Definition: itkProcessObject.h:472
itk::OutputDataObjectConstIterator
A forward iterator over outputs of a ProcessObject.
Definition: itkOutputDataObjectConstIterator.h:30
itk::ProcessObject::GetPrimaryOutput
const DataObject * GetPrimaryOutput() const
Definition: itkProcessObject.h:737
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::ProcessObject::EnlargeOutputRequestedRegion
virtual void EnlargeOutputRequestedRegion(DataObject *)
Definition: itkProcessObject.h:426
itk::TotalProgressReporter
A progress reporter for concurrent threads.
Definition: itkTotalProgressReporter.h:40
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
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:61
itkNumericTraits.h
itk::ProcessObject::GetMultiThreader
MultiThreaderType * GetMultiThreader() const
Definition: itkProcessObject.h:502
itk::ProgressReporter
Implements progress tracking for a filter.
Definition: itkProgressReporter.h:60
itk::ProcessObject::GetPrimaryInputName
virtual const char * GetPrimaryInputName() const
Definition: itkProcessObject.h:613
itk::ProcessObject::progressFixedToFloat
static constexpr float progressFixedToFloat(uint32_t fixed)
Definition: itkProcessObject.h:911
itk::DataObject::Pointer
SmartPointer< Self > Pointer
Definition: itkDataObject.h:301
itk::ProcessObject::GetPrimaryInput
DataObject * GetPrimaryInput()
Definition: itkProcessObject.h:598
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:709