|
| 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 | + |
| 21 | +#include <SofaPython3/Sofa/Core/Binding_MultiVecId.h> |
| 22 | +#include <sofa/core/MultiVecId.h> |
| 23 | +#include <sofa/core/BaseState.h> |
| 24 | + |
| 25 | +namespace py { using namespace pybind11; } |
| 26 | +using namespace sofa::core; |
| 27 | + |
| 28 | +namespace sofapython3 { |
| 29 | + |
| 30 | +template <VecType vtype, VecAccess vaccess> |
| 31 | +void bindMultiVecId(py::module& m, const std::string & name) { |
| 32 | + using MultiVecId = TMultiVecId<vtype, vaccess>; |
| 33 | + py::class_<MultiVecId> c (m, name.c_str()); |
| 34 | + |
| 35 | + // Constructors |
| 36 | + c.def(py::init()); |
| 37 | + |
| 38 | + // Read-only can be constructed from both read-only and read-write multi vectors |
| 39 | + if constexpr (vaccess == V_READ) { |
| 40 | + |
| 41 | + c.def(py::init<const TVecId<vtype, V_WRITE> &>()); |
| 42 | + py::implicitly_convertible<TVecId<vtype, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 43 | + |
| 44 | + c.def(py::init<const TMultiVecId<vtype, V_WRITE> &>()); |
| 45 | + py::implicitly_convertible<TMultiVecId<vtype, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 46 | + |
| 47 | + if constexpr(vtype != V_ALL) { |
| 48 | + c.def(py::init<const TMultiVecId<V_ALL, V_WRITE> &>()); |
| 49 | + py::implicitly_convertible<TMultiVecId<V_ALL, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 50 | + } else { |
| 51 | + c.def(py::init<const TMultiVecId<V_COORD, V_WRITE> &>()); |
| 52 | + py::implicitly_convertible<TMultiVecId<V_COORD, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 53 | + |
| 54 | + c.def(py::init<const TMultiVecId<V_DERIV, V_WRITE> &>()); |
| 55 | + py::implicitly_convertible<TMultiVecId<V_DERIV, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 56 | + |
| 57 | + c.def(py::init<const TMultiVecId<V_MATDERIV, V_WRITE> &>()); |
| 58 | + py::implicitly_convertible<TMultiVecId<V_MATDERIV, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 59 | + |
| 60 | + c.def(py::init<const TVecId<V_COORD, V_WRITE> &>()); |
| 61 | + py::implicitly_convertible<TVecId<V_COORD, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 62 | + |
| 63 | + c.def(py::init<const TVecId<V_DERIV, V_WRITE> &>()); |
| 64 | + py::implicitly_convertible<TVecId<V_DERIV, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 65 | + |
| 66 | + c.def(py::init<const TVecId<V_MATDERIV, V_WRITE> &>()); |
| 67 | + py::implicitly_convertible<TVecId<V_MATDERIV, V_WRITE>, TMultiVecId<vtype, vaccess>>(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Construct from the same access type |
| 72 | + c.def(py::init<const TVecId<vtype, vaccess>&>()); |
| 73 | + py::implicitly_convertible<TVecId<vtype, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 74 | + |
| 75 | + c.def(py::init<const TMultiVecId<vtype, vaccess>&>()); |
| 76 | + |
| 77 | + // Construct from a ALL vector type |
| 78 | + if constexpr(vtype != V_ALL) { |
| 79 | + c.def(py::init<const TMultiVecId<V_ALL, vaccess> &>()); |
| 80 | + py::implicitly_convertible<TMultiVecId<V_ALL, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 81 | + } else { |
| 82 | + c.def(py::init<const TVecId<V_COORD, vaccess> &>()); |
| 83 | + py::implicitly_convertible<TVecId<V_COORD, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 84 | + |
| 85 | + c.def(py::init<const TMultiVecId<V_COORD, vaccess> &>()); |
| 86 | + py::implicitly_convertible<TMultiVecId<V_COORD, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 87 | + |
| 88 | + c.def(py::init<const TVecId<V_DERIV, vaccess> &>()); |
| 89 | + py::implicitly_convertible<TVecId<V_DERIV, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 90 | + |
| 91 | + c.def(py::init<const TMultiVecId<V_DERIV, vaccess> &>()); |
| 92 | + py::implicitly_convertible<TMultiVecId<V_DERIV, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 93 | + |
| 94 | + c.def(py::init<const TVecId<V_MATDERIV, vaccess> &>()); |
| 95 | + py::implicitly_convertible<TVecId<V_MATDERIV, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 96 | + |
| 97 | + c.def(py::init<const TMultiVecId<V_MATDERIV, vaccess> &>()); |
| 98 | + py::implicitly_convertible<TMultiVecId<V_MATDERIV, vaccess>, TMultiVecId<vtype, vaccess>>(); |
| 99 | + } |
| 100 | + |
| 101 | + // Public methods/properties |
| 102 | + c.def_property_readonly("defaultId", &MultiVecId::getDefaultId); |
| 103 | + c.def("getDefaultId", &MultiVecId::getDefaultId); |
| 104 | + |
| 105 | + c.def("getName", &MultiVecId::getName); |
| 106 | + c.def("isNull", &MultiVecId::isNull); |
| 107 | + c.def("__str__", &MultiVecId::getName); |
| 108 | + c.def("getId", &MultiVecId::getId, py::arg("state")); |
| 109 | + |
| 110 | + // Static methods/properties |
| 111 | + c.def_static("null", &MultiVecId::null); |
| 112 | +} |
| 113 | + |
| 114 | +void moduleAddMultiVecId(py::module& m) { |
| 115 | + bindMultiVecId<V_ALL, V_READ> (m, "ConstMultiVecId"); |
| 116 | + bindMultiVecId<V_ALL, V_WRITE> (m, "MultiVecId"); |
| 117 | + bindMultiVecId<V_COORD, V_READ> (m, "ConstMultiVecCoordId"); |
| 118 | + bindMultiVecId<V_COORD, V_WRITE> (m, "MultiVecCoordId"); |
| 119 | + bindMultiVecId<V_DERIV, V_READ> (m, "ConstMultiVecDerivId"); |
| 120 | + bindMultiVecId<V_DERIV, V_WRITE> (m, "MultiVecDerivId"); |
| 121 | + bindMultiVecId<V_MATDERIV, V_READ> (m, "ConstMultiMatrixDerivId"); |
| 122 | + bindMultiVecId<V_MATDERIV, V_WRITE> (m, "MultiMatrixDerivId"); |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace sofapython3 |
0 commit comments