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
44class MLMemoryProfile;
45
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
84 MLint maxNumKB = -1,
85 bool areExceptionsOnFlag = false);
86
96 MLdouble fillVal = 0,
98 MLint maxNumKB = -1,
99 bool areExceptionsOnFlag = false);
100
103 MLdouble fillVal = 0,
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
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
257 bool enableWrittenPages=true) const;
258
262
266
267 protected:
268
290 const ImageVector &ext,
291 const ImageVector &pageExtent,
294 MLint maxNumKB = -1,
295 bool areExceptionsOnFlag = false);
296
298 void _reset();
299
302
303
304
305 // ---- Protected member functions:
306
308 template<typename> friend class TVirtualVolume;
309
311 void _default();
312
314 inline static void* _getPageCB(PageBuffer *pageBuffer);
315
322
327
331
335
336 private:
337
345 MLuint _makePowerOfTwo(MLint &num) const;
346
349 MLint _getPowerOfTwo(MLuint num) const;
350
353 MLMemoryProfile* _getMemoryProfile();
354
355
356 // ---- Members:
357
358
361
364 bool _valid;
365
367 Module *_module;
368
370 MLint32 _outIdx;
371
373 PagedImage *_pagedImg;
374
376 SubImageBox _pagedImgBox;
377
379 ImageVector _origVolumeExt;
380
382 ImageVector _pageExt;
383
385 MLint _maxNumKB;
386
388 MLDataType _dataType;
389
391 MLdouble _fillValue;
392
395 bool _areExceptionsOn;
396
399 PageFunc _pageFunc;
400
403 void *_pageFuncData;
405
406
408 MLuint _numPageBytes;
409
411 ImageVector _volumeExt;
412
414 MLint _dim;
415
417 ImageVector _pageArrayExt;
418
420 MLuint _numPageArrayEntries;
421
423 MLuint _maxMappedPages;
424
425
427 ImageVector _strides;
428
430 ImageVector _pStrides;
431
434 ImageVector _rShift;
435
437 ImageVector _idxMask;
438
439
441 PageBuffer *_pageArray;
442
444 PageBuffer **_mappedPages;
445
447 MLuint _numMappedPages;
448
450 PageBuffer **_writtenPages;
451
453 MLuint _numWrittenPages;
454
456 MLuint _nextThrowPage;
457
459 VirtualVolumeMemoryTracker* _currentlyUsedMemoryTracker;
460
461 friend class VirtualVolumeMemoryTracker;
462 };
463
464 //--------------------------------------------------------------------------------
475 //--------------------------------------------------------------------------------
477
479 typedef void *(*ReturnPageFunc)(PageBuffer *toMePtr);
480
482 inline PageBuffer(): _virtVol(nullptr), _page(nullptr), _locked(false), _getPageFkt(nullptr) { }
483
486
489
492
496 void *_page;
497
500
502 ReturnPageFunc _getPageFkt;
503 };
504
507 inline void* VirtualVolume::_getPageCB(PageBuffer *pageBuffer) { return pageBuffer->_page; }
508
510
511// For compatibilities we include the TVirtualVolume here.
512#include "mlTVirtualVolume.h"
513
514#endif // __mlVirtualVolume_H
#define MLTOOLS_EXPORT
Resolves system dependencies for this project.
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
Class which represents an image, which manages properties of an image and image data which is located...
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.
PageBuffer * _getPageBuffer6D(const ImageVector &pos)
Returns the pageBuffer containing the voxel passed as position.
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.
void * getWrittenPageData(MLuint32 wp)
Returns untyped pointer to data of written page with page id wp.
MLDataType getDataType() const
Return data type enum.
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.
const SubImageBox & getBox() const
Get box extents of input volume.
Module * getModule() const
Returns the pointer to the module which contains the paged image the virtual volume is associated wit...
BitImage * getUsedPagesMask(bool enableMappedPages=true, bool enableWrittenPages=true) const
Returns a pointer BitImage containing a bit for each page of the 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.
const SubImageBox & getWrittenPageBox(MLuint32 wp)
Returns box of written page no.
MLuint getNumPages()
Returns number of all (mapped and written) pages.
void _default()
Set all members to default values without freeing anything.
void unMapAndClearWrittenPages()
This function throws away all written pages.
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.
void * createTypedVirtualVolume()
Creates and returns a typed version of this virtual volume as void pointer or NULL on failure.
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.
const ImageVector & getPageArrayExtent() const
Returns the extent of the array of page buffers.
void registerPageFunction(PageFunc pFunc, void *userData)
Register a function to be called when a page is mapped or unmapped.
static void * _loadPageCB(PageBuffer *pageBuffer)
This function calls the page loading method of Virtual volume.
PagedImage * getInputImage() const
Returns the pointer to the paged image the virtual volume is associated with.
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.
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
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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
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.