Point Cloud Library (PCL)  1.12.0-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& operator>><>(std::istream& is, MeshIndex<IndexTagT>& index);
164 };
165 
166 /** \brief ostream operator. */
167 template <class IndexTagT>
168 inline std::ostream&
169 operator<<(std::ostream& os, const MeshIndex<IndexTagT>& index)
170 {
171  return (os << index.get());
172 }
173 
174 /** \brief istream operator. */
175 template <class IndexTagT>
176 inline std::istream&
177 operator>>(std::istream& is, MeshIndex<IndexTagT>& index)
178 {
179  return (is >> index.index_);
180 }
181 
182 } // End namespace detail
183 } // End namespace pcl
184 
185 namespace pcl {
186 namespace geometry {
187 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
188  * wrapper around an integer with a few added methods.
189  * \author Martin Saelzle
190  * \ingroup geometry
191  */
193 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
194  * wrapper around an integer with a few added methods.
195  * \author Martin Saelzle
196  * \ingroup geometry
197  */
199 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
200  * wrapper around an integer with a few added methods.
201  * \author Martin Saelzle
202  * \ingroup geometry
203  */
205 /** \brief Index used to access elements in the half-edge mesh. It is basically just a
206  * wrapper around an integer with a few added methods.
207  * \author Martin Saelzle
208  * \ingroup geometry
209  */
211 
212 } // End namespace geometry
213 } // End namespace pcl
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 // Conversions
217 ////////////////////////////////////////////////////////////////////////////////
218 
219 namespace pcl {
220 namespace geometry {
221 /** \brief Convert the given half-edge index to an edge index. */
222 inline EdgeIndex
224 {
225  return (index.isValid() ? EdgeIndex(index.get() / 2) : EdgeIndex());
226 }
227 
228 /** \brief Convert the given edge index to a half-edge index.
229  * \param index
230  * \param[in] get_first The first half-edge of the edge is returned if this
231  * variable is true; elsewise the second.
232  */
233 inline HalfEdgeIndex
234 toHalfEdgeIndex(const EdgeIndex& index, const bool get_first = true)
235 {
236  return (index.isValid()
237  ? HalfEdgeIndex(index.get() * 2 + static_cast<int>(!get_first))
238  : HalfEdgeIndex());
239 }
240 } // End namespace geometry
241 } // End namespace pcl
pcl
Definition: convolution.h:46
pcl::detail::operator<<
std::ostream & operator<<(std::ostream &os, const MeshIndex< IndexTagT > &index)
ostream operator.
Definition: mesh_indices.h:169
pcl::detail::MeshIndex
Definition: mesh_indices.h:55
pcl::detail::MeshIndex::Base
boost::totally_ordered< MeshIndex< IndexTagT >, boost::unit_steppable< MeshIndex< IndexTagT >, boost::additive< MeshIndex< IndexTagT > >> > Base
Definition: mesh_indices.h:74
pcl::detail::MeshIndex::operator++
Self & operator++()
Increment operators (with boost::operators): ++ (pre and post)
Definition: mesh_indices.h:129
pcl::detail::MeshIndex::Self
MeshIndex< IndexTagT > Self
Definition: mesh_indices.h:75
pcl::detail::MeshIndex::operator+=
Self & operator+=(const Self &other)
Addition operators (with boost::operators): + +=.
Definition: mesh_indices.h:145
pcl::geometry::HalfEdgeIndex
pcl::detail::MeshIndex< struct HalfEdgeIndexTag > HalfEdgeIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:198
pcl::detail::MeshIndex::operator-=
Self & operator-=(const Self &other)
Subtraction operators (with boost::operators): - -=.
Definition: mesh_indices.h:153
pcl::detail::MeshIndex::isValid
bool isValid() const
Returns true if the index is valid.
Definition: mesh_indices.h:87
pcl::detail::MeshIndex::set
void set(const int index)
Set the index.
Definition: mesh_indices.h:108
pcl::geometry::EdgeIndex
pcl::detail::MeshIndex< struct EdgeIndexTag > EdgeIndex
Index used to access elements in the half-edge mesh.
Definition: mesh_indices.h:204
pcl::detail::MeshIndex::operator<
bool operator<(const Self &other) const
Comparison operators (with boost::operators): < > <= >=.
Definition: mesh_indices.h:115
pcl::detail::MeshIndex::operator--
Self & operator--()
Decrement operators (with boost::operators): -- (pre and post)
Definition: mesh_indices.h:137
pcl::detail::MeshIndex::operator==
bool operator==(const Self &other) const
Comparison operators (with boost::operators): == !=.
Definition: mesh_indices.h:122
pcl::detail::MeshIndex::MeshIndex
MeshIndex(const int index)
Constructor.
Definition: mesh_indices.h:83
pcl::detail::MeshIndex::invalidate
void invalidate()
Invalidate the index.
Definition: mesh_indices.h:94
pcl::detail::MeshIndex::MeshIndex
MeshIndex()
Constructor.
Definition: mesh_indices.h:78
pcl::detail::MeshIndex::get
int get() const
Get the index.
Definition: mesh_indices.h:101
pcl::geometry::toEdgeIndex
EdgeIndex toEdgeIndex(const HalfEdgeIndex &index)
Convert the given half-edge index to an edge index.
Definition: mesh_indices.h:223
pcl::detail::operator>>
std::istream & operator>>(std::istream &is, MeshIndex< IndexTagT > &)
istream operator.
Definition: mesh_indices.h:177
pcl::geometry::toHalfEdgeIndex
HalfEdgeIndex toHalfEdgeIndex(const EdgeIndex &index, const bool get_first=true)
Convert the given edge index to a half-edge index.
Definition: mesh_indices.h:234