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