MeshLib C++ Docs
Loading...
Searching...
No Matches
MRVolumeInterpolation.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace MR
6{
9
10
19template <typename Accessor>
21{
22public:
23 using VolumeType = typename Accessor::VolumeType;
24 using ValueType = typename Accessor::ValueType;
25
28 explicit VoxelsVolumeInterpolatedAccessor( const VolumeType& volume, const Accessor& accessor )
29 : volume_( volume ), accessor_( accessor )
30 {}
31
35 explicit VoxelsVolumeInterpolatedAccessor( const VoxelsVolumeInterpolatedAccessor& other, const Accessor& accessor )
36 : volume_( other.volume_ ), accessor_( accessor )
37 {}
38
40 ValueType get( const Vector3f& pos ) const
41 {
42 IndexAndPos index = getIndexAndPos( pos - mult( accessor_.shift(), volume_.voxelSize ) );
43 ValueType value{};
44 float cx[2] = { 1.0f - index.pos.x, index.pos.x };
45 float cy[2] = { 1.0f - index.pos.y, index.pos.y };
46 float cz[2] = { 1.0f - index.pos.z, index.pos.z };
47 for ( int i = 0; i < 8; i++ )
48 {
49 Vector3i d{ i & 1, ( i >> 1 ) & 1, i >> 2 };
50 const auto voxPos = index.index + d;
51 if ( voxPos.x >= 0 && voxPos.x < volume_.dims.x &&
52 voxPos.y >= 0 && voxPos.y < volume_.dims.y &&
53 voxPos.z >= 0 && voxPos.z < volume_.dims.z )
54 {
55 value += accessor_.get( voxPos ) * ( cx[d.x] * cy[d.y] * cz[d.z] );
56 }
57 }
58 return value;
59 }
60
61private:
62 const VolumeType& volume_;
63 const Accessor& accessor_;
64
65 struct IndexAndPos
66 {
67 Vector3i index;
68 Vector3f pos;
69 };
70
71 IndexAndPos getIndexAndPos( Vector3f pos ) const
72 {
73 IndexAndPos res;
74 res.pos.x = pos.x / volume_.voxelSize.x;
75 res.pos.y = pos.y / volume_.voxelSize.y;
76 res.pos.z = pos.z / volume_.voxelSize.z;
77 pos.x = floor( res.pos.x );
78 pos.y = floor( res.pos.y );
79 pos.z = floor( res.pos.z );
80 res.index.x = int( pos.x );
81 res.index.y = int( pos.y );
82 res.index.z = int( pos.z );
83 res.pos.x -= pos.x;
84 res.pos.y -= pos.y;
85 res.pos.z -= pos.z;
86 return res;
87 }
88};
89
91template <typename Accessor>
93 const typename Accessor::VolumeType &volume,
94 const Accessor &accessor,
95 const Vector3f &newVoxelSize )
96{
97 SimpleVolumeMinMax res{
98 { .voxelSize{ newVoxelSize } },
99 { volume.min, volume.max }
100 };
101 res.dims.x = int( volume.dims.x * volume.voxelSize.x / res.voxelSize.x );
102 res.dims.y = int( volume.dims.y * volume.voxelSize.y / res.voxelSize.y );
103 res.dims.z = int( volume.dims.z * volume.voxelSize.z / res.voxelSize.z );
104 res.data.resize( size_t( res.dims.x ) * res.dims.y * res.dims.z );
105 VolumeIndexer indexer( res.dims );
106
107 VoxelsVolumeInterpolatedAccessor<Accessor> interpolator( volume, accessor );
108 for ( int k = 0; k < res.dims.z; k++ )
109 for ( int j = 0; j < res.dims.y; j++ )
110 for ( int i = 0; i < res.dims.x; i++ )
111 res.data[indexer.toVoxelId( { i, j, k } )] = interpolator.get(
112 { i * res.voxelSize.x, j * res.voxelSize.y, k * res.voxelSize.z } );
113
114 return res;
115}
116
117}
Definition MRVolumeIndexer.h:65
Definition MRVolumeInterpolation.h:21
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:40
MRMESH_API double volume(const MeshTopology &topology, const VertCoords &points, const FaceBitSet *region=nullptr)
VoxelsVolumeInterpolatedAccessor(const VolumeType &volume, const Accessor &accessor)
Definition MRVolumeInterpolation.h:28
Vector3i index
Definition MRVolumeInterpolation.h:67
Vector3f pos
Zero-based voxel index in the volume.
Definition MRVolumeInterpolation.h:68
typename Accessor::VolumeType VolumeType
Definition MRVolumeInterpolation.h:23
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:92
typename Accessor::ValueType ValueType
Definition MRVolumeInterpolation.h:24
VoxelsVolumeInterpolatedAccessor(const VoxelsVolumeInterpolatedAccessor &other, const Accessor &accessor)
a copying-like constructor with explicitly provided accessor
Definition MRVolumeInterpolation.h:35
@ other
Angle, normally float. Measure in radians.
VoxelId toVoxelId(const Vector3i &pos) const
Definition MRVolumeIndexer.h:141
only for bindings generation
Definition MRCameraOrientationPlugin.h:8