Skip to content

Commit

Permalink
[py] building trajectories from numpy arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Feb 10, 2021
1 parent 85a651e commit afadfd7
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/doc/manual/04-static-contractors/01-ctc-function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ The boxes are contracted in order to remove some vectors that are not consistent

.. #include <codac.h>
.. #include <codac-rob.h>
.. #include "codac_CtcHC4.h"
.. #include "ibex_CtcHC4.h"
.. #include "ibex_SystemFactory.h"
.. #include "codac_Ctc3BCid.h"
.. #include "ibex_Ctc3BCid.h"
..
.. using namespace std;
.. using namespace codac;
Expand Down
2 changes: 1 addition & 1 deletion doc/doc/manual/04-static-contractors/03-ctc-polar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Calls to :math:`\mathcal{C}_{\textrm{polar}}` will allow the contraction of the

// ...

pyCtcPolar ctc_polar;
CtcPolar ctc_polar;

ctc_polar.contract(b1[0], b1[1], d1, theta1);
ctc_polar.contract(b2[0], b2[1], d2, theta2);
Expand Down
4 changes: 4 additions & 0 deletions python/src/core/variables/trajectory/codac_py_Trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void export_Trajectory(py::module& m)
TRAJECTORY_TRAJECTORY_MAPDOUBLEDOUBLE,
"m_map_values"_a)

.def(py::init<const std::list<double> &,const std::list<double> &>(),
TRAJECTORY_TRAJECTORY_LISTDOUBLE_LISTDOUBLE,
"list_t"_a, "list_x"_a)

.def(py::init<const Trajectory&>(),
TRAJECTORY_TRAJECTORY_TRAJECTORY,
"traj"_a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <pybind11/stl.h>
#include <pybind11/operators.h>
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include "pyIbex_type_caster.h"

#include "codac_TrajectoryVector.h"
Expand All @@ -35,7 +36,36 @@ TrajectoryVector* create_trajectoryvector_from_list(py::list& lst)
TrajectoryVector *instance = new TrajectoryVector(lst.size());
for(size_t i = 0 ; i < lst.size() ; i++)
(*instance)[i] = lst[i].cast<Trajectory>();
return instance;
return instance; // todo: manage delete of pointer
}

TrajectoryVector* create_trajectoryvector_from_arrays(py::array_t<double>& lst_t, py::array_t<double>& lst_x)
{
if(lst_t.size() < 1 || lst_x.size() < 1)
throw std::invalid_argument("Empty Trajectory list");

list<Vector> std_lst_x;

py::buffer_info info = lst_x.request();
auto ptr = static_cast<double*>(info.ptr);

int nb_values = 1;
for(auto r : info.shape)
nb_values *= r;

assert(nb_values % lst_t.size() == 0);
int n = nb_values / lst_t.size();

for(int i = 0 ; i < nb_values ; i+=n)
{
Vector x(n);
for(int k = 0 ; k < n ; k++)
x[k] = *ptr++;
std_lst_x.push_back(x);
}

TrajectoryVector *instance = new TrajectoryVector(lst_t.cast<std::list<double> >(), std_lst_x);
return instance; // todo: manage delete of pointer
}

void export_TrajectoryVector(py::module& m)
Expand Down Expand Up @@ -65,6 +95,14 @@ void export_TrajectoryVector(py::module& m)
TRAJECTORYVECTOR_TRAJECTORYVECTOR_VECTORMAPDOUBLEDOUBLE,
"v_map_values"_a)

.def(py::init<const std::list<double> &,const std::list<Vector> &>(),
TRAJECTORYVECTOR_TRAJECTORYVECTOR_LISTDOUBLE_LISTVECTOR,
"list_t"_a, "list_x"_a)

.def(py::init(&create_trajectoryvector_from_arrays),
TRAJECTORYVECTOR_TRAJECTORYVECTOR_LISTDOUBLE_LISTVECTOR,
"list_t"_a, "list_x"_a)

// Used instead of .def(py::init<initializer_list<Trajectory>>(),...
.def(py::init(&create_trajectoryvector_from_list),
TRAJECTORYVECTOR_TRAJECTORYVECTOR_INITIALIZERLISTTRAJECTORY,
Expand Down
15 changes: 15 additions & 0 deletions src/core/variables/trajectory/codac_Trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ namespace codac
// Codomain:
compute_codomain();
}

Trajectory::Trajectory(const list<double>& list_t, const list<double>& list_x)
: m_traj_def_type(TrajDefnType::MAP_OF_VALUES)
{
assert(!list_t.empty());
assert(list_t.size() == list_x.size());

for(list<double>::const_iterator it_t = list_t.begin(), it_x = list_x.begin() ;
it_t != list_t.end() && it_x != list_x.end();
++it_t, ++it_x)
{
m_map_values[*it_t] = *it_x;
m_tdomain |= *it_t;
}
}

Trajectory::~Trajectory()
{
Expand Down
11 changes: 11 additions & 0 deletions src/core/variables/trajectory/codac_Trajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define __CODAC_TRAJECTORY_H__

#include <map>
#include <list>
#include "codac_DynamicalItem.h"
#include "codac_TFunction.h"
#include "codac_traj_arithmetic.h"
Expand Down Expand Up @@ -69,6 +70,16 @@ namespace codac
*/
explicit Trajectory(const std::map<double,double>& m_map_values);

/**
* \brief Creates a scalar trajectory \f$x(\cdot)\f$ from a list of values
*
* Values and datations are separated into two lists.
*
* \param list_t list of time keys
* \param list_x list of values
*/
explicit Trajectory(const std::list<double>& list_t, const std::list<double>& list_x);

/**
* \brief Creates a copy of a scalar trajectory \f$x(\cdot)\f$
*
Expand Down
20 changes: 20 additions & 0 deletions src/core/variables/trajectory/codac_TrajectoryVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ namespace codac
for(int i = 0 ; i < size() ; i++)
(*this)[i] = Trajectory(v_map_values[i]);
}

TrajectoryVector::TrajectoryVector(const list<double>& list_t, const list<Vector>& list_x)
{
assert(!list_t.empty());
assert(list_t.size() == list_x.size());

int n = list_x.begin()->size();

list<double>::const_iterator it_t = list_t.begin();
list<Vector>::const_iterator it_x = list_x.begin();
for( ;
it_t != list_t.end() && it_x != list_x.end();
++it_t, ++it_x)
{
assert(it_x->size() == n);
set(*it_x, *it_t);
}

// todo: optimize this by building Trajectory objects here directly?
}

TrajectoryVector::TrajectoryVector(int n, const Trajectory& x)
: TrajectoryVector(n)
Expand Down
11 changes: 11 additions & 0 deletions src/core/variables/trajectory/codac_TrajectoryVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define __CODAC_TRAJECTORYVECTOR_H__

#include <map>
#include <list>
#include <initializer_list>
#include "codac_Vector.h"
#include "codac_Interval.h"
Expand Down Expand Up @@ -79,6 +80,16 @@ namespace codac
*/
explicit TrajectoryVector(const std::vector<std::map<double,double> >& v_map_values);

/**
* \brief Creates a n-dimensional trajectory \f$\mathbf{x}(\cdot)\f$ from a list of vector values
*
* Values and datations are separated into two lists.
*
* \param list_t list of time keys
* \param list_x list of vector values
*/
explicit TrajectoryVector(const std::list<double>& list_t, const std::list<Vector>& list_x);

/**
* \brief Creates a n-dimensional trajectory \f$\mathbf{x}(\cdot)\f$ from a list of Trajectory objects
*
Expand Down

0 comments on commit afadfd7

Please sign in to comment.