Carna  Version 3.3.2
BufferedHUVolume.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef BUFFEREDHUVOLUME_H_6014714286
13 #define BUFFEREDHUVOLUME_H_6014714286
14 
19 #include <Carna/base/HUVolume.h>
20 #include <Carna/base/Composition.h>
22 #include <vector>
23 #include <memory>
24 
25 namespace Carna
26 {
27 
28 namespace base
29 {
30 
31 
32 
33 // ----------------------------------------------------------------------------------
34 // BufferedHUVolume
35 // ----------------------------------------------------------------------------------
36 
48 template< typename VoxelType, typename BufferType >
49 class BufferedHUVolume : public HUVolume
50 {
51 
52 public:
53 
57  typedef BufferType Buffer;
58 
62  typedef VoxelType Voxel;
63 
73  : HUVolume( size )
74  , myBuffer( buffer )
75  {
76  initializeBuffer();
77  }
78 
82  : HUVolume( size )
83  , myBuffer( new Composition< BufferType >( new BufferType( size.x() * size.y() * size.z() ) ) )
84  {
85  initializeBuffer();
86  }
87 
91  static HUV bufferValueToHUV( VoxelType bufferValue )
92  {
93  return HUV::abs( ( bufferValue >> ( sizeof( VoxelType ) * 8 - 12 ) ) - 1024 );
94  }
95 
99  static VoxelType HUVToBufferValue( HUV huValue )
100  {
101  return ( static_cast< VoxelType >( huValue + 1024 ) << ( sizeof( VoxelType ) * 8 - 12 ) );
102  }
103 
107  HUV operator()( unsigned int x
108  , unsigned int y
109  , unsigned int z ) const
110  {
111  const std::size_t index = x + size.x() * y + size.y() * size.x() * z;
112  return bufferValueToHUV( myBuffer->get()->at( index ) );
113  }
114 
118  HUV operator()( const math::Vector3ui& at ) const
119  {
120  return ( *this )( at.x(), at.y(), at.z() );
121  }
122 
126  void setVoxel( unsigned int x, unsigned int y, unsigned int z, HUV huv )
127  {
128  CARNA_ASSERT( x < size.x() && y < size.y() && z < size.z() );
129  const std::size_t index = x + size.x() * y + size.y() * size.x() * z;
130  myBuffer->get()->at( index ) = HUVToBufferValue( huv );
131  }
132 
136  void setVoxel( const math::Vector3ui& at, HUV huv )
137  {
138  this->setVoxel( at.x(), at.y(), at.z(), huv );
139  }
140 
144  BufferType& buffer()
145  {
146  return **myBuffer;
147  }
148 
152  const BufferType& buffer() const
153  {
154  return **myBuffer;
155  }
156 
157 protected:
158 
168  const std::unique_ptr< Association< BufferType > > myBuffer;
169 
170 private:
171 
172  void initializeBuffer()
173  {
175  ( myBuffer.get() && myBuffer->get()
176  , "No volume data buffer supplied!" );
177 
179  ( myBuffer->get()->size() >= size.x() * size.y() * size.z()
180  , "Supplied volume data buffer is of size "
181  << myBuffer->get()->size()
182  << " bytes but must be at least "
183  << size.x() * size.y() * size.z()
184  << " bytes!" );
185  }
186 
187 }; // BufferedHUVolume
188 
189 
190 
191 } // namespace Carna :: base
192 
193 } // namespace Carna
194 
195 #endif // BUFFEREDHUVOLUME_H_6014714286
Implements HUVolume generically for a particular VoxelType.
const BufferType & buffer() const
References the underlying buffer.
BufferType Buffer
Holds the used buffer type.
static VoxelType HUVToBufferValue(HUV huValue)
Returns the buffer value corresponding to huValue.
static HUV bufferValueToHUV(VoxelType bufferValue)
Returns the HU 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...
HUV operator()(unsigned int x, unsigned int y, unsigned int z) const
Returns HUV of specified voxel.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition: math.h:199
HUV operator()(const math::Vector3ui &at) const
Returns HUV of specified voxel.
math::Vector3ui size
Holds the resolution.
Definition: HUVolume.h:66
BufferType & buffer()
References the underlying buffer.
BufferedHUVolume(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates with , where is size.
void setVoxel(const math::Vector3ui &at, HUV huv)
Sets the HUV of a voxel.
Defines interface to volumetric data.
Definition: HUVolume.h:46
BufferedHUVolume(const math::Vector3ui &size)
void setVoxel(unsigned int x, unsigned int y, unsigned int z, HUV huv)
Sets the HUV of a voxel.
Defines Carna::base::HUVolume.
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the underlying buffer.
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
Represents values in .
Definition: HUV.h:34
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
VoxelType Voxel
Holds the type used to store the value of a single voxel.
Defines Carna::base::Composition.
static HUV abs(T value)
Wraps an absolute HU value.
Definition: HUV.h:134