diff --git a/bindings/SofaTypes/CMakeLists.txt b/bindings/SofaTypes/CMakeLists.txt index d725579fa..4b50f338e 100644 --- a/bindings/SofaTypes/CMakeLists.txt +++ b/bindings/SofaTypes/CMakeLists.txt @@ -4,6 +4,7 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Mat.h ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Quat.h ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Vec.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Transform.h ) set(SOURCE_FILES @@ -11,6 +12,7 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Mat.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Quat.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Vec.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Transform.cpp ) SP3_add_python_package( diff --git a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp index 21441f172..5addacf22 100644 --- a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp +++ b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp @@ -129,22 +129,10 @@ void moduleAddQuat(py::module &m) { }); p.def("__str__", [](Quat &self) { - std::string s("("); - s += std::to_string(self[0]) - + ", " + std::to_string(self[1]) - + ", " + std::to_string(self[2]) - + ", " + std::to_string(self[3]) - + ")"; - return s; + return pyQuat::__str__(self, false); }); p.def("__repr__", [](Quat &self) { - std::string s("Quat("); - s += std::to_string(self[0]) - + ", " + std::to_string(self[1]) - + ", " + std::to_string(self[2]) - + ", " + std::to_string(self[3]) - + ")"; - return s; + return pyQuat::__str__(self, true); }); } diff --git a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h index 38bffd501..6450c4ec3 100644 --- a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h +++ b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h @@ -28,3 +28,24 @@ using namespace pybind11::literals; #include void moduleAddQuat(py::module& m); + +namespace pyQuat +{ +template +std::string __str__(const sofa::type::Quat &self, bool repr = false) +{ + std::string s; + if (repr) + { + s += "Quat"; + } + s += "("; + s += std::to_string(self[0]) + + ", " + std::to_string(self[1]) + + ", " + std::to_string(self[2]) + + ", " + std::to_string(self[3]) + + ")"; + return s; +} + +} // namespace pyQuat diff --git a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.cpp b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.cpp new file mode 100644 index 000000000..816bcd71d --- /dev/null +++ b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.cpp @@ -0,0 +1,68 @@ +/****************************************************************************** +* SofaPython3 plugin * +* (c) 2021 CNRS, University of Lille, INRIA * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include +#include +#include +#include + +namespace pyTransform +{ +template +std::string __str__(const sofa::type::Transform& self, bool repr) +{ + std::string s; + if (repr) + { + s += "Transform" + sofa::defaulttype::DataTypeInfo::name(); + } + s += "("; + s += pyVec::__str__(self.getOrigin(), repr); + s += std::string(", "); + s += pyQuat::__str__(self.getOrientation(), repr); + s += ")"; + return s; +} +} + +namespace sofapython3::SofaTypes +{ + +template +void addTransform(py::module& m) +{ + const auto className = "Transform" + sofa::defaulttype::DataTypeInfo::name(); + py::class_> p(m, className.c_str()); + + p.def("__str__", [](sofa::type::Transform& self) + { + return pyTransform::__str__(self, false); + }); + p.def("__repr__", [](sofa::type::Transform& self) + { + return pyTransform::__str__(self, false); + }); +} + +void moduleAddTransform(py::module& m) +{ + addTransform(m); +} + +} diff --git a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.h b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.h new file mode 100644 index 000000000..f49d5bce9 --- /dev/null +++ b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.h @@ -0,0 +1,37 @@ +/****************************************************************************** +* SofaPython3 plugin * +* (c) 2021 CNRS, University of Lille, INRIA * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +namespace py = pybind11; + +#include +#include "Binding_Quat.h" + +namespace sofapython3::SofaTypes +{ +void moduleAddTransform(py::module &m); +} + +namespace pyTransform +{ +template +std::string __str__(const sofa::type::Transform& self, bool repr); +} diff --git a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp index e6a55a833..a0c26302c 100644 --- a/bindings/SofaTypes/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp +++ b/bindings/SofaTypes/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include /// The first parameter must be named the same as the module file to load. @@ -32,4 +33,5 @@ PYBIND11_MODULE(SofaTypes, m) { moduleAddMat(m); moduleAddQuat(m); moduleAddVec(m); + sofapython3::SofaTypes::moduleAddTransform(m); }