MeshLib C++ Docs
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
32 explicit VoxelsVolumeInterpolatedAccessor( const VoxelsVolumeInterpolatedAccessor& other, const Accessor& accessor )
33 : volume_( other.volume_ ), accessor_( accessor )
34 {}
35
37 ValueType get( const Vector3f& pos ) const
38 {
39 IndexAndPos index = getIndexAndPos( pos - mult( accessor_.shift(), volume_.voxelSize ) );
40 ValueType value{};
41 float cx[2] = { 1.0f - index.pos.x, index.pos.x };
42 float cy[2] = { 1.0f - index.pos.y, index.pos.y };
43 float cz[2] = { 1.0f - index.pos.z, index.pos.z };
44 for ( int i = 0; i < 8; i++ )
45 {
46 Vector3i d{ i & 1, ( i >> 1 ) & 1, i >> 2 };
47 const auto voxPos = index.index + d;
48 if ( voxPos.x >= 0 && voxPos.x < volume_.dims.x &&
49 voxPos.y >= 0 && voxPos.y < volume_.dims.y &&
50 voxPos.z >= 0 && voxPos.z < volume_.dims.z )
51 {
52 value += accessor_.get( voxPos ) * ( cx[d.x] * cy[d.y] * cz[d.z] );
53 }
54 }
55 return value;
56 }
57
58private:
59 const VolumeType& volume_;
60 const Accessor& accessor_;
61
62 struct IndexAndPos
63 {
64 Vector3i index; // Zero-based voxel index in the volume
65 Vector3f pos; // [0;1) position of a point in the voxel: 0 corresponds to index and 1 to next voxel
66 };
67
68 IndexAndPos getIndexAndPos( Vector3f pos ) const
69 {
70 IndexAndPos res;
71 res.pos.x = pos.x / volume_.voxelSize.x;
72 res.pos.y = pos.y / volume_.voxelSize.y;
73 res.pos.z = pos.z / volume_.voxelSize.z;
74 pos.x = floor( res.pos.x );
75 pos.y = floor( res.pos.y );
76 pos.z = floor( res.pos.z );
77 res.index.x = int( pos.x );
78 res.index.y = int( pos.y );
79 res.index.z = int( pos.z );
80 res.pos.x -= pos.x;
81 res.pos.y -= pos.y;
82 res.pos.z -= pos.z;
83 return res;
84 }
85};
86
88template <typename Accessor>
90 const typename Accessor::VolumeType &volume,
91 const Accessor &accessor,
92 const Vector3f &newVoxelSize )
93{
94 SimpleVolumeMinMax res{
95 { .voxelSize{ newVoxelSize } },
96 { volume.min, volume.max }
97 };
98 res.dims.x = int( volume.dims.x * volume.voxelSize.x / res.voxelSize.x );
99 res.dims.y = int( volume.dims.y * volume.voxelSize.y / res.voxelSize.y );
100 res.dims.z = int( volume.dims.z * volume.voxelSize.z / res.voxelSize.z );
101 res.data.resize( size_t( res.dims.x ) * res.dims.y * res.dims.z );
102 VolumeIndexer indexer( res.dims );
103
104 VoxelsVolumeInterpolatedAccessor<Accessor> interpolator( volume, accessor );
105 for ( int k = 0; k < res.dims.z; k++ )
106 for ( int j = 0; j < res.dims.y; j++ )
107 for ( int i = 0; i < res.dims.x; i++ )
108 res.data[indexer.toVoxelId( { i, j, k } )] = interpolator.get(
109 { i * res.voxelSize.x, j * res.voxelSize.y, k * res.voxelSize.z } );
110
111 return res;
112}
113
114} // 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:37
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:32
VoxelId toVoxelId(const Vector3i &pos) const
Definition MRVolumeIndexer.h:141
Definition MRCameraOrientationPlugin.h:8
MRMESH_API double volume(const MeshTopology &topology, const VertCoords &points, const FaceBitSet *region=nullptr)
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:89