Developer Documentation
VHierarchyWindow.cc
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2025, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42
43
44//=============================================================================
45//
46// CLASS newClass - IMPLEMENTATION
47//
48//=============================================================================
49
50
51//== INCLUDES =================================================================
52
53#include <OpenMesh/Tools/VDPM/VHierarchyWindow.hh>
54
55#include <cstring>
56
57#ifndef WIN32
58#else
59 #if defined(__MINGW32__)
60 #include <stdlib.h>
61 #include <string.h>
62 #endif
63#endif
64
65//== NAMESPACES ===============================================================
66
67namespace OpenMesh {
68namespace VDPM {
69
70
71//== IMPLEMENTATION ==========================================================
72
73
74VHierarchyWindow::
75VHierarchyWindow() :
76 vhierarchy_(nullptr), buffer_(nullptr),buffer_min_ (0), buffer_max_(0), current_pos_(0) , window_min_(0), window_max_(0), n_shift_(0)
77{
78
79}
80
81
82VHierarchyWindow::
83VHierarchyWindow(VHierarchy &_vhierarchy) :
84 vhierarchy_(&_vhierarchy),buffer_(nullptr),buffer_min_ (0), buffer_max_(0), current_pos_(0) , window_min_(0), window_max_(0) ,n_shift_(0)
85{
86}
87
88
89VHierarchyWindow::
90~VHierarchyWindow(void)
91{
92 if (buffer_ != nullptr)
93 free(buffer_);
94}
95
96
97bool
98VHierarchyWindow::
99update_buffer(VHierarchyNodeHandle _node_handle)
100{
101 if (underflow(_node_handle) != true && overflow(_node_handle) != true)
102 return false;
103
104 // tightly update window_min_ & window_max_
105 int none_zero_pos;
106 for (none_zero_pos=int(buffer_size()-1); none_zero_pos >= 0; --none_zero_pos)
107 {
108 if (buffer_[none_zero_pos] != 0) break;
109 }
110 window_max_ = buffer_min_ + none_zero_pos + 1;
111 for(none_zero_pos=0; none_zero_pos < int(buffer_size()); ++none_zero_pos)
112 {
113 if (buffer_[none_zero_pos] != 0) break;
114 }
115 window_min_ = buffer_min_ + none_zero_pos;
116
117 assert(window_min_ < window_max_);
118
119 while (underflow(_node_handle) == true) buffer_min_ /= 2;
120 while (overflow(_node_handle) == true)
121 {
122 buffer_max_ *= 2;
123 if (buffer_max_ > vhierarchy_->num_nodes() / 8)
124 buffer_max_ = 1 + vhierarchy_->num_nodes() / 8;
125 }
126
127 unsigned char *new_buffer = static_cast<unsigned char *>(malloc(buffer_size()));
128 memset(new_buffer, 0, buffer_size());
129 memcpy(&(new_buffer[window_min_-buffer_min_]),
130 &(buffer_[none_zero_pos]),
131 window_size());
132 free(buffer_);
133 buffer_ = new_buffer;
134
135 return true;
136}
137
138void
139VHierarchyWindow::init(VHierarchyNodeHandleContainer &_roots)
140{
141 if (buffer_ != nullptr)
142 free(buffer_);
143
144 buffer_min_ = 0;
145 buffer_max_ = _roots.size() / 8;
146 if (_roots.size() % 8 > 0)
147 ++buffer_max_;
148
149 buffer_ = static_cast<unsigned char *>(malloc(buffer_size()));
150 memset(buffer_, 0, buffer_size());
151
152 window_min_ = 0;
153 window_max_= 0;
154 current_pos_ = 0;
155 n_shift_ = 0;
156
157 for (unsigned int i=0; i<_roots.size(); i++)
158 {
159 activate(VHierarchyNodeHandle((int) i));
160 }
161}
162
163
164void
165VHierarchyWindow::
166update_with_vsplit(VHierarchyNodeHandle _parent_handle)
167{
168 VHierarchyNodeHandle
169 lchild_handle = vhierarchy_->lchild_handle(_parent_handle),
170 rchild_handle = vhierarchy_->rchild_handle(_parent_handle);
171
172 assert(is_active(_parent_handle) == true);
173 assert(is_active(lchild_handle) != true);
174 assert(is_active(rchild_handle) != true);
175
176 inactivate(_parent_handle);
177 activate(rchild_handle);
178 activate(lchild_handle);
179}
180
181void
182VHierarchyWindow::
183update_with_ecol(VHierarchyNodeHandle _parent_handle)
184{
185 VHierarchyNodeHandle
186 lchild_handle = vhierarchy_->lchild_handle(_parent_handle),
187 rchild_handle = vhierarchy_->rchild_handle(_parent_handle);
188
189 assert(is_active(_parent_handle) != true);
190 assert(is_active(lchild_handle) == true);
191 assert(is_active(rchild_handle) == true);
192
193 activate(_parent_handle);
194 inactivate(rchild_handle);
195 inactivate(lchild_handle);
196}
197
198//=============================================================================
199} // namespace VDPM
200} // namespace OpenMesh
201//=============================================================================