MeshLib Documentation
Loading...
Searching...
No Matches
MRVoxelsVolumeCachingAccessor.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRMesh/MRTimer.h"
5
6namespace MR
7{
8
11template <typename V>
13{
14public:
15 using VolumeType = V;
16 using ValueType = typename V::ValueType;
17
19 {
22 };
23
24 VoxelsVolumeCachingAccessor( const VoxelsVolumeAccessor<V>& accessor, const VolumeIndexer& indexer, Parameters parameters = {} )
25 : accessor_( accessor )
26 , indexer_( indexer )
27 , params_( std::move( parameters ) )
28 , layers_( params_.preloadedLayerCount )
29 , firstLayerVoxelId_( params_.preloadedLayerCount )
30 {
31 assert( params_.preloadedLayerCount > 0 );
32 for ( auto & l : layers_ )
33 l.resize( indexer_.sizeXY() );
34 }
35
37 [[nodiscard]] int currentLayer() const
38 {
39 return z_;
40 }
41
43 void preloadLayer( int z )
44 {
45 assert( 0 <= z && z < indexer_.dims().z );
46 z_ = z;
47 for ( auto layerIndex = 0; layerIndex < layers_.size(); ++layerIndex )
48 {
49 if ( indexer_.dims().z <= z_ + layerIndex )
50 break;
51 preloadLayer_( layerIndex );
52 }
53 }
54
57 {
58 z_ += 1;
59 for ( auto i = 0; i + 1 < layers_.size(); ++i )
60 {
61 std::swap( layers_[i], layers_[i + 1] );
62 firstLayerVoxelId_[i] = firstLayerVoxelId_[i + 1];
63 }
64 if ( z_ + params_.preloadedLayerCount - 1 < indexer_.dims().z )
65 preloadLayer_( params_.preloadedLayerCount - 1 );
66 }
67
69 ValueType get( const VoxelLocation & loc ) const
70 {
71 const auto layerIndex = loc.pos.z - z_;
72 assert( 0 <= layerIndex && layerIndex < layers_.size() );
73 assert( loc.id >= firstLayerVoxelId_[layerIndex] );
74 assert( loc.id < firstLayerVoxelId_[layerIndex] + indexer_.sizeXY() );
75 return layers_[layerIndex][loc.id - firstLayerVoxelId_[layerIndex]];
76 }
77
78private:
79 [[nodiscard]] size_t toLayerIndex( const Vector3i& pos ) const
80 {
81 return indexer_.toVoxelId( { pos.x, pos.y, 0 } );
82 }
83
84 void preloadLayer_( size_t layerIndex )
85 {
87 assert( layerIndex < layers_.size() );
88 auto& layer = layers_[layerIndex];
89 const auto z = z_ + (int)layerIndex;
90 const auto& dims = indexer_.dims();
91 assert( 0 <= z && z < dims.z );
92 auto loc = indexer_.toLoc( Vector3i{ 0, 0, z } );
93 firstLayerVoxelId_[layerIndex] = loc.id;
94 size_t n = 0;
95 for ( loc.pos.y = 0; loc.pos.y < dims.y; ++loc.pos.y )
96 for ( loc.pos.x = 0; loc.pos.x < dims.x; ++loc.pos.x, ++loc.id, ++n )
97 layer[n] = accessor_.get( loc );
98 }
99
100private:
101 const VoxelsVolumeAccessor<V>& accessor_;
102 VolumeIndexer indexer_;
103 Parameters params_;
104
105 int z_ = -1;
106 std::vector<std::vector<ValueType>> layers_;
107 std::vector<VoxelId> firstLayerVoxelId_;
108};
109
110} // namespace MR
111
#define MR_TIMER
Definition MRTimer.h:53
Definition MRVolumeIndexer.h:65
helper class for generalized voxel volume data access
Definition MRVoxelsVolumeAccess.h:14
Definition MRVoxelsVolumeCachingAccessor.h:13
V VolumeType
Definition MRVoxelsVolumeCachingAccessor.h:15
typename V::ValueType ValueType
Definition MRVoxelsVolumeCachingAccessor.h:16
ValueType get(const VoxelLocation &loc) const
get voxel volume data
Definition MRVoxelsVolumeCachingAccessor.h:69
int currentLayer() const
get current layer
Definition MRVoxelsVolumeCachingAccessor.h:37
void preloadNextLayer()
preload the next layer
Definition MRVoxelsVolumeCachingAccessor.h:56
VoxelsVolumeCachingAccessor(const VoxelsVolumeAccessor< V > &accessor, const VolumeIndexer &indexer, Parameters parameters={})
Definition MRVoxelsVolumeCachingAccessor.h:24
void preloadLayer(int z)
preload layers, starting from z
Definition MRVoxelsVolumeCachingAccessor.h:43
VoxelId toVoxelId(const Vector3i &pos) const
Definition MRVolumeIndexer.h:141
const Vector3i & dims() const
Definition MRVolumeIndexer.h:69
VoxelLocation toLoc(VoxelId id) const
Definition MRVolumeIndexer.h:83
VoxelId id
Definition MRVolumeIndexer.h:57
Vector3i pos
Definition MRVolumeIndexer.h:58
size_t sizeXY() const
Definition MRVolumeIndexer.h:77
Definition MRCameraOrientationPlugin.h:8
contains both linear Id and 3D coordinates of the same voxel
Definition MRVolumeIndexer.h:56
Definition MRVoxelsVolumeCachingAccessor.h:19
size_t preloadedLayerCount
amount of layers to be preloaded
Definition MRVoxelsVolumeCachingAccessor.h:21