Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
VHierarchy.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/VHierarchy.hh>
59 
60 
61 //== NAMESPACES ===============================================================
62 
63 namespace OpenMesh {
64 namespace VDPM {
65 
66 //== IMPLEMENTATION ==========================================================
67 
68 
69 VHierarchy::
70 VHierarchy() :
71  n_roots_(0), tree_id_bits_(0)
72 {
73  clear();
74 }
75 
76 void
77 VHierarchy::
78 set_num_roots(unsigned int _n_roots)
79 {
80  n_roots_ = _n_roots;
81 
82  tree_id_bits_ = 0;
83  while (n_roots_ > ((unsigned int) 0x00000001 << tree_id_bits_))
84  ++tree_id_bits_;
85 }
86 
87 
88 VHierarchyNodeHandle
89 VHierarchy::
90 add_node()
91 {
92  return add_node(VHierarchyNode());
93 }
94 
95 VHierarchyNodeHandle
96 VHierarchy::
97 add_node(const VHierarchyNode &_node)
98 {
99  nodes_.push_back(_node);
100 
101  return VHierarchyNodeHandle(int(nodes_.size() - 1));
102 }
103 
104 
105 void
106 VHierarchy::
107 make_children(VHierarchyNodeHandle &_parent_handle)
108 {
109  VHierarchyNodeHandle lchild_handle = add_node();
110  VHierarchyNodeHandle rchild_handle = add_node();
111 
112  VHierarchyNode &parent = node(_parent_handle);
113  VHierarchyNode &lchild = node(lchild_handle);
114  VHierarchyNode &rchild = node(rchild_handle);
115 
116  parent.set_children_handle(lchild_handle);
117  lchild.set_parent_handle(_parent_handle);
118  rchild.set_parent_handle(_parent_handle);
119 
120  lchild.set_index(VHierarchyNodeIndex(parent.node_index().tree_id(tree_id_bits_), 2*parent.node_index().node_id(tree_id_bits_), tree_id_bits_));
121  rchild.set_index(VHierarchyNodeIndex(parent.node_index().tree_id(tree_id_bits_), 2*parent.node_index().node_id(tree_id_bits_)+1, tree_id_bits_));
122 }
123 
124 VHierarchyNodeHandle
125 VHierarchy::
126 node_handle(VHierarchyNodeIndex _node_index)
127 {
128  if (_node_index.is_valid(tree_id_bits_) != true)
129  return InvalidVHierarchyNodeHandle;
130 
131  VHierarchyNodeHandle node_handle = root_handle(_node_index.tree_id(tree_id_bits_));
132  unsigned int node_id = _node_index.node_id(tree_id_bits_);
133  unsigned int flag = 0x80000000;
134 
135  while (!(node_id & flag)) flag >>= 1;
136  flag >>= 1;
137 
138  while (flag > 0 && is_leaf_node(node_handle) != true)
139  {
140  if (node_id & flag) // 1: rchild
141  {
142  node_handle = rchild_handle(node_handle);
143  }
144  else // 0: lchild
145  {
146  node_handle = lchild_handle(node_handle);
147  }
148  flag >>= 1;
149  }
150 
151  return node_handle;
152 }
153 
154 bool
155 VHierarchy::
156 is_ancestor(VHierarchyNodeIndex _ancestor_index, VHierarchyNodeIndex _descendent_index)
157 {
158  if (_ancestor_index.tree_id(tree_id_bits_) != _descendent_index.tree_id(tree_id_bits_))
159  return false;
160 
161  unsigned int ancestor_node_id = _ancestor_index.node_id(tree_id_bits_);
162  unsigned int descendent_node_id = _descendent_index.node_id(tree_id_bits_);
163 
164  if (ancestor_node_id > descendent_node_id)
165  return false;
166 
167  while (descendent_node_id > 0)
168  {
169  if (ancestor_node_id == descendent_node_id)
170  return true;
171  descendent_node_id >>= 1; // descendent_node_id /= 2
172  }
173 
174  return false;
175 }
176 
177 //=============================================================================
178 } // namespace VDPM
179 } // namespace OpenMesh
180 //=============================================================================