Developer Documentation
OpenVolumeMeshHandle.hh
1 /*===========================================================================*\
2  * *
3  * OpenVolumeMesh *
4  * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openvolumemesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenVolumeMesh. *
9  * *
10  * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenVolumeMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenVolumeMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 #pragma once
36 
37 #include <algorithm>
38 #include <iosfwd>
39 #include <vector>
40 #include <cassert>
41 #include <limits>
42 
43 #include "Entities.hh"
44 #include "../System/FunctionalInclude.hh"
45 #include "../System/Deprecation.hh"
46 
47 namespace OpenVolumeMesh {
48 
49 // Define handle types in order to distinguish different entities by their indices
51 public:
52  // Default constructor
53  explicit constexpr OpenVolumeMeshHandle(int _idx) : idx_(_idx) {}
54 
55  OpenVolumeMeshHandle& operator=(int _idx) {
56  idx_ = _idx;
57  return *this;
58  }
59 
60  OpenVolumeMeshHandle(const OpenVolumeMeshHandle& _idx) = default;
61  OpenVolumeMeshHandle& operator=(const OpenVolumeMeshHandle& _idx) = default;
62 
63  inline bool is_valid() const { return idx_ != -1; }
64 
65  inline bool operator<(const OpenVolumeMeshHandle& _idx) const { return (this->idx_ < _idx.idx_); }
66 
67  inline bool operator<(int _idx) const { return idx_ < _idx; }
68 
69  inline bool operator>(const OpenVolumeMeshHandle& _idx) const { return (this->idx_ > _idx.idx_); }
70 
71  inline bool operator>(int _idx) const { return idx_ > _idx; }
72 
73  inline bool operator==(const OpenVolumeMeshHandle& _h) const { return _h.idx_ == this->idx_; }
74 
75  inline bool operator!=(const OpenVolumeMeshHandle& _h) const { return _h.idx_ != this->idx_; }
76 
77  inline const int& idx() const { return idx_; }
78 
80  inline size_t uidx() const { assert(is_valid()); return static_cast<size_t>(idx_); }
81 
82  void idx(const int& _idx) { idx_ = _idx; }
83 
84 #if OVM_ENABLE_DEPRECATED_APIS
85  OVM_DEPRECATED("use explicit .idx() instead")
86  inline operator int() const { return idx_; }
87 #endif
88 
89  void reset() { idx_ = -1; }
90 
91 private:
92  int idx_;
93 };
94 
95 template<typename EntityTag,
96  typename = typename std::enable_if<is_entity<EntityTag>::value>::type>
97 class PropHandleTag {};
98 
99 template <typename T> struct is_prop_handle_tag : std::false_type {};
100 template<typename T>
101 struct is_prop_handle_tag<PropHandleTag<T>> : std::true_type {};
102 
103 template<typename T>
104 using is_handle_tag = std::enable_if<is_entity<T>::value || is_prop_handle_tag<T>::value>;
105 
106 
107 template<typename EntityTag, typename = typename is_handle_tag<EntityTag>::type>
109 {
110 public:
111  using Entity = EntityTag;
112  explicit constexpr HandleT(int _idx = -1) : OpenVolumeMeshHandle(_idx) {}
113 
114  static HandleT<EntityTag>
115  from_unsigned(size_t _idx)
116  {
117  if (_idx <= static_cast<size_t>(std::numeric_limits<int>::max())) {
118  return HandleT<EntityTag>(static_cast<int>(_idx));
119  } else {
120  assert(false);
121  return HandleT<EntityTag>(-1);
122  }
123  }
124 };
125 
126 // Default entity handles
127 
135 
136 // Helper class that is used to decrease all handles
137 // exceeding a certain threshold
138 
140 public:
141  explicit VHandleCorrection(VertexHandle _thld) : thld_(_thld) {}
142  void correctValue(VertexHandle& _h) {
143  if(_h > thld_) _h.idx(_h.idx() - 1);
144  }
145 private:
146  VertexHandle thld_;
147 };
149 public:
150  explicit HEHandleCorrection(HalfEdgeHandle _thld) : thld_(_thld) {}
151  void correctVecValue(std::vector<HalfEdgeHandle>& _vec) {
152 #if defined(__clang_major__) && (__clang_major__ >= 5)
153  for(std::vector<HalfEdgeHandle>::iterator it = _vec.begin(), end = _vec.end(); it != end; ++it) {
154  correctValue(*it);
155  }
156 #else
157  std::for_each(_vec.begin(), _vec.end(), fun::bind(&HEHandleCorrection::correctValue, this, fun::placeholders::_1));
158 #endif
159  }
160  void correctValue(HalfEdgeHandle& _h) {
161  if(_h > thld_) _h.idx(_h.idx() - 2);
162  }
163 private:
164  HalfEdgeHandle thld_;
165 };
167 public:
168  explicit HFHandleCorrection(HalfFaceHandle _thld) : thld_(_thld) {}
169  void correctVecValue(std::vector<HalfFaceHandle>& _vec) {
170 #if defined(__clang_major__) && (__clang_major__ >= 5)
171  for(std::vector<HalfFaceHandle>::iterator it = _vec.begin(), end = _vec.end(); it != end; ++it) {
172  correctValue(*it);
173  }
174 #else
175  std::for_each(_vec.begin(), _vec.end(), fun::bind(&HFHandleCorrection::correctValue, this, fun::placeholders::_1));
176 #endif
177  }
178  void correctValue(HalfFaceHandle& _h) {
179  if(_h > thld_) _h.idx(_h.idx() - 2);
180  }
181 private:
182  HalfFaceHandle thld_;
183 };
185 public:
186  explicit CHandleCorrection(CellHandle _thld) : thld_(_thld) {}
187  void correctValue(CellHandle& _h) {
188  if(_h > thld_) _h.idx(_h.idx() - 1);
189  }
190 private:
191  CellHandle thld_;
192 };
193 
194 bool operator==(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
195 
196 bool operator==(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
197 
198 bool operator!=(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
199 
200 bool operator!=(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
201 
202 std::ostream& operator<<(std::ostream& _ostr, const OpenVolumeMeshHandle& _handle);
203 
204 std::istream& operator>>(std::istream& _istr, OpenVolumeMeshHandle& _handle);
205 
206 } // Namespace OpenVolumeMesh
207 
size_t uidx() const
return unsigned idx - handle must be valid
bool bind(osg::GeometryPtr &_geo, Mesh &_mesh)
Definition: bindT.hh:101