Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,39 @@ namespace sofapython3

py::object ret = _addKToMatrix(mparams, nNodes, nDofs);

if(!py::isinstance<py::array>(ret))
{
throw py::type_error("Can't read return value of AddKToMatrix. A numpy array is expected");
}

// if ret is numpy array
if(py::isinstance<py::array>(ret))
auto r = py::cast<py::array>(ret);
if (r.ndim() == 3 && r.shape(2) == 1)
{
auto r = py::cast<py::array>(ret);
if (r.ndim() == 3 && r.shape(2) == 1)
// read K as a plain 2D matrix
auto kMatrix = r.unchecked<double, 3>();
for (size_t x = 0 ; x < size_t(kMatrix.shape(0)) ; ++x)
{
// read K as a plain 2D matrix
auto kMatrix = r.unchecked<double, 3>();
for (size_t x = 0 ; x < size_t(kMatrix.shape(0)) ; ++x)
for (size_t y = 0 ; y < size_t(kMatrix.shape(1)) ; ++y)
{
for (size_t y = 0 ; y < size_t(kMatrix.shape(1)) ; ++y)
{
mat->add(int(offset + x), int(offset + y), kMatrix(x,y, 0));
}
mat->add(int(offset + x), int(offset + y), kMatrix(x,y, 0));
}
}
else if (r.ndim() == 2 && r.shape(1) == 3)
{
// consider ret to be a list of tuples [(i,j,[val])]
auto kMatrix = r.unchecked<double, 2>();
for (auto x = 0 ; x < kMatrix.shape(0) ; ++x)
{
mat->add(int(offset + size_t(kMatrix(x,0))), int(offset + size_t(kMatrix(x,1))), kMatrix(x,2));
}
}
else
}
else if (r.ndim() == 2 && r.shape(1) == 3)
{
// consider ret to be a list of tuples [(i,j,[val])]
auto kMatrix = r.unchecked<double, 2>();
for (auto x = 0 ; x < kMatrix.shape(0) ; ++x)
{
throw py::type_error("Can't read return value of AddKToMatrix. The method should return either a plain 2D matrix or a vector of tuples (i, j, val)");
mat->add(int(offset + size_t(kMatrix(x,0))), int(offset + size_t(kMatrix(x,1))), kMatrix(x,2));
}
}
else
{
throw py::type_error("Can't read return value of AddKToMatrix. The method should return either a plain 2D matrix or a vector of tuples (i, j, val)");
}

}


Expand Down
Loading