Carna  Version 3.0.1
BufferedNormalMap3D.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 BUFFEREDNORMALMAP3D_H_6014714286
13 #define BUFFEREDNORMALMAP3D_H_6014714286
14 
19 #include <Carna/base/NormalMap3D.h>
20 #include <Carna/base/Composition.h>
22 #include <Carna/base/text.h>
23 #include <vector>
24 #include <memory>
25 #include <type_traits>
26 #include <climits>
27 
28 namespace Carna
29 {
30 
31 namespace base
32 {
33 
34 
35 
36 // ----------------------------------------------------------------------------------
37 // BufferedNormalMap3D
38 // ----------------------------------------------------------------------------------
39 
58 template< typename BufferedVectorComponentType, typename BufferType >
60 {
61 
62  static_assert
63  ( std::is_integral< BufferedVectorComponentType >::value
64  , "Only integral buffer vector component types allowed." );
65 
66 public:
67 
71  typedef BufferType Buffer;
72 
76  typedef BufferedVectorComponentType BufferedVectorComponent;
77 
85  : NormalMap3D( size )
86  , myBuffer( buffer )
87  {
88  checkBuffer();
89  }
90 
94  : NormalMap3D( size )
95  , myBuffer( new Composition< BufferType >
96  ( new BufferType( 4 * sizeof( BufferedVectorComponentType ) * size.x() * size.y() * size.z() ) ) )
97  {
98  checkBuffer();
99  }
100 
109  static float decodeComponent( BufferedVectorComponentType encodedVectorComponent )
110  {
111  const float range = static_cast< float >( static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::max() )
112  - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() ) );
113  const signed long x = encodedVectorComponent;
114  const float fraction = ( x - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() ) ) / range;
115  return ( fraction - 0.5f ) * 2;
116  }
117 
127  static BufferedVectorComponentType encodeComponent( float actualVectorComponent )
128  {
130  ( std::abs( actualVectorComponent ) <= 1
131  , "Unnormalized vector! Component: " + text::lexical_cast< std::string >( actualVectorComponent ) );
132 
133  const signed long range = static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::max() )
134  - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() );
135  const signed long result = static_cast< signed long >( ( ( actualVectorComponent + 1 ) * range ) / 2 )
136  + static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() );
137  return static_cast< BufferedVectorComponentType >( result );
138  }
139 
143  math::Vector3f operator()( const math::Vector3ui& location ) const
144  {
145  return ( *this )( location.x(), location.y(), location.z() );
146  }
147 
150  math::Vector3f operator()
151  ( unsigned int x
152  , unsigned int y
153  , unsigned int z ) const
154  {
155  math::Vector3f result;
156  const std::size_t index = 4 * ( x + size.x() * y + size.y() * size.x() * z );
157  result.x() = decodeComponent( myBuffer->get()->at( index + 0 ) );
158  result.y() = decodeComponent( myBuffer->get()->at( index + 1 ) );
159  result.z() = decodeComponent( myBuffer->get()->at( index + 2 ) );
160  return result;
161  }
162 
166  void setVoxel( const math::Vector3ui& location, const math::Vector3f& normal )
167  {
168  this->setVoxel( location.x(), location.y(), location.z(), normal );
169  }
170 
173  void setVoxel( unsigned int x, unsigned int y, unsigned int z, const math::Vector3f& normal )
174  {
175  CARNA_ASSERT( x < size.x() && y < size.y() && z < size.z() );
176  const std::size_t index = 4 * ( x + size.x() * y + size.y() * size.x() * z );
177  myBuffer->get()->at( index + 0 ) = encodeComponent( normal.x() );
178  myBuffer->get()->at( index + 1 ) = encodeComponent( normal.y() );
179  myBuffer->get()->at( index + 2 ) = encodeComponent( normal.z() );
180  }
181 
185  BufferType& buffer()
186  {
187  return **myBuffer;
188  }
189 
193  const BufferType& buffer() const
194  {
195  return **myBuffer;
196  }
197 
198 protected:
199 
203  const std::unique_ptr< Association< BufferType > > myBuffer;
204 
205 private:
206 
207  void checkBuffer()
208  {
210  ( myBuffer.get() && myBuffer->get()
211  , "No volume data buffer supplied!" );
212 
214  ( myBuffer->get()->size() >= size.x() * size.y() * size.z()
215  , "Supplied volume data buffer is of size "
216  << myBuffer->get()->size()
217  << " bytes but must be at least "
218  << 4 * sizeof( BufferedVectorComponentType ) * size.x() * size.y() * size.z()
219  << " bytes!" );
220  }
221 
222 }; // BufferedHUVolume
223 
224 
225 
226 } // namespace Carna :: base
227 
228 } // namespace Carna
229 
230 #endif // BUFFEREDNORMALMAP3D_H_6014714286
static BufferedVectorComponentType encodeComponent(float actualVectorComponent)
Returns the buffered vector component corresponding to actualVectorComponent.
static float decodeComponent(BufferedVectorComponentType encodedVectorComponent)
Returns the actual normal vector component corresponding to encodedVectorComponent.
Defines Carna::base::text.
#define CARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
void setVoxel(unsigned int x, unsigned int y, unsigned int z, const math::Vector3f &normal)
Implements NormalMap3D generically for a particular VoxelType.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition: math.h:199
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Definition: math.h:196
BufferedNormalMap3D(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates.
math::Vector3ui size
Holds the resolution.
Definition: NormalMap3D.h:66
Defines Carna::base::NormalMap3D.
BufferType Buffer
Holds the used buffer type.
void setVoxel(const math::Vector3ui &location, const math::Vector3f &normal)
Encodes normal and stores it at location.
BufferedNormalMap3D(const math::Vector3ui &size)
BufferType & buffer()
References the underlying buffer.
BufferedVectorComponentType BufferedVectorComponent
Holds the type used to store the components of the normal vectors.
Defines interface to mapping.
Definition: NormalMap3D.h:46
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
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the buffer.
const BufferType & buffer() const
References the underlying buffer.
Defines Carna::base::Composition.
math::Vector3f operator()(const math::Vector3ui &location) const
Decodes and tells the vector stored at location.