Robotics Library  0.7.0
CircularVector3.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Andre Gaschler
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_MATH_CIRCULARVECTOR3_H
28 #define RL_MATH_CIRCULARVECTOR3_H
29 
30 #define EIGEN_MATRIXBASE_PLUGIN <rl/math/MatrixBaseAddons.h>
31 #define EIGEN_QUATERNIONBASE_PLUGIN <rl/math/QuaternionBaseAddons.h>
32 #define EIGEN_TRANSFORM_PLUGIN <rl/math/TransformAddons.h>
33 
34 #include <cassert>
35 #include <cmath>
36 #include <limits>
37 #include <Eigen/Dense>
38 #include <Eigen/LU>
39 
40 #include "Circular.h"
41 #include "CircularVector2.h"
42 #include "Function.h"
43 #include "Matrix.h"
44 #include "Vector.h"
45 
46 namespace rl
47 {
48  namespace math
49  {
55  template<>
56  class Circular<Vector3> : public Function<Vector3>
57  {
58  public:
61  angle(0)
62  {
63  }
64 
65  virtual ~Circular<Vector3>()
66  {
67  }
68 
74  static Circular<Vector3> ThreePoints(const Vector3& y0, const Vector3& yi, const Vector3& y1, const Real& x1 = 1)
75  {
76  Circular f;
77 
78  Vector3 planeNormal = (yi - y0).cross(y1 - y0);
79  assert(planeNormal.norm() > 1e-8 && "Circular motion is ambiguous. Given three points must not be colinear!");
80  planeNormal.normalize();
81  Real planeD = planeNormal.transpose() * y0;
82 
83  // Numerically stable orthonormals
84  Vector3 planeDirection1, planeDirection2;
85  Vector3 someUnit(1, 0, 0);
86  Vector3 someOtherUnit(0, 1, 0);
87 
88  if ((planeNormal.transpose() * someUnit).norm() < (planeNormal.transpose() * someOtherUnit).norm())
89  {
90  planeDirection1 = planeNormal.cross(someUnit);
91  }
92  else
93  {
94  planeDirection1 = planeNormal.cross(someOtherUnit);
95  }
96 
97  planeDirection1.normalize();
98  planeDirection2 = planeNormal.cross(planeDirection1);
99  planeDirection2.normalize();
100 
101  // Project all points to a plane that contains the circular motion
102  ::Eigen::Matrix<Real, 2, 3> projectionTo2d;
103  projectionTo2d << planeDirection1.transpose(), planeDirection2.transpose();
104  Vector2 y02d = projectionTo2d * y0;
105  Vector2 yi2d = projectionTo2d * yi;
106  Vector2 y12d = projectionTo2d * y1;
107 
108  Circular<Vector2> circ2d = Circular<Vector2>::ThreePoints(y02d, yi2d, y12d);
109  f.center = projectionTo2d.transpose() * circ2d.getCenter() + planeNormal * planeD;
110  f.axisX = y0 - f.center;
111  f.axisY = planeNormal.cross(Vector3(f.axisX));
112  f.angle = circ2d.getAngle();
113 
114  f.x1 = x1;
115 
116  return f;
117  }
118 
131  static Circular<Vector3> ThreePointsAngle(const Vector3& y0, const Vector3& yi, const Vector3& y1, const Real& angle, const Real& x1 = 1)
132  {
133  Circular<Vector3> c = ThreePoints(y0, yi, y1, x1);
134  c.angle = angle;
135  return c;
136  }
137 
139  {
140  return new Circular<Vector3>(*this);
141  }
142 
143  Real getAngle() const
144  {
145  return this->angle;
146  }
147 
149  {
150  return this->axisX;
151  }
152 
154  {
155  return this->axisY;
156  }
157 
159  {
160  return this->center;
161  }
162 
169  Vector3 operator()(const Real& x, const ::std::size_t& derivative = 0) const
170  {
171  assert(x >= this->lower() - FUNCTION_BOUNDARY);
172  assert(x <= this->upper() + FUNCTION_BOUNDARY);
173  assert(derivative <= 2 && "Circular: higher derivatives not implemented");
174 
175  Real c = this->angle / this->x1;
176 
177  if (derivative == 0)
178  {
179  return this->center + ::std::cos(c * x) * this->axisX + ::std::sin(c * x) * this->axisY;
180  }
181  else if (derivative == 1)
182  {
183  return -c * ::std::sin(c * x) * this->axisX + c * ::std::cos(c * x) * this->axisY;
184  }
185  else if (derivative == 2)
186  {
187  return -::std::pow(c, 2) * ::std::cos(c * x) * this->axisX - ::std::pow(c, 2) * ::std::sin(c * x) * this->axisY;
188  }
189  else
190  {
191  return this->axisX * ::std::numeric_limits<Real>::signaling_NaN();
192  }
193  }
194 
195  protected:
198 
201 
204 
207 
208  private:
209 
210  };
211  }
212 }
213 
214 #endif // RL_MATH_CIRCULARVECTOR3_H
rl::math::Circular< Vector3 >::getCenter
Vector3 getCenter() const
Definition: CircularVector3.h:158
pow
Quaternion< Scalar > pow(const Scalar &t) const
Definition: QuaternionBaseAddons.h:128
rl::math::Circular
Circular segment function that maps from a time x to a point on a circular trajectory.
Definition: Circular.h:43
rl::math::Circular< Vector2 >::getCenter
Vector2 getCenter() const
Definition: CircularVector2.h:151
rl::math::Function
A mathematical mapping from Real -> ArrayX.
Definition: Function.h:45
rl::math::Function::x1
Real x1
Definition: Function.h:104
rl::math::Vector3
::Eigen::Matrix< Real, 3, 1 > Vector3
Definition: Vector.h:46
rl::math::Circular< Vector2 >
Circular segment function that maps from a time x to a point in 2D on a circular trajectory.
Definition: CircularVector2.h:56
rl::math::Circular< Vector3 >::getAxisY
Vector3 getAxisY() const
Definition: CircularVector3.h:153
rl::math::Circular< Vector3 >::operator()
Vector3 operator()(const Real &x, const ::std::size_t &derivative=0) const
Evaluates the circular segment function for a given x.
Definition: CircularVector3.h:169
rl::math::Circular< Vector3 >
Circular segment function that maps from a time x to a point in 3D on a circular trajectory.
Definition: CircularVector3.h:57
CircularVector2.h
rl::math::Circular< Vector3 >::getAngle
Real getAngle() const
Definition: CircularVector3.h:143
Vector.h
rl::math::Circular< Vector3 >::ThreePointsAngle
static Circular< Vector3 > ThreePointsAngle(const Vector3 &y0, const Vector3 &yi, const Vector3 &y1, const Real &angle, const Real &x1=1)
Generates a circular segment through three given points in 3D with a given segment angle.
Definition: CircularVector3.h:131
Circular.h
rl::math::Circular< Vector3 >::axisX
Vector3 axisX
First axis of the circular motion.
Definition: CircularVector3.h:200
rl::math::Circular< Vector3 >::clone
Circular< Vector3 > * clone() const
Definition: CircularVector3.h:138
rl::math::Function::upper
Real & upper()
Definition: Function.h:74
rl::math::Circular< Vector3 >::center
Vector3 center
Center of the circle.
Definition: CircularVector3.h:206
rl::math::Circular< Vector3 >::getAxisX
Vector3 getAxisX() const
Definition: CircularVector3.h:148
rl::math::Vector2
::Eigen::Matrix< Real, 2, 1 > Vector2
Definition: Vector.h:44
rl::math::Circular< Vector3 >::ThreePoints
static Circular< Vector3 > ThreePoints(const Vector3 &y0, const Vector3 &yi, const Vector3 &y1, const Real &x1=1)
Generates a circular segment through three given points in 3D.
Definition: CircularVector3.h:74
Matrix.h
rl::math::Circular< Vector2 >::getAngle
Real getAngle() const
Definition: CircularVector2.h:136
rl::math::FUNCTION_BOUNDARY
static const Real FUNCTION_BOUNDARY
Definition: Function.h:110
rl::math::Function::lower
Real & lower()
Definition: Function.h:64
rl::math::Real
double Real
Definition: Real.h:42
Function.h
rl::math::Circular< Vector3 >::angle
Real angle
Angle of circular motion.
Definition: CircularVector3.h:197
rl::math::Circular< Vector3 >::axisY
Vector3 axisY
Second axis of the circular motion.
Definition: CircularVector3.h:203
rl
Robotics Library.
Definition: AnalogInput.cpp:30