Carna  Version 3.3.2
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 
14 #include <Carna/base/Composition.h>
16 #include <vector>
17 #include <memory>
18 
19 namespace Carna
20 {
21 
22 namespace base
23 {
24 
25 
26 
27 // ----------------------------------------------------------------------------------
28 // BufferedIntensityVolume
29 // ----------------------------------------------------------------------------------
30 
41 template< typename VoxelType, typename BufferType >
43 {
44 
45 public:
46 
50  typedef BufferType Buffer;
51 
55  typedef VoxelType Voxel;
56 
66  : IntensityVolume( size )
67  , myBuffer( buffer )
68  {
69  initializeBuffer();
70  }
71 
75  : IntensityVolume( size )
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 
139  BufferType& buffer()
140  {
141  return **myBuffer;
142  }
143 
147  const BufferType& buffer() const
148  {
149  return **myBuffer;
150  }
151 
152 protected:
153 
163  const std::unique_ptr< Association< BufferType > > myBuffer;
164 
165 private:
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
void setVoxel(unsigned int x, unsigned int y, unsigned int z, float intensity)
Sets the HUV of a voxel.
void setVoxel(const math::Vector3ui &at, float intensity)
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the underlying buffer.
Defines interface to volume data.
BufferedIntensityVolume(const math::Vector3ui &size)
Implements IntensityVolume generically for a particular VoxelType.
static float bufferValueToIntensity(VoxelType bufferValue)
Returns the intensity value corresponding to bufferValue.
#define CARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition: math.h:199
Defines Carna::base::IntensityVolume.
VoxelType Voxel
Holds the type used to store the value of a single voxel.
float operator()(const math::Vector3ui &at) const
math::Vector3ui size
Holds the resolution.
float operator()(unsigned int x, unsigned int y, unsigned int z) const
Returns intensity of specified voxel.
BufferType & buffer()
References the underlying buffer.
static VoxelType intensityToBufferValue(float intensity)
Returns the buffer value corresponding to an intensity.
BufferedIntensityVolume(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates with , where is size.
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
Represents a composition, i.e. a strong reference. This basically is a std::unique_ptr that supports ...
Definition: Composition.h:52
const BufferType & buffer() const
References the underlying buffer.
BufferType Buffer
Holds the used buffer type.
#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.