Ignition Math

API Reference

6.4.0
Plane.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_PLANE_HH_
18 #define IGNITION_MATH_PLANE_HH_
19 
21 #include <ignition/math/Vector2.hh>
22 #include <ignition/math/Vector3.hh>
23 #include <ignition/math/config.hh>
24 
25 namespace ignition
26 {
27  namespace math
28  {
29  // Inline bracket to help doxygen filtering.
30  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
31  //
34  template<typename T>
35  class Plane
36  {
40  public: enum PlaneSide
41  {
44  NEGATIVE_SIDE = 0,
45 
48  POSITIVE_SIDE = 1,
49 
51  NO_SIDE = 2,
52 
54  BOTH_SIDE = 3
55  };
56 
58  public: Plane()
59  : d(0.0)
60  {
61  }
62 
66  public: Plane(const Vector3<T> &_normal, T _offset = 0.0)
67  : normal(_normal), d(_offset)
68  {
69  }
70 
75  public: Plane(const Vector3<T> &_normal, const Vector2<T> &_size,
76  T _offset)
77  {
78  this->Set(_normal, _size, _offset);
79  }
80 
83  public: Plane(const Plane &_plane)
84  : normal(_plane.normal), size(_plane.size), d(_plane.d)
85  {}
86 
88  public: virtual ~Plane() {}
89 
93  public: void Set(const Vector3<T> &_normal, T _offset)
94  {
95  this->normal = _normal;
96  this->d = _offset;
97  }
98 
103  public: void Set(const Vector3<T> &_normal, const Vector2<T> &_size,
104  T _offset)
105  {
106  this->normal = _normal;
107  this->size = _size;
108  this->d = _offset;
109  }
110 
117  public: T Distance(const Vector3<T> &_point) const
118  {
119  return this->normal.Dot(_point) - this->d;
120  }
121 
128  public: PlaneSide Side(const Vector3<T> &_point) const
129  {
130  T dist = this->Distance(_point);
131 
132  if (dist < 0.0)
133  return NEGATIVE_SIDE;
134 
135  if (dist > 0.0)
136  return POSITIVE_SIDE;
137 
138  return NO_SIDE;
139  }
140 
147  public: PlaneSide Side(const math::AxisAlignedBox &_box) const
148  {
149  double dist = this->Distance(_box.Center());
150  double maxAbsDist = this->normal.AbsDot(_box.Size()/2.0);
151 
152  if (dist < -maxAbsDist)
153  return NEGATIVE_SIDE;
154 
155  if (dist > maxAbsDist)
156  return POSITIVE_SIDE;
157 
158  return BOTH_SIDE;
159  }
160 
165  public: T Distance(const Vector3<T> &_origin,
166  const Vector3<T> &_dir) const
167  {
168  T denom = this->normal.Dot(_dir);
169 
170  if (std::abs(denom) < 1e-3)
171  {
172  // parallel
173  return 0;
174  }
175  else
176  {
177  T nom = _origin.Dot(this->normal) - this->d;
178  T t = -(nom/denom);
179  return t;
180  }
181  }
182 
184  public: inline const Vector2<T> &Size() const
185  {
186  return this->size;
187  }
188 
190  public: inline Vector2<T> &Size()
191  {
192  return this->size;
193  }
194 
196  public: inline const Vector3<T> &Normal() const
197  {
198  return this->normal;
199  }
200 
202  public: inline Vector3<T> &Normal()
203  {
204  return this->normal;
205  }
206 
208  public: inline T Offset() const
209  {
210  return this->d;
211  }
212 
216  public: Plane<T> &operator=(const Plane<T> &_p)
217  {
218  this->normal = _p.normal;
219  this->size = _p.size;
220  this->d = _p.d;
221 
222  return *this;
223  }
224 
226  private: Vector3<T> normal;
227 
229  private: Vector2<T> size;
230 
232  private: T d;
233  };
234 
238  }
239  }
240 }
241 
242 #endif
const Vector3< T > & Normal() const
Get the plane offset.
Definition: Plane.hh:196
virtual ~Plane()
Destructor.
Definition: Plane.hh:88
T Distance(const Vector3< T > &_origin, const Vector3< T > &_dir) const
Get distance to the plane give an origin and direction.
Definition: Plane.hh:165
const Vector2< T > & Size() const
Get the plane size.
Definition: Plane.hh:184
Plane< double > Planed
Definition: Plane.hh:236
Vector2< T > & Size()
Get the plane size.
Definition: Plane.hh:190
Vector3< T > & Normal()
Get the plane offset.
Definition: Plane.hh:202
Two dimensional (x, y) vector.
Definition: Vector2.hh:35
A plane and related functions.
Definition: Plane.hh:35
Plane(const Plane &_plane)
Copy constructor.
Definition: Plane.hh:83
PlaneSide Side(const math::AxisAlignedBox &_box) const
The side of the plane a box is on.
Definition: Plane.hh:147
Plane< T > & operator=(const Plane< T > &_p)
Equal operator.
Definition: Plane.hh:216
void Set(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Set the plane.
Definition: Plane.hh:103
Plane()
Constructor.
Definition: Plane.hh:58
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:199
Plane(const Vector3< T > &_normal, T _offset=0.0)
Constructor from a normal and a distance.
Definition: Plane.hh:66
math::Vector3d Center() const
Get the box center.
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
PlaneSide
Enum used to indicate a side of the plane, no side, or both sides for entities on the plane...
Definition: Plane.hh:40
T Offset() const
Get the plane offset.
Definition: Plane.hh:208
Plane(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Constructor.
Definition: Plane.hh:75
Definition: Angle.hh:42
T Distance(const Vector3< T > &_point) const
The distance to the plane from the given point. The distance can be negative, which indicates the poi...
Definition: Plane.hh:117
Plane< int > Planei
Definition: Plane.hh:235
math::Vector3d Size() const
Get the size of the box.
void Set(const Vector3< T > &_normal, T _offset)
Set the plane.
Definition: Plane.hh:93
Mathematical representation of a box that is aligned along an X,Y,Z axis.
Definition: AxisAlignedBox.hh:42
PlaneSide Side(const Vector3< T > &_point) const
The side of the plane a point is on.
Definition: Plane.hh:128
Plane< float > Planef
Definition: Plane.hh:237