3 #ifndef SYNCHRONIZED_TEXTURE_HEAP_DETAIL_OCTREE_H
4 #define SYNCHRONIZED_TEXTURE_HEAP_DETAIL_OCTREE_H
16 namespace Synchronized {
31 inline Octree(
unsigned int const height = 0 );
34 inline unsigned int const Height()
const;
40 inline unsigned int const CountLeaves()
const;
42 template<
typename t_Iterator >
43 inline void Leaves( t_Iterator
const& destination )
const;
58 inline Node(
unsigned int const splits );
61 unsigned int const CountLeaves()
const;
63 template<
typename t_Iterator >
65 t_Iterator& destination,
67 unsigned int const height
71 bool const FindInsert(
73 unsigned int const logSize,
74 unsigned int const height
79 unsigned int const logSize,
80 unsigned int const height
86 std::unique_ptr< Node >& owner,
87 unsigned int const logSize,
88 unsigned int const height
94 std::unique_ptr< Node > m_children[ 8 ];
98 std::unique_ptr< Node > m_root;
99 unsigned int m_height;
110 Octree::Octree(
unsigned int const height ) : m_height( height ) {
114 unsigned int const Octree::Height()
const {
123 logSizes[ 2 ] = logSize / 3;
124 logSizes[ 1 ] = ( logSize - logSizes[ 2 ] ) / 2;
125 logSizes[ 0 ] = logSize - logSizes[ 1 ] - logSizes[ 2 ];
126 assert( ( logSizes[ 0 ] >= logSizes[ 1 ] ) && ( logSizes[ 1 ] >= logSizes[ 2 ] ) );
127 assert( logSizes[ 2 ] + 1 >= logSizes[ 0 ] );
129 unsigned int splits = 0;
130 if ( logSizes[ 1 ] < logSizes[ 0 ] )
132 if ( logSizes[ 2 ] < logSizes[ 0 ] )
135 m_root.reset(
new Node( splits ) );
136 m_height = logSizes[ 0 ];
144 unsigned int const Octree::CountLeaves()
const {
146 unsigned int result = 0;
148 result = m_root->CountLeaves();
153 template<
typename t_Iterator >
154 void Octree::Leaves( t_Iterator
const& destination )
const {
158 t_Iterator destinationCopy = destination;
160 m_root->Leaves( destinationCopy, coordinates, m_height );
172 Octree::Node::Node() {
176 Octree::Node::Node(
unsigned int const splits ) {
178 assert( ( splits >= 0 ) && ( splits < 3 ) );
182 unsigned int const children = ( 1u << ( 3 - splits ) );
183 for (
unsigned int ii = 0; ii < children; ++ii )
184 m_children[ ii ].reset(
new Node );
189 template<
typename t_Iterator >
190 void Octree::Node::Leaves( t_Iterator& destination,
Vector< unsigned int, 3 >& coordinates,
unsigned int const height )
const {
192 unsigned int children = 0;
194 for (
unsigned int ii = 0; ii < 8; ++ii ) {
196 if ( m_children[ ii ] ) {
198 coordinates &= ~( ( 1u << height ) - 1 );
200 coordinates[ 0 ] |= ( 1u << ( height - 1 ) );
202 coordinates[ 1 ] |= ( 1u << ( height - 1 ) );
204 coordinates[ 2 ] |= ( 1u << ( height - 1 ) );
206 m_children[ ii ]->Leaves( destination, coordinates, height - 1 );
211 if ( children == 0 ) {
213 *destination = std::pair< Vector< unsigned int, 3 >,
unsigned int >( coordinates, height );
229 #endif // SYNCHRONIZED_TEXTURE_HEAP_DETAIL_OCTREE_H