Skip to content

Commit

Permalink
Merge pull request #40 from SofaDefrost/fixTests
Browse files Browse the repository at this point in the history
Code clean, test fix by fixing code,
  • Loading branch information
damienmarchal committed Jul 11, 2019
2 parents a606c92 + 1cb9112 commit 7a4a8b5
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 332 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Expand Up @@ -93,7 +93,7 @@ set(SOURCE_FILES
set(EXTRA_FILES
SofaPython3Config.cmake.in
README.md
)
)

if(SP3_BUILD_TEST)
list(APPEND HEADER_FILES
Expand Down Expand Up @@ -139,5 +139,12 @@ install(TARGETS ${PROJECT_NAME} DESTINATION modules/SofaPython3)
# the example directory in the INSTALL set.
sofa_create_package(${PROJECT_NAME} ${PROJECT_VERSION} SofaPython3 "SofaPython3")

set(EXAMPLES_FILES
examples/example-forcefield.py
examples/example-scriptcontroller.py
)

add_custom_target(Examples SOURCES ${EXAMPLES_FILES})

### Python binding
add_subdirectory(bindings)
2 changes: 1 addition & 1 deletion bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp
Expand Up @@ -106,7 +106,7 @@ py::object BindingBase::GetAttr(Base* self, const std::string& s, bool doThrowEx
/// Search if there is a data with the given name.
/// If this is the case returns the corresponding python type.
if(BaseData* d = self->findData(s)){
return py::cast(d);
return dataToPython(d);
}

/// Search if there is a link with the given name.
Expand Down
14 changes: 0 additions & 14 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseData.cpp
Expand Up @@ -32,20 +32,6 @@ void moduleAddBaseData(py::module& m)
data.def("getOwner", &BaseData::getOwner);
data.def("getParent", &BaseData::getParent);
data.def("typeName", [](BaseData& data){ return data.getValueTypeInfo()->name(); });

// TODO: Implementation should look like: https://github.com/sofa-framework/sofa/issues/767
// p.def("__setitem__", [](BaseData& self, py::object& key, py::object& value)
// {
// std::cout << "mapping protocol, __setitem__ to implement)" << std::endl ;
// return py::none();
// });

// p.def("__len__", [](BaseData& b) -> size_t
// {
// auto nfo = b.getValueTypeInfo();
// return nfo->size(b.getValueVoidPtr()) / nfo->size();
// });

data.def("getPathName", [](BaseData& self)
{
Base* b= self.getOwner();
Expand Down
87 changes: 48 additions & 39 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_DataContainer.cpp
Expand Up @@ -18,38 +18,6 @@ using sofa::core::objectmodel::BaseNode;

#include <sofa/defaulttype/BoundingBox.h>

//namespace pybind11 { namespace detail {
// template <> struct type_caster<sofa::helper::types::RGBAColor> {
// public:
// /**
// * This macro establishes the name 'inty' in
// * function signatures and declares a local variable
// * 'value' of type inty
// */
// PYBIND11_TYPE_CASTER(sofa::helper::types::RGBAColor, _("RGBAColor"));

// /**
// * Conversion part 1 (Python->C++): convert a PyObject into a inty
// * instance or return false upon failure. The second argument
// * indicates whether implicit conversions should be applied.
// */
// bool load(handle src, bool) {
// std::cout << "LOAD LOAD" << std::endl ;
// }

// /**
// * Conversion part 2 (C++ -> Python): convert an inty instance into
// * a Python object. The second and third arguments are used to
// * indicate the return value policy and parent object (for
// * ``return_value_policy::reference_internal``) and are generally
// * ignored by implicit casters.
// */
// static handle cast(sofa::core::objectmodel::RGBAColor src, return_value_policy /* policy */, handle /* parent */) {
// std::cout << "CAST CAST" << std::endl ;
// }
// };
//}} // namespace pybind11::detail

namespace sofapython3
{

Expand All @@ -63,7 +31,39 @@ void moduleAddDataAsString(py::module& m)
});
}

/// Following numpy convention returns the number of element in each dimmensions.
py::tuple DataContainer::getShape()
{
/// Detect if we are in a one or two day array.
auto nfo = getValueTypeInfo();
if( nfo->size() == nfo->size(getValueVoidPtr() ))
{
py::tuple p {1};
p[0] = nfo->size();
}
py::tuple p {2};
p[0] = py::int_{nfo->size(getValueVoidPtr())/nfo->size()};
p[1] = py::int_{nfo->size()};
return p;
}

/// Following numpy convention the number of dimmension in the container.
size_t DataContainer::getNDim()
{
auto nfo = getValueTypeInfo();
if( nfo->size() == nfo->size(getValueVoidPtr() ))
return 1;
return 2;
}

/// Following numpy convention the number of elements in the first dimmension
size_t DataContainer::getSize()
{
auto nfo = getValueTypeInfo();
if( nfo->size() == nfo->size(getValueVoidPtr() ))
return nfo->size();
return nfo->size(getValueVoidPtr())/nfo->size();
}

void moduleAddDataContainer(py::module& m)
{
Expand Down Expand Up @@ -124,7 +124,9 @@ void moduleAddDataContainer(py::module& m)

p.def("__str__", [](BaseData* self)
{
return py::str(convertToPython(self));
std::stringstream tmp;
tmp << "Sofa.Core.DataContainer <'" << self->getName() << "', " << self << ">";
return py::str(tmp.str());
});

p.def("__repr__", [](BaseData* self)
Expand All @@ -136,6 +138,19 @@ void moduleAddDataContainer(py::module& m)
return convertToPython(self);
});

/// This is the standard terminology in numpy so we keep it for consistency purpose.
p.def_property_readonly("ndim", &DataContainer::getNDim); /// 1 or 2D array
p.def_property_readonly("shape", &DataContainer::getShape); /// array containing the size in each dimmension
p.def_property_readonly("size", &DataContainer::getSize); /// get the total number of elements
p.def("__len__", [](DataContainer* self) /// In numpy the len is the number of element in the first
/// dimmension.
{
auto nfo = self->getValueTypeInfo();
if( nfo->size() == nfo->size(self->getValueVoidPtr() ))
return nfo->size();
return nfo->size(self->getValueVoidPtr())/nfo->size();
});

p.def("array", [](DataContainer* self){
auto capsule = py::capsule(new Base::SPtr(self->getOwner()));
py::buffer_info ninfo = toBufferInfo(*self);
Expand All @@ -145,12 +160,6 @@ void moduleAddDataContainer(py::module& m)
return a;
});

p.def("__len__", [](DataContainer* self){
auto nfo = self->getValueTypeInfo();
//return nfo->size(self->getValueVoidPtr()) / nfo->size();
return nfo->size();
});

p.def("writeable", [](DataContainer* self, py::object f) -> py::object
{
if(self!=nullptr)
Expand Down
Expand Up @@ -13,7 +13,12 @@ void moduleAddDataContainer(py::module& m);
void moduleAddDataAsString(py::module& m);
void moduleAddWriteAccessor(py::module& m);

class DataContainer : public BaseData {} ;
class DataContainer : public BaseData {
public:
py::tuple getShape();
size_t getNDim();
size_t getSize();
} ;
class DataAsString : public BaseData {} ;

class WriteAccessor
Expand Down
11 changes: 6 additions & 5 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Expand Up @@ -69,26 +69,27 @@ void moduleAddBaseIterator(py::module &m)
{
if(index>=d.size(d.owner.get()))
throw py::index_error("Too large index '"+std::to_string(index)+"'");
return py::cast(d.get(d.owner.get(), index));
return PythonDownCast::toPython(d.get(d.owner.get(), index).get());
});

d.def("__getitem__", [](BaseIterator& d, const std::string& name) -> py::object
{
BaseObject* obj =d.owner->getObject(name);
if(obj==nullptr)
throw py::index_error("Not existing object '"+name+"'");
return py::cast(obj);
throw py::index_error("No existing object '"+name+"'");
return PythonDownCast::toPython(obj);
});

d.def("__iter__", [](BaseIterator& d)
{
return d;
});

d.def("__next__", [](BaseIterator& d) -> py::object
{
if(d.index>=d.size(d.owner.get()))
throw py::stop_iteration();
return py::cast(d.get(d.owner.get(), d.index++));
return PythonDownCast::toPython(d.get(d.owner.get(), d.index++).get());
});
d.def("__len__", [](BaseIterator& d) -> py::object
{
Expand Down Expand Up @@ -284,7 +285,7 @@ void moduleAddNode(py::module &m) {
p.def_property_readonly("children", [](Node* node)
{
return new BaseIterator(node, [](Node* n) -> size_t { return n->child.size(); },
[](Node* n, unsigned int index) -> Node::SPtr { return n->child[index]; });
[](Node* n, unsigned int index) -> Base::SPtr { return n->child[index]; });
}, sofapython3::doc::sofa::core::Node::children);

p.def_property_readonly("parents", [](Node* node)
Expand Down
2 changes: 0 additions & 2 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp
Expand Up @@ -64,9 +64,7 @@ PYBIND11_MODULE(Core, core)
moduleAddBaseCamera(core);
moduleAddBaseData(core);
moduleAddWriteAccessor(core);
std::cout << "0" << std::endl;
moduleAddDataContainer(core);
std::cout << "1" << std::endl;
moduleAddBaseObject(core);
moduleAddController(core);
moduleAddForceField(core);
Expand Down
1 change: 0 additions & 1 deletion bindings/Sofa/tests/CMakeLists.txt
Expand Up @@ -23,7 +23,6 @@ set(SOURCE_FILES

set(PYTHON_FILES
Components/Components.py
Core/AutoDiffForceField.py
Core/BaseData.py
Core/Base.py
Core/BaseObject.py
Expand Down
106 changes: 0 additions & 106 deletions bindings/Sofa/tests/Core/AutoDiffForceField.py

This file was deleted.

0 comments on commit 7a4a8b5

Please sign in to comment.