-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PyBind setters for Cpp implementations - motion_state
- Loading branch information
1 parent
f69a916
commit 7dcad8e
Showing
5 changed files
with
140 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "py_motion_state.hpp" | ||
#include <iostream> | ||
#include <memory> | ||
#include <glog/logging.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
using namespace p3iv_types; | ||
namespace py = pybind11; | ||
|
||
|
||
// The module name here *must* match the name of the python project. You can use the PYTHON_API_MODULE_NAME definition. | ||
PYBIND11_MODULE(PYTHON_API_MODULE_NAME, m) { | ||
m.doc() = R"pbdoc( | ||
Python bindings for C++ Implementation | ||
----------------------- | ||
.. currentmodule:: PYTHON_API_MODULE_NAME | ||
.. autosummary:: | ||
:toctree: _generate | ||
)pbdoc"; | ||
|
||
py::class_<PyMotionState>(m, "PyMotionState", py::module_local()) | ||
.def(py::init<>()) | ||
.def("setPosition", &PyMotionState::setPosition, py::arg("mean"), py::arg("covariance")) | ||
.def("setYaw", &PyMotionState::setYaw, py::arg("mean"), py::arg("covariance")) | ||
.def("setVelocity", &PyMotionState::setVelocity, py::arg("mean"), py::arg("covariance")); | ||
|
||
py::class_<PyMotionStateArray>(m, "PyMotionStateArray", py::module_local()) | ||
.def(py::init<>()) | ||
.def("setPosition", &PyMotionStateArray::setPosition, py::arg("mean"), py::arg("covariance")) | ||
.def("setYaw", &PyMotionStateArray::setYaw, py::arg("mean"), py::arg("covariance")) | ||
.def("setVelocity", &PyMotionStateArray::setVelocity, py::arg("mean"), py::arg("covariance")); | ||
|
||
|
||
#ifdef VERSION_INFO | ||
m.attr("__version__") = VERSION_INFO; | ||
#else | ||
m.attr("__version__") = "dev"; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
#include <pybind11/pybind11.h> | ||
#include "motion_state.hpp" | ||
#include "pybind_utils.hpp" | ||
|
||
namespace py = pybind11; | ||
using PyArray = py::array_t<double>; | ||
|
||
namespace p3iv_types { | ||
|
||
using namespace pybind_utils; | ||
|
||
struct PyMotionState : MotionState { | ||
|
||
void setPosition(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->position.setMean(m[0], m[1]); | ||
this->position.setCovariance(c[0], c[1], c[2], c[3]); | ||
} | ||
|
||
void setYaw(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->yaw.setMean(m[0]); | ||
this->yaw.setCovariance(c[0]); | ||
} | ||
|
||
void setVelocity(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->velocity.setMean(m[0], m[1]); | ||
this->velocity.setCovariance(c[0], c[1], c[2], c[3]); | ||
} | ||
}; | ||
|
||
|
||
struct PyMotionStateArray : MotionStateArray { | ||
|
||
void setPosition(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->position.setMean(m); | ||
this->position.setCovariance(c); | ||
} | ||
|
||
void setYaw(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->yaw.setMean(m); | ||
this->yaw.setCovariance(c); | ||
} | ||
|
||
void setVelocity(PyArray mean, PyArray covariance) { | ||
std::vector<double> m = pyArray2Vector<double>(mean); | ||
std::vector<double> c = pyArray2Vector<double>(covariance); | ||
this->velocity.setMean(m); | ||
this->velocity.setCovariance(c); | ||
} | ||
}; | ||
|
||
} // namespace p3iv_types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import unittest | ||
import numpy as np | ||
from p3iv_types.py_motion_state import PyMotionState, PyMotionStateArray | ||
|
||
|
||
class PyMotionStateTest(unittest.TestCase): | ||
def test_empty(self): | ||
m = PyMotionState() | ||
mean = np.asarray([4.0, 0.0]) | ||
cov = np.asarray([16.0, 0.0, 0.0, 16.0]) | ||
m.setPosition(mean, cov) | ||
m.setYaw(mean[:1], cov[:1]) | ||
m.setVelocity(mean, cov) | ||
|
||
|
||
class PyMotionStateArrayTest(unittest.TestCase): | ||
def test_empty(self): | ||
PyMotionStateArray() | ||
|
||
def test_mp(self): | ||
m = PyMotionStateArray() | ||
mean = np.asarray([4.0, 0.0, 4.0, 0.0]) | ||
cov = np.asarray([16.0, 0.0, 0.0, 16.0, 16.0, 0.0, 0.0, 16.0]) | ||
m.setPosition(mean, cov) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |