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_MATH_PLANE_HH_
19 #define IGNITION_MATH_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  : d(0.0)
57  {
58  }
59 
63  public: Plane(const Vector3<T> &_normal, T _offset = 0.0)
64  : normal(_normal), d(_offset)
65  {
66  }
67 
72  public: Plane(const Vector3<T> &_normal, const Vector2<T> &_size,
73  T _offset)
74  {
75  this->Set(_normal, _size, _offset);
76  }
77 
79  public: virtual ~Plane() {}
80 
84  public: void Set(const Vector3<T> &_normal, T _offset)
85  {
86  this->normal = _normal;
87  this->d = _offset;
88  }
89 
94  public: void Set(const Vector3<T> &_normal, const Vector2<T> &_size,
95  T _offset)
96  {
97  this->normal = _normal;
98  this->size = _size;
99  this->d = _offset;
100  }
101 
108  public: T Distance(const Vector3<T> &_point) const
109  {
110  return this->normal.Dot(_point) - this->d;
111  }
112 
119  public: PlaneSide Side(const Vector3<T> &_point) const
120  {
121  T dist = this->Distance(_point);
122 
123  if (dist < 0.0)
124  return NEGATIVE_SIDE;
125 
126  if (dist > 0.0)
127  return POSITIVE_SIDE;
128 
129  return NO_SIDE;
130  }
131 
138  public: PlaneSide Side(const math::Box &_box) const
139  {
140  double dist = this->Distance(_box.Center());
141  double maxAbsDist = this->normal.AbsDot(_box.Size()/2.0);
142 
143  if (dist < -maxAbsDist)
144  return NEGATIVE_SIDE;
145 
146  if (dist > maxAbsDist)
147  return POSITIVE_SIDE;
148 
149  return BOTH_SIDE;
150  }
151 
156  public: T Distance(const Vector3<T> &_origin,
157  const Vector3<T> &_dir) const
158  {
159  T denom = this->normal.Dot(_dir);
160 
161  if (std::abs(denom) < 1e-3)
162  {
163  // parallel
164  return 0;
165  }
166  else
167  {
168  T nom = _origin.Dot(this->normal) - this->d;
169  T t = -(nom/denom);
170  return t;
171  }
172  }
173 
175  public: inline const Vector2<T> &Size() const
176  {
177  return this->size;
178  }
179 
181  public: inline Vector2<T> &Size()
182  {
183  return this->size;
184  }
185 
187  public: inline const Vector3<T> &Normal() const
188  {
189  return this->normal;
190  }
191 
193  public: inline Vector3<T> &Normal()
194  {
195  return this->normal;
196  }
197 
199  public: inline T Offset() const
200  {
201  return this->d;
202  }
203 
207  public: Plane<T> &operator=(const Plane<T> &_p)
208  {
209  this->normal = _p.normal;
210  this->size = _p.size;
211  this->d = _p.d;
212 
213  return *this;
214  }
215 
217  private: Vector3<T> normal;
218 
220  private: Vector2<T> size;
221 
223  private: T d;
224  };
225 
229  }
230 }
231 
232 #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:138
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:228
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:227
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:119
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:175
T Distance(const Vector3< T > &_origin, const Vector3< T > &_dir) const
Get distance to the plane give an origin and direction.
Definition: Plane.hh:156
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:196
T Distance(const Vector3< T > &_point) const
The distance to the plane from the given point.
Definition: Plane.hh:108
Mathematical representation of a box and related functions.
Definition: Box.hh:35
T Offset() const
Get the plane offset.
Definition: Plane.hh:199
math::Vector3d Size() const
Get the size of the box.
const Vector3< T > & Normal() const
Get the plane offset.
Definition: Plane.hh:187
Vector2< T > & Size()
Get the plane size.
Definition: Plane.hh:181
Plane(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Constructor.
Definition: Plane.hh:72
void Set(const Vector3< T > &_normal, T _offset)
Set the plane.
Definition: Plane.hh:84
Vector3< T > & Normal()
Get the plane offset.
Definition: Plane.hh:193
virtual ~Plane()
Destructor.
Definition: Plane.hh:79
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:207
Negative side of the plane.
Definition: Plane.hh:41
Plane< int > Planei
Definition: Plane.hh:226
void Set(const Vector3< T > &_normal, const Vector2< T > &_size, T _offset)
Set the plane.
Definition: Plane.hh:94
Definition: AffineException.hh:30