Carna Version 3.3.3
Loading...
Searching...
No Matches
BufferedIntensityVolume.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Leonid Kostrykin
3 *
4 */
5
6#ifndef BUFFEREDINTENSITYVOLUME_H_6014714286
7#define BUFFEREDINTENSITYVOLUME_H_6014714286
8
16#include <vector>
17#include <memory>
18
19namespace Carna
20{
21
22namespace base
23{
24
25
26
27// ----------------------------------------------------------------------------------
28// BufferedIntensityVolume
29// ----------------------------------------------------------------------------------
30
41template< typename VoxelType, typename BufferType >
43{
44
45public:
46
51
55 typedef VoxelType Voxel;
56
71
76 , myBuffer( new Composition< BufferType >( new BufferType( size.x() * size.y() * size.z() ) ) )
77 {
78 initializeBuffer();
79 }
80
84 static float bufferValueToIntensity( VoxelType bufferValue )
85 {
86 return bufferValue / static_cast< float >( ( 1 << ( 8 * sizeof( VoxelType ) ) ) - 1 );
87 }
88
92 static VoxelType intensityToBufferValue( float intensity )
93 {
94 CARNA_ASSERT_EX( 0 <= intensity && intensity <= 1, "Intensity out of range: " << intensity ); // TODO: check how much this slows down?
95 const auto valueMax = ( 1 << ( 8 * sizeof( VoxelType ) ) ) - 1;
96 float value = intensity * valueMax + 0.5f;
97 if( value > valueMax ) value = valueMax;
98 return static_cast< VoxelType >( value );
99 }
100
104 float operator()( unsigned int x
105 , unsigned int y
106 , unsigned int z ) const
107 {
108 const std::size_t index = x + size.x() * y + size.y() * size.x() * z;
109 return bufferValueToIntensity( myBuffer->get()->at( index ) );
110 }
111
114 float operator()( const math::Vector3ui& at ) const
115 {
116 return ( *this )( at.x(), at.y(), at.z() );
117 }
118
122 void setVoxel( unsigned int x, unsigned int y, unsigned int z, float intensity )
123 {
124 CARNA_ASSERT( x < size.x() && y < size.y() && z < size.z() );
125 const std::size_t index = x + size.x() * y + size.y() * size.x() * z;
126 myBuffer->get()->at( index ) = intensityToBufferValue( intensity );
127 }
128
131 void setVoxel( const math::Vector3ui& at, float intensity )
132 {
133 this->setVoxel( at.x(), at.y(), at.z(), intensity );
134 }
135
140 {
141 return **myBuffer;
142 }
143
147 const BufferType& buffer() const
148 {
149 return **myBuffer;
150 }
151
152protected:
153
163 const std::unique_ptr< Association< BufferType > > myBuffer;
164
165private:
166
167 void initializeBuffer()
168 {
170 ( myBuffer.get() && myBuffer->get()
171 , "No volume data buffer supplied!" );
172
174 ( myBuffer->get()->size() >= size.x() * size.y() * size.z()
175 , "Supplied volume data buffer is of size "
176 << myBuffer->get()->size()
177 << " bytes but must be at least "
178 << size.x() * size.y() * size.z()
179 << " bytes!" );
180 }
181
182}; // BufferedIntensityVolume
183
184
185
186} // namespace Carna :: base
187
188} // namespace Carna
189
190#endif // BUFFEREDINTENSITYVOLUME_H_6014714286
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
#define CARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Defines Carna::base::Composition.
Defines Carna::base::IntensityVolume.
Represents an association.
Definition Association.h:45
AssociatedObjectType * get() const
Returns raw pointer to the referenced object.
Definition Association.h:61
Implements IntensityVolume generically for a particular VoxelType.
BufferType Buffer
Holds the used buffer type.
static float bufferValueToIntensity(VoxelType bufferValue)
Returns the intensity value corresponding to bufferValue.
BufferedIntensityVolume(const math::Vector3ui &size)
float operator()(const math::Vector3ui &at) const
void setVoxel(const math::Vector3ui &at, float intensity)
const BufferType & buffer() const
References the underlying buffer.
BufferType & buffer()
References the underlying buffer.
void setVoxel(unsigned int x, unsigned int y, unsigned int z, float intensity)
Sets the HUV of a voxel.
BufferedIntensityVolume(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates with , where is size.
VoxelType Voxel
Holds the type used to store the value of a single voxel.
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the underlying buffer.
float operator()(unsigned int x, unsigned int y, unsigned int z) const
Returns intensity of specified voxel.
static VoxelType intensityToBufferValue(float intensity)
Returns the buffer value corresponding to an intensity.
Represents a composition, i.e. a strong reference. This basically is a std::unique_ptr that supports ...
Definition Composition.h:53
Defines interface to volume data.
math::Vector3ui size
Holds the resolution.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition math.h:199