LibCarna Version 3.4.0
Loading...
Searching...
No Matches
BufferedNormalMap3D.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef BUFFEREDNORMALMAP3D_H_6014714286
16#define BUFFEREDNORMALMAP3D_H_6014714286
17
27#include <vector>
28#include <memory>
29#include <type_traits>
30#include <climits>
31
32namespace LibCarna
33{
34
35namespace base
36{
37
38
39
40// ----------------------------------------------------------------------------------
41// BufferedNormalMap3D
42// ----------------------------------------------------------------------------------
43
61template< typename BufferedVectorComponentType, typename BufferType >
63{
64
65 static_assert
66 ( std::is_integral< BufferedVectorComponentType >::value
67 , "Only integral buffer vector component types allowed." );
68
69public:
70
75
80
93
97 : NormalMap3D( size )
99 ( new BufferType( 4 * sizeof( BufferedVectorComponentType ) * size.x() * size.y() * size.z() ) ) )
100 {
101 checkBuffer();
102 }
103
113 {
114 const float range = static_cast< float >( static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::max() )
115 - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() ) );
116 const signed long x = encodedVectorComponent;
117 const float fraction = ( x - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() ) ) / range;
118 return ( fraction - 0.5f ) * 2;
119 }
120
131 {
133 ( std::abs( actualVectorComponent ) <= 1
134 , "Unnormalized vector! Component: " + text::lexical_cast< std::string >( actualVectorComponent ) );
135
136 const signed long range = static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::max() )
137 - static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() );
138 const signed long result = static_cast< signed long >( ( ( actualVectorComponent + 1 ) * range ) / 2 )
139 + static_cast< signed long >( std::numeric_limits< BufferedVectorComponentType >::min() );
140 return static_cast< BufferedVectorComponentType >( result );
141 }
142
147 {
148 return ( *this )( location.x(), location.y(), location.z() );
149 }
150
154 ( unsigned int x
155 , unsigned int y
156 , unsigned int z ) const
157 {
158 math::Vector3f result;
159 const std::size_t index = 4 * ( x + size.x() * y + size.y() * size.x() * z );
160 result.x() = decodeComponent( myBuffer->get()->at( index + 0 ) );
161 result.y() = decodeComponent( myBuffer->get()->at( index + 1 ) );
162 result.z() = decodeComponent( myBuffer->get()->at( index + 2 ) );
163 return result;
164 }
165
169 void setVoxel( const math::Vector3ui& location, const math::Vector3f& normal )
170 {
171 this->setVoxel( location.x(), location.y(), location.z(), normal );
172 }
173
176 void setVoxel( unsigned int x, unsigned int y, unsigned int z, const math::Vector3f& normal )
177 {
178 LIBCARNA_ASSERT( x < size.x() && y < size.y() && z < size.z() );
179 const std::size_t index = 4 * ( x + size.x() * y + size.y() * size.x() * z );
180 myBuffer->get()->at( index + 0 ) = encodeComponent( normal.x() );
181 myBuffer->get()->at( index + 1 ) = encodeComponent( normal.y() );
182 myBuffer->get()->at( index + 2 ) = encodeComponent( normal.z() );
183 }
184
189 {
190 return **myBuffer;
191 }
192
196 const BufferType& buffer() const
197 {
198 return **myBuffer;
199 }
200
201protected:
202
206 const std::unique_ptr< Association< BufferType > > myBuffer;
207
208private:
209
210 void checkBuffer()
211 {
213 ( myBuffer.get() && myBuffer->get()
214 , "No volume data buffer supplied!" );
215
217 ( myBuffer->get()->size() >= size.x() * size.y() * size.z()
218 , "Supplied volume data buffer is of size "
219 << myBuffer->get()->size()
220 << " bytes but must be at least "
221 << 4 * sizeof( BufferedVectorComponentType ) * size.x() * size.y() * size.z()
222 << " bytes!" );
223 }
224
225}; // BufferedNormalMap3D
226
227
228
229} // namespace LibCarna :: base
230
231} // namespace LibCarna
232
233#endif // BUFFEREDNORMALMAP3D_H_6014714286
Defines LibCarna::base::Composition.
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define LIBCARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Defines LibCarna::base::NormalMap3D.
Represents an association.
AssociatedObjectType * get() const
Returns raw pointer to the referenced object.
Implements NormalMap3D generically for a particular VoxelType.
void setVoxel(const math::Vector3ui &location, const math::Vector3f &normal)
Encodes normal and stores it at location.
BufferType Buffer
Holds the used buffer type.
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.
void setVoxel(unsigned int x, unsigned int y, unsigned int z, const math::Vector3f &normal)
math::Vector3f operator()(const math::Vector3ui &location) const
Decodes and tells the vector stored at location.
const BufferType & buffer() const
References the underlying buffer.
const std::unique_ptr< Association< BufferType > > myBuffer
Holds the buffer.
BufferedNormalMap3D(const math::Vector3ui &size)
BufferedVectorComponentType BufferedVectorComponent
Holds the type used to store the components of the normal vectors.
BufferType & buffer()
References the underlying buffer.
BufferedNormalMap3D(const math::Vector3ui &size, Association< BufferType > *buffer)
Instantiates.
Represents a composition, i.e. a strong reference. This basically is a std::unique_ptr that supports ...
Defines interface to mapping.
math::Vector3ui size
Holds the resolution.
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Definition math.hpp:200
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition math.hpp:203
Defines LibCarna::base::text.