Developer Documentation
VHierarchyWindow.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, 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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 //=============================================================================
50 //
51 // CLASS newClass - IMPLEMENTATION
52 //
53 //=============================================================================
54 
55 
56 //== INCLUDES =================================================================
57 
58 #include <OpenMesh/Tools/VDPM/VHierarchyWindow.hh>
59 
60 #include <cstring>
61 
62 #ifndef WIN32
63 #else
64  #if defined(__MINGW32__)
65  #include <stdlib.h>
66  #include <string.h>
67  #endif
68 #endif
69 
70 //== NAMESPACES ===============================================================
71 
72 namespace OpenMesh {
73 namespace VDPM {
74 
75 
76 //== IMPLEMENTATION ==========================================================
77 
78 
79 VHierarchyWindow::
80 VHierarchyWindow() :
81  vhierarchy_(NULL), buffer_(NULL),buffer_min_ (0), buffer_max_(0), current_pos_(0) , window_min_(0), window_max_(0), n_shift_(0)
82 {
83 
84 }
85 
86 
87 VHierarchyWindow::
88 VHierarchyWindow(VHierarchy &_vhierarchy) :
89  vhierarchy_(&_vhierarchy),buffer_(NULL),buffer_min_ (0), buffer_max_(0), current_pos_(0) , window_min_(0), window_max_(0) ,n_shift_(0)
90 {
91 }
92 
93 
94 VHierarchyWindow::
95 ~VHierarchyWindow(void)
96 {
97  if (buffer_ != NULL)
98  free(buffer_);
99 }
100 
101 
102 bool
103 VHierarchyWindow::
104 update_buffer(VHierarchyNodeHandle _node_handle)
105 {
106  if (underflow(_node_handle) != true && overflow(_node_handle) != true)
107  return false;
108 
109  // tightly update window_min_ & window_max_
110  int none_zero_pos;
111  for (none_zero_pos=int(buffer_size()-1); none_zero_pos >= 0; --none_zero_pos)
112  {
113  if (buffer_[none_zero_pos] != 0) break;
114  }
115  window_max_ = buffer_min_ + none_zero_pos + 1;
116  for(none_zero_pos=0; none_zero_pos < int(buffer_size()); ++none_zero_pos)
117  {
118  if (buffer_[none_zero_pos] != 0) break;
119  }
120  window_min_ = buffer_min_ + none_zero_pos;
121 
122  assert(window_min_ < window_max_);
123 
124  while (underflow(_node_handle) == true) buffer_min_ /= 2;
125  while (overflow(_node_handle) == true)
126  {
127  buffer_max_ *= 2;
128  if (buffer_max_ > vhierarchy_->num_nodes() / 8)
129  buffer_max_ = 1 + vhierarchy_->num_nodes() / 8;
130  }
131 
132  unsigned char *new_buffer = (unsigned char *) malloc(buffer_size());
133  memset(new_buffer, 0, buffer_size());
134  memcpy(&(new_buffer[window_min_-buffer_min_]),
135  &(buffer_[none_zero_pos]),
136  window_size());
137  free(buffer_);
138  buffer_ = new_buffer;
139 
140  return true;
141 }
142 
143 void
144 VHierarchyWindow::init(VHierarchyNodeHandleContainer &_roots)
145 {
146  if (buffer_ != NULL)
147  free(buffer_);
148 
149  buffer_min_ = 0;
150  buffer_max_ = _roots.size() / 8;
151  if (_roots.size() % 8 > 0)
152  ++buffer_max_;
153 
154  buffer_ = (unsigned char *) malloc(buffer_size());
155  memset(buffer_, 0, buffer_size());
156 
157  window_min_ = 0;
158  window_max_= 0;
159  current_pos_ = 0;
160  n_shift_ = 0;
161 
162  for (unsigned int i=0; i<_roots.size(); i++)
163  {
164  activate(VHierarchyNodeHandle((int) i));
165  }
166 }
167 
168 
169 void
170 VHierarchyWindow::
171 update_with_vsplit(VHierarchyNodeHandle _parent_handle)
172 {
173  VHierarchyNodeHandle
174  lchild_handle = vhierarchy_->lchild_handle(_parent_handle),
175  rchild_handle = vhierarchy_->rchild_handle(_parent_handle);
176 
177  assert(is_active(_parent_handle) == true);
178  assert(is_active(lchild_handle) != true);
179  assert(is_active(rchild_handle) != true);
180 
181  inactivate(_parent_handle);
182  activate(rchild_handle);
183  activate(lchild_handle);
184 }
185 
186 void
187 VHierarchyWindow::
188 update_with_ecol(VHierarchyNodeHandle _parent_handle)
189 {
190  VHierarchyNodeHandle
191  lchild_handle = vhierarchy_->lchild_handle(_parent_handle),
192  rchild_handle = vhierarchy_->rchild_handle(_parent_handle);
193 
194  assert(is_active(_parent_handle) != true);
195  assert(is_active(lchild_handle) == true);
196  assert(is_active(rchild_handle) == true);
197 
198  activate(_parent_handle);
199  inactivate(rchild_handle);
200  inactivate(lchild_handle);
201 }
202 
203 //=============================================================================
204 } // namespace VDPM
205 } // namespace OpenMesh
206 //=============================================================================
std::vector< VHierarchyNodeHandle > VHierarchyNodeHandleContainer
Container for vertex hierarchy node handles.