MeVisLab Toolbox Reference
mlVirtualVolume.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_VIRTUAL_VOLUME_H
14 #define ML_VIRTUAL_VOLUME_H
15 
16 
19 
36 // Include dll-specific settings.
37 #include "MLToolsSystem.h"
38 
39 // Include most ml specific things.
40 #include "mlModuleIncludes.h"
41 
42 #include <mlMemoryManager.h>
43 
44 class MLMemoryProfile;
45 
46 ML_START_NAMESPACE
47 
50  class VirtualVolume;
51  class BitImage;
52  struct PageBuffer;
53  template<class DATATYPE> class TVirtualVolume;
54 
55  class VirtualVolumeMemoryTracker;
56 
57  //--------------------------------------------------------------------------------
73  //--------------------------------------------------------------------------------
75 
76  public:
77 
78  // ---- Public member functions:
79 
83  MLDataType dType,
84  MLint maxNumKB = -1,
85  bool areExceptionsOnFlag = false);
86 
96  MLdouble fillVal = 0,
97  MLDataType dType = MLuint8Type,
98  MLint maxNumKB = -1,
99  bool areExceptionsOnFlag = false);
100 
102  VirtualVolume(const ImageVector &extent, const ImageVector& pageExtent,
103  MLdouble fillVal = 0,
104  MLDataType dType = MLuint8Type,
105  MLint maxNumKB = -1,
106  bool areExceptionsOnFlag = false);
107 
109  virtual ~VirtualVolume() { _reset(); }
110 
112  inline MLint getDim() const { return _dim; }
113 
115  inline const SubImageBox &getBox() const { return _pagedImgBox; }
116 
118  inline ImageVector getExtent() const { return _pagedImgBox.getExtent(); }
119 
121  inline MLDataType getDataType() const { return _dataType; }
122 
125  inline Module *getModule() const { return _module; }
126 
129  inline PagedImage *getInputImage() const { return _pagedImg; }
130 
132  inline MLint getInputIndex() const { return _module ? _outIdx : -1; }
133 
135  inline bool isValid() const { return _valid; }
136 
138  inline void invalidate() { _valid = false; _cleanUpMemory(); }
139 
141  inline MLuint getNumMappedPages() const { return _numMappedPages; }
142 
144  inline MLuint getNumWrittenPages() { _updateWrittenPages(); return _numWrittenPages;}
145 
147  inline MLuint getNumPages() { return _numMappedPages+getNumWrittenPages(); }
148 
150  inline MLuint getNumBytes() { return getNumPages()*_numPageBytes; }
151 
153  inline bool areExceptionsOn() const { return _areExceptionsOn; }
154 
156  inline const ImageVector &getPageExtent() const { return _pageExt; }
157 
159  inline const ImageVector &getPageArrayExtent() const { return _pageArrayExt; }
160 
164 
172 
178 
181  enum PageMapped { IsMapped, IsUnMapped };
182 
188  typedef void (*PageFunc) (VirtualVolume *obj, PageBuffer *page, PageMapped mapped, void *userData);
189 
194  void registerPageFunction(PageFunc pFunc, void *userData);
195 
210  void resize(const ImageVector &newExt);
211 
223  PagedImage &image,
224  const ImageVector &shift,
225  const ScaleShiftData &scaleShift=ScaleShiftData());
226 
227 
234  void copyToSubImage(SubImage &outSubImg);
235 
236 #if 0
237  // Copy area from inImg into virtual volume. This non-template version is still
238  // not implemented but desirable. Only valid areas are copied; regions outside
239  // input or virtual volume areas are not handled.
240  // Errors are reported by thrown exceptions if exception handling is enabled;
241  // otherwise they lead to ML_PRINT_ERROR or ML_PRINT_FATAL_ERROR messages.
242  // Only valid regions of the virtual volume are written; box is clipped against
243  // the virtual volume extents before writing the data region.
244  void copyFromSubImg(const SubImage &inImg,
245  const SubImageBox &box,
246  const ImageVector &pos);
247 #endif
248 
256  BitImage *getUsedPagesMask(bool enableMappedPages=true,
257  bool enableWrittenPages=true) const;
258 
262 
265  static void destroyTypedVirtualVolume(void *tVV, MLDataType dt);
266 
267 #if ML_DEPRECATED_SINCE(3,5,0)
268 
271 
272 public:
275  inline ML_DEPRECATED ImageVector getExt() const { return getExtent(); }
276 
279  ML_DEPRECATED BaseOp* getBaseOp() const;
280 
283  inline ML_DEPRECATED PagedImage* getInImg() const { return _pagedImg; }
284 
287  inline ML_DEPRECATED MLint getInIdx() const { return _module ? _outIdx : -1; }
288 
291  inline ML_DEPRECATED const ImageVector& getPageExt() const { return _pageExt; }
292 
295  inline ML_DEPRECATED const ImageVector& getPageArrayExt() const { return _pageArrayExt; }
296 
299  inline ML_DEPRECATED void copyTileFromInImg(const SubImageBox &box,
300  PagedImage &image,
301  const ImageVector &shift,
302  const ScaleShiftData &scaleShift=ScaleShiftData()) { copyTileFromInputImage(box, image, shift, scaleShift); }
303 
306  inline ML_DEPRECATED void copyToSubImg(SubImage &outSubImg) { copyToSubImage(outSubImg); }
307 
310  ML_DEPRECATED_CONSTRUCTOR VirtualVolume(Module *baseOp,
311  MLint connIdx,
312  MLDataType dType,
313  MLint maxNumKB = -1,
314  bool areExceptionsOnFlag = false,
315  bool useOutConnectors = false);
316 
319  ML_DEPRECATED void copyTileFromInImg(const SubImageBox &box,
320  Module &op,
321  MLint outIdx,
322  const ImageVector &shift,
323  const ScaleShiftData &scaleShift=ScaleShiftData());
324 
325 
326 #endif
327 
328  protected:
329 
350  void _init(PagedImage *img,
351  const ImageVector &ext,
352  const ImageVector &pageExtent,
353  MLdouble fillVal,
354  MLDataType dType,
355  MLint maxNumKB = -1,
356  bool areExceptionsOnFlag = false);
357 
359  void _reset();
360 
363 
364 
365 
366  // ---- Protected member functions:
367 
369  template<typename> friend class TVirtualVolume;
370 
372  void _default();
373 
375  inline static void* _getPageCB(PageBuffer *pageBuffer);
376 
382  static void* _loadPageCB(PageBuffer *pageBuffer);
383 
387  void* _loadPage(PageBuffer *pageBuffer);
388 
392 
396 
397  private:
398 
406  MLuint _makePowerOfTwo(MLint &num) const;
407 
410  MLint _getPowerOfTwo(MLuint num) const;
411 
414  MLMemoryProfile* _getMemoryProfile();
415 
416 
417  // ---- Members:
418 
419 
422 
425  bool _valid;
426 
428  Module *_module;
429 
431  MLint32 _outIdx;
432 
434  PagedImage *_pagedImg;
435 
437  SubImageBox _pagedImgBox;
438 
440  ImageVector _origVolumeExt;
441 
443  ImageVector _pageExt;
444 
446  MLint _maxNumKB;
447 
449  MLDataType _dataType;
450 
452  MLdouble _fillValue;
453 
456  bool _areExceptionsOn;
457 
460  PageFunc _pageFunc;
461 
464  void *_pageFuncData;
466 
467 
469  MLuint _numPageBytes;
470 
472  ImageVector _volumeExt;
473 
475  MLint _dim;
476 
478  ImageVector _pageArrayExt;
479 
481  MLuint _numPageArrayEntries;
482 
484  MLuint _maxMappedPages;
485 
486 
488  ImageVector _strides;
489 
491  ImageVector _pStrides;
492 
495  ImageVector _rShift;
496 
498  ImageVector _idxMask;
499 
500 
502  PageBuffer *_pageArray;
503 
505  PageBuffer **_mappedPages;
506 
508  MLuint _numMappedPages;
509 
511  PageBuffer **_writtenPages;
512 
514  MLuint _numWrittenPages;
515 
517  MLuint _nextThrowPage;
518 
520  VirtualVolumeMemoryTracker* _currentlyUsedMemoryTracker;
521 
522  friend class VirtualVolumeMemoryTracker;
523  };
524 
525  //--------------------------------------------------------------------------------
536  //--------------------------------------------------------------------------------
538 
540  typedef void *(*ReturnPageFunc)(PageBuffer *toMePtr);
541 
543  inline PageBuffer(): _virtVol(nullptr), _page(nullptr), _locked(false), _getPageFkt(nullptr) { }
544 
547 
550 
553 
557  void *_page;
558 
560  bool _locked;
561 
563  ReturnPageFunc _getPageFkt;
564  };
565 
568  inline void* VirtualVolume::_getPageCB(PageBuffer *pageBuffer) { return pageBuffer->_page; }
569 
570 ML_END_NAMESPACE
571 
572 // For compatibilities we include the TVirtualVolume here.
573 #include "mlTVirtualVolume.h"
574 
575 #endif // __mlVirtualVolume_H
#define ML_DEPRECATED
Definition: CSOGroup.h:371
#define MLTOOLS_EXPORT
Resolves system dependencies for this project.
Definition: MLToolsSystem.h:26
The strong handle of a MLMemoryBlock.
Class to manage a binary image.
Definition: mlBitImage.h:67
Base class for an image processing module of the ML.
Definition: mlModule.h:156
Class which represents an image, which manages properties of an image and image data which is located...
Definition: mlPagedImage.h:70
This class manages/represents a rectangular 6d image region which is organized linearly in memory.
Definition: mlSubImage.h:75
The TVirtualVolume class implements random access to a paged input image or a pure virtual image with...
This class manages a virtual volume organizing efficient voxel access to the output image of an input...
const ImageVector & getPageExtent() const
Returns the extent of any written/read page used by the virtual volume.
void * _loadPage(PageBuffer *pageBuffer)
This function really loads the page into the memory and updates the corresponding page buffer so that...
MLuint getNumWrittenPages()
Returns number of written (and therefore locked) pages.
void _cleanUpMemory()
Cleans up all allocated memory areas.
VirtualVolume(PagedImage *image, MLDataType dType, MLint maxNumKB=-1, bool areExceptionsOnFlag=false)
Constructor to map a pagedImg.
MLint getDim() const
Return dimension of specified volume.
const SubImageBox & getBox() const
Get box extents of input volume.
void copyTileFromInputImage(const SubImageBox &box, PagedImage &image, const ImageVector &shift, const ScaleShiftData &scaleShift=ScaleShiftData())
Copy an image region box from the image and write to the position resulting from box + shift into the...
void resize(const ImageVector &newExt)
This method resizes the virtual volume to the extents given by ext.
MLuint getNumBytes()
Returns number of bytes currently allocated as pages in the virtual volume.
MLDataType getDataType() const
Return data type enum.
void * createTypedVirtualVolume()
Creates and returns a typed version of this virtual volume as void pointer or NULL on failure.
void _init(PagedImage *img, const ImageVector &ext, const ImageVector &pageExtent, MLdouble fillVal, MLDataType dType, MLint maxNumKB=-1, bool areExceptionsOnFlag=false)
Constructor-like initialization of the instance.
VirtualVolume(const ImageVector &ext=ImageVector(0), MLdouble fillVal=0, MLDataType dType=MLuint8Type, MLint maxNumKB=-1, bool areExceptionsOnFlag=false)
Constructor to map an image of extents ext (default is 256) filled with fillVal (default is 0).
bool isValid() const
Returns true if instance is valid, otherwise false.
MLuint getNumMappedPages() const
Returns number of currently mapped (volatile) pages. Written pages are counted only temporarily here.
Module * getModule() const
Returns the pointer to the module which contains the paged image the virtual volume is associated wit...
const SubImageBox & getWrittenPageBox(MLuint32 wp)
Returns box of written page no.
static void * _loadPageCB(PageBuffer *pageBuffer)
This function calls the page loading method of Virtual volume.
void _reset()
Cleans up all allocated memory and resets instance to state after _default().
MLint getInputIndex() const
Returns the output index of the input baseOp or -1 if there is no input baseOp.
PagedImage * getInputImage() const
Returns the pointer to the paged image the virtual volume is associated with.
MLuint getNumPages()
Returns number of all (mapped and written) pages.
void _default()
Set all members to default values without freeing anything.
void * getWrittenPageData(MLuint32 wp)
Returns untyped pointer to data of written page with page id wp.
void unMapAndClearWrittenPages()
This function throws away all written pages.
PageBuffer * _getPageBuffer6D(const ImageVector &pos)
Returns the pageBuffer containing the voxel passed as position.
void copyToSubImage(SubImage &outSubImg)
Copy area from virtual volume into outSubImg.
VirtualVolume(const ImageVector &extent, const ImageVector &pageExtent, MLdouble fillVal=0, MLDataType dType=MLuint8Type, MLint maxNumKB=-1, bool areExceptionsOnFlag=false)
Constructor that allows to pass the pageExtent.
static void destroyTypedVirtualVolume(void *tVV, MLDataType dt)
Destroys a typed version of a virtual volume given by a void pointer tVV of data type dt.
void registerPageFunction(PageFunc pFunc, void *userData)
Register a function to be called when a page is mapped or unmapped.
const ImageVector & getPageArrayExtent() const
Returns the extent of the array of page buffers.
virtual ~VirtualVolume()
Destructor.
PageMapped
Enum which describes whether a page is mapped into the currently used page list or unmapped from used...
ImageVector getExtent() const
Get extents of input volume.
void _updateWrittenPages()
This function moves all written pages to _writtenPages to have a valid base to work on all written pa...
bool areExceptionsOn() const
Returns true if exception handling is enabled otherwise false.
BitImage * getUsedPagesMask(bool enableMappedPages=true, bool enableWrittenPages=true) const
Returns a pointer BitImage containing a bit for each page of the virtual volume.
void invalidate()
Invalidates state of virtual volume, e.g. after an error.
MLint32 MLDataType
MLDataType.
Definition: mlTypeDefs.h:684
@ MLuint8Type
Enumerator for the unsigned 8 bit ML integer type.
Definition: mlTypeDefs.h:723
unsigned int MLuint32
Definition: mlTypeDefs.h:191
MLuint64 MLuint
An unsigned ML integer type with at least 64 bits used for index calculations on very large images ev...
Definition: mlTypeDefs.h:594
double MLdouble
Definition: mlTypeDefs.h:223
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:578
signed int MLint32
Definition: mlTypeDefs.h:167
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.
PageBuffer is a helper structure to manage one page of input data of the VirtualVolume class and it a...
SubImageBox _box
Box of original image mapped in this page.
VirtualVolume * _virtVol
Pointer to the virtual volume object.
ReturnPageFunc _getPageFkt
Pointer to function to return the page managed by the PageBuffer.
MLMemoryBlockHandle _memoryBlockHandle
The memory block handle for holding the page data locked.
bool _locked
If true this page contains written data and must not be removed.
PageBuffer()
Initialize the structure.
void * _page
Pointer to the page data reduced by the offset to the page origin.