Geometry.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <OpenKarto/Geometry.h>
19 #include <OpenKarto/String.h>
20 
21 namespace karto
22 {
23 
27 
29  : m_Heading(0.0)
30  {
31  }
32 
33  Pose2::Pose2(const Vector2d& rPosition, kt_double heading)
34  : m_Position(rPosition)
35  , m_Heading(heading)
36  {
37  }
38 
40  : m_Position(x, y)
41  , m_Heading(heading)
42  {
43  }
44 
45  Pose2::Pose2(const Pose3& rPose)
46  : m_Position(rPose.GetPosition().GetX(), rPose.GetPosition().GetY())
47  {
48  kt_double t1, t2;
49 
50  // calculates heading from orientation
51  rPose.GetOrientation().ToEulerAngles(m_Heading, t1, t2);
52  }
53 
54  Pose2::Pose2(const Pose2& rOther)
55  : m_Position(rOther.m_Position)
56  , m_Heading(rOther.m_Heading)
57  {
58  }
59 
63 
65  : m_Minimum(DBL_MAX, DBL_MAX)
66  , m_Maximum(-DBL_MAX, -DBL_MAX)
67  {
68  }
69 
70  BoundingBox2::BoundingBox2(const Vector2d& rMinimum, const Vector2d& rMaximum)
71  : m_Minimum(rMinimum)
72  , m_Maximum(rMaximum)
73  {
74  }
75 
79 
81  : m_Minimum(DBL_MAX, DBL_MAX, DBL_MAX)
82  , m_Maximum(-DBL_MAX, -DBL_MAX, -DBL_MAX)
83  {
84  }
85 
87  {
88  }
89 
93 
95  {
96  // The quaternion representing the rotation is
97  // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
98 
99  kt_double fSqrLength = m_Values[0] * m_Values[0] + m_Values[1] * m_Values[1] + m_Values[2] * m_Values[2];
100  if ( fSqrLength > 0.0 )
101  {
102  rAngle = 2.0 * acos(m_Values[3]);
103  kt_double fInvLength = 1.0 / sqrt(fSqrLength);
104  rAxis.SetX(m_Values[0] * fInvLength);
105  rAxis.SetY(m_Values[1] * fInvLength);
106  rAxis.SetZ(m_Values[2] * fInvLength);
107  }
108  else
109  {
110  // angle is 0 (mod 2*pi), so any axis will do
111  rAngle = 0.0;
112  rAxis.SetX(1.0);
113  rAxis.SetY(0.0);
114  rAxis.SetZ(0.0);
115  }
116  }
117 
118  void Quaternion::FromAngleAxis(kt_double angleInRadians, const karto::Vector3d& rAxis)
119  {
120  kt_double axisLength = rAxis.Length();
121  if (axisLength < KT_TOLERANCE)
122  {
123  assert(false);
124  // special case for zero length
125  *this = Quaternion();
126  return;
127  }
128  kt_double halfAngle = 0.5*angleInRadians;
129 
130  kt_double inverseLength = 1.0;// / axisLength;
131  kt_double sinHalfAngle = sin(halfAngle);
132  kt_double cosHalfAngle = cos(halfAngle);
133 
134  m_Values[0] = inverseLength * sinHalfAngle * rAxis.GetX();
135  m_Values[1] = inverseLength * sinHalfAngle * rAxis.GetY();
136  m_Values[2] = inverseLength * sinHalfAngle * rAxis.GetZ();
137  m_Values[3] = cosHalfAngle;
138  }
139 
140  void Quaternion::ToEulerAngles(kt_double& rYaw, kt_double& rPitch, kt_double& rRoll) const
141  {
142  kt_double test = m_Values[0] * m_Values[1] + m_Values[2] * m_Values[3];
143 
144  if (test > 0.499)
145  {
146  // singularity at north pole
147  rYaw = 2 * atan2(m_Values[0], m_Values[3]);;
148  rPitch = KT_PI_2;
149  rRoll = 0;
150  }
151  else if (test < -0.499)
152  {
153  // singularity at south pole
154  rYaw = -2 * atan2(m_Values[0], m_Values[3]);
155  rPitch = -KT_PI_2;
156  rRoll = 0;
157  }
158  else
159  {
160  kt_double sqx = m_Values[0] * m_Values[0];
161  kt_double sqy = m_Values[1] * m_Values[1];
162  kt_double sqz = m_Values[2] * m_Values[2];
163 
164  rYaw = atan2(2 * m_Values[1] * m_Values[3] - 2 * m_Values[0] * m_Values[2], 1 - 2 * sqy - 2 * sqz);
165  rPitch = asin(2 * test);
166  rRoll = atan2(2 * m_Values[0] * m_Values[3] - 2 * m_Values[1] * m_Values[2], 1 - 2 * sqx - 2 * sqz);
167  }
168  }
169 
171  {
173 
174  angle = yaw * 0.5;
175  kt_double cYaw = cos(angle);
176  kt_double sYaw = sin(angle);
177 
178  angle = pitch * 0.5;
179  kt_double cPitch = cos(angle);
180  kt_double sPitch = sin(angle);
181 
182  angle = roll * 0.5;
183  kt_double cRoll = cos(angle);
184  kt_double sRoll = sin(angle);
185 
186  m_Values[0] = sYaw * sPitch * cRoll + cYaw * cPitch * sRoll;
187  m_Values[1] = sYaw * cPitch * cRoll + cYaw * sPitch * sRoll;
188  m_Values[2] = cYaw * sPitch * cRoll - sYaw * cPitch * sRoll;
189  m_Values[3] = cYaw * cPitch * cRoll - sYaw * sPitch * sRoll;
190  }
191 
193  {
194  String valueString;
195  valueString.Append(StringHelper::ToString(GetX()));
196  valueString.Append(" ");
197  valueString.Append(StringHelper::ToString(GetY()));
198  valueString.Append(" ");
199  valueString.Append(StringHelper::ToString(GetZ()));
200  valueString.Append(" ");
201  valueString.Append(StringHelper::ToString(GetW()));
202  return valueString;
203  }
204 
208 
209 }
void ToAngleAxis(kt_double &rAngle, karto::Vector3d &rAxis) const
Definition: Geometry.cpp:94
void Append(const String &rString)
Definition: String.cpp:82
void SetY(const T &y)
Definition: Geometry.h:699
Vector2d m_Maximum
Definition: Geometry.h:1735
void FromEulerAngles(kt_double yaw, kt_double pitch, kt_double roll)
Definition: Geometry.cpp:170
const String ToString() const
Definition: Geometry.cpp:192
kt_double GetX() const
Definition: Geometry.h:2220
void FromAngleAxis(kt_double angleInRadians, const karto::Vector3d &rAxis)
Definition: Geometry.cpp:118
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
const kt_double KT_PI_2
Definition: Math.h:53
Vector2d m_Minimum
Definition: Geometry.h:1734
const T & GetY() const
Definition: Geometry.h:690
void SetX(const T &x)
Definition: Geometry.h:681
static String ToString(const char *value)
const kt_double KT_TOLERANCE
Definition: Math.h:71
virtual ~BoundingBox3()
Definition: Geometry.cpp:86
const Vector2d & GetPosition() const
Definition: Geometry.h:2256
double kt_double
Definition: Types.h:160
kt_double Length() const
Definition: Geometry.h:766
const Quaternion & GetOrientation() const
Definition: Geometry.h:2481
kt_double GetY() const
Definition: Geometry.h:2238
Vector2d m_Position
Definition: Geometry.h:2388
Definition: Any.cpp:20
const T & GetX() const
Definition: Geometry.h:672
const T & GetZ() const
Definition: Geometry.h:708
void SetZ(const T &z)
Definition: Geometry.h:717
kt_double m_Heading
Definition: Geometry.h:2390
void ToEulerAngles(kt_double &rYaw, kt_double &rPitch, kt_double &rRoll) const
Definition: Geometry.cpp:140


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36