Point Cloud Library (PCL)  1.14.1-dev
mesh_indices.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #pragma once
42 
43 #include <boost/operators.hpp>
44 
45 #include <iostream>
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 // MeshIndex
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 namespace pcl {
52 namespace detail {
53 
54 template <class IndexTagT>
55 class MeshIndex;
56 
57 template <class IndexTagT>
58 std::istream&
59 operator>>(std::istream& is, MeshIndex<IndexTagT>&);
60 
61 template <class IndexTagT>
62 class MeshIndex
63 : boost::totally_ordered<
64  MeshIndex<IndexTagT>, // < > <= >= == !=
65  boost::unit_steppable<MeshIndex<IndexTagT>, // ++ -- (pre and post)
66  boost::additive<MeshIndex<IndexTagT> // += +
67  // -= -
68  >>> {
69 
70 public:
71  using Base = boost::totally_ordered<
73  boost::unit_steppable<MeshIndex<IndexTagT>,
74  boost::additive<MeshIndex<IndexTagT>>>>;
76 
77  /** \brief Constructor. Initializes with an invalid index. */
78  MeshIndex() : index_(-1) {}
79 
80  /** \brief Constructor.
81  * \param[in] index The integer index.
82  */
83  explicit MeshIndex(const int index) : index_(index) {}
84 
85  /** \brief Returns true if the index is valid. */
86  inline bool
87  isValid() const
88  {
89  return (index_ >= 0);
90  }
91 
92  /** \brief Invalidate the index. */
93  inline void
95  {
96  index_ = -1;
97  }
98 
99  /** \brief Get the index. */
100  inline int
101  get() const
102  {
103  return (index_);
104  }
105 
106  /** \brief Set the index. */
107  inline void
108  set(const int index)
109  {
110  index_ = index;
111  }
112 
113  /** \brief Comparison operators (with boost::operators): < > <= >= */
114  inline bool
115  operator<(const Self& other) const
116  {
117  return (this->get() < other.get());
118  }
119 
120  /** \brief Comparison operators (with boost::operators): == != */
121  inline bool
122  operator==(const Self& other) const
123  {
124  return (this->get() == other.get());
125  }
126 
127  /** \brief Increment operators (with boost::operators): ++ (pre and post) */
128  inline Self&
130  {
131  ++index_;
132  return (*this);
133  }
134 
135  /** \brief Decrement operators (with boost::operators): \-\- (pre and post) */
136  inline Self&
138  {
139  --index_;
140  return (*this);
141  }
142 
143  /** \brief Addition operators (with boost::operators): + += */
144  inline Self&
145  operator+=(const Self& other)
146  {
147  index_ += other.get();
148  return (*this);
149  }
150 
151  /** \brief Subtraction operators (with boost::operators): - -= */
152  inline Self&
153  operator-=(const Self& other)
154  {
155  index_ -= other.get();
156  return (*this);
157  }
158 
159 private:
160  /** \brief Stored index. */
161  int index_;
162 
163  friend std::istream&
164  operator>><>(std::istream& is, MeshIndex<IndexTagT>& index);
165 };
166 
167 /** \brief ostream operator. */
168 template <class IndexTagT>
169 inline std::ostream&
170 operator<<(std::ostream& os, const MeshIndex<IndexTagT>& index)
171 {
172  return (os << index.get());
173 }
174 
175 /** \brief istream operator. */
176 template <class IndexTagT>
177 inline std::istream&
178 operator>>(std::istream& is, MeshIndex<IndexTagT>& index)
179 {
180  return (is >> index.index_);
181 }
182 
183 } // End namespace detail
184 } // End namespace pcl
185 
186 namespace pcl {
187 namespace geometry {
188 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
189  * wrapper around an integer with a few added methods.
190  * \author Martin Saelzle
191  * \ingroup geometry
192  */
194 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
195  * wrapper around an integer with a few added methods.
196  * \author Martin Saelzle
197  * \ingroup geometry
198  */
200 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
201  * wrapper around an integer with a few added methods.
202  * \author Martin Saelzle
203  * \ingroup geometry
204  */
206 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
207  * wrapper around an integer with a few added methods.
208  * \author Martin Saelzle
209  * \ingroup geometry
210  */
212 
213 } // End namespace geometry
214 } // End namespace pcl
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 // Conversions
218 ////////////////////////////////////////////////////////////////////////////////
219 
220 namespace pcl {
221 namespace geometry {
222 /** \brief Convert the given half-edge index to an edge index. */
223 inline EdgeIndex
225 {
226  return (index.isValid() ? EdgeIndex(index.get() / 2) : EdgeIndex());
227 }
228 
229 /** \brief Convert the given edge index to a half-edge index.
230  * \param index
231  * \param[in] get_first The first half-edge of the edge is returned if this
232  * variable is true; elsewise the second.
233  */
234 inline HalfEdgeIndex
235 toHalfEdgeIndex(const EdgeIndex& index, const bool get_first = true)
236 {
237  return (index.isValid()
238  ? HalfEdgeIndex(index.get() * 2 + static_cast<int>(!get_first))
239  : HalfEdgeIndex());
240 }
241 } // End namespace geometry
242 } // End namespace pcl
void invalidate()
Invalidate the index.
Definition: mesh_indices.h:94
boost::totally_ordered< MeshIndex< IndexTagT >, boost::unit_steppable< MeshIndex< IndexTagT >, boost::additive< MeshIndex< IndexTagT > >> > Base
Definition: mesh_indices.h:74
Self & operator--()
Decrement operators (with boost::operators): -- (pre and post)
Definition: mesh_indices.h:137
MeshIndex< IndexTagT > Self
Definition: mesh_indices.h:75
bool isValid() const
Returns true if the index is valid.
Definition: mesh_indices.h:87
void set(const int index)
Set the index.
Definition: mesh_indices.h:108
int get() const
Get the index.
Definition: mesh_indices.h:101
bool operator==(const Self &other) const
Comparison operators (with boost::operators): == !=.
Definition: mesh_indices.h:122
Self & operator+=(const Self &other)
Addition operators (with boost::operators): + +=.
Definition: mesh_indices.h:145
Self & operator++()
Increment operators (with boost::operators): ++ (pre and post)
Definition: mesh_indices.h:129
bool operator<(const Self &other) const
Comparison operators (with boost::operators): < > <= >=.
Definition: mesh_indices.h:115
Self & operator-=(const Self &other)
Subtraction operators (with boost::operators): - -=.
Definition: mesh_indices.h:153
MeshIndex(const int index)
Constructor.
Definition: mesh_indices.h:83
MeshIndex()
Constructor.
Definition: mesh_indices.h:78
pcl::detail::MeshIndex< struct EdgeIndexTag > EdgeIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:205
pcl::detail::MeshIndex< struct HalfEdgeIndexTag > HalfEdgeIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:199
std::istream & operator>>(std::istream &is, MeshIndex< IndexTagT > &)
istream operator.
Definition: mesh_indices.h:178
std::ostream & operator<<(std::ostream &os, const MeshIndex< IndexTagT > &index)
ostream operator.
Definition: mesh_indices.h:170
EdgeIndex toEdgeIndex(const HalfEdgeIndex &index)
Convert the given half-edge index to an edge index.
Definition: mesh_indices.h:224
HalfEdgeIndex toHalfEdgeIndex(const EdgeIndex &index, const bool get_first=true)
Convert the given edge index to a half-edge index.
Definition: mesh_indices.h:235