Developer Documentation
Handles.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 #ifndef OPENMESH_HANDLES_HH
45 #define OPENMESH_HANDLES_HH
46 
47 
48 //== INCLUDES =================================================================
49 
51 #include <ostream>
52 
53 
54 //== NAMESPACES ===============================================================
55 
56 namespace OpenMesh {
57 
58 //== CLASS DEFINITION =========================================================
59 
60 
62 class OPENMESHDLLEXPORT BaseHandle
63 {
64 public:
65 
66  explicit BaseHandle(int _idx=-1) : idx_(_idx) {}
67 
69  int idx() const { return idx_; }
70 
72  bool is_valid() const { return idx_ >= 0; }
73 
75  void reset() { idx_=-1; }
77  void invalidate() { idx_ = -1; }
78 
79  bool operator==(const BaseHandle& _rhs) const {
80  return (this->idx_ == _rhs.idx_);
81  }
82 
83  bool operator!=(const BaseHandle& _rhs) const {
84  return (this->idx_ != _rhs.idx_);
85  }
86 
87  bool operator<(const BaseHandle& _rhs) const {
88  return (this->idx_ < _rhs.idx_);
89  }
90 
91 
92  // this is to be used only by the iterators
93  void __increment() { ++idx_; }
94  void __decrement() { --idx_; }
95 
96  void __increment(int amount) { idx_ += amount; }
97  void __decrement(int amount) { idx_ -= amount; }
98 
99 private:
100 
101  int idx_;
102 };
103 
104 // this is used by boost::unordered_set/map
105 inline size_t hash_value(const BaseHandle& h) { return h.idx(); }
106 
107 //-----------------------------------------------------------------------------
108 
110 inline std::ostream& operator<<(std::ostream& _os, const BaseHandle& _hnd)
111 {
112  return (_os << _hnd.idx());
113 }
114 
115 
116 //-----------------------------------------------------------------------------
117 
118 
120 struct OPENMESHDLLEXPORT VertexHandle : public BaseHandle
121 {
122  explicit VertexHandle(int _idx=-1) : BaseHandle(_idx) {}
123 };
124 
125 
127 struct OPENMESHDLLEXPORT HalfedgeHandle : public BaseHandle
128 {
129  explicit HalfedgeHandle(int _idx=-1) : BaseHandle(_idx) {}
130 };
131 
132 
134 struct OPENMESHDLLEXPORT EdgeHandle : public BaseHandle
135 {
136  explicit EdgeHandle(int _idx=-1) : BaseHandle(_idx) {}
137 };
138 
139 
141 struct OPENMESHDLLEXPORT FaceHandle : public BaseHandle
142 {
143  explicit FaceHandle(int _idx=-1) : BaseHandle(_idx) {}
144 };
145 
146 
148 struct OPENMESHDLLEXPORT MeshHandle : public BaseHandle
149 {
150  explicit MeshHandle(int _idx=-1) : BaseHandle(_idx) {}
151 };
152 
153 
154 
155 
156 //=============================================================================
157 } // namespace OpenMesh
158 //=============================================================================
159 
160 #ifdef OM_HAS_HASH
161 #include <functional>
162 namespace std {
163 
164 #if defined(_MSVC_VER)
165 # pragma warning(push)
166 # pragma warning(disable:4099) // For VC++ it is class hash
167 #endif
168 
169 
170 template <>
171 struct hash<OpenMesh::BaseHandle >
172 {
173  typedef OpenMesh::BaseHandle argument_type;
174  typedef std::size_t result_type;
175 
176  std::size_t operator()(const OpenMesh::BaseHandle& h) const
177  {
178  return h.idx();
179  }
180 };
181 
182 template <>
183 struct hash<OpenMesh::VertexHandle >
184 {
185  typedef OpenMesh::VertexHandle argument_type;
186  typedef std::size_t result_type;
187 
188  std::size_t operator()(const OpenMesh::VertexHandle& h) const
189  {
190  return h.idx();
191  }
192 };
193 
194 template <>
195 struct hash<OpenMesh::HalfedgeHandle >
196 {
197 
198  typedef OpenMesh::HalfedgeHandle argument_type;
199  typedef std::size_t result_type;
200 
201  std::size_t operator()(const OpenMesh::HalfedgeHandle& h) const
202  {
203  return h.idx();
204  }
205 };
206 
207 template <>
208 struct hash<OpenMesh::EdgeHandle >
209 {
210 
211  typedef OpenMesh::EdgeHandle argument_type;
212  typedef std::size_t result_type;
213 
214  std::size_t operator()(const OpenMesh::EdgeHandle& h) const
215  {
216  return h.idx();
217  }
218 };
219 
220 template <>
221 struct hash<OpenMesh::FaceHandle >
222 {
223 
224  typedef OpenMesh::FaceHandle argument_type;
225  typedef std::size_t result_type;
226 
227  std::size_t operator()(const OpenMesh::FaceHandle& h) const
228  {
229  return h.idx();
230  }
231 };
232 
233 #if defined(_MSVC_VER)
234 # pragma warning(pop)
235 #endif
236 
237 }
238 #endif // OM_HAS_HASH
239 
240 
241 #endif // OPENMESH_HANDLES_HH
242 //=============================================================================
Handle for a edge entity.
Definition: Handles.hh:134
Handle for a face entity.
Definition: Handles.hh:141
auto operator<<(std::ostream &os, const VectorT< Scalar, DIM > &_vec) -> typename std::enable_if< sizeof(decltype(os<< _vec[0])) >=0
output a vector by printing its space-separated compontens
Definition: VectorT.hh:647
Handle type for meshes to simplify some template programming.
Definition: Handles.hh:148
Handle for a halfedge entity.
Definition: Handles.hh:127
int idx() const
Get the underlying index of this handle.
Definition: Handles.hh:69
STL namespace.
Base class for all handle types.
Definition: Handles.hh:62
Handle for a vertex entity.
Definition: Handles.hh:120
bool is_valid() const
The handle is valid iff the index is not negative.
Definition: Handles.hh:72
void invalidate()
reset handle to be invalid
Definition: Handles.hh:77
void reset()
reset handle to be invalid
Definition: Handles.hh:75