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 #include <ignition/math/config.hh>
24 
25 namespace ignition
26 {
27  namespace math
28  {
29  inline namespace IGNITION_MATH_VERSION_NAMESPACE
30  {
33  template<typename T>
34  class Plane
35  {
39  public: enum PlaneSide
40  {
44 
48 
50  NO_SIDE = 2,
51 
54  };
55 
57  public: Plane()
58  : d(0.0)
59  {
60  }
61 
65  public: Plane(const Vector3<T> &_normal, T _offset = 0.0)
66  : normal(_normal), d(_offset)
67  {
68  }
69 
74  public: Plane(const Vector3<T> &_normal, const Vector2<T> &_size,
75  T _offset)
76  {
77  this->Set(_normal, _size, _offset);
78  }
79 
81  public: virtual ~Plane() {}
82 
86  public: void Set(const Vector3<T> &_normal, T _offset)
87  {
88  this->normal = _normal;
89  this->d = _offset;
90  }
91 
96  public: void Set(const Vector3<T> &_normal, const Vector2<T> &_size,
97  T _offset)
98  {
99  this->normal = _normal;
100  this->size = _size;
101  this->d = _offset;
102  }
103 
110  public: T Distance(const Vector3<T> &_point) const
111  {
112  return this->normal.Dot(_point) - this->d;
113  }
114 
121  public: PlaneSide Side(const Vector3<T> &_point) const
122  {
123  T dist = this->Distance(_point);
124 
125  if (dist < 0.0)
126  return NEGATIVE_SIDE;
127 
128  if (dist > 0.0)
129  return POSITIVE_SIDE;
130 
131  return NO_SIDE;
132  }
133 
140  public: PlaneSide Side(const math::Box &_box) const
141  {
142  double dist = this->Distance(_box.Center());
143  double maxAbsDist = this->normal.AbsDot(_box.Size()/2.0);
144 
145  if (dist < -maxAbsDist)
146  return NEGATIVE_SIDE;
147 
148  if (dist > maxAbsDist)
149  return POSITIVE_SIDE;
150 
151  return BOTH_SIDE;
152  }
153 
158  public: T Distance(const Vector3<T> &_origin,
159  const Vector3<T> &_dir) const
160  {
161  T denom = this->normal.Dot(_dir);
162 
163  if (std::abs(denom) < 1e-3)
164  {
165  // parallel
166  return 0;
167  }
168  else
169  {
170  T nom = _origin.Dot(this->normal) - this->d;
171  T t = -(nom/denom);
172  return t;
173  }
174  }
175 
177  public: inline const Vector2<T> &Size() const
178  {
179  return this->size;
180  }
181 
183  public: inline Vector2<T> &Size()
184  {
185  return this->size;
186  }
187 
189  public: inline const Vector3<T> &Normal() const
190  {
191  return this->normal;
192  }
193 
195  public: inline Vector3<T> &Normal()
196  {
197  return this->normal;
198  }
199 
201  public: inline T Offset() const
202  {
203  return this->d;
204  }
205 
209  public: Plane<T> &operator=(const Plane<T> &_p)
210  {
211  this->normal = _p.normal;
212  this->size = _p.size;
213  this->d = _p.d;
214 
215  return *this;
216  }
217 
219  private: Vector3<T> normal;
220 
222  private: Vector2<T> size;
223 
225  private: T d;
226  };
227 
231  }
232  }
233 }
234 
235 #endif
Plane< float > Planef
Definition: Plane.hh:230
Plane(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Constructor.
Definition: Plane.hh:74
PlaneSide
Enum used to indicate a side of the plane, no side, or both sides for entities on the plane...
Definition: Plane.hh:39
Positive side of the plane.
Definition: Plane.hh:47
void Set(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Set the plane.
Definition: Plane.hh:96
Vector2< T > & Size()
Get the plane size.
Definition: Plane.hh:183
A plane and related functions.
Definition: Plane.hh:34
Plane< T > & operator=(const Plane< T > &_p)
Equal operator.
Definition: Plane.hh:209
T Distance(const Vector3< T > &_origin, const Vector3< T > &_dir) const
Get distance to the plane give an origin and direction.
Definition: Plane.hh:158
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
Vector3< T > & Normal()
Get the plane offset.
Definition: Plane.hh:195
Two dimensional (x, y) vector.
Definition: Vector2.hh:32
On both sides of the plane.
Definition: Plane.hh:53
Plane< int > Planei
Definition: Plane.hh:228
const Vector2< T > & Size() const
Get the plane size.
Definition: Plane.hh:177
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:198
virtual ~Plane()
Destructor.
Definition: Plane.hh:81
Plane< double > Planed
Definition: Plane.hh:229
Plane(const Vector3< T > &_normal, T _offset=0.0)
Constructor from a normal and a distance.
Definition: Plane.hh:65
void Set(const Vector3< T > &_normal, T _offset)
Set the plane.
Definition: Plane.hh:86
PlaneSide Side(const Vector3< T > &_point) const
The side of the plane a point is on.
Definition: Plane.hh:121
Negative side of the plane.
Definition: Plane.hh:43
Definition: Angle.hh:39
const Vector3< T > & Normal() const
Get the plane offset.
Definition: Plane.hh:189
T Distance(const Vector3< T > &_point) const
The distance to the plane from the given point.
Definition: Plane.hh:110
T Offset() const
Get the plane offset.
Definition: Plane.hh:201
PlaneSide Side(const math::Box &_box) const
The side of the plane a box is on.
Definition: Plane.hh:140
Plane()
Constructor.
Definition: Plane.hh:57