File: bindings_tangent_base.h

package info (click to toggle)
manif 0.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 11,789; ansic: 8,774; python: 2,158; sh: 24; makefile: 23; xml: 21
file content (397 lines) | stat: -rw-r--r-- 9,997 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#ifndef _MANIF_PYTHON_BINDINGS_TANGENT_BASE_H_
#define _MANIF_PYTHON_BINDINGS_TANGENT_BASE_H_

template <typename _Tangent, typename... _Args>
void wrap_tangent_base(pybind11::class_<_Tangent, _Args...>& py_class) {

  using Scalar = typename _Tangent::Scalar;
  using LieGroup = typename _Tangent::LieGroup;
  using DataType = typename _Tangent::DataType;
  using Jacobian = typename _Tangent::Jacobian;
  using OptJacobianRef = typename _Tangent::OptJacobianRef;

  py_class.attr("Dim") = _Tangent::Dim;
  py_class.attr("DoF") = _Tangent::DoF;
  py_class.attr("RepSize") = _Tangent::RepSize;

  py_class.def(
    pybind11::init<>(),
    "Default constructor, uninitialized data."
  );

  py_class.def(
    pybind11::init<const DataType&>(),
    "Constructor given data vector."
  );

  py_class.def(
    "coeffs_copy",
    static_cast<DataType& (_Tangent::*)(void)>(&_Tangent::coeffs),
    "Return a copy of underlying data."
  ); // Makes a copy!

  py_class.def(
    "coeffs",
    static_cast<DataType& (_Tangent::*)(void)>(&_Tangent::coeffs),
    pybind11::return_value_policy::reference_internal,
    "Get a reference to underlying data."
  );

  py_class.def(
    "generator",
    &_Tangent::generator,
    pybind11::arg("i"),
    "Get the ith basis element of the Lie Algebra."
  );

  py_class.def(
    "innerWeights",
    &_Tangent::innerWeights,
    "Get the weight matrix of the Weighted Euclidean inner product, "
    "relative to the space basis."
  );

  py_class.def(
    "inner",
    &_Tangent::template inner<_Tangent>,
    pybind11::arg("other"),
    R"(
      Get inner product of this and another Tangent weighted by W.

      ret = self^T x W x other
    )"
  );

  py_class.def(
    "weightedNorm",
    &_Tangent::weightedNorm,
    "Get the Euclidean weighted norm."
  );

  py_class.def(
    "squaredWeightedNorm",
    &_Tangent::squaredWeightedNorm,
    "Get the squared Euclidean weighted norm."
  );

  py_class.def(
    "hat",
    &_Tangent::hat,
    R"(
      Get the isomorphic element in the Lie algebra.

      See Eq. (10).
    )"
  );

  py_class.def(
    "exp",
    &_Tangent::exp,
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    R"(
      Get the corresponding Lie group element.

      Eq. (23).

      Parameters
      ----------
      J_out_self [out] : numpy.ndarray
        Jacobian of the log wrt self.
    )"
  );

  py_class.def(
    "rplus",
    &_Tangent::rplus,
    pybind11::arg("state"),
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    pybind11::arg_v("J_out_state", OptJacobianRef(), "None"),
    R"(
      Right oplus operation of the Lie group.

      See Eqs. (25).

      Parameters
      ----------
      other : Lie group
        Another object of the same Lie group.
      J_out_self [out] : numpy.ndarray
        Jacobian of the oplus operation wrt self.
      J_out_state [out] : numpy.ndarray
        Jacobian of the oplus operation wrt state.
    )"
  );

  py_class.def(
    "lplus",
    &_Tangent::lplus,
    pybind11::arg("state"),
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    pybind11::arg_v("J_out_state", OptJacobianRef(), "None"),
    R"(
      Left oplus operation of the Lie group.

      See Eqs. (27).

      Parameters
      ----------
      other : Lie group
        Another object of the same Lie group.
      J_out_self [out] : numpy.ndarray
        Jacobian of the oplus operation wrt self.
      J_out_state [out] : numpy.ndarray
        Jacobian of the oplus operation wrt state.
    )"
  );

  py_class.def(
    "plus",
    static_cast<LieGroup (_Tangent::*)(const LieGroup&, OptJacobianRef, OptJacobianRef) const>(&_Tangent::plus),
    pybind11::arg("state"),
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    pybind11::arg_v("J_out_state", OptJacobianRef(), "None"),
    "An alias for the 'rplus' function."
  );

  py_class.def(
    "plus",
    &_Tangent::template plus<_Tangent>,
    pybind11::arg("other"),
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    pybind11::arg_v("J_out_other", OptJacobianRef(), "None"),
    R"(
      Plus operation in the vector space.

      Parameters
      ----------
      other : Lie group tangent
        Another object of the same Lie group tangent.
      J_out_self [out] : numpy.ndarray
        Jacobian of the oplus operation wrt self.
      J_out_other [out] : numpy.ndarray
        Jacobian of the oplus operation wrt other.
    )"
  );

  py_class.def(
    "minus",
    &_Tangent::template minus<_Tangent>,
    pybind11::arg("other"),
    pybind11::arg_v("J_out_self", OptJacobianRef(), "None"),
    pybind11::arg_v("J_out_other", OptJacobianRef(), "None"),
    R"(
      Minus operation in the vector space.

      Parameters
      ----------
      other : Lie group tangent
        Another object of the same Lie group tangent.
      J_out_self [out] : numpy.ndarray
        Jacobian of the oplus operation wrt self.
      J_out_other [out] : numpy.ndarray
        Jacobian of the oplus operation wrt other.
    )"
  );

  py_class.def(
    "rjac",
    &_Tangent::rjac,
    R"(
      Get the right Jacobian.

      This is the right Jacobian of 'exp',
      what is commonly known as "the right Jacobian".

      See Eq. (41) for the right Jacobian of general functions.
      See Eqs. (126,143,163,179,191) for implementations of the
      right Jacobian of exp.
    )"
  );

  py_class.def(
    "ljac",
    &_Tangent::ljac,
    R"(
      Get the left Jacobian.

      This is the left Jacobian of 'exp',
      what is commonly known as "the left Jacobian".

      See Eq. (44) for the left Jacobian of general functions.
      See Eqs. (126,145,164,179,191) for implementations of the
      left Jacobian of exp.
    )"
  );

  // py_class.def("rjacinv", &_Tangent::rjacinv);
  // py_class.def("ljacinv", &_Tangent::ljacinv);

  py_class.def("smallAdj", &_Tangent::smallAdj);

  py_class.def(
    "isApprox",
    [](const _Tangent& self, const _Tangent& t, Scalar eps) {
      return self.isApprox(t, eps);
    },
    pybind11::arg("other"),
    pybind11::arg_v("eps", Scalar(manif::Constants<Scalar>::eps), "1e-10"),
    R"(
      Evaluate whether self and other are 'close'.

      Parameters
      ----------
      other : Lie group tangent
        Another object of the same Lie group tangent.
      eps : double
        Threshold for equality comparison. Default: 1e-10.
    )"
  );

  py_class.def(
    "isApprox",
    [](const _Tangent& self, const DataType& t, Scalar eps) {
      return self.isApprox(t, eps);
    },
    pybind11::arg("other"),
    pybind11::arg_v("eps", Scalar(manif::Constants<Scalar>::eps), "1e-10"),
    R"(
      Evaluate whether self and other are 'close'.

      Parameters
      ----------
      other : numpy.array
        Another object of the same Lie group tangent.
      eps : double
        Threshold for equality comparison. Default: 1e-10.
    )"
  );

  py_class.def(
    "setZero",
    &_Tangent::setZero,
    "Set self to zero."
  );

  py_class.def(
    "setRandom",
    &_Tangent::setRandom,
    "Set self to a random value."
  );

  py_class.def_static(
    "Zero",
    &_Tangent::Zero,
    "Static helper to create an object of the Lie group tangent set to zero."
  );

  py_class.def_static(
    "Random",
    &_Tangent::Random,
    "Static helper to create a random object of the Lie group."
  );

  py_class.def_static(
    "Generator",
    &_Tangent::Generator,
    pybind11::arg("i"),
    "Static helper to get the ith basis element of the Lie Algebra."
  );

  py_class.def_static(
    "InnerWeights",
    &_Tangent::InnerWeights,
    "Static helper to get the weight matrix of the "
    "Weighted Euclidean inner product, "
    "relative to the space basis."
  );

  // operator overloads
  py_class.def(-pybind11::self)
    .def(
      pybind11::self + LieGroup(),
      "Operator overload for the 'plus' function."
    )
    .def(
      pybind11::self + pybind11::self,
      "Operator overload for the 'plus' function."
    )
    // .def(pybind11::self += pybind11::self)
    .def(
      pybind11::self - pybind11::self,
      "Operator overload for the 'minus' function."
    )
    // .def(pybind11::self -= pybind11::self)
    .def(
      pybind11::self * Scalar(),
      "Multiply the vector by a scalar."
    )
    .def(
      Scalar() * pybind11::self,
      "Multiply the vector by a scalar."
    )
    .def(
      pybind11::self / Scalar(),
      "Divide the vector by a scalar."
    )
    .def(
      pybind11::self == pybind11::self,
      "Operator overload for the 'isApprox' function."
    )
    ;

  // Jacobian() @ pybind11::self
  py_class.def(
    "__rmatmul__",
    [](const _Tangent& t, pybind11::array_t<Scalar> lhs) {

    pybind11::buffer_info lhs_buf = lhs.request();

    if (lhs_buf.ndim != 2)
        throw std::runtime_error("Number of dimensions must be 2");

    if (lhs_buf.size != _Tangent::DoF * _Tangent::DoF)
        throw std::runtime_error("Input shapes must match");

    _Tangent result = Eigen::Map<Jacobian>(static_cast<Scalar*>(lhs_buf.ptr)) * t;

    return result;

    },
    pybind11::is_operator()
  );

  // Jacobian() * pybind11::self
  py_class.def(
    "__rmul__",
    [](const _Tangent& t, pybind11::array_t<Scalar> lhs) {

    pybind11::buffer_info lhs_buf = lhs.request();

    // if (lhs_buf.ndim != 2)
        // throw std::runtime_error("Number of dimensions must be 2");

    if (lhs_buf.size != _Tangent::DoF * _Tangent::DoF)
        throw std::runtime_error("Input shapes must match");

    _Tangent result = Eigen::Map<Jacobian>(static_cast<Scalar*>(lhs_buf.ptr)) * t;

    return result;

    },
    pybind11::is_operator()
  );

  // This is necessary to 'override' numpy's ndarray operators
  // with the __*mul*__ operator above
  py_class.attr("__array_priority__") = 10000;

  py_class.def(
    "__str__",
    [](const _Tangent &t) {
      std::ostringstream ss; ss << t;
      return ss.str();
    }
  );
}

#endif // _MANIF_PYTHON_BINDINGS_TANGENT_BASE_H_