Handles.hh 7.45 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41
42
43
44
45
46
47

/*===========================================================================*\
 *                                                                           *             
 *   $Revision$                                                         *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
48
49
50
51
52
53
54
55

#ifndef OPENMESH_HANDLES_HH
#define OPENMESH_HANDLES_HH


//== INCLUDES =================================================================

#include <OpenMesh/Core/System/config.h>
56
#include <ostream>
Jan Möbius's avatar
Jan Möbius committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


//== NAMESPACES ===============================================================

namespace OpenMesh {

//== CLASS DEFINITION =========================================================


/// Base class for all handle types
class BaseHandle
{ 
public:
  
  explicit BaseHandle(int _idx=-1) : idx_(_idx) {}

  /// Get the underlying index of this handle
  int idx() const { return idx_; }

76
77
  /// The handle is valid iff the index is not negative.
  bool is_valid() const { return idx_ >= 0; }
Jan Möbius's avatar
Jan Möbius committed
78
79
80
81
82
83
84

  /// reset handle to be invalid
  void reset() { idx_=-1; }
  /// reset handle to be invalid
  void invalidate() { idx_ = -1; }

  bool operator==(const BaseHandle& _rhs) const { 
Mike Kremer's avatar
Mike Kremer committed
85
    return (this->idx_ == _rhs.idx_); 
Jan Möbius's avatar
Jan Möbius committed
86
87
88
  }

  bool operator!=(const BaseHandle& _rhs) const { 
Mike Kremer's avatar
Mike Kremer committed
89
    return (this->idx_ != _rhs.idx_); 
Jan Möbius's avatar
Jan Möbius committed
90
91
92
  }

  bool operator<(const BaseHandle& _rhs) const { 
Mike Kremer's avatar
Mike Kremer committed
93
    return (this->idx_ < _rhs.idx_); 
Jan Möbius's avatar
Jan Möbius committed
94
95
96
97
98
99
100
  }


  // this is to be used only by the iterators
  void __increment() { ++idx_; }
  void __decrement() { --idx_; }

101
  void __increment(int amount) { idx_ += amount; }
102
  void __decrement(int amount) { idx_ -= amount; }
Jan Möbius's avatar
Jan Möbius committed
103
104
105
106
107
108

private:

  int idx_; 
};

109
110
// this is used by boost::unordered_set/map
inline size_t hash_value(const BaseHandle&  h)   { return h.idx(); }
Jan Möbius's avatar
Jan Möbius committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

//-----------------------------------------------------------------------------

/// Write handle \c _hnd to stream \c _os
inline std::ostream& operator<<(std::ostream& _os, const BaseHandle& _hnd) 
{
  return (_os << _hnd.idx());
}


//-----------------------------------------------------------------------------


/// Handle for a vertex entity
struct VertexHandle : public BaseHandle
{
  explicit VertexHandle(int _idx=-1) : BaseHandle(_idx) {}
};


/// Handle for a halfedge entity
struct HalfedgeHandle : public BaseHandle
{
  explicit HalfedgeHandle(int _idx=-1) : BaseHandle(_idx) {}
};


/// Handle for a edge entity
struct EdgeHandle : public BaseHandle
{
  explicit EdgeHandle(int _idx=-1) : BaseHandle(_idx) {}
};


/// Handle for a face entity
struct FaceHandle : public BaseHandle
{
  explicit FaceHandle(int _idx=-1) : BaseHandle(_idx) {}
};


//=============================================================================
} // namespace OpenMesh
//=============================================================================
155
156
157
158
159
160
161
162
163
164
165
166

#ifdef OM_HAS_HASH
#include <functional>
namespace std {

#if defined(_MSVC_VER)
#  pragma warning(push)
#  pragma warning(disable:4099) // For VC++ it is class hash
#endif


template <>
167
struct hash<OpenMesh::BaseHandle >
168
{
Jan Möbius's avatar
Jan Möbius committed
169
170
  typedef OpenMesh::BaseHandle argument_type;
  typedef std::size_t result_type;
171
172
173
174
175
176
177
178
179
180

  std::size_t operator()(const OpenMesh::BaseHandle& h) const
  {
    return h.idx();
  }
};

template <>
struct hash<OpenMesh::VertexHandle >
{
Jan Möbius's avatar
Jan Möbius committed
181
182
  typedef OpenMesh::VertexHandle argument_type;
  typedef std::size_t result_type;
183
184
185
186
187
188
189
190
191
192
193

  std::size_t operator()(const OpenMesh::VertexHandle& h) const
  {
    return h.idx();
  }
};

template <>
struct hash<OpenMesh::HalfedgeHandle >
{

Jan Möbius's avatar
Jan Möbius committed
194
195
196
  typedef OpenMesh::HalfedgeHandle argument_type;
  typedef std::size_t result_type;
  
197
198
199
200
201
202
203
204
205
206
  std::size_t operator()(const OpenMesh::HalfedgeHandle& h) const
  {
    return h.idx();
  }
};

template <>
struct hash<OpenMesh::EdgeHandle >
{

Jan Möbius's avatar
Jan Möbius committed
207
208
209
  typedef OpenMesh::EdgeHandle argument_type;
  typedef std::size_t result_type;
  
210
211
212
213
214
215
216
217
218
219
  std::size_t operator()(const OpenMesh::EdgeHandle& h) const
  {
    return h.idx();
  }
};

template <>
struct hash<OpenMesh::FaceHandle >
{

Jan Möbius's avatar
Jan Möbius committed
220
221
222
  typedef OpenMesh::FaceHandle argument_type;
  typedef std::size_t result_type;
  
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  std::size_t operator()(const OpenMesh::FaceHandle& h) const
  {
    return h.idx();
  }
};

#if defined(_MSVC_VER)
#  pragma warning(pop)
#endif

}
#endif  // OM_HAS_HASH


Jan Möbius's avatar
Jan Möbius committed
237
238
#endif // OPENMESH_HANDLES_HH
//=============================================================================