All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Plane.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 
18 #ifndef _IGNITION_PLANE_HH_
19 #define _IGNITION_PLANE_HH_
20 
21 #include <ignition/math/Box.hh>
22 #include <ignition/math/Vector3.hh>
23 #include <ignition/math/Vector2.hh>
24 
25 namespace ignition
26 {
27  namespace math
28  {
31  template<typename T>
32  class Plane
33  {
37  public: enum PlaneSide
38  {
42 
46 
48  NO_SIDE = 2,
49 
52  };
53 
55  public: Plane()
56  {
57  this->d = 0.0;
58  }
59 
63  public: Plane(const Vector3<T> &_normal, T _offset = 0.0)
64  {
65  this->normal = _normal;
66  this->d = _offset;
67  }
68 
73  public: Plane(const Vector3<T> &_normal, const Vector2<T> &_size,
74  T _offset)
75  {
76  this->Set(_normal, _size, _offset);
77  }
78 
80  public: virtual ~Plane() {}
81 
85  public: void Set(const Vector3<T> &_normal, T _offset)
86  {
87  this->normal = _normal;
88  this->d = _offset;
89  }
90 
95  public: void Set(const Vector3<T> &_normal, const Vector2<T> &_size,
96  T _offset)
97  {
98  this->normal = _normal;
99  this->size = _size;
100  this->d = _offset;
101  }
102 
109  public: T Distance(const Vector3<T> &_point) const
110  {
111  return this->normal.Dot(_point) - this->d;
112  }
113 
120  public: PlaneSide Side(const Vector3<T> &_point) const
121  {
122  T dist = this->Distance(_point);
123 
124  if (dist < 0.0)
125  return NEGATIVE_SIDE;
126 
127  if (dist > 0.0)
128  return POSITIVE_SIDE;
129 
130  return NO_SIDE;
131  }
132 
139  public: PlaneSide Side(const math::Box &_box) const
140  {
141  double dist = this->Distance(_box.Center());
142  double maxAbsDist = this->normal.AbsDot(_box.Size()/2.0);
143 
144  if (dist < -maxAbsDist)
145  return NEGATIVE_SIDE;
146 
147  if (dist > maxAbsDist)
148  return POSITIVE_SIDE;
149 
150  return BOTH_SIDE;
151  }
152 
157  public: T Distance(const Vector3<T> &_origin,
158  const Vector3<T> &_dir) const
159  {
160  T denom = this->normal.Dot(_dir);
161 
162  if (std::abs(denom) < 1e-3)
163  {
164  // parallel
165  return 0;
166  }
167  else
168  {
169  T nom = _origin.Dot(this->normal) - this->d;
170  T t = -(nom/denom);
171  return t;
172  }
173  }
174 
176  public: inline const Vector2<T> &Size() const
177  {
178  return this->size;
179  }
180 
182  public: inline Vector2<T> &Size()
183  {
184  return this->size;
185  }
186 
188  public: inline const Vector3<T> &Normal() const
189  {
190  return this->normal;
191  }
192 
194  public: inline Vector3<T> &Normal()
195  {
196  return this->normal;
197  }
198 
200  public: inline T Offset() const
201  {
202  return this->d;
203  }
204 
208  public: Plane<T> &operator=(const Plane<T> &_p)
209  {
210  this->normal = _p.normal;
211  this->size = _p.size;
212  this->d = _p.d;
213 
214  return *this;
215  }
216 
218  private: Vector3<T> normal;
219 
221  private: Vector2<T> size;
222 
224  private: T d;
225  };
226 
230  }
231 }
232 
233 #endif
On both sides of the plane.
Definition: Plane.hh:51
PlaneSide Side(const math::Box &_box) const
The side of the plane a box is on.
Definition: Plane.hh:139
Plane(const Vector3< T > &_normal, T _offset=0.0)
Constructor from a normal and a distance.
Definition: Plane.hh:63
Plane< float > Planef
Definition: Plane.hh:229
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
A plane and related functions.
Definition: Plane.hh:32
Positive side of the plane.
Definition: Plane.hh:45
Plane< double > Planed
Definition: Plane.hh:228
On the plane.
Definition: Plane.hh:48
Plane()
Constructor.
Definition: Plane.hh:55
PlaneSide Side(const Vector3< T > &_point) const
The side of the plane a point is on.
Definition: Plane.hh:120
PlaneSide
Enum used to indicate a side of the plane, no side, or both sides for entities on the plane...
Definition: Plane.hh:37
const Vector2< T > & Size() const
Get the plane size.
Definition: Plane.hh:176
T Distance(const Vector3< T > &_origin, const Vector3< T > &_dir) const
Get distance to the plane give an origin and direction.
Definition: Plane.hh:157
math::Vector3d Center() const
Get the box center.
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:191
T Distance(const Vector3< T > &_point) const
The distance to the plane from the given point.
Definition: Plane.hh:109
Mathematical representation of a box and related functions.
Definition: Box.hh:33
T Offset() const
Get the plane offset.
Definition: Plane.hh:200
math::Vector3d Size() const
Get the size of the box.
const Vector3< T > & Normal() const
Get the plane offset.
Definition: Plane.hh:188
Vector2< T > & Size()
Get the plane size.
Definition: Plane.hh:182
Plane(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Constructor.
Definition: Plane.hh:73
void Set(const Vector3< T > &_normal, T _offset)
Set the plane.
Definition: Plane.hh:85
Vector3< T > & Normal()
Get the plane offset.
Definition: Plane.hh:194
virtual ~Plane()
Destructor.
Definition: Plane.hh:80
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
Plane< T > & operator=(const Plane< T > &_p)
Equal operator.
Definition: Plane.hh:208
Negative side of the plane.
Definition: Plane.hh:41
Plane< int > Planei
Definition: Plane.hh:227
void Set(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Set the plane.
Definition: Plane.hh:95