Carna Version 3.3.3
Loading...
Searching...
No Matches
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
22#include <Carna/base/text.h>
23#include <vector>
24#include <memory>
25#include <type_traits>
26#include <climits>
27
28namespace Carna
29{
30
31namespace base
32{
33
34
35
36// ----------------------------------------------------------------------------------
37// BufferedNormalMap3D
38// ----------------------------------------------------------------------------------
39
58template< typename BufferedVectorComponentType, typename BufferType >
60{
61
62 static_assert
63 ( std::is_integral< BufferedVectorComponentType >::value
64 , "Only integral buffer vector component types allowed." );
65
66public:
67
72
77
90
94 : NormalMap3D( size )
96 ( new BufferType( 4 * sizeof( BufferedVectorComponentType ) * size.x() * size.y() * size.z() ) ) )
97 {
98 checkBuffer();
99 }
100
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
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
144 {
145 return ( *this )( location.x(), location.y(), location.z() );
146 }
147
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
186 {
187 return **myBuffer;
188 }
189
193 const BufferType& buffer() const
194 {
195 return **myBuffer;
196 }
197
198protected:
199
203 const std::unique_ptr< Association< BufferType > > myBuffer;
204
205private:
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
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::NormalMap3D.
Represents an association.
Definition Association.h:45
AssociatedObjectType * get() const
Returns raw pointer to the referenced object.
Definition Association.h:61
Implements NormalMap3D generically for a particular VoxelType.
static float decodeComponent(BufferedVectorComponentType encodedVectorComponent)
Returns the actual normal vector component corresponding to encodedVectorComponent.
static BufferedVectorComponentType encodeComponent(float actualVectorComponent)
Returns the buffered vector component corresponding to actualVectorComponent.
BufferedVectorComponentType BufferedVectorComponent
Holds the type used to store the components of the normal vectors.
BufferType & buffer()
References the underlying buffer.
const BufferType & buffer() const
References the underlying buffer.
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the buffer.
void setVoxel(const math::Vector3ui &location, const math::Vector3f &normal)
Encodes normal and stores it at location.
math::Vector3f operator()(const math::Vector3ui &location) const
Decodes and tells the vector stored at location.
BufferedNormalMap3D(const math::Vector3ui &size)
void setVoxel(unsigned int x, unsigned int y, unsigned int z, const math::Vector3f &normal)
BufferedNormalMap3D(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates.
BufferType Buffer
Holds the used buffer type.
Represents a composition, i.e. a strong reference. This basically is a std::unique_ptr that supports ...
Definition Composition.h:53
Defines interface to mapping.
Definition NormalMap3D.h:47
math::Vector3ui size
Holds the resolution.
Definition NormalMap3D.h:66
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Definition math.h:196
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition math.h:199
Defines Carna::base::text.