Developer Documentation

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