MeVisLab Toolbox Reference
mlModule.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_MODULE_H
14 #define ML_MODULE_H
15 
18 
19 // ML-includes
20 #include "mlInitSystemML.h"
21 #include "mlRuntimeSubClass.h"
22 #include "mlBase.h"
23 #include "mlFields.h"
24 #include "mlFieldContainer.h"
25 
26 #include "mlModuleInterfaces.h"
28 #include "mlPagedImage.h"
29 
30 #include <mlMetaProfilePtr.h>
31 
32 
35 #define ML_ADD_FIELD(NAME_P, TYPE_P, VALUE_P) \
36 (static_cast<TYPE_P##Field*> (this->addField(NAME_P, #TYPE_P, VALUE_P)));
37 
39 
40 // Namespace ML. It is recommended that all ML classes and modules reside in this
41 // namespace to avoid collisions with types of other libraries.
42 ML_START_NAMESPACE
43 
44 // ML-classes
45 class SubImage;
46 class PagedImage;
47 class ProcessAllPagesHandler;
48 
49 //-------------------------------------------------------------------------
52 
149 //-------------------------------------------------------------------------
151 {
152 
153  //---------------------------------------------------------------------------
155  //---------------------------------------------------------------------------
156  friend class Host;
157  friend class PagedImage;
158  friend class PageRequest;
159  friend class TileRequest;
160  friend class ModuleCalculateOutputImageHandler;
161  friend class State;
162  friend class ClassicHost;
163 
165 
166 public:
167  //---------------------------------------------------------------------------
170  //---------------------------------------------------------------------------
171 
179 public: Module(int numInputImages, int numOutputImages);
180 
186 public: ~Module() override;
188 
189 
190 
191  //---------------------------------------------------------------------------
194  //---------------------------------------------------------------------------
195 
202 protected: void setOutputImageInplace(MLint outputIndex=0, MLint inputIndex=0);
203 
207 protected: void unsetOutputImageInplace(MLint outputIndex=0);
208 
213 public: MLint getOutputImageInplace(MLint outputIndex=0) const;
214 
215 
216 
222 protected: void setBypass(MLint outputIndex=0, MLint inputIndex=0);
223 
227 public: MLint getBypass(MLint outputIndex=0) const;
228 
232 protected: void setClampPagesToImageExtent(bool flag = true);
233 
237  NO_THREAD_SUPPORT = PagedImage::NO_THREAD_SUPPORT,
238  MULTITHREADED = PagedImage::MULTITHREADED,
239  IO_THREAD = PagedImage::IO_THREAD,
240  };
241 
244 protected: void setThreadSupport(THREAD_SUPPORT supportMode);
245 
249 
250 
254 protected: void permitRecursiveHandleNotifications(bool enable);
255 
259 
266 
268 
269 
270  //---------------------------------------------------------------------------
273  //---------------------------------------------------------------------------
275 public: enum PERMITTED_TYPES {
279 };
280 
296 protected: void setVoxelDataTypeSupport(PERMITTED_TYPES permTypes);
297 
302 
305 public: bool isSupportedVoxelDataType(MLDataType dt) const;
307 
308  //---------------------------------------------------------------------------
311  //---------------------------------------------------------------------------
312 
314 public: enum INPUT_STATE {
315  DISCONNECTED = 0, //<! Defines that the input is disconnected. In the case of handleInput, this means that the input image cannot be updated to a valid image.
316  CONNECTED_BUT_INVALID = 1, //<! Defines that the input is connected but has no valid data.
317  CONNECTED_AND_VALID = 2, //<! Defines that the input is connected and valid.
318  CONNECTED_BUT_NEEDS_UPDATE = 3, //<! Defines that the input is connected and needs to be updated via updateImageProperties(). After the update, it may either become CONNECTED_AND_VALID or CONNECTED_BUT_INVALID.
319 };
320 
322 public: enum INPUT_HANDLE {
323  INVALIDATE = 0, //<! Used as return value of handleInput() to indicate that the given input is not allowed to be invalid and the output images of the module should be invalidated without calling calculateOutputImageProperties().
324  ALLOW_INVALID_INPUT = 2, //<! Used as return value of handleInput() to indicate that the given input is allowed to be invalid. calculateOutputImageProperties() will be called and getInputImage() for that input index will return NULL.
325 };
326 
353 public: virtual INPUT_HANDLE handleInput(int /*inputIndex*/, INPUT_STATE /*state*/) const { return INVALIDATE; }
354 
363 
370 
371 
372 
373  //---------------------------------------------------------------------------
376  //---------------------------------------------------------------------------
377 
378 public:
383 
386 
389 
391  PagedImage* getOutputImage(MLint outputIndex=0) const;
392 
396  InputConnectorField* addInputImage(const char* name = nullptr);
397 
402  OutputConnectorField* addOutputImage(const char* name = nullptr);
403 
404  //---------------------------------------------------------------------------
417  //---------------------------------------------------------------------------
418 public: PagedImage* getInputImage(MLint inputIndex, bool getReal=false) const;
419 
420  //---------------------------------------------------------------------------
427  //---------------------------------------------------------------------------
428 public: PagedImage *getUpdatedInputImage(MLint inputIndex, bool getReal=false) const;
429 
431 
432 
433 
434  //---------------------------------------------------------------------------
437  // These functions are identical to those from
438  // Host::getDefaultHost() and are
439  // wrapped here for convenience.
440  // See mlHost.h for documentation.
441  //---------------------------------------------------------------------------
442 public:
443 
446  [[nodiscard]]
448  SubImageBox loc,
449  MLDataType datatype,
450  void** data,
451  const ScaleShiftData &scaleShiftData=ScaleShiftData(),
452  MLRequestProgressCB* progressCallback = nullptr,
453  void* progressCallbackUserData = nullptr);
454 
458  [[nodiscard]]
460  SubImage &subimg,
461  const ScaleShiftData &scaleShiftData = ScaleShiftData(),
462  MLRequestProgressCB* progressCallback = nullptr,
463  void* progressCallbackUserData = nullptr);
464 
468 
470  static void freeTile(void* data);
471 
473  static void updateProgress(const char* info1, const char* info2);
474 
476  static bool shouldTerminate();
477 
480 
491  static void adaptPageExtent (ImageVector& pageExtent,
492  MLDataType imageType,
493  const ImageVector& newImgExtent,
494  const ImageVector& oldImgExtent,
495  const ImageVector& pageUnit = ImageVector(0),
496  const ImageVector& minPageExtent = ImageVector(0),
497  const ImageVector& maxPageExtent = ImageVector(0));
498 
500 
501  //---------------------------------------------------------------------------
504  //---------------------------------------------------------------------------
505 
512  static std::string getVoxelValueAsString(PagedImage* image,
513  const ImageVector& position,
514  MLErrorCode* errorCode=nullptr,
515  const std::string& errorResult="");
517 
518 
519 
520  //---------------------------------------------------------------------------
523  //---------------------------------------------------------------------------
524 
554 
570 protected:
571  virtual void calculateOutputImageProperties([[maybe_unused]] int outputIndex, [[maybe_unused]] PagedImage* outputImage) {}
572 
573 protected:
580  virtual SubImageBox calculateInputSubImageBox(int /*inputIndex*/,
581  const SubImageBox& outputSubImageBox,
582  int /*outputIndex*/)
583  {
584  return outputSubImageBox;
585  };
586 
601 protected: virtual void calculateOutputSubImage(SubImage *outputSubImage, int outputIndex, SubImage *inputSubImages);
602 
604 
605  //---------------------------------------------------------------------------
608  //---------------------------------------------------------------------------
609 
617 protected: virtual void provideCustomPage(int /*outputIndex*/, const SubImageBox& /*pageBox*/, MLMemoryBlockHandle& /*resultPage*/) {};
618 
620 
621  //---------------------------------------------------------------------------
624  //---------------------------------------------------------------------------
662 public:
663  [[nodiscard]]
664  MLErrorCode processAllPages(int outputIndex = -1,
665  SubImageBox region = SubImageBox(),
666  MLRequestProgressCB* progressCallback = nullptr,
667  void* progressCallbackUserData = nullptr);
668 
674 public:
675  [[nodiscard]]
677  SubImageBox region = SubImageBox(),
678  MLRequestProgressCB* progressCallback = nullptr,
679  void* progressCallbackUserData = nullptr);
680 
685 public:
686  [[nodiscard]]
687  MLErrorCode processMissingPages(int outputIndex = 0,
688  SubImageBox region = SubImageBox(),
689  MLRequestProgressCB* progressCallback = nullptr,
690  void* progressCallbackUserData = nullptr);
692 
693 
694 
695  //---------------------------------------------------------------------------
698  //---------------------------------------------------------------------------
699 
701 public: void clearOutputImage(MLint i=0);
703 
706 
707 
708 
709 public:
710  //---------------------------------------------------------------------------
713  //---------------------------------------------------------------------------
716 protected: void handleNotificationOff();
717 
720 protected: void handleNotificationOn();
721 
723 protected: bool isHandleNotificationOn();
725 
726 
727  //---------------------------------------------------------------------------
730  //---------------------------------------------------------------------------
731 
734 public: bool isInputImageField(Field* field) const;
735 
737 public: void touchOutputImageFields() const;
738 
753 protected: virtual void handleNotification(Field* /*field*/) {}
754 
758 public: virtual void beginSaveFields() {}
759 
763 public: virtual void endSaveFields() {}
765 
768 protected: void handleNotificationInternal(Field* field, FieldSensor::Strength strength) override;
769 
770 
771  //---------------------------------------------------------------------------
774  //---------------------------------------------------------------------------
776 public: static size_t getNumModules();
777 
779 public: static Module* getModule(size_t index);
780 
782 public: static MLint findModuleIndex(Module& module);
783 
786 public: static void destroyModule();
788 
791 
793 private: void setWasVisited(bool flag) { _visitedFlag = flag; }
794 
796 private: bool wasVisited() { return _visitedFlag; }
797 
799 
800 private:
801  //-----------Private methods:
805  bool _addBaseOp(Module& module);
806 
810  MLint _removeBaseOp(Module& module);
811 
812 
813  //-----------Private members:
815  std::vector <OutputConnectorField*> _outputConnectorFields;
816 
818  std::vector <InputConnectorField*> _inputConnectorFields;
819 
821  THREAD_SUPPORT _threadSupport;
822 
829  MLint _handleNotificationEntryCounter;
830 
834  bool _permitRecursiveHandleNotifications;
835 
837  bool _visitedFlag;
838 
840  PERMITTED_TYPES _permittedTypes;
841 
846  MLint _handleNotificationBlockCounter;
847 
849  mutable MLMetaProfilePtr _metaProfilePtr;
850 
852  static std::vector<Module*> _instantiatedModules;
853 
860 
861 public:
862 
865  const char* getTypeNameFast() const { return getTypeId() ? getTypeId()->getName() : "<unregistered_module>"; }
866 
868 };
869 
871 {
872 public:
873  ModuleDiagnosisStackScope(Module* module) : _module(module) { if (pushModuleForDiagnosis) { pushModuleForDiagnosis(_module); } }
874  ~ModuleDiagnosisStackScope() { if (popModuleForDiagnosis) { popModuleForDiagnosis(_module); } }
875 
876  typedef void (*DiagnosisModuleStackCallback)(void*);
877 
878  static void setCallbacksForDiagnosisModuleStack(DiagnosisModuleStackCallback pushCallback, DiagnosisModuleStackCallback popCallback)
879  {
880  pushModuleForDiagnosis = pushCallback;
881  popModuleForDiagnosis = popCallback;
882  }
883 
884 private:
885  static DiagnosisModuleStackCallback pushModuleForDiagnosis;
886  static DiagnosisModuleStackCallback popModuleForDiagnosis;
887 
888  Module* _module;
889 };
890 
891 ML_END_NAMESPACE
892 
893 #endif
894 
895 
The strong handle of a MLMemoryBlock.
The pointer is automatically reset when the metaprofile is destroyed.
Base class for the calculation of pages of an output image (PagedImage) of a Module.
Classic ML host used internally to calculate getTile and processAllPages requests.
Definition: mlClassicHost.h:39
Defines the class FieldContainer to encapsulate a vector of fields (see class Field).
Strength
Enumeration type describing the strength of notifications.
Definition: mlFieldSensor.h:44
Base class for all fields used in the ML.
Definition: mlField.h:73
The Host is the central image processing class in the ML.
Definition: mlHost.h:115
Field to encapsulate a pointer to an input connector that represents a module input.
Definition: mlFields.h:685
ModuleDiagnosisStackScope(Module *module)
Definition: mlModule.h:873
static void setCallbacksForDiagnosisModuleStack(DiagnosisModuleStackCallback pushCallback, DiagnosisModuleStackCallback popCallback)
Definition: mlModule.h:878
Base class for an image processing module of the ML.
Definition: mlModule.h:151
static void adaptPageExtent(ImageVector &pageExtent, MLDataType imageType, const ImageVector &newImgExtent, const ImageVector &oldImgExtent, const ImageVector &pageUnit=ImageVector(0), const ImageVector &minPageExtent=ImageVector(0), const ImageVector &maxPageExtent=ImageVector(0))
Adapt page size.
static void updateProgress(const char *info1, const char *info2)
For documentation see Host::updateProgress().
MLMetaProfilePtr & getMetaProfile() const
Returns the metaprofile for the base operator.
void handleNotificationOff()
Decreases lock counter for handleNotification calls.
bool isHandleNotificationOn()
Returns true if handleNotification calls are permitted; otherwise, it returns false.
bool isSupportedVoxelDataType(MLDataType dt) const
Checks whether a given data type is supported on the module as configured with setVoxelDataTypeSuppor...
MLint getOutputImageInplace(MLint outputIndex=0) const
Returns the optimization flag: returns the index of input image whose input tile is used also as outp...
MLErrorCode processAllPages(ProcessAllPagesHandler &handler, SubImageBox region=SubImageBox(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
Processes input images with a ProcessAllPagesHandler handler on a temporary output image (which alloc...
virtual void calculateOutputImageProperties([[maybe_unused]] int outputIndex, [[maybe_unused]] PagedImage *outputImage)
Set properties of output image outputImage with output index outputIndex.
Definition: mlModule.h:571
void setOutputImageInplace(MLint outputIndex=0, MLint inputIndex=0)
Sets the optimization flag: if calculating a page in calculateOutputSubImage(), the output image page...
void touchOutputImageFields() const
Touches all output image fields to indicate that the images have changed.
~Module() override
Destructor: Destroys this module.
INPUT_HANDLE
See documentation of handleInput.
Definition: mlModule.h:322
Module(int numInputImages, int numOutputImages)
Constructor: Initializes numInputImages input images and numOutputImages output images of the module.
MLErrorCode processMissingPages(int outputIndex=0, SubImageBox region=SubImageBox(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
Processes all missing pages on the given output image.
static Module * getModule(size_t index)
Returns a pointer to instantiated module index.
OutputConnectorField * addOutputImage(const char *name=nullptr)
Adds a new output image with name to the module.
virtual void beginSaveFields()
Called before all fields data contents of this modules are saved.
Definition: mlModule.h:758
static std::string getVoxelValueAsString(PagedImage *image, const ImageVector &position, MLErrorCode *errorCode=nullptr, const std::string &errorResult="")
Returns the voxel value at position position of the image image.
void setClampPagesToImageExtent(bool flag=true)
Enables/disables page clamping on all output images.
void permitRecursiveHandleNotifications(bool enable)
If this flag is set to enable =true, recursive entries in handleNotification are permitted.
INPUT_STATE
See documentation of handleInput.
Definition: mlModule.h:314
INPUT_STATE getInputState(MLint index)
Returns the current state of the input connection.
void setBypass(MLint outputIndex=0, MLint inputIndex=0)
Sets the input image whose pages can also be used instead of output pages for the output image at out...
void setThreadSupport(THREAD_SUPPORT supportMode)
Passes any THREAD_SUPPORT supportMode to decide whether and what type of multithreading is supported ...
bool isInputImageField(Field *field) const
Returns whether the given field is an input image field.
static bool shouldTerminate()
For documentation see Host::shouldTerminate().
virtual CalculateOutputImageHandler * createCalculateOutputImageHandler(PagedImage *outputImage)
Creates the CalculateOutputImageHandler for the given output image outputImage.
void handleNotificationOn()
Increases lock counter for handleNotification calls.
MLint getHandleNotificationEntryCounter() const
A counter returning the current number of (recursive re-)entries in handleNotification.
bool areRecursiveHandleNotificationsPermitted() const
Returns whether recursive entries in handleNotification are permitted.
const char * getTypeNameFast() const
Macro to declare methods for the ML runtime type system (see mlRuntimeSubClass.h).
Definition: mlModule.h:865
void setVoxelDataTypeSupport(PERMITTED_TYPES permTypes)
Specifies which types this module supports.
PERMITTED_TYPES
See documentation of setVoxelDataTypeSupport.
Definition: mlModule.h:275
@ ALL_REGISTERED_TYPES
Definition: mlModule.h:278
@ ONLY_SCALAR_TYPES
Definition: mlModule.h:276
@ ONLY_DEFAULT_TYPES
Definition: mlModule.h:277
MLint getNumInputImages() const
Returns the number of input images of this module.
PagedImage * getOutputImage(MLint outputIndex=0) const
Returns the output image outputIndex. The index needs to be in the range [0, getNumOutputImages()-1].
MLint getNumOutputImages() const
Returns the number of output images of this module.
PagedImage * getInputImage(MLint inputIndex, bool getReal=false) const
Returns the output image of the module connected to input inputIndex.
OutputConnectorField * getOutputImageField(MLint i=0) const
Returns the field representing output image i. i must be a valid index.
THREAD_SUPPORT
Enumerator deciding whether and which type of multithreading is supported by this module.
Definition: mlModule.h:236
InputConnectorField * addInputImage(const char *name=nullptr)
Adds a new input image with name to the module.
static MLErrorCode getTile(PagedImage *image, SubImageBox loc, MLDataType datatype, void **data, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
For documentation see Host::getTile() function with same signature.
virtual void endSaveFields()
Called after saving all field contents of this module.
Definition: mlModule.h:763
MLint getBypass(MLint outputIndex=0) const
Returns the currently bypass index or -1 if bypassing if disabled (default).
virtual void calculateOutputSubImage(SubImage *outputSubImage, int outputIndex, SubImage *inputSubImages)
Calculates page outputSubImage of output image with index outputIndex given the input image tiles in ...
static void freeTile(void *data)
For documentation see Host::freeTile().
void unsetOutputImageInplace(MLint outputIndex=0)
Clears the optimization flag: output page of output at outputIndex and input tile shall use different...
static void destroyModule()
Destroys static allocated tables to avoid memory leaks.
PagedImage * getUpdatedInputImage(MLint inputIndex, bool getReal=false) const
Convenience method for a safe access to the input image at index inputIndex.
THREAD_SUPPORT getThreadSupport() const
Returns the multithreading mode supported by this module, default is NO_THREAD_SUPPORT.
static MLErrorCode getTile(PagedImage *image, SubImage &subimg, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
For documentation see Host::getTile() function with same signature and Module passed as reference.
InputConnectorField * getInputImageField(MLint i) const
Returns the field representing input image i. i must be a valid index.
static MLint findModuleIndex(Module &module)
Returns the index of Module module. If not found, -1 is returned.
void clearOutputImage(MLint i=0)
Clears cached output image pages of output image i.
static MLErrorCode updateImageProperties(PagedImage *image)
For documentation see Host::updateImageProperties() function with same signature.
void handleNotificationInternal(Field *field, FieldSensor::Strength strength) override
Called from FieldContainer whenever a field changes.
virtual SubImageBox calculateInputSubImageBox(int, const SubImageBox &outputSubImageBox, int)
Called by the Host to determine which input image region (tile) of input inputIndex is required to ca...
Definition: mlModule.h:580
MLErrorCode processAllPages(int outputIndex=-1, SubImageBox region=SubImageBox(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
Processes all pages of a module for easy implementation of page-based image processing of complete im...
static size_t getNumModules()
Returns the number of instantiated modules.
virtual void provideCustomPage(int, const SubImageBox &, MLMemoryBlockHandle &)
This method allows a Module to provide its own pages to the Host for the outputIndex output image.
Definition: mlModule.h:617
virtual void handleNotification(Field *)
Called when any field data in the field container of this module is modified.
Definition: mlModule.h:753
PERMITTED_TYPES getVoxelDataTypeSupport() const
Returns the current state of supported voxel datatypes.
static Field * getPressedNotifyField()
For documentation see Host::getPressedNotifyField().
INPUT_STATE getUpdatedInputState(MLint index)
Returns the updated state of the input image by calling updateImageProperties(index) and returning ge...
virtual INPUT_HANDLE handleInput(int, INPUT_STATE) const
This method may be reimplemented to enable support for invalid input images on the module.
Definition: mlModule.h:353
Field to encapsulate a pointer to an output connector which represents a module output.
Definition: mlFields.h:634
A PageRequest represents the request for the calculation of a single page of a PagedImage.
Definition: mlPageRequest.h:32
The class PagedImage, representing a fragmented image that manages properties and data of an image lo...
Definition: mlPagedImage.h:66
@ MULTITHREADED
< The calculation of the image data can be called from multiple threads.
Definition: mlPagedImage.h:80
@ IO_THREAD
< The calculation of the image data can be called from a designated I/O thread.
Definition: mlPagedImage.h:81
@ NO_THREAD_SUPPORT
< The image can only be calculated from the main thread.
Definition: mlPagedImage.h:79
Base class for handlers that are used for the Module::processAllPages facility.
This class manages/represents a rectangular 6D image region that is organized linearly in memory.
Definition: mlSubImage.h:75
A TileRequest either represents the input subimage that is needed by a PageRequest or if it is a root...
Definition: mlTileRequest.h:50
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Definition: mlMacros.h:23
#define ML_ABSTRACT_CLASS_HEADER(className)
Similar to ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non-existing export symbol.
MLint32 MLDataType
MLDataType.
Definition: mlTypeDefs.h:596
MLint32 MLErrorCode
Type of an ML Error code.
Definition: mlTypeDefs.h:716
#define MLEXPORT
To export symbols from a DLL/shared object, we need to mark them with the MLEXPORT symbol.
void MLRequestProgressCB(void *usrData, double progress)
Definition: mlTypeDefs.h:1202
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:490
TScaleShiftData< MLdouble > ScaleShiftData
Double version of TScaleShiftData for maximum reasonable precision.
TSubImageBox< MLint > SubImageBox
Defines the standard SubImageBox type used in the ML. Its size varies with the size of the MLint type...
TImageVector< MLint > ImageVector
Defines the standard ImageVector type that is used by the ML for indexing and coordinates.