Developer Documentation

ArrayKernel.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 #include <OpenMesh/Core/Mesh/ArrayKernel.hh>
45
46 namespace OpenMesh
47 {
48
49 ArrayKernel::ArrayKernel()
50 : refcount_vstatus_(0), refcount_hstatus_(0),
51 refcount_estatus_(0), refcount_fstatus_(0)
52 {
53 init_bit_masks(); //Status bit masks initialization
54 }
55
56 ArrayKernel::~ArrayKernel()
57 {
58 clear();
59 }
60
61 // ArrayKernel::ArrayKernel(const ArrayKernel& _rhs)
62 // : BaseKernel(_rhs),
63 // vertices_(_rhs.vertices_), edges_(_rhs.edges_), faces_(_rhs.faces_),
64 // vertex_status_(_rhs.vertex_status_), halfedge_status_(_rhs.halfedge_status_),
65 // edge_status_(_rhs.edge_status_), face_status_(_rhs.face_status_),
66 // refcount_vstatus_(_rhs.refcount_vstatus_), refcount_hstatus_(_rhs.refcount_hstatus_),
67 // refcount_estatus_(_rhs.refcount_estatus_), refcount_fstatus_(_rhs.refcount_fstatus_)
68 // {}
69
70
71 void ArrayKernel::assign_connectivity(const ArrayKernel& _other)
72 {
73 vertices_ = _other.vertices_;
74 edges_ = _other.edges_;
75 faces_ = _other.faces_;
76
77 vprops_resize(n_vertices());
78 hprops_resize(n_halfedges());
79 eprops_resize(n_edges());
80 fprops_resize(n_faces());
81
82 //just copy status properties for now,
83 //until a proper solution for refcounted
84 //properties is available
85 vertex_status_ = _other.vertex_status_;
86 halfedge_status_ = _other.halfedge_status_;
87 edge_status_ = _other.edge_status_;
88 face_status_ = _other.face_status_;
89
90 //initialize refcounter to 1 for the new mesh,
91 //if status is available.
92 refcount_estatus_ = _other.refcount_estatus_ > 0 ? 1 : 0;
93 refcount_vstatus_ = _other.refcount_vstatus_ > 0 ? 1 : 0;
94 refcount_hstatus_ = _other.refcount_hstatus_ > 0 ? 1 : 0;
95 refcount_fstatus_ = _other.refcount_fstatus_ > 0 ? 1 : 0;
96 }
97
98 // --- handle -> item ---
99 VertexHandle ArrayKernel::handle(const Vertex& _v) const
100 {
101 return VertexHandle( int( &_v - &vertices_.front()));
102 }
103
104 HalfedgeHandle ArrayKernel::handle(const Halfedge& _he) const
105 {
106 // Calculate edge belonging to given halfedge
107 // There are two halfedges stored per edge
108 // Get memory position inside edge vector and devide by size of an edge
109 // to get the corresponding edge for the requested halfedge
110 size_t eh = ( (char*)&_he - (char*)&edges_.front() ) / sizeof(Edge) ;
111 assert((&_he == &edges_[eh].halfedges_[0]) ||
112 (&_he == &edges_[eh].halfedges_[1]));
113 return ((&_he == &edges_[eh].halfedges_[0]) ?
114 HalfedgeHandle( int(eh)<<1) : HalfedgeHandle((int(eh)<<1)+1));
115 }
116
117 EdgeHandle ArrayKernel::handle(const Edge& _e) const
118 {
119 return EdgeHandle( int(&_e - &edges_.front() ) );
120 }
121
122 FaceHandle ArrayKernel::handle(const Face& _f) const
123 {
124 return FaceHandle( int(&_f - &faces_.front()) );
125 }
126
127 #define SIGNED(x) signed( (x) )
128
129 bool ArrayKernel::is_valid_handle(VertexHandle _vh) const
130 {
131 return 0 <= _vh.idx() && _vh.idx() < SIGNED(n_vertices());
132 }
133
134 bool ArrayKernel::is_valid_handle(HalfedgeHandle _heh) const
135 {
136 return 0 <= _heh.idx() && _heh.idx() < SIGNED(n_edges()*2);
137 }
138
139 bool ArrayKernel::is_valid_handle(EdgeHandle _eh) const
140 {
141 return 0 <= _eh.idx() && _eh.idx() < SIGNED(n_edges());
142 }
143
144 bool ArrayKernel::is_valid_handle(FaceHandle _fh) const
145 {
146 return 0 <= _fh.idx() && _fh.idx() < SIGNED(n_faces());
147 }
148
149 #undef SIGNED
150
151 unsigned int ArrayKernel::delete_isolated_vertices()
152 {
153 assert(has_vertex_status());//this function requires vertex status property
154 unsigned int n_isolated = 0;
155 for (KernelVertexIter v_it = vertices_begin(); v_it != vertices_end(); ++v_it)
156 {
157 if (is_isolated(handle(*v_it)))
158 {
159 status(handle(*v_it)).set_deleted(true);
160 n_isolated++;
161 }
162 }
163 return n_isolated;
164 }
165
166 void ArrayKernel::garbage_collection(bool _v, bool _e, bool _f)
167 {
168 std::vector<VertexHandle*> empty_vh;
169 std::vector<HalfedgeHandle*> empty_hh;
170 std::vector<FaceHandle*> empty_fh;
171 garbage_collection( empty_vh,empty_hh,empty_fh,_v, _e, _f);
172 }
173
174 void ArrayKernel::clean_keep_reservation()
175 {
176 vertices_.clear();
177
178 edges_.clear();
179
180 faces_.clear();
181
182 }
183
184 void ArrayKernel::clean()
185 {
186
187 vertices_.clear();
188 VertexContainer().swap( vertices_ );
189
190 edges_.clear();
191 EdgeContainer().swap( edges_ );
192
193 faces_.clear();
194 FaceContainer().swap( faces_ );
195
196 }
197
198
199 void ArrayKernel::clear()
200 {
201 vprops_clear();
202 eprops_clear();
203 hprops_clear();
204 fprops_clear();
205
206 clean();
207 }
208
209
210
211 void ArrayKernel::resize( size_t _n_vertices, size_t _n_edges, size_t _n_faces )
212 {
213 vertices_.resize(_n_vertices);
214 edges_.resize(_n_edges);
215 faces_.resize(_n_faces);
216
217 vprops_resize(n_vertices());
218 hprops_resize(n_halfedges());
219 eprops_resize(n_edges());
220 fprops_resize(n_faces());
221 }
222
223 void ArrayKernel::reserve(size_t _n_vertices, size_t _n_edges, size_t _n_faces )
224 {
225 vertices_.reserve(_n_vertices);
226 edges_.reserve(_n_edges);
227 faces_.reserve(_n_faces);
228
229 vprops_reserve(_n_vertices);
230 hprops_reserve(_n_edges*2);
231 eprops_reserve(_n_edges);
232 fprops_reserve(_n_faces);
233 }
234
235 // Status Sets API
236 void ArrayKernel::init_bit_masks(BitMaskContainer& _bmc)
237 {
238 for (unsigned int i = Attributes::UNUSED; i != 0; i <<= 1)
239 {
240 _bmc.push_back(i);
241 }
242 }
243
244 void ArrayKernel::init_bit_masks()
245 {
246 init_bit_masks(vertex_bit_masks_);
247 edge_bit_masks_ = vertex_bit_masks_;//init_bit_masks(edge_bit_masks_);
248 face_bit_masks_ = vertex_bit_masks_;//init_bit_masks(face_bit_masks_);
249 halfedge_bit_masks_= vertex_bit_masks_;//init_bit_masks(halfedge_bit_masks_);
250 }
251
252
253 };
254
OpenMesh::VertexHandle
Handle for a vertex entity.
Definition: Handles.hh:120
OpenMesh::ArrayKernel::garbage_collection
void garbage_collection(bool _v=true, bool _e=true, bool _f=true)
garbage collection
Definition: ArrayKernel.cc:166
OpenMesh::ArrayKernel::clear
void clear()
Does the same as clean() and in addition erases all properties.
Definition: ArrayKernel.cc:199
OpenMesh::HalfedgeHandle
Handle for a halfedge entity.
Definition: Handles.hh:127
OpenMesh::Attributes::UNUSED
Unused.
Definition: Status.hh:84
OpenMesh::ArrayKernel::clean
void clean()
Remove all vertices, edges and faces and deallocates their memory.
Definition: ArrayKernel.cc:184
OpenMesh::ArrayKernel::clean_keep_reservation
void clean_keep_reservation()
Remove all vertices, edges and faces but keep memory allocated.
Definition: ArrayKernel.cc:174
OpenMesh::FaceHandle
Handle for a face entity.
Definition: Handles.hh:141
OpenMesh::ArrayKernel::assign_connectivity
void assign_connectivity(const ArrayKernel &_other)
Definition: ArrayKernel.cc:71
OpenMesh::EdgeHandle
Handle for a edge entity.
Definition: Handles.hh:134
OpenMesh::BaseHandle::idx
int idx() const
Get the underlying index of this handle.
Definition: Handles.hh:69
OpenMesh::ArrayKernel::is_valid_handle
bool is_valid_handle(VertexHandle _vh) const
checks handle validity - useful for debugging
Definition: ArrayKernel.cc:129
OpenMesh
Definition: MeshItems.hh:59
OpenMesh::ArrayKernel
Definition: ArrayKernel.hh:87