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
41 #include "Entities.hh"
42 #include "../System/FunctionalInclude.hh"
43 #include "../System/Deprecation.hh"
44
45 namespace OpenVolumeMesh {
46
47 // Define handle types in order to distinguish different entities by their indices
48 class OpenVolumeMeshHandle {
49 public:
50 // Default constructor
51 explicit OpenVolumeMeshHandle(int _idx) : idx_(_idx) {};
52
53 OpenVolumeMeshHandle& operator=(int _idx) {
54 idx_ = _idx;
55 return *this;
56 }
57
58 OpenVolumeMeshHandle& operator=(const OpenVolumeMeshHandle& _idx) {
59 idx_ = _idx.idx_;
60 return *this;
61 }
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
79 void idx(const int& _idx) { idx_ = _idx; }
80
81 OVM_DEPRECATED("use explicit .idx() instead")
82 inline operator int() const { return idx_; }
83
84 void reset() { idx_ = -1; }
85
86 private:
87 int idx_;
88 };
89
90
91 template<typename EntityTag,
92 typename = typename std::enable_if<is_entity<EntityTag>::value>::type>
93 class HandleT : public OpenVolumeMeshHandle
94 {
95 public:
96 using Entity = EntityTag;
97 explicit HandleT(int _idx = -1) : OpenVolumeMeshHandle(_idx) {}
98 };
99
100 // Default entity handles
101
102 using VertexHandle = HandleT<Entity::Vertex>;
103 using HalfEdgeHandle = HandleT<Entity::HalfEdge>;
104 using EdgeHandle = HandleT<Entity::Edge>;
105 using HalfFaceHandle = HandleT<Entity::HalfFace>;
106 using FaceHandle = HandleT<Entity::Face>;
107 using CellHandle = HandleT<Entity::Cell>;
108 using MeshHandle = HandleT<Entity::Mesh>;
109
110 // Helper class that is used to decrease all handles
111 // exceeding a certain threshold
112
113 class VHandleCorrection {
114 public:
115 explicit VHandleCorrection(VertexHandle _thld) : thld_(_thld) {}
116 void correctValue(VertexHandle& _h) {
117 if(_h > thld_) _h.idx(_h.idx() - 1);
118 }
119 private:
120 VertexHandle thld_;
121 };
122 class HEHandleCorrection {
123 public:
124 explicit HEHandleCorrection(HalfEdgeHandle _thld) : thld_(_thld) {}
125 void correctVecValue(std::vector<HalfEdgeHandle>& _vec) {
126 #if defined(__clang_major__) && (__clang_major__ >= 5)
127 for(std::vector<HalfEdgeHandle>::iterator it = _vec.begin(), end = _vec.end(); it != end; ++it) {
128 correctValue(*it);
129 }
130 #else
131 std::for_each(_vec.begin(), _vec.end(), fun::bind(&HEHandleCorrection::correctValue, this, fun::placeholders::_1));
132 #endif
133 }
134 void correctValue(HalfEdgeHandle& _h) {
135 if(_h > thld_) _h.idx(_h.idx() - 2);
136 }
137 private:
138 HalfEdgeHandle thld_;
139 };
140 class HFHandleCorrection {
141 public:
142 explicit HFHandleCorrection(HalfFaceHandle _thld) : thld_(_thld) {}
143 void correctVecValue(std::vector<HalfFaceHandle>& _vec) {
144 #if defined(__clang_major__) && (__clang_major__ >= 5)
145 for(std::vector<HalfFaceHandle>::iterator it = _vec.begin(), end = _vec.end(); it != end; ++it) {
146 correctValue(*it);
147 }
148 #else
149 std::for_each(_vec.begin(), _vec.end(), fun::bind(&HFHandleCorrection::correctValue, this, fun::placeholders::_1));
150 #endif
151 }
152 void correctValue(HalfFaceHandle& _h) {
153 if(_h > thld_) _h.idx(_h.idx() - 2);
154 }
155 private:
156 HalfFaceHandle thld_;
157 };
158 class CHandleCorrection {
159 public:
160 explicit CHandleCorrection(CellHandle _thld) : thld_(_thld) {}
161 void correctValue(CellHandle& _h) {
162 if(_h > thld_) _h.idx(_h.idx() - 1);
163 }
164 private:
165 CellHandle thld_;
166 };
167
168 bool operator==(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
169
170 bool operator==(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
171
172 bool operator!=(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
173
174 bool operator!=(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
175
176 std::ostream& operator<<(std::ostream& _ostr, const OpenVolumeMeshHandle& _handle);
177
178 std::istream& operator>>(std::istream& _istr, OpenVolumeMeshHandle& _handle);
179
180 } // Namespace OpenVolumeMesh
181
OpenMesh::Kernel_OSG::bind
bool bind(osg::GeometryPtr &_geo, Mesh &_mesh)
Definition: bindT.hh:101
OpenVolumeMesh::VHandleCorrection
Definition: OpenVolumeMeshHandle.hh:113
OpenVolumeMesh::HFHandleCorrection
Definition: OpenVolumeMeshHandle.hh:140
OpenVolumeMesh::HandleT
Definition: OpenVolumeMeshHandle.hh:93
OpenVolumeMesh
Definition: ColorAttrib.hh:53
OpenVolumeMesh::HEHandleCorrection
Definition: OpenVolumeMeshHandle.hh:122
OpenVolumeMesh::OpenVolumeMeshHandle
Definition: OpenVolumeMeshHandle.hh:48
OpenVolumeMesh::CHandleCorrection
Definition: OpenVolumeMeshHandle.hh:158