OpenVolumeMesh
 All Classes Functions Variables Typedefs Pages
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 /*===========================================================================*\
36  * *
37  * $Revision: 216 $ *
38  * $Date: 2012-07-18 10:27:26 +0200 (Wed, 18 Jul 2012) $ *
39  * $LastChangedBy: kremer $ *
40  * *
41 \*===========================================================================*/
42 
43 #ifndef OPENVOLUMEMESHHANDLE_HH_
44 #define OPENVOLUMEMESHHANDLE_HH_
45 
46 #include <iostream>
47 #include <vector>
48 #include <OpenVolumeMesh/System/FunctionalInclude.hh>
49 #include <algorithm>
50 
51 namespace OpenVolumeMesh {
52 
53 // Define handle types in order to distinguish different entities by their indices
55 public:
56  // Default constructor
57  explicit OpenVolumeMeshHandle(int _idx) : idx_(_idx) {};
58 
59  OpenVolumeMeshHandle& operator=(int _idx) {
60  idx_ = _idx;
61  return *this;
62  }
63 
64  OpenVolumeMeshHandle& operator=(const OpenVolumeMeshHandle& _idx) {
65  idx_ = _idx.idx_;
66  return *this;
67  }
68 
69  inline bool is_valid() const { return idx_ != -1; }
70 
71  inline bool operator<(const OpenVolumeMeshHandle& _idx) const { return (this->idx_ < _idx.idx_); }
72 
73  inline bool operator<(int _idx) const { return idx_ < _idx; }
74 
75  inline bool operator>(const OpenVolumeMeshHandle& _idx) const { return (this->idx_ > _idx.idx_); }
76 
77  inline bool operator>(int _idx) const { return idx_ > _idx; }
78 
79  inline bool operator==(const OpenVolumeMeshHandle& _h) const { return _h.idx_ == this->idx_; }
80 
81  inline bool operator!=(const OpenVolumeMeshHandle& _h) const { return _h.idx_ != this->idx_; }
82 
83  inline const int& idx() const { return idx_; }
84 
85  void idx(const int& _idx) { idx_ = _idx; }
86 
87  inline operator int() const { return idx_; }
88 
89  void reset() { idx_ = -1; }
90 
91 private:
92  int idx_;
93 };
94 
95 // Default entity handles
96 
97 class VertexHandle : public OpenVolumeMeshHandle { public: VertexHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
98 class EdgeHandle : public OpenVolumeMeshHandle { public: EdgeHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
99 class FaceHandle : public OpenVolumeMeshHandle { public: FaceHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
100 class CellHandle : public OpenVolumeMeshHandle { public: CellHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
101 class HalfEdgeHandle : public OpenVolumeMeshHandle { public: HalfEdgeHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
102 class HalfFaceHandle : public OpenVolumeMeshHandle { public: HalfFaceHandle(int _idx = -1) : OpenVolumeMeshHandle(_idx) {} };
103 
104 // Helper class that is used to decrease all handles
105 // exceeding a certain threshold
106 
108 public:
109  VHandleCorrection(VertexHandle _thld) : thld_(_thld) {}
110  void correctValue(VertexHandle& _h) {
111  if(_h > thld_) _h.idx(_h.idx() - 1);
112  }
113 private:
114  VertexHandle thld_;
115 };
117 public:
118  HEHandleCorrection(HalfEdgeHandle _thld) : thld_(_thld) {}
119  void correctVecValue(std::vector<HalfEdgeHandle>& _vec) {
120  std::for_each(_vec.begin(), _vec.end(), fun::bind(&HEHandleCorrection::correctValue, this, fun::placeholders::_1));
121  }
122  void correctValue(HalfEdgeHandle& _h) {
123  if(_h > thld_) _h.idx(_h.idx() - 2);
124  }
125 private:
126  HalfEdgeHandle thld_;
127 };
129 public:
130  HFHandleCorrection(HalfFaceHandle _thld) : thld_(_thld) {}
131  void correctVecValue(std::vector<HalfFaceHandle>& _vec) {
132  std::for_each(_vec.begin(), _vec.end(), fun::bind(&HFHandleCorrection::correctValue, this, fun::placeholders::_1));
133  }
134  void correctValue(HalfFaceHandle& _h) {
135  if(_h > thld_) _h.idx(_h.idx() - 2);
136  }
137 private:
138  HalfFaceHandle thld_;
139 };
141 public:
142  CHandleCorrection(CellHandle _thld) : thld_(_thld) {}
143  void correctValue(CellHandle& _h) {
144  if(_h > thld_) _h.idx(_h.idx() - 1);
145  }
146 private:
147  CellHandle thld_;
148 };
149 
150 bool operator==(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
151 
152 bool operator==(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
153 
154 bool operator!=(const int& _lhs, const OpenVolumeMeshHandle& _rhs);
155 
156 bool operator!=(const unsigned int& _lhs, const OpenVolumeMeshHandle& _rhs);
157 
158 std::ostream& operator<<(std::ostream& _ostr, const OpenVolumeMeshHandle& _handle);
159 
160 std::istream& operator>>(std::istream& _istr, OpenVolumeMeshHandle& _handle);
161 
162 } // Namespace OpenVolumeMesh
163 
164 #endif /* OPENVOLUMEMESHHANDLE_HH_ */