Developer Documentation
VHierarchyWindow.hh
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 //=============================================================================
45 //
46 // CLASS newClass
47 //
48 //=============================================================================
49 
50 #ifndef OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
51 #define OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
52 
53 
54 //== INCLUDES =================================================================
55 
56 #include <OpenMesh/Tools/VDPM/VHierarchy.hh>
57 #include <algorithm>
58 
59 //== FORWARDDECLARATIONS ======================================================
60 
61 
62 //== NAMESPACES ===============================================================
63 
64 namespace OpenMesh {
65 namespace VDPM {
66 
67 //== CLASS DEFINITION =========================================================
68 
69 
73 {
74 private:
75 
76  // reference of vertex hierarchy
77  VHierarchy *vhierarchy_;
78 
79  // bits buffer (byte units)
80  unsigned char *buffer_;
81  int buffer_min_;
82  size_t buffer_max_;
83  int current_pos_;
84 
85  // window (byte units)
86  int window_min_;
87  int window_max_;
88 
89 
90  // # of right shift (bit units)
91  unsigned char n_shift_; // [0, 7]
92 
93  unsigned char flag8(unsigned char n_shift) const
94  { return 0x80 >> n_shift; }
95 
96  unsigned char flag8(VHierarchyNodeHandle _node_handle) const
97  {
98  assert(_node_handle.idx() >= 0);
99  return 0x80 >> (unsigned int) (_node_handle.idx() % 8);
100  }
101  int byte_idx(VHierarchyNodeHandle _node_handle) const
102  {
103  assert(_node_handle.idx() >= 0);
104  return _node_handle.idx() / 8;
105  }
106  int buffer_idx(VHierarchyNodeHandle _node_handle) const
107  { return byte_idx(_node_handle) - buffer_min_; }
108 
109  bool before_window(VHierarchyNodeHandle _node_handle) const
110  { return (_node_handle.idx()/8 < window_min_) ? true : false; }
111 
112  bool after_window(VHierarchyNodeHandle _node_handle) const
113  { return (_node_handle.idx()/8 < window_max_) ? false : true; }
114 
115  bool underflow(VHierarchyNodeHandle _node_handle) const
116  { return (_node_handle.idx()/8 < buffer_min_) ? true : false; }
117 
118  bool overflow(VHierarchyNodeHandle _node_handle) const
119  { return (_node_handle.idx()/8 < int(buffer_max_) ) ? false : true; }
120 
121  bool update_buffer(VHierarchyNodeHandle _node_handle);
122 
123 public:
125  VHierarchyWindow(VHierarchy &_vhierarchy);
126  ~VHierarchyWindow(void);
127 
128  void set_vertex_hierarchy(VHierarchy &_vhierarchy)
129  { vhierarchy_ = &_vhierarchy; }
130 
131  void begin()
132  {
133  int new_window_min = window_min_;
134  for (current_pos_=window_min_-buffer_min_;
135  current_pos_ < window_size(); ++current_pos_)
136  {
137  if (buffer_[current_pos_] == 0)
138  ++new_window_min;
139  else
140  {
141  n_shift_ = 0;
142  while ((buffer_[current_pos_] & flag8(n_shift_)) == 0)
143  ++n_shift_;
144  break;
145  }
146  }
147  window_min_ = new_window_min;
148  }
149 
150  void next()
151  {
152  ++n_shift_;
153  if (n_shift_ == 8)
154  {
155  n_shift_ = 0;
156  ++current_pos_;
157  }
158 
159  while (current_pos_ < window_max_-buffer_min_)
160  {
161  if (buffer_[current_pos_] != 0) // if the current byte has non-zero bits
162  {
163  while (n_shift_ != 8)
164  {
165  if ((buffer_[current_pos_] & flag8(n_shift_)) != 0)
166  return; // find 1 bit in the current byte
167  ++n_shift_;
168  }
169  }
170  n_shift_ = 0;
171  ++current_pos_;
172  }
173  }
174  bool end() { return !(current_pos_ < window_max_-buffer_min_); }
175 
176  int window_size() const { return window_max_ - window_min_; }
177  size_t buffer_size() const { return buffer_max_ - buffer_min_; }
178 
179  VHierarchyNodeHandle node_handle()
180  {
181  return VHierarchyNodeHandle(8*(buffer_min_+current_pos_) + (int)n_shift_);
182  }
183 
184  void activate(VHierarchyNodeHandle _node_handle)
185  {
186  update_buffer(_node_handle);
187  buffer_[buffer_idx(_node_handle)] |= flag8(_node_handle);
188  window_min_ = std::min(window_min_, byte_idx(_node_handle));
189  window_max_ = std::max(window_max_, 1+byte_idx(_node_handle));
190  }
191 
192 
193  void inactivate(VHierarchyNodeHandle _node_handle)
194  {
195  if (is_active(_node_handle) != true) return;
196  buffer_[buffer_idx(_node_handle)] ^= flag8(_node_handle);
197  }
198 
199 
200  bool is_active(VHierarchyNodeHandle _node_handle) const
201  {
202  if (before_window(_node_handle) == true ||
203  after_window(_node_handle) == true)
204  return false;
205  return ((buffer_[buffer_idx(_node_handle)] & flag8(_node_handle)) > 0);
206  }
207 
208  void init(VHierarchyNodeHandleContainer &_roots);
209  void update_with_vsplit(VHierarchyNodeHandle _parent_handle);
210  void update_with_ecol(VHierarchyNodeHandle _parent_handle);
211 };
212 
213 //=============================================================================
214 } // namespace VDPM
215 } // namespace OpenMesh
216 //=============================================================================
217 #endif // OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
218 //=============================================================================
219 
int idx() const
Get the underlying index of this handle.
Definition: Handles.hh:69
std::vector< VHierarchyNodeHandle > VHierarchyNodeHandleContainer
Container for vertex hierarchy node handles.