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 
20 #include <ignition/math/Box.hh>
21 #include <ignition/math/Vector3.hh>
22 #include <ignition/math/Vector2.hh>
23 
24 namespace ignition
25 {
26  namespace math
27  {
30  template<typename T>
31  class Plane
32  {
36  public: enum PlaneSide
37  {
41 
45 
47  NO_SIDE = 2,
48 
51  };
52 
54  public: Plane()
55  : d(0.0)
56  {
57  }
58 
62  public: Plane(const Vector3<T> &_normal, T _offset = 0.0)
63  : normal(_normal), d(_offset)
64  {
65  }
66 
71  public: Plane(const Vector3<T> &_normal, const Vector2<T> &_size,
72  T _offset)
73  {
74  this->Set(_normal, _size, _offset);
75  }
76 
78  public: virtual ~Plane() {}
79 
83  public: void Set(const Vector3<T> &_normal, T _offset)
84  {
85  this->normal = _normal;
86  this->d = _offset;
87  }
88 
93  public: void Set(const Vector3<T> &_normal, const Vector2<T> &_size,
94  T _offset)
95  {
96  this->normal = _normal;
97  this->size = _size;
98  this->d = _offset;
99  }
100 
107  public: T Distance(const Vector3<T> &_point) const
108  {
109  return this->normal.Dot(_point) - this->d;
110  }
111 
118  public: PlaneSide Side(const Vector3<T> &_point) const
119  {
120  T dist = this->Distance(_point);
121 
122  if (dist < 0.0)
123  return NEGATIVE_SIDE;
124 
125  if (dist > 0.0)
126  return POSITIVE_SIDE;
127 
128  return NO_SIDE;
129  }
130 
137  public: PlaneSide Side(const math::Box &_box) const
138  {
139  double dist = this->Distance(_box.Center());
140  double maxAbsDist = this->normal.AbsDot(_box.Size()/2.0);
141 
142  if (dist < -maxAbsDist)
143  return NEGATIVE_SIDE;
144 
145  if (dist > maxAbsDist)
146  return POSITIVE_SIDE;
147 
148  return BOTH_SIDE;
149  }
150 
155  public: T Distance(const Vector3<T> &_origin,
156  const Vector3<T> &_dir) const
157  {
158  T denom = this->normal.Dot(_dir);
159 
160  if (std::abs(denom) < 1e-3)
161  {
162  // parallel
163  return 0;
164  }
165  else
166  {
167  T nom = _origin.Dot(this->normal) - this->d;
168  T t = -(nom/denom);
169  return t;
170  }
171  }
172 
174  public: inline const Vector2<T> &Size() const
175  {
176  return this->size;
177  }
178 
180  public: inline Vector2<T> &Size()
181  {
182  return this->size;
183  }
184 
186  public: inline const Vector3<T> &Normal() const
187  {
188  return this->normal;
189  }
190 
192  public: inline Vector3<T> &Normal()
193  {
194  return this->normal;
195  }
196 
198  public: inline T Offset() const
199  {
200  return this->d;
201  }
202 
206  public: Plane<T> &operator=(const Plane<T> &_p)
207  {
208  this->normal = _p.normal;
209  this->size = _p.size;
210  this->d = _p.d;
211 
212  return *this;
213  }
214 
216  private: Vector3<T> normal;
217 
219  private: Vector2<T> size;
220 
222  private: T d;
223  };
224 
228  }
229 }
230 
231 #endif
math::Vector3d Size() const
Get the size of the box.
On both sides of the plane.
Definition: Plane.hh:50
const Vector3< T > & Normal() const
Get the plane offset.
Definition: Plane.hh:186
PlaneSide Side(const math::Box &_box) const
The side of the plane a box is on.
Definition: Plane.hh:137
Plane(const Vector3< T > &_normal, T _offset=0.0)
Constructor from a normal and a distance.
Definition: Plane.hh:62
Plane< float > Planef
Definition: Plane.hh:227
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
A plane and related functions.
Definition: Plane.hh:31
Positive side of the plane.
Definition: Plane.hh:44
Plane< double > Planed
Definition: Plane.hh:226
On the plane.
Definition: Plane.hh:47
Plane()
Constructor.
Definition: Plane.hh:54
PlaneSide
Enum used to indicate a side of the plane, no side, or both sides for entities on the plane...
Definition: Plane.hh:36
Mathematical representation of a box and related functions.
Definition: Box.hh:35
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:195
T Offset() const
Get the plane offset.
Definition: Plane.hh:198
PlaneSide Side(const Vector3< T > &_point) const
The side of the plane a point is on.
Definition: Plane.hh:118
Vector2< T > & Size()
Get the plane size.
Definition: Plane.hh:180
T Distance(const Vector3< T > &_origin, const Vector3< T > &_dir) const
Get distance to the plane give an origin and direction.
Definition: Plane.hh:155
Plane(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Constructor.
Definition: Plane.hh:71
void Set(const Vector3< T > &_normal, T _offset)
Set the plane.
Definition: Plane.hh:83
Vector3< T > & Normal()
Get the plane offset.
Definition: Plane.hh:192
virtual ~Plane()
Destructor.
Definition: Plane.hh:78
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:36
Plane< T > & operator=(const Plane< T > &_p)
Equal operator.
Definition: Plane.hh:206
Negative side of the plane.
Definition: Plane.hh:40
Plane< int > Planei
Definition: Plane.hh:225
void Set(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Set the plane.
Definition: Plane.hh:93
math::Vector3d Center() const
Get the box center.
const Vector2< T > & Size() const
Get the plane size.
Definition: Plane.hh:174
Definition: Angle.hh:38
T Distance(const Vector3< T > &_point) const
The distance to the plane from the given point.
Definition: Plane.hh:107