Skip to content

Commit

Permalink
Add bindings for type Transform
Browse files Browse the repository at this point in the history
  • Loading branch information
alxbilger committed Mar 12, 2024
1 parent 5f97936 commit fd7df3b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 14 deletions.
2 changes: 2 additions & 0 deletions bindings/SofaTypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp
${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(
Expand Down
16 changes: 2 additions & 14 deletions bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

}
21 changes: 21 additions & 0 deletions bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ using namespace pybind11::literals;
#include <sofa/type/Quat.h>

void moduleAddQuat(py::module& m);

namespace pyQuat
{
template <class T>
std::string __str__(const sofa::type::Quat<T> &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
68 changes: 68 additions & 0 deletions bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/
#include <SofaPython3/SofaTypes/Binding_Transform.h>
#include <sofa/defaulttype/typeinfo/DataTypeInfo.h>
#include <SofaPython3/SofaTypes/Binding_Vec.h>
#include <SofaPython3/SofaTypes/Binding_Quat.h>

namespace pyTransform
{
template<class TReal>
std::string __str__(const sofa::type::Transform<TReal>& self, bool repr)
{
std::string s;
if (repr)
{
s += "Transform" + sofa::defaulttype::DataTypeInfo<TReal>::name();
}
s += "(";
s += pyVec::__str__(self.getOrigin(), repr);
s += std::string(", ");
s += pyQuat::__str__(self.getOrientation(), repr);
s += ")";
return s;
}
}

namespace sofapython3::SofaTypes
{

template<class TReal>
void addTransform(py::module& m)
{
const auto className = "Transform" + sofa::defaulttype::DataTypeInfo<TReal>::name();
py::class_<sofa::type::Transform<TReal>> p(m, className.c_str());

p.def("__str__", [](sofa::type::Transform<TReal>& self)
{
return pyTransform::__str__(self, false);
});
p.def("__repr__", [](sofa::type::Transform<TReal>& self)
{
return pyTransform::__str__(self, false);
});
}

void moduleAddTransform(py::module& m)
{
addTransform<SReal>(m);
}

}
37 changes: 37 additions & 0 deletions bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Transform.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/
#pragma once

#include <pybind11/pybind11.h>
namespace py = pybind11;

#include <sofa/type/Transform.h>
#include "Binding_Quat.h"

namespace sofapython3::SofaTypes
{
void moduleAddTransform(py::module &m);
}

namespace pyTransform
{
template<class TReal>
std::string __str__(const sofa::type::Transform<TReal>& self, bool repr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <SofaPython3/SofaTypes/Binding_Mat.h>
#include <SofaPython3/SofaTypes/Binding_Quat.h>
#include <SofaPython3/SofaTypes/Binding_Vec.h>
#include <SofaPython3/SofaTypes/Binding_Transform.h>
#include <sofa/defaulttype/init.h>

/// The first parameter must be named the same as the module file to load.
Expand All @@ -32,4 +33,5 @@ PYBIND11_MODULE(SofaTypes, m) {
moduleAddMat(m);
moduleAddQuat(m);
moduleAddVec(m);
sofapython3::SofaTypes::moduleAddTransform(m);
}

0 comments on commit fd7df3b

Please sign in to comment.