farm-ng-core
scaling.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
12 
13 namespace sophus {
14 namespace lie {
15 
16 template <class TScalar, int kDim>
17 class ScalingImpl {
18  public:
19  using Scalar = TScalar;
20  static int const kDof = kDim;
21  static int const kNumParams = kDim;
22  static int const kPointDim = kDim;
23  static int const kAmbientDim = kDim;
24 
25  using Tangent = Eigen::Vector<Scalar, kDof>;
26  using Params = Eigen::Vector<Scalar, kNumParams>;
27  using Point = Eigen::Vector<Scalar, kPointDim>;
28 
29  static bool constexpr kIsOriginPreserving = true;
30  static bool constexpr kIsAxisDirectionPreserving = true;
31  static bool constexpr kIsDirectionVectorPreserving = false;
32  static bool constexpr kIsShapePreserving = false;
33  static bool constexpr kIisSizePreserving = false;
34  static bool constexpr kIisParallelLinePreserving = true;
35 
36  template <class TCompatibleScalar>
37  using ScalarReturn = typename Eigen::
38  ScalarBinaryOpTraits<Scalar, TCompatibleScalar>::ReturnType;
39 
40  template <class TCompatibleScalar>
41  using ParamsReturn =
42  Eigen::Vector<ScalarReturn<TCompatibleScalar>, kNumParams>;
43 
44  template <class TCompatibleScalar>
45  using PointReturn = Eigen::Vector<ScalarReturn<TCompatibleScalar>, kPointDim>;
46 
47  template <class TCompatibleScalar>
48  using UnitVectorReturn =
50 
51  // constructors and factories
52 
53  static auto identityParams() -> Params {
54  return Eigen::Vector<Scalar, kDim>::Ones();
55  }
56 
57  static auto areParamsValid(Params const& scale_factors)
58  -> sophus::Expected<Success> {
59  static const Scalar kThr = kEpsilon<Scalar>;
60 
61  if (!(scale_factors.array() > kThr).all()) {
62  return SOPHUS_UNEXPECTED(
63  "scale factors ({}) too close to zero.\n",
64  "thr: {}",
65  scale_factors.transpose(),
66  kThr);
67  }
68  if (!(scale_factors.array() < 1.0 / kThr).all()) {
69  return SOPHUS_UNEXPECTED(
70  "inverse of scale factors ({}) too close to zero.\n",
71  "1.0 / thr: {}",
72  scale_factors.transpose(),
73  1.0 / kThr);
74  }
75  return sophus::Expected<Success>{};
76  }
77 
78  static auto hasShortestPathAmbiguity(Params const&) -> bool { return false; }
79 
80  // Manifold / Lie Group concepts
81 
82  static auto exp(Tangent const& log_scale_factors) -> Params {
83  using std::exp;
84  return log_scale_factors.array().exp();
85  }
86 
87  static auto log(Params const& scale_factors) -> Tangent {
88  using std::log;
89  return scale_factors.array().log();
90  }
91 
92  static auto hat(Tangent const& scale_factors)
93  -> Eigen::Matrix<Scalar, kAmbientDim, kAmbientDim> {
94  Eigen::Matrix<Scalar, kAmbientDim, kAmbientDim> mat;
95  mat.setZero();
96  for (int i = 0; i < kDof; ++i) {
97  mat.diagonal()[i] = scale_factors[i];
98  }
99  return mat;
100  }
101 
102  static auto vee(Eigen::Matrix<Scalar, kAmbientDim, kAmbientDim> const& mat)
103  -> Eigen::Matrix<Scalar, kDof, 1> {
104  return mat.diagonal();
105  }
106 
107  // group operations
108 
109  static auto inverse(Params const& scale_factors) -> Params {
110  Eigen::Vector<Scalar, kDim> params;
111  for (int i = 0; i < kDof; ++i) {
112  params[i] = 1.0 / scale_factors[i];
113  }
114  return params;
115  }
116 
117  template <class TCompatibleScalar>
118  static auto multiplication(
119  Params const& lhs_params,
120  Eigen::Vector<TCompatibleScalar, kPointDim> const& rhs_params)
122  return lhs_params.array() * rhs_params.array();
123  }
124 
125  // Point actions
126 
127  template <class TCompatibleScalar>
128  static auto action(
129  Params const& scale_factors,
130  Eigen::Vector<TCompatibleScalar, kPointDim> const& point)
132  return scale_factors.array() * point.array();
133  }
134 
135  template <class TCompatibleScalar>
136  static auto action(
137  Params const& scale_factors,
138  UnitVector<TCompatibleScalar, kPointDim> const& direction_vector)
141  action(scale_factors, direction_vector.params()));
142  }
143 
144  static auto toAmbient(Point const& point)
145  -> Eigen::Vector<Scalar, kAmbientDim> {
146  return point;
147  }
148 
149  static auto adj(Params const& /*unused*/)
150  -> Eigen::Matrix<Scalar, kDof, kDof> {
151  return Eigen::Matrix<Scalar, kDof, kDof>::Identity();
152  }
153 
154  // Matrices
155 
156  static auto compactMatrix(Params const& scale_factors)
157  -> Eigen::Matrix<Scalar, kPointDim, kAmbientDim> {
158  return hat(scale_factors);
159  }
160 
161  static auto matrix(Params const& scale_factors)
162  -> Eigen::Matrix<Scalar, kAmbientDim, kAmbientDim> {
163  return compactMatrix(scale_factors);
164  }
165 
166  // factor group concepts
167 
168  static auto matV(Params const& params, Tangent const& tangent)
169  -> Eigen::Matrix<Scalar, kPointDim, kPointDim> {
170  using std::abs;
171  Eigen::Matrix<Scalar, kPointDim, kPointDim> mat =
172  Eigen::Matrix<Scalar, kPointDim, kPointDim>::Identity();
173  for (int i = 0; i < kDof; ++i) {
174  Scalar t = tangent[i];
175  if (abs(t) < kEpsilon<Scalar>) {
176  mat(i, i) = abs(1.0 - 2.0 * t + 1.5 * t * t);
177  } else {
178  mat(i, i) = abs((params[i] - 1.0) / tangent[i]);
179  }
180  }
181  return mat;
182  }
183 
184  static auto matVInverse(Params const& params, Params const& tangent)
185  -> Eigen::Matrix<Scalar, kPointDim, kPointDim> {
186  Eigen::Matrix<Scalar, kPointDim, kPointDim> mat =
187  Eigen::Matrix<Scalar, kPointDim, kPointDim>::Identity();
188  using std::abs;
189  for (int i = 0; i < kDof; ++i) {
190  Scalar t = tangent[i];
191  if (abs(t) < kEpsilon<Scalar>) {
192  mat(i, i) = abs(1.0 + 2.0 * t + 2.5 * t * t);
193  } else {
194  mat(i, i) = abs(tangent[i] / (params[i] - 1.0));
195  }
196  }
197  return mat;
198  }
199 
200  static auto adjOfTranslation(Params const& params, Point const& point)
201  -> Eigen::Matrix<Scalar, kPointDim, kDof> {
202  return matrix(-point);
203  }
204 
205  static auto adOfTranslation(Point const& point)
206  -> Eigen::Matrix<Scalar, kPointDim, kDof> {
207  return matrix(-point);
208  }
209 
210  // derivatives
211  static auto ad(Tangent const& /*unused*/)
212  -> Eigen::Matrix<Scalar, kDof, kDof> {
213  return Eigen::Matrix<Scalar, kDof, kDof>::Zero();
214  }
215 
216  static auto dxExpX(Tangent const& /*unused*/)
217  -> Eigen::Matrix<Scalar, kNumParams, kDof> {
218  return Eigen::Matrix<Scalar, kNumParams, kDof>::Identity();
219  }
220 
221  static auto dxExpXAt0() -> Eigen::Matrix<Scalar, kNumParams, kDof> {
222  return Eigen::Matrix<Scalar, kNumParams, kDof>::Identity();
223  }
224 
225  static auto dxExpXTimesPointAt0(Point const& point)
226  -> Eigen::Matrix<Scalar, kPointDim, kDof> {
227  Eigen::Matrix<Scalar, kPointDim, kDof> j;
228  j.setZero();
229  j.diagonal() = point;
230  return j;
231  }
232 
233  static auto dxThisMulExpXAt0(Params const& unit_quat)
234  -> Eigen::Matrix<Scalar, kNumParams, kDof> {
235  Eigen::Matrix<Scalar, kNumParams, kDof> j;
236  j.setZero();
237  j.diagonal() = unit_quat;
238  return j;
239  }
240 
241  static auto dxLogThisInvTimesXAtThis(Params const& unit_quat)
242  -> Eigen::Matrix<Scalar, kDof, kNumParams> {
243  Eigen::Matrix<Scalar, kDof, kNumParams> j;
244  j.setZero();
245  j.diagonal() = 1.0 / unit_quat.array();
246  return j;
247  }
248 
249  // for tests
250 
251  static auto tangentExamples() -> std::vector<Tangent> {
252  if constexpr (kPointDim == 2) {
253  return std::vector<Tangent>({
254  Tangent({std::exp(1.0), std::exp(1.0)}),
255  Tangent({1.1, 1.1}),
256  Tangent({2.0, 1.1}),
257  Tangent({2.0, std::exp(1.0)}),
258  });
259  } else {
260  if constexpr (kPointDim == 3) {
261  return std::vector<Tangent>({
262  Tangent(
263  {Scalar(std::exp(1.0)),
264  Scalar(std::exp(1.0)),
265  Scalar(std::exp(1.0))}),
266  Tangent({Scalar(1.1), Scalar(1.1), Scalar(1.7)}),
267  Tangent({Scalar(2.0), Scalar(1.1), Scalar(2.0)}),
268  Tangent({Scalar(2.0), Scalar(std::exp(1.0)), Scalar(2.2)}),
269  });
270  }
271  }
272  }
273 
274  static auto paramsExamples() -> std::vector<Params> {
275  if constexpr (kPointDim == 2) {
276  return std::vector<Params>(
277  {Params({1.0, 1.0}),
278  Params({1.0, 2.0}),
279  Params({1.0, 0.5}),
280  Params({0.2, 0.5}),
281  Params({1.5, 1.0}),
282  Params({5.0, 1.237}),
283  Params({0.5, 2.0})});
284  } else {
285  if constexpr (kPointDim == 3) {
286  return std::vector<Params>(
287  {Params({Scalar(1.0), Scalar(1.0), Scalar(1.0)}),
288  Params({Scalar(1.0), Scalar(2.0), Scalar(1.05)}),
289  Params({Scalar(1.5), Scalar(1.0), Scalar(2.8)}),
290  Params({Scalar(5.0), Scalar(1.237), Scalar(2)}),
291  Params({Scalar(0.5), Scalar(1.237), Scalar(0.2)})});
292  }
293  }
294  }
295 
296  static auto invalidParamsExamples() -> std::vector<Params> {
297  return std::vector<Params>({
298  Params::Zero(),
299  -Params::Ones(),
300  -Params::UnitX(),
301  });
302  }
303 };
304 
305 } // namespace lie
306 
307 template <class TScalar, int kDim>
308 class Scaling;
309 
310 namespace lie {
311 template <int kDim>
313  template <class TScalar>
315 
316  template <class TScalar>
318 };
319 
320 } // namespace lie
321 } // namespace sophus
sophus::lie::ScalingImpl::toAmbient
static auto toAmbient(Point const &point) -> Eigen::Vector< Scalar, kAmbientDim >
Definition: scaling.h:144
sophus::lie::ScalingImpl::paramsExamples
static auto paramsExamples() -> std::vector< Params >
Definition: scaling.h:274
sophus::lie::ScalingImpl::kNumParams
static const int kNumParams
Definition: scaling.h:21
lie_group.h
sophus::lie::ScalingImpl::log
static auto log(Params const &scale_factors) -> Tangent
Definition: scaling.h:87
sophus::lie::ScalingImpl::dxExpXTimesPointAt0
static auto dxExpXTimesPointAt0(Point const &point) -> Eigen::Matrix< Scalar, kPointDim, kDof >
Definition: scaling.h:225
sophus::lie::ScalingImpl::kPointDim
static const int kPointDim
Definition: scaling.h:22
sophus::UnitVector::fromVectorAndNormalize
static auto fromVectorAndNormalize(Eigen::Matrix< TScalar, kN, 1 > const &v) -> UnitVector
Definition: unit_vector.h:180
sophus::lie::ScalingImpl::dxLogThisInvTimesXAtThis
static auto dxLogThisInvTimesXAtThis(Params const &unit_quat) -> Eigen::Matrix< Scalar, kDof, kNumParams >
Definition: scaling.h:241
sophus::lie::ScalingImpl::kIsAxisDirectionPreserving
static constexpr bool kIsAxisDirectionPreserving
Definition: scaling.h:30
sophus::lie::ScalingImpl::kIisSizePreserving
static constexpr bool kIisSizePreserving
Definition: scaling.h:33
sophus::lie::ScalingImpl::dxExpX
static auto dxExpX(Tangent const &) -> Eigen::Matrix< Scalar, kNumParams, kDof >
Definition: scaling.h:216
sophus::UnitVector
Definition: lie_group.h:14
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::lie::ScalingImpl::Params
Eigen::Vector< Scalar, kNumParams > Params
Definition: scaling.h:26
sophus::lie::ScalingImpl::identityParams
static auto identityParams() -> Params
Definition: scaling.h:53
sophus::lie::ScalingImpl::hasShortestPathAmbiguity
static auto hasShortestPathAmbiguity(Params const &) -> bool
Definition: scaling.h:78
sophus::lie::ScalingImpl::inverse
static auto inverse(Params const &scale_factors) -> Params
Definition: scaling.h:109
sophus::lie::ScalingImpl::action
static auto action(Params const &scale_factors, Eigen::Vector< TCompatibleScalar, kPointDim > const &point) -> PointReturn< TCompatibleScalar >
Definition: scaling.h:128
sophus::lie::ScalingImpl::areParamsValid
static auto areParamsValid(Params const &scale_factors) -> sophus::Expected< Success >
Definition: scaling.h:57
sophus::lie::ScalingImpl::kIsShapePreserving
static constexpr bool kIsShapePreserving
Definition: scaling.h:32
sophus::lie::ScalingImpl::compactMatrix
static auto compactMatrix(Params const &scale_factors) -> Eigen::Matrix< Scalar, kPointDim, kAmbientDim >
Definition: scaling.h:156
sophus::lie::ScalingImpl::ScalarReturn
typename Eigen::ScalarBinaryOpTraits< Scalar, TCompatibleScalar >::ReturnType ScalarReturn
Definition: scaling.h:38
sophus::lie::ScalingImpl::ad
static auto ad(Tangent const &) -> Eigen::Matrix< Scalar, kDof, kDof >
Definition: scaling.h:211
sophus::lie::ScalingWithDim
Definition: scaling.h:312
sophus::lie::ScalingImpl::matVInverse
static auto matVInverse(Params const &params, Params const &tangent) -> Eigen::Matrix< Scalar, kPointDim, kPointDim >
Definition: scaling.h:184
sophus::lie::ScalingImpl::kAmbientDim
static const int kAmbientDim
Definition: scaling.h:23
sophus::lie::ScalingImpl::vee
static auto vee(Eigen::Matrix< Scalar, kAmbientDim, kAmbientDim > const &mat) -> Eigen::Matrix< Scalar, kDof, 1 >
Definition: scaling.h:102
sophus::lie::ScalingImpl::invalidParamsExamples
static auto invalidParamsExamples() -> std::vector< Params >
Definition: scaling.h:296
sophus::lie::ScalingImpl::adjOfTranslation
static auto adjOfTranslation(Params const &params, Point const &point) -> Eigen::Matrix< Scalar, kPointDim, kDof >
Definition: scaling.h:200
sophus::lie::ScalingImpl::kDof
static const int kDof
Definition: scaling.h:20
sophus::lie::ScalingImpl::hat
static auto hat(Tangent const &scale_factors) -> Eigen::Matrix< Scalar, kAmbientDim, kAmbientDim >
Definition: scaling.h:92
SOPHUS_UNEXPECTED
#define SOPHUS_UNEXPECTED(...)
Definition: common.h:57
sophus::lie::ScalingImpl::Point
Eigen::Vector< Scalar, kPointDim > Point
Definition: scaling.h:27
unit_vector.h
sophus::lie::ScalingImpl::matrix
static auto matrix(Params const &scale_factors) -> Eigen::Matrix< Scalar, kAmbientDim, kAmbientDim >
Definition: scaling.h:161
sophus::lie::ScalingImpl::matV
static auto matV(Params const &params, Tangent const &tangent) -> Eigen::Matrix< Scalar, kPointDim, kPointDim >
Definition: scaling.h:168
sophus::lie::ScalingImpl::action
static auto action(Params const &scale_factors, UnitVector< TCompatibleScalar, kPointDim > const &direction_vector) -> UnitVectorReturn< TCompatibleScalar >
Definition: scaling.h:136
sophus::lie::ScalingImpl
Definition: scaling.h:17
sophus::lie::ScalingImpl::adOfTranslation
static auto adOfTranslation(Point const &point) -> Eigen::Matrix< Scalar, kPointDim, kDof >
Definition: scaling.h:205
sophus::lie::ScalingImpl::kIsOriginPreserving
static constexpr bool kIsOriginPreserving
Definition: scaling.h:29
sophus::lie::ScalingImpl::tangentExamples
static auto tangentExamples() -> std::vector< Tangent >
Definition: scaling.h:251
sophus::Scaling
Definition: scaling.h:308
sophus::lie::ScalingImpl::dxExpXAt0
static auto dxExpXAt0() -> Eigen::Matrix< Scalar, kNumParams, kDof >
Definition: scaling.h:221
sophus::lie::ScalingImpl::ParamsReturn
Eigen::Vector< ScalarReturn< TCompatibleScalar >, kNumParams > ParamsReturn
Definition: scaling.h:42
sophus::lie::ScalingImpl::dxThisMulExpXAt0
static auto dxThisMulExpXAt0(Params const &unit_quat) -> Eigen::Matrix< Scalar, kNumParams, kDof >
Definition: scaling.h:233
sophus::lie::ScalingImpl::adj
static auto adj(Params const &) -> Eigen::Matrix< Scalar, kDof, kDof >
Definition: scaling.h:149
sophus::lie::ScalingImpl::Scalar
TScalar Scalar
Definition: scaling.h:19
sophus::lie::ScalingImpl::Tangent
Eigen::Vector< Scalar, kDof > Tangent
Definition: scaling.h:25
sophus::lie::ScalingImpl::multiplication
static auto multiplication(Params const &lhs_params, Eigen::Vector< TCompatibleScalar, kPointDim > const &rhs_params) -> ParamsReturn< TCompatibleScalar >
Definition: scaling.h:118
sophus::lie::ScalingImpl::exp
static auto exp(Tangent const &log_scale_factors) -> Params
Definition: scaling.h:82
sophus::lie::ScalingImpl::kIisParallelLinePreserving
static constexpr bool kIisParallelLinePreserving
Definition: scaling.h:34
sophus::lie::ScalingImpl::kIsDirectionVectorPreserving
static constexpr bool kIsDirectionVectorPreserving
Definition: scaling.h:31
sophus::lie::ScalingImpl::PointReturn
Eigen::Vector< ScalarReturn< TCompatibleScalar >, kPointDim > PointReturn
Definition: scaling.h:45