Rot2.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/geometry/Rot2.h>
20 #include <iostream>
21 
22 using namespace std;
23 
24 namespace gtsam {
25 
26 /* ************************************************************************* */
27 Rot2 Rot2::fromCosSin(double c, double s) {
28  Rot2 R(c, s);
29  return R.normalize();
30 }
31 
32 /* ************************************************************************* */
33 Rot2 Rot2::atan2(double y, double x) {
34  Rot2 R(x, y);
35  return R.normalize();
36 }
37 
38 /* ************************************************************************* */
39 Rot2 Rot2::Random(std::mt19937& rng) {
40  uniform_real_distribution<double> randomAngle(-M_PI, M_PI);
41  double angle = randomAngle(rng);
42  return fromAngle(angle);
43 }
44 
45 /* ************************************************************************* */
46 void Rot2::print(const string& s) const {
47  cout << s << ": " << theta() << endl;
48 }
49 
50 /* ************************************************************************* */
51 bool Rot2::equals(const Rot2& R, double tol) const {
52  return std::abs(c_ - R.c_) <= tol && std::abs(s_ - R.s_) <= tol;
53 }
54 
55 /* ************************************************************************* */
57  double scale = c_*c_ + s_*s_;
58  if(std::abs(scale-1.0) > 1e-10) {
59  scale = 1 / sqrt(scale);
60  c_ *= scale;
61  s_ *= scale;
62  }
63  return *this;
64 }
65 
66 /* ************************************************************************* */
68  if (H)
69  *H = I_1x1;
70  if (v.isZero())
71  return (Rot2());
72  else
73  return Rot2::fromAngle(v(0));
74 }
75 
76 /* ************************************************************************* */
77 Vector1 Rot2::Logmap(const Rot2& r, OptionalJacobian<1, 1> H) {
78  if (H)
79  *H = I_1x1;
80  Vector1 v;
81  v << r.theta();
82  return v;
83 }
84 /* ************************************************************************* */
85 Matrix2 Rot2::Hat(const Vector1& xi) {
86  Matrix2 X;
87  X << 0., -xi.x(),
88  xi.x(), 0.;
89  return X;
90 }
91 
92 /* ************************************************************************* */
93 Vector1 Rot2::Vee(const Matrix2& X) {
95  v << X(1, 0);
96  return v;
97 }
98 
99 /* ************************************************************************* */
100 Matrix2 Rot2::matrix() const {
101  Matrix2 rvalue_;
102  rvalue_ << c_, -s_, s_, c_;
103  return rvalue_;
104 }
105 
106 /* ************************************************************************* */
107 Matrix2 Rot2::transpose() const {
108  Matrix2 rvalue_;
109  rvalue_ << c_, s_, -s_, c_;
110  return rvalue_;
111 }
112 
113 /* ************************************************************************* */
114 // see doc/math.lyx, SO(2) section
116  OptionalJacobian<2, 2> H2) const {
117  const Point2 q = Point2(c_ * p.x() + -s_ * p.y(), s_ * p.x() + c_ * p.y());
118  if (H1) *H1 << -q.y(), q.x();
119  if (H2) *H2 = matrix();
120  return q;
121 }
122 
123 /* ************************************************************************* */
124 // see doc/math.lyx, SO(2) section
127  const Point2 q = Point2(c_ * p.x() + s_ * p.y(), -s_ * p.x() + c_ * p.y());
128  if (H1) *H1 << q.y(), -q.x();
129  if (H2) *H2 = transpose();
130  return q;
131 }
132 
133 /* ************************************************************************* */
134 Rot2 Rot2::relativeBearing(const Point2& d, OptionalJacobian<1, 2> H) {
135  double x = d.x(), y = d.y(), d2 = x * x + y * y, n = sqrt(d2);
136  if(std::abs(n) > 1e-5) {
137  if (H) {
138  *H << -y / d2, x / d2;
139  }
140  return Rot2::fromCosSin(x / n, y / n);
141  } else {
142  if (H) *H << 0.0, 0.0;
143  return Rot2();
144  }
145 }
146 
147 /* ************************************************************************* */
148 Rot2 Rot2::ClosestTo(const Matrix2& M) {
150  const Matrix2& U = svd.matrixU();
151  const Matrix2& V = svd.matrixV();
152  const double det = (U * V.transpose()).determinant();
153  Matrix2 M_prime = (U * Vector2(1, det).asDiagonal() * V.transpose());
154 
155  double c = M_prime(0, 0);
156  double s = M_prime(1, 0);
157  return Rot2::fromCosSin(c, s);
158 }
159 
160 /* ************************************************************************* */
161 
162 } // gtsam
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
rng
static std::mt19937 rng
Definition: timeFactorOverhead.cpp:31
Eigen::ComputeFullV
@ ComputeFullV
Definition: Constants.h:397
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
d
static const double d[K][N]
Definition: igam.h:11
screwPose2::xi
Vector xi
Definition: testPose2.cpp:169
gtsam::Vector2
Eigen::Vector2d Vector2
Definition: Vector.h:43
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::Rot2::theta
double theta() const
Definition: Rot2.h:197
Eigen::ComputeFullU
@ ComputeFullU
Definition: Constants.h:393
X
#define X
Definition: icosphere.cpp:20
svd
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf > svd(m, ComputeThinU|ComputeThinV)
Rot2.h
2D rotation
determinant
void determinant(const MatrixType &m)
Definition: determinant.cpp:14
n
int n
Definition: BiCGSTAB_simple.cpp:1
Expmap
Pose2_ Expmap(const Vector3_ &xi)
Definition: InverseKinematicsExampleExpressions.cpp:47
gtsam::normalize
static void normalize(Signature::Row &row)
Definition: Signature.cpp:88
scale
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
Definition: gnuplot_common_settings.hh:54
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
gtsam::Point2
Vector2 Point2
Definition: Point2.h:32
y
Scalar * y
Definition: level1_cplx_impl.h:124
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
atan2
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:110
test_docs.d2
d2
Definition: test_docs.py:29
Eigen::JacobiSVD
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: ForwardDeclarations.h:278
gtsam::Rot2
Definition: Rot2.h:35
gtsam
traits
Definition: SFMdata.h:40
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
std
Definition: BFloat16.h:88
p
float * p
Definition: Tutorial_Map_using.cpp:9
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::testing::unrotate
P unrotate(const T &r, const P &pt)
Definition: lieProxies.h:50
gtsam::tol
const G double tol
Definition: Group.h:79
U
@ U
Definition: testDecisionTree.cpp:342
V
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
abs
#define abs(x)
Definition: datatypes.h:17
M_PI
#define M_PI
Definition: mconf.h:117
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
gtsam::testing::rotate
P rotate(const T &r, const P &pt)
Definition: lieProxies.h:47
R
Rot2 R(Rot2::fromAngle(0.1))
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Mon Mar 10 2025 03:04:14