MeshLib Documentation
Loading...
Searching...
No Matches
MRVolumeInterpolation.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace MR
6{
7
16template <typename Accessor>
18{
19public:
20 using VolumeType = typename Accessor::VolumeType;
21 using ValueType = typename Accessor::ValueType;
22
25 explicit VoxelsVolumeInterpolatedAccessor( const VolumeType& volume, const Accessor& accessor )
26 : volume_( volume ), accessor_( accessor )
27 {
28 if constexpr ( std::is_same_v<VolumeType, VdbVolume> )
29 {
30 openvdb::Coord coord = volume.data->evalActiveVoxelBoundingBox().min();
31 minCoord_ = { coord.x(), coord.y(), coord.z() };
32 }
33 }
34
38 explicit VoxelsVolumeInterpolatedAccessor( const VoxelsVolumeInterpolatedAccessor& other, const Accessor& accessor )
39 : volume_( other.volume_ ), accessor_( accessor ), minCoord_( other.minCoord_ )
40 {}
41
43 ValueType get( const Vector3f& pos ) const
44 {
45 IndexAndPos index = getIndexAndPos(pos);
46 ValueType value{};
47 float cx[2] = { 1.0f - index.pos.x, index.pos.x };
48 float cy[2] = { 1.0f - index.pos.y, index.pos.y };
49 float cz[2] = { 1.0f - index.pos.z, index.pos.z };
50 for ( int i = 0; i < 8; i++ )
51 {
52 Vector3i d{ i & 1, ( i >> 1 ) & 1, i >> 2 };
53 const auto voxPos = index.index + d;
54 if ( voxPos.x >= 0 && voxPos.x < volume_.dims.x &&
55 voxPos.y >= 0 && voxPos.y < volume_.dims.y &&
56 voxPos.z >= 0 && voxPos.z < volume_.dims.z )
57 {
58 value += accessor_.get( voxPos ) * ( cx[d.x] * cy[d.y] * cz[d.z] );
59 }
60 }
61 return value;
62 }
63
64private:
65 const VolumeType& volume_;
66 const Accessor& accessor_;
67 Vector3i minCoord_{};
68
69 struct IndexAndPos
70 {
71 Vector3i index; // Zero-based voxel index in the volume
72 Vector3f pos; // [0;1) position of a point in the voxel: 0 corresponds to index and 1 to next voxel
73 };
74
75 IndexAndPos getIndexAndPos( Vector3f pos ) const
76 {
77 IndexAndPos res;
78 res.pos.x = pos.x / volume_.voxelSize.x;
79 res.pos.y = pos.y / volume_.voxelSize.y;
80 res.pos.z = pos.z / volume_.voxelSize.z;
81 pos.x = floor( res.pos.x );
82 pos.y = floor( res.pos.y );
83 pos.z = floor( res.pos.z );
84 res.index.x = int( pos.x );
85 res.index.y = int( pos.y );
86 res.index.z = int( pos.z );
87 res.pos.x -= pos.x;
88 res.pos.y -= pos.y;
89 res.pos.z -= pos.z;
90 if constexpr ( std::is_same_v<VolumeType, VdbVolume> )
91 res.index -= minCoord_;
92 return res;
93 }
94};
95
97template <typename Accessor>
99 const typename Accessor::VolumeType &volume,
100 const Accessor &accessor,
101 const Vector3f &newVoxelSize )
102{
103 SimpleVolumeMinMax res{
104 { .voxelSize{ newVoxelSize } },
105 { volume.min, volume.max }
106 };
107 res.dims.x = int( volume.dims.x * volume.voxelSize.x / res.voxelSize.x );
108 res.dims.y = int( volume.dims.y * volume.voxelSize.y / res.voxelSize.y );
109 res.dims.z = int( volume.dims.z * volume.voxelSize.z / res.voxelSize.z );
110 res.data.resize( size_t( res.dims.x ) * res.dims.y * res.dims.z );
111 VolumeIndexer indexer( res.dims );
112
113 VoxelsVolumeInterpolatedAccessor<Accessor> interpolator( volume, accessor );
114 for ( int k = 0; k < res.dims.z; k++ )
115 for ( int j = 0; j < res.dims.y; j++ )
116 for ( int i = 0; i < res.dims.x; i++ )
117 res.data[indexer.toVoxelId( { i, j, k } )] = interpolator.get(
118 { i * res.voxelSize.x, j * res.voxelSize.y, k * res.voxelSize.z } );
119
120 return res;
121}
122
123} // namespace MR
Definition MRVolumeIndexer.h:65
Definition MRVolumeInterpolation.h:18
VoxelsVolumeInterpolatedAccessor(const VoxelsVolumeInterpolatedAccessor &)=delete
delete copying constructor to avoid accidentally creating non-thread-safe accessors
ValueType get(const Vector3f &pos) const
get value at specified coordinates
Definition MRVolumeInterpolation.h:43
VoxelsVolumeInterpolatedAccessor(const VolumeType &volume, const Accessor &accessor)
Definition MRVolumeInterpolation.h:25
typename Accessor::VolumeType VolumeType
Definition MRVolumeInterpolation.h:20
typename Accessor::ValueType ValueType
Definition MRVolumeInterpolation.h:21
VoxelsVolumeInterpolatedAccessor(const VoxelsVolumeInterpolatedAccessor &other, const Accessor &accessor)
a copying-like constructor with explicitly provided accessor
Definition MRVolumeInterpolation.h:38
VoxelId toVoxelId(const Vector3i &pos) const
Definition MRVolumeIndexer.h:141
constexpr A floor(A a)
Definition MRImGuiVectorOperators.h:126
Definition MRCameraOrientationPlugin.h:8
SimpleVolumeMinMax resampleVolumeByInterpolation(const typename Accessor::VolumeType &volume, const Accessor &accessor, const Vector3f &newVoxelSize)
sample function that resamples the voxel volume using interpolating accessor
Definition MRVolumeInterpolation.h:98