ML 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 are in this
41// name space to avoid collisions with types of other libraries.
42ML_START_NAMESPACE
43
44// ML-classes
45class SubImage;
46class PagedImage;
47class 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
166public:
167 //---------------------------------------------------------------------------
170 //---------------------------------------------------------------------------
171
180public: Module(int numInputImages, int numOutputImages);
181
187public: ~Module() override;
189
190
191
192 //---------------------------------------------------------------------------
195 //---------------------------------------------------------------------------
196
203protected: void setOutputImageInplace(MLint outputIndex=0, MLint inputIndex=0);
204
208protected: void unsetOutputImageInplace(MLint outputIndex=0);
209
214public: MLint getOutputImageInplace(MLint outputIndex=0) const;
215
216
217
223protected: void setBypass(MLint outputIndex=0, MLint inputIndex=0);
224
228public: MLint getBypass(MLint outputIndex=0) const;
229
233protected: void setClampPagesToImageExtent(bool flag = true);
234
238 NO_THREAD_SUPPORT = PagedImage::NO_THREAD_SUPPORT,
241 };
242
245protected: void setThreadSupport(THREAD_SUPPORT supportMode);
246
250
251
255protected: void permitRecursiveHandleNotifications(bool enable);
256
260
267
269
270
271 //---------------------------------------------------------------------------
274 //---------------------------------------------------------------------------
281
297protected: void setVoxelDataTypeSupport(PERMITTED_TYPES permTypes);
298
303
308
309 //---------------------------------------------------------------------------
312 //---------------------------------------------------------------------------
313
315public: enum INPUT_STATE {
316 DISCONNECTED = 0, //<! defines that the input is disconnected. In the case of handleInput, this means that the input image can not be updated to a valid image.
317 CONNECTED_BUT_INVALID = 1, //<! defines that the input is connected but has no valid data.
318 CONNECTED_AND_VALID = 2, //<! defines that the input is connected and valid.
319 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.
320};
321
323public: enum INPUT_HANDLE {
324 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().
325 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.
326};
327
354public: virtual INPUT_HANDLE handleInput(int /*inputIndex*/, INPUT_STATE /*state*/) const { return INVALIDATE; }
355
364
371
372
373
374 //---------------------------------------------------------------------------
377 //---------------------------------------------------------------------------
378
379public:
384
387
390
392 PagedImage* getOutputImage(MLint outputIndex=0) const;
393
397 InputConnectorField* addInputImage(const char* name = nullptr);
398
403 OutputConnectorField* addOutputImage(const char* name = nullptr);
404
405 //---------------------------------------------------------------------------
418 //---------------------------------------------------------------------------
419public: PagedImage* getInputImage(MLint inputIndex, bool getReal=false) const;
420
421 //---------------------------------------------------------------------------
428 //---------------------------------------------------------------------------
429public: PagedImage *getUpdatedInputImage(MLint inputIndex, bool getReal=false) const;
430
432
433
434
435 //---------------------------------------------------------------------------
438 // These functions are identical to those from
439 // Host::getDefaultHost() and are
440 // wrapped here for convenience.
441 // See mlHost.h for documentation.
442 //---------------------------------------------------------------------------
443public:
444
447 [[nodiscard]]
449 SubImageBox loc,
450 MLDataType datatype,
451 void** data,
452 const ScaleShiftData &scaleShiftData=ScaleShiftData(),
453 MLRequestProgressCB* progressCallback = nullptr,
454 void* progressCallbackUserData = nullptr);
455
459 [[nodiscard]]
461 SubImage &subimg,
462 const ScaleShiftData &scaleShiftData = ScaleShiftData(),
463 MLRequestProgressCB* progressCallback = nullptr,
464 void* progressCallbackUserData = nullptr);
465
469
471 static void freeTile(void* data);
472
474 static void updateProgress(const char* info1, const char* info2);
475
477 static bool shouldTerminate();
478
481
492 static void adaptPageExtent (ImageVector& pageExtent,
493 MLDataType imageType,
494 const ImageVector& newImgExtent,
495 const ImageVector& oldImgExtent,
496 const ImageVector& pageUnit = ImageVector(0),
497 const ImageVector& minPageExtent = ImageVector(0),
498 const ImageVector& maxPageExtent = ImageVector(0));
499
501
502 //---------------------------------------------------------------------------
505 //---------------------------------------------------------------------------
506
513 static std::string getVoxelValueAsString(PagedImage* image,
514 const ImageVector& position,
515 MLErrorCode* errorCode=nullptr,
516 const std::string& errorResult="");
518
519
520
521 //---------------------------------------------------------------------------
524 //---------------------------------------------------------------------------
525
555
571protected:
572 virtual void calculateOutputImageProperties([[maybe_unused]] int outputIndex, [[maybe_unused]] PagedImage* outputImage) {}
573
574private:
575 // Not supported anymore:
576 // prevent that anybody overrides this method, so that old deprecated uses will be rejected by the compiler:
577 virtual void calculateOutputImageProperties(int /*outputIndex*/) final {}
578
585protected: virtual SubImageBox calculateInputSubImageBox(int /*inputIndex*/,
586 const SubImageBox& outputSubImageBox,
587 int /*outputIndex*/)
588 {
589 return outputSubImageBox;
590 };
591
606protected: virtual void calculateOutputSubImage(SubImage *outputSubImage, int outputIndex, SubImage *inputSubImages);
607
609
610 //---------------------------------------------------------------------------
613 //---------------------------------------------------------------------------
614
622protected: virtual void provideCustomPage(int /*outputIndex*/, const SubImageBox& /*pageBox*/, MLMemoryBlockHandle& /*resultPage*/) {};
623
625
626 //---------------------------------------------------------------------------
629 //---------------------------------------------------------------------------
667public:
668 [[nodiscard]]
669 MLErrorCode processAllPages(int outputIndex = -1,
670 SubImageBox region = SubImageBox(),
671 MLRequestProgressCB* progressCallback = nullptr,
672 void* progressCallbackUserData = nullptr);
673
679public:
680 [[nodiscard]]
682 SubImageBox region = SubImageBox(),
683 MLRequestProgressCB* progressCallback = nullptr,
684 void* progressCallbackUserData = nullptr);
685
690public:
691 [[nodiscard]]
692 MLErrorCode processMissingPages(int outputIndex = 0,
693 SubImageBox region = SubImageBox(),
694 MLRequestProgressCB* progressCallback = nullptr,
695 void* progressCallbackUserData = nullptr);
697
698
699
700 //---------------------------------------------------------------------------
703 //---------------------------------------------------------------------------
704
706public: void clearOutputImage(MLint i=0);
708
710public: MLMetaProfilePtr& getMetaProfile() const;
711
712
713
714public:
715 //---------------------------------------------------------------------------
718 //---------------------------------------------------------------------------
721protected: void handleNotificationOff();
722
725protected: void handleNotificationOn();
726
728protected: bool isHandleNotificationOn();
730
731
732 //---------------------------------------------------------------------------
735 //---------------------------------------------------------------------------
736
738public: bool isInputImageField(Field* field) const;
739
741public: void touchOutputImageFields() const;
742
757protected: virtual void handleNotification(Field* /*field*/) {}
758
762public: virtual void beginSaveFields() {}
763
767public: virtual void endSaveFields() {}
769
772protected: void handleNotificationInternal(Field* field, FieldSensor::Strength strength) override;
773
774
775 //---------------------------------------------------------------------------
778 //---------------------------------------------------------------------------
780public: static size_t getNumModules();
781
783public: static Module* getModule(size_t index);
784
786public: static MLint findModuleIndex(Module& module);
787
790public: static void destroyModule();
792
795
797private: void setWasVisited(bool flag) { _visitedFlag = flag; }
798
800private: bool wasVisited() { return _visitedFlag; }
801
803
804private:
805 //-----------Private methods:
809 bool _addBaseOp(Module& module);
810
814 MLint _removeBaseOp(Module& module);
815
816
817 //-----------Private members:
819 std::vector <OutputConnectorField*> _outputConnectorFields;
820
822 std::vector <InputConnectorField*> _inputConnectorFields;
823
825 THREAD_SUPPORT _threadSupport;
826
833 MLint _handleNotificationEntryCounter;
834
838 bool _permitRecursiveHandleNotifications;
839
841 bool _visitedFlag;
842
844 PERMITTED_TYPES _permittedTypes;
845
850 MLint _handleNotificationBlockCounter;
851
853 mutable MLMetaProfilePtr _metaProfilePtr;
854
856 static std::vector<Module*> _instantiatedModules;
857
864
865public:
866
869 const char* getTypeNameFast() const { return getTypeId() ? getTypeId()->getName() : "<unregistered_module>"; }
870
872};
873
875{
876public:
877 ModuleDiagnosisStackScope(Module* module) : _module(module) { if (pushModuleForDiagnosis) { pushModuleForDiagnosis(_module); } }
878 ~ModuleDiagnosisStackScope() { if (popModuleForDiagnosis) { popModuleForDiagnosis(_module); } }
879
880 typedef void (*DiagnosisModuleStackCallback)(void*);
881
882 static void setCallbacksForDiagnosisModuleStack(DiagnosisModuleStackCallback pushCallback, DiagnosisModuleStackCallback popCallback)
883 {
884 pushModuleForDiagnosis = pushCallback;
885 popModuleForDiagnosis = popCallback;
886 }
887
888private:
889 static DiagnosisModuleStackCallback pushModuleForDiagnosis;
890 static DiagnosisModuleStackCallback popModuleForDiagnosis;
891
892 Module* _module;
893};
894
895ML_END_NAMESPACE
896
897#endif
898
899
The strong handle of a MLMemoryBlock.
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.
Defines the class FieldContainer to encapsulate a vector of fields for (see class Field).
Strength
Enumeration type describing the strength of notifications.
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 which represents a module input.
Definition mlFields.h:750
ModuleDiagnosisStackScope(Module *module)
Definition mlModule.h:877
static void setCallbacksForDiagnosisModuleStack(DiagnosisModuleStackCallback pushCallback, DiagnosisModuleStackCallback popCallback)
Definition mlModule.h:882
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.
PagedImage * getOutputImage(MLint outputIndex=0) const
Returns output image outputIndex. The index needs to be in the range [0, getNumOutputImages()-1].
static void updateProgress(const char *info1, const char *info2)
For documentation see Host::updateProgress().
const char * getTypeNameFast() const
Macro to declare methods for the ML runtime type system (see mlRuntimeSubClass.h).
Definition mlModule.h:869
InputConnectorField * getInputImageField(MLint i) const
Returns the field representing input image i. i must be a valid index.
void handleNotificationOff()
Decreases lock counter for handleNotification calls.
bool isHandleNotificationOn()
Returns true if handleNotification calls are permitted, otherwise false.
bool isSupportedVoxelDataType(MLDataType dt) const
Check if a given data type is supported on the module as configured with setVoxelDataTypeSupport.
MLint getOutputImageInplace(MLint outputIndex=0) const
Return optimization flag: Return index of input image whose input tile is used also as output page fo...
OutputConnectorField * addOutputImage(const char *name=nullptr)
Adds a new output image with name to the module.
virtual void calculateOutputImageProperties(int outputIndex, PagedImage *outputImage)
Set properties of output image outputImage with output index outputIndex.
Definition mlModule.h:572
MLErrorCode processAllPages(ProcessAllPagesHandler &handler, SubImageBox region=SubImageBox(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
Process input images with a ProcessAllPagesHandler handler on a temporary output image (which allocat...
void setOutputImageInplace(MLint outputIndex=0, MLint inputIndex=0)
Set optimization flag: If calculating a page in calculateOutputSubImage(), the output image page of o...
InputConnectorField * addInputImage(const char *name=nullptr)
Adds a new input image with name to the module.
void touchOutputImageFields() const
Touch 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:323
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.
virtual void beginSaveFields()
Called before all fields data contents of this modules are saved.
Definition mlModule.h:762
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:315
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)
Pass any THREAD_SUPPORT supportMode to decide whether and what type of multithreading is supported by...
bool isInputImageField(Field *field) const
Returns if the given field is an input image field. This can be used in handleNotification(),...
static bool shouldTerminate()
For documentation see Host::shouldTerminate().
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 if recursive entries in handleNotification are permitted.
void setVoxelDataTypeSupport(PERMITTED_TYPES permTypes)
Specifies which types this module supports.
virtual CalculateOutputImageHandler * createCalculateOutputImageHandler(PagedImage *outputImage)
Creates the CalculateOutputImageHandler for the given output image outputImage.
PERMITTED_TYPES
See documentation of setVoxelDataTypeSupport.
Definition mlModule.h:276
@ ALL_REGISTERED_TYPES
Definition mlModule.h:279
@ ONLY_SCALAR_TYPES
Definition mlModule.h:277
@ ONLY_DEFAULT_TYPES
Definition mlModule.h:278
MLint getNumInputImages() const
Returns number of input images of this module.
MLint getNumOutputImages() const
Returns number of output images of this module.
static Module * getModule(size_t index)
Returns pointer to instantiated module index.
THREAD_SUPPORT
Enumerator deciding whether and which type of multithreading is supported by this module.
Definition mlModule.h:237
static Field * getPressedNotifyField()
For documentation see Host::getPressedNotifyField().
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.
PagedImage * getUpdatedInputImage(MLint inputIndex, bool getReal=false) const
Convenience method for safe access to the input image at index inputIndex.
virtual void endSaveFields()
Called after saving all field contents of this module.
Definition mlModule.h:767
MLint getBypass(MLint outputIndex=0) const
Returns the currently bypass index or -1 if bypassing is disabled (default).
virtual void calculateOutputSubImage(SubImage *outputSubImage, int outputIndex, SubImage *inputSubImages)
Calculate page outputSubImage of output image with index outputIndex given the input image tiles in a...
static void freeTile(void *data)
For documentation see Host::freeTile().
void unsetOutputImageInplace(MLint outputIndex=0)
Clear optimization flag: output page of output at outputIndex and input tile shall use different memo...
static void destroyModule()
Destroys static allocated tables to avoid memory leaks.
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.
static MLint findModuleIndex(Module &module)
Returns index of Module module. If not found -1 is returned.
void clearOutputImage(MLint i=0)
Clear 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.
OutputConnectorField * getOutputImageField(MLint i=0) const
Returns field representing output image i. i must be a valid index.
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:585
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 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:622
PagedImage * getInputImage(MLint inputIndex, bool getReal=false) const
Returns the output image of the module connected to input inputIndex.
virtual void handleNotification(Field *)
Called when any field data in the field container of this module is modified.
Definition mlModule.h:757
PERMITTED_TYPES getVoxelDataTypeSupport() const
Returns the current state of supported voxel data types.
MLMetaProfilePtr & getMetaProfile() const
Returns the meta profile for the base operator.
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:354
Field to encapsulate a pointer to an output connector which represents a module output.
Definition mlFields.h:695
A PageRequest represents the request for the calculation of a single page of a PagedImage.
Class which represents an image, which manages properties of an image and image data which is located...
@ MULTITHREADED
< The calculation of the image data can be called from multiple threads
@ IO_THREAD
< The calculation of the image data can be called from a designated IO thread
@ NO_THREAD_SUPPORT
< The image can only be calculated from the main thread
Base class for handlers that are used for the Module::processAllPages facility.
This class manages/represents a rectangular 6d image region which is organized linearly in memory.
Definition mlSubImage.h:75
A TileRequest either represents the input sub image that is needed by a PageRequest or if it is a roo...
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Definition mlMacros.h:23
#define ML_ABSTRACT_CLASS_HEADER(className)
Same like 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)
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
Define 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 which is used by the ML for indexing and coordinates.