Debian Codegraph @

src/thirdparty/PoissonRecon/FEMTree.SortedTreeNodes.inl

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 /////////////////////
30 // SortedTreeNodes //
31 /////////////////////
32 template< unsigned int Dim >
33 SortedTreeNodes< Dim >::SortedTreeNodes( void )
34 {
35 _sliceStart = NullPointer( Pointer( node_index_type ) );
36 treeNodes = NullPointer( TreeNode* );
37 _levels = 0;
38 }
39
40 template< unsigned int Dim >
41 SortedTreeNodes< Dim >::~SortedTreeNodes( void )
42 {
43 if( _sliceStart ) for( int d=0 ; d<_levels ; d++ ) FreePointer( _sliceStart[d] );
44 FreePointer( _sliceStart );
45 DeletePointer( treeNodes );
46 }
47
48 template< unsigned int Dim >
49 void SortedTreeNodes< Dim >::write( BinaryStream &stream ) const
50 {
51 stream.write( _levels );
52 for( int l=0 ; l<_levels ; l++ )
53 {
54 size_t sz = ((size_t)1<<l)+1;
55 stream.write( _sliceStart[l] , sz );
56 }
57 }
58
59 template< unsigned int Dim >
60 void SortedTreeNodes< Dim >::read( BinaryStream &stream , TreeNode &root )
61 {
62 for( int l=0 ; l<_levels ; l++ ) FreePointer( _sliceStart[l] );
63 FreePointer( _sliceStart );
64 DeletePointer( treeNodes );
65
66 _levels = 0;
67 _sliceStart = NullPointer( Pointer( node_index_type ) );
68 treeNodes = NullPointer( TreeNode* );
69
70 if( !stream.read( _levels ) ) MK_THROW( "Failed to read levels" );
71 if( _levels )
72 {
73 _sliceStart = AllocPointer< Pointer( node_index_type ) >( _levels );
74 for( int l=0 ; l<_levels ; l++ )
75 {
76 size_t sz = ((size_t)1<<l)+1;
77 _sliceStart[l] = AllocPointer< node_index_type >( sz );
78 if( !stream.read( _sliceStart[l] , sz ) ) MK_THROW( "Failed to read slices at level: " , l );
79 }
80
81 size_t sz = _sliceStart[_levels-1][(size_t)1<<(_levels-1)];
82 treeNodes = NewPointer< TreeNode* >( sz );
83 auto nodeFunctor = [&]( TreeNode *n ){ if( n->nodeData.nodeIndex>=0 && n->nodeData.nodeIndex<(node_index_type)sz ) treeNodes[ n->nodeData.nodeIndex ] = n; };
84 root.processNodes( nodeFunctor );
85 }
86 }
87
88 template< unsigned int Dim >
89 void SortedTreeNodes< Dim >::reset( TreeNode& root , std::vector< node_index_type > &map )
90 {
91 _set( root );
92 map.resize( _sliceStart[_levels-1][(size_t)1<<(_levels-1)] , -1 );
93 for( node_index_type i=0 ; i<_sliceStart[_levels-1][(size_t)1<<(_levels-1)] ; i++ )
94 {
95 if( treeNodes[i]->nodeData.nodeIndex>=0 ) map[i] = treeNodes[i]->nodeData.nodeIndex;
96 treeNodes[i]->nodeData.nodeIndex = i;
97 }}
98
99 template< unsigned int Dim >
100 void SortedTreeNodes< Dim >::set( TreeNode& root )
101 {
102 _set( root );
103 for( node_index_type i=0 ; i<_sliceStart[_levels-1][(size_t)1<<(_levels-1)] ; i++ ) treeNodes[i]->nodeData.nodeIndex = i;
104 }
105
106 template< unsigned int Dim >
107 void SortedTreeNodes< Dim >::_set( TreeNode& root )
108 {
109 if( _sliceStart ) for( int d=0 ; d<_levels ; d++ ) FreePointer( _sliceStart[d] );
110 FreePointer( _sliceStart );
111 DeletePointer( treeNodes );
112 _levels = root.maxDepth()+1;
113
114 _sliceStart = AllocPointer< Pointer( node_index_type ) >( _levels );
115 for( int l=0 ; l<_levels ; l++ )
116 {
117 _sliceStart[l] = AllocPointer< node_index_type >( ((size_t)1<<l)+1 );
118 memset( _sliceStart[l] , 0 , sizeof(node_index_type)*( ((size_t)1<<l)+1 ) );
119 }
120
121 // Count the number of nodes in each slice
122 auto nodeFunctor = [&]( TreeNode *node )
123 {
124 if( !GetGhostFlag< Dim >( node ) )
125 {
126 int d , off[Dim];
127 node->depthAndOffset( d , off );
128 _sliceStart[d][ off[Dim-1]+1 ]++;
129 }
130 };
131 root.processNodes( nodeFunctor );
132
133 // Get the start index for each slice
134 {
135 node_index_type levelOffset = 0;
136 for( int l=0 ; l<_levels ; l++ )
137 {
138 _sliceStart[l][0] = levelOffset;
139 for( int s=0 ; s<((size_t)1<<l); s++ ) _sliceStart[l][s+1] += _sliceStart[l][s];
140 levelOffset = _sliceStart[l][(size_t)1<<l];
141 }
142 }
143 // Allocate memory for the tree nodes
144 treeNodes = NewPointer< TreeNode* >( _sliceStart[_levels-1][(size_t)1<<(_levels-1)] );
145
146 // Add the tree nodes
147 {
148 auto nodeFunctor = [&]( TreeNode *node )
149 {
150 if( !GetGhostFlag< Dim >( node ) )
151 {
152 int d , off[Dim];
153 node->depthAndOffset( d , off );
154 treeNodes[ _sliceStart[d][ off[Dim-1] ]++ ] = node;
155 }
156 };
157 root.processNodes( nodeFunctor );
158 }
159
160 // Shift the slice offsets up since we incremented as we added
161 for( int l=0 ; l<_levels ; l++ )
162 {
163 for( int s=(1<<l) ; s>0 ; s-- ) _sliceStart[l][s] = _sliceStart[l][s-1];
164 _sliceStart[l][0] = l>0 ? _sliceStart[l-1][(size_t)1<<(l-1)] : 0;
165 }
166 }
167