File: Writing-generic-code.md

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 (72 lines) | stat: -rw-r--r-- 1,594 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
# Writing generic code

- [Writing generic code](#writing-generic-code)
  - [Examples](#examples)
    - [Small example](#small-example)
    - [Multiple templated arguments](#multiple-templated-arguments)

All Lie group classes defined in `manif` have in common that they inherit from a templated base class.
Therefore, template-based generic code can be written - similarly to Eigen.

## Examples

### Small example

Let us write a simple function that take any group object and prints some information about it,

```cpp
#include <iostream>
#include <manif/manif.h>

using namespace manif;

template <typename Derived>
void print(const LieGroupBase<Derived>& g)
{
  std::cout << "Degrees of freedom: " << g::DoF << "\n"
            << "Underlying representation vector size: " << g::RepSize << "\n"
            << "Current values: " << g << "\n;
}

int main()
{
  SE2d p_2d;
  print(p_2d);

  SE3d p_3d;
  print(p_3d);
}
```

### Multiple templated arguments

Let us write a function that takes two group objects and performs some computation,

```cpp
#include <manif/manif.h>

using namespace manif;

template <typename DerivedA, typename DerivedB>
typename DerivedA::Scalar
ominusSquaredWeightedNorm(
  const LieGroupBase<DerivedA>& state,
  const LieGroupBase<DerivedB>& state_other
)
{
  return (state-state_other).squaredWeightedNorm();
}

int main()
{
  SE2d state = SE2d::Random();

  SE2d::DataType state_other_data = SE2d::DataType::Random();

  Eigen::Map<SE2d> state_other_map(state_other_data.data());

  double osn = ominusSquaredWeightedNorm(state, state_other_map);

  ...
}
```