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