Skip to content

Commit fd7df3b

Browse files
committed
Add bindings for type Transform
1 parent 5f97936 commit fd7df3b

File tree

6 files changed

+132
-14
lines changed

6 files changed

+132
-14
lines changed

bindings/SofaTypes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ set(HEADER_FILES
44
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Mat.h
55
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Quat.h
66
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Vec.h
7+
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Transform.h
78
)
89

910
set(SOURCE_FILES
1011
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Mat.cpp
1213
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Quat.cpp
1314
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Vec.cpp
15+
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaTypes/Binding_Transform.cpp
1416
)
1517

1618
SP3_add_python_package(

bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,10 @@ void moduleAddQuat(py::module &m) {
129129
});
130130

131131
p.def("__str__", [](Quat &self) {
132-
std::string s("(");
133-
s += std::to_string(self[0])
134-
+ ", " + std::to_string(self[1])
135-
+ ", " + std::to_string(self[2])
136-
+ ", " + std::to_string(self[3])
137-
+ ")";
138-
return s;
132+
return pyQuat::__str__(self, false);
139133
});
140134
p.def("__repr__", [](Quat &self) {
141-
std::string s("Quat(");
142-
s += std::to_string(self[0])
143-
+ ", " + std::to_string(self[1])
144-
+ ", " + std::to_string(self[2])
145-
+ ", " + std::to_string(self[3])
146-
+ ")";
147-
return s;
135+
return pyQuat::__str__(self, true);
148136
});
149137

150138
}

bindings/SofaTypes/src/SofaPython3/SofaTypes/Binding_Quat.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ using namespace pybind11::literals;
2828
#include <sofa/type/Quat.h>
2929

3030
void moduleAddQuat(py::module& m);
31+
32+
namespace pyQuat
33+
{
34+
template <class T>
35+
std::string __str__(const sofa::type::Quat<T> &self, bool repr = false)
36+
{
37+
std::string s;
38+
if (repr)
39+
{
40+
s += "Quat";
41+
}
42+
s += "(";
43+
s += std::to_string(self[0])
44+
+ ", " + std::to_string(self[1])
45+
+ ", " + std::to_string(self[2])
46+
+ ", " + std::to_string(self[3])
47+
+ ")";
48+
return s;
49+
}
50+
51+
} // namespace pyQuat
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
#include <SofaPython3/SofaTypes/Binding_Transform.h>
21+
#include <sofa/defaulttype/typeinfo/DataTypeInfo.h>
22+
#include <SofaPython3/SofaTypes/Binding_Vec.h>
23+
#include <SofaPython3/SofaTypes/Binding_Quat.h>
24+
25+
namespace pyTransform
26+
{
27+
template<class TReal>
28+
std::string __str__(const sofa::type::Transform<TReal>& self, bool repr)
29+
{
30+
std::string s;
31+
if (repr)
32+
{
33+
s += "Transform" + sofa::defaulttype::DataTypeInfo<TReal>::name();
34+
}
35+
s += "(";
36+
s += pyVec::__str__(self.getOrigin(), repr);
37+
s += std::string(", ");
38+
s += pyQuat::__str__(self.getOrientation(), repr);
39+
s += ")";
40+
return s;
41+
}
42+
}
43+
44+
namespace sofapython3::SofaTypes
45+
{
46+
47+
template<class TReal>
48+
void addTransform(py::module& m)
49+
{
50+
const auto className = "Transform" + sofa::defaulttype::DataTypeInfo<TReal>::name();
51+
py::class_<sofa::type::Transform<TReal>> p(m, className.c_str());
52+
53+
p.def("__str__", [](sofa::type::Transform<TReal>& self)
54+
{
55+
return pyTransform::__str__(self, false);
56+
});
57+
p.def("__repr__", [](sofa::type::Transform<TReal>& self)
58+
{
59+
return pyTransform::__str__(self, false);
60+
});
61+
}
62+
63+
void moduleAddTransform(py::module& m)
64+
{
65+
addTransform<SReal>(m);
66+
}
67+
68+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
#pragma once
21+
22+
#include <pybind11/pybind11.h>
23+
namespace py = pybind11;
24+
25+
#include <sofa/type/Transform.h>
26+
#include "Binding_Quat.h"
27+
28+
namespace sofapython3::SofaTypes
29+
{
30+
void moduleAddTransform(py::module &m);
31+
}
32+
33+
namespace pyTransform
34+
{
35+
template<class TReal>
36+
std::string __str__(const sofa::type::Transform<TReal>& self, bool repr);
37+
}

bindings/SofaTypes/src/SofaPython3/SofaTypes/Module_SofaTypes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <SofaPython3/SofaTypes/Binding_Mat.h>
2424
#include <SofaPython3/SofaTypes/Binding_Quat.h>
2525
#include <SofaPython3/SofaTypes/Binding_Vec.h>
26+
#include <SofaPython3/SofaTypes/Binding_Transform.h>
2627
#include <sofa/defaulttype/init.h>
2728

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

0 commit comments

Comments
 (0)