Skip to content

Commit c758dcc

Browse files
committed
Add more doc for usage
1 parent 5a5ebbe commit c758dcc

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,79 @@ These features were finally added to eigenpy with a lot of developer effort. Thi
1313

1414
## Features
1515

16+
**nanoeigenpy** provides the following features for helping you bind features from Eigen to Python:
17+
1618
- bindings for Eigen's [Geometry module](https://libeigen.gitlab.io/docs/group__Geometry__Module.html) - quaternions, angle-axis representations...
1719
- bindings for Eigen's matrix dense and sparse decompositions and solvers
1820

21+
## Example usage
22+
23+
The features included in **nanoeigenpy** are distributed in a Python module which can be imported, or through standalone headers which can be included in your own Python bindings code using a CMake target.
24+
25+
### Using the nanoeigenpy headers (with CMake)
26+
27+
To directly use the tools in **nanoeigenpy**'s headers, link to it in CMake (or whichever build tool you have, but only CMake support is planned so far).
28+
29+
```cmake
30+
# look for the nanoeigenpy CMake package
31+
find_package(nanoeigenpy REQUIRED)
32+
33+
nanobind_add_module(my_ext NB_STATIC my_ext.cpp)
34+
target_link_libraries(my_ext PRIVATE nanoeigenpy::nanoeigenpy_headers)
35+
```
36+
37+
Then, in your C++ extension module code, include the relevant headers and call functions to expose the required type:
38+
39+
```cpp
40+
#include <nanoeigenpy/geometry/quaternion.hpp>
41+
42+
namespace nb = nanobind;
43+
44+
void f(const Eigen::Quaterniond &quat) {
45+
// ...
46+
}
47+
48+
NB_MODULE(my_ext, m) {
49+
nanoeigenpy::exposeQuaternion<double>(m, "Quaternion");
50+
m.def("f", f, nb::arg("quat"));
51+
}
52+
```
53+
54+
### Using the compiled Python module
55+
56+
In the case above, **nanoeigenpy**'s Python extension module already includes bindings for `Eigen::Quaternion` with the `double` scalar type (AKA `Eigen::Quaterniond`). Then, we can simply get nanobind to import it in our extension module:
57+
58+
```cpp
59+
#include <Eigen/Geometry>
60+
61+
namespace nb = nanobind;
62+
63+
void f(const Eigen::Quaterniond &quat) {
64+
// ...
65+
}
66+
67+
NB_MODULE(my_ext, m) {
68+
// import nanoeigenpy's module **here**
69+
nb::module_::import_("nanoeigenpy");
70+
m.def("f", f, nb::arg("quat"));
71+
}
72+
```
73+
74+
Alternatively, Python code which uses our extension `my_ext` can also bring in **nanoeigenpy**:
75+
76+
```python
77+
import nanoeigenpy
78+
from nanoeigenpy import Quaternion
79+
from my_ext import f
80+
81+
quat = Quaternion(0., 1., 0., 0.)
82+
f(quat)
83+
```
84+
85+
> [!NOTE]
86+
> If you have a specific scalar type (e.g. `float16`) with which you want to use `Eigen::Quaternion`, or matrix solvers, or other features in **nanoeigenpy**, you should refer to the first approach and use **nanoeigenpy** from C++ directly.
87+
88+
1989
## Installation
2090

2191
### Dependencies
@@ -28,3 +98,11 @@ These features were finally added to eigenpy with a lot of developer effort. Thi
2898
```bash
2999
conda install -c conda-forge nanobind eigen # or mamba install
30100
```
101+
102+
#### Building
103+
104+
```bash
105+
cmake -S . -B build/ -DCMAKE_INSTALL_PREFIX=<your-prefix> # prefix can be e.g. $CONDA_PREFIX
106+
cd build/
107+
cmake --build . --target install
108+
```

0 commit comments

Comments
 (0)