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 
243  BitImage *getUsedPagesMask(bool enableMappedPages=true,
244  bool enableWrittenPages=true) const;
245 
249 
252  static void destroyTypedVirtualVolume(void *tVV, MLDataType dt);
253 
254  protected:
255 
276  void _init(PagedImage *img,
277  const ImageVector &ext,
278  const ImageVector &pageExtent,
279  MLdouble fillVal,
280  MLDataType dType,
281  MLint maxNumKB = -1,
282  bool areExceptionsOnFlag = false);
283 
285  void _reset();
286 
289 
290 
291 
292  // ---- Protected member functions:
293 
295  template<typename> friend class TVirtualVolume;
296 
298  void _default();
299 
301  inline static void* _getPageCB(PageBuffer *pageBuffer);
302 
308  static void* _loadPageCB(PageBuffer *pageBuffer);
309 
313  void* _loadPage(PageBuffer *pageBuffer);
314 
318 
322 
323  private:
324 
332  MLuint _makePowerOfTwo(MLint &num) const;
333 
336  MLint _getPowerOfTwo(MLuint num) const;
337 
340  MLMemoryProfile* _getMemoryProfile();
341 
342 
343  // ---- Members:
344 
345 
348 
351  bool _valid;
352 
354  Module *_module;
355 
357  MLint32 _outIdx;
358 
360  PagedImage *_pagedImg;
361 
363  SubImageBox _pagedImgBox;
364 
366  ImageVector _origVolumeExt;
367 
369  ImageVector _pageExt;
370 
372  MLint _maxNumKB;
373 
375  MLDataType _dataType;
376 
378  MLdouble _fillValue;
379 
382  bool _areExceptionsOn;
383 
386  PageFunc _pageFunc;
387 
390  void *_pageFuncData;
392 
393 
395  MLuint _numPageBytes;
396 
398  ImageVector _volumeExt;
399 
401  MLint _dim;
402 
404  ImageVector _pageArrayExt;
405 
407  MLuint _numPageArrayEntries;
408 
410  MLuint _maxMappedPages;
411 
412 
414  ImageVector _strides;
415 
417  ImageVector _pStrides;
418 
421  ImageVector _rShift;
422 
424  ImageVector _idxMask;
425 
426 
428  PageBuffer *_pageArray;
429 
431  PageBuffer **_mappedPages;
432 
434  MLuint _numMappedPages;
435 
437  PageBuffer **_writtenPages;
438 
440  MLuint _numWrittenPages;
441 
443  MLuint _nextThrowPage;
444 
446  VirtualVolumeMemoryTracker* _currentlyUsedMemoryTracker;
447 
448  friend class VirtualVolumeMemoryTracker;
449  };
450 
451  //--------------------------------------------------------------------------------
462  //--------------------------------------------------------------------------------
464 
466  typedef void *(*ReturnPageFunc)(PageBuffer *toMePtr);
467 
469  inline PageBuffer(): _virtVol(nullptr), _page(nullptr), _locked(false), _getPageFkt(nullptr) { }
470 
473 
476 
479 
483  void *_page;
484 
486  bool _locked;
487 
489  ReturnPageFunc _getPageFkt;
490  };
491 
494  inline void* VirtualVolume::_getPageCB(PageBuffer *pageBuffer) { return pageBuffer->_page; }
495 
496 ML_END_NAMESPACE
497 
498 // For compatibilities we include the TVirtualVolume here.
499 #include "mlTVirtualVolume.h"
500 
501 #endif // __mlVirtualVolume_H
#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:151
The class PagedImage, representing a fragmented image that manages properties and data of an image lo...
Definition: mlPagedImage.h:66
This class manages/represents a rectangular 6D image region that 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, it returns 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:596
@ MLuint8Type
Enumerator for the unsigned 8-bit ML integer type.
Definition: mlTypeDefs.h:621
unsigned int MLuint32
Definition: mlTypeDefs.h:185
MLuint64 MLuint
An unsigned ML integer type with at least 64 bits used for index calculations on very large images ev...
Definition: mlTypeDefs.h:506
double MLdouble
Definition: mlTypeDefs.h:217
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
signed int MLint32
Definition: mlTypeDefs.h:161
TScaleShiftData< MLdouble > ScaleShiftData
Double version of TScaleShiftData for maximum reasonable precision.
TImageVector< MLint > ImageVector
Defines the standard ImageVector type that 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.