Skip to content
Snippets Groups Projects
Commit bdb06597 authored by florent's avatar florent
Browse files

Remove debug output

       * src/entity-py.cc.
parent 083612ca
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <dynamic-graph/entity.h> #include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h> #include <dynamic-graph/factory.h>
#include <dynamic-graph/linear-algebra.h>
#include "dynamic-graph/command.h" #include "dynamic-graph/command.h"
#include "dynamic-graph/value.h" #include "dynamic-graph/value.h"
...@@ -19,6 +20,8 @@ using dynamicgraph::SignalBase; ...@@ -19,6 +20,8 @@ using dynamicgraph::SignalBase;
using dynamicgraph::ExceptionAbstract; using dynamicgraph::ExceptionAbstract;
using dynamicgraph::command::Command; using dynamicgraph::command::Command;
using dynamicgraph::command::Value; using dynamicgraph::command::Value;
using dynamicgraph::Vector;
using dynamicgraph::Matrix;
namespace dynamicgraph { namespace dynamicgraph {
namespace python { namespace python {
...@@ -28,9 +31,14 @@ namespace dynamicgraph { ...@@ -28,9 +31,14 @@ namespace dynamicgraph {
namespace entity { namespace entity {
static void destroy (void* self); static void destroy (void* self);
static void fillMatrixRow(Matrix& m, unsigned index,
PyObject* tuple);
static Value pythonToValue(PyObject* pyObject, static Value pythonToValue(PyObject* pyObject,
const Value::Type& valueType); const Value::Type& valueType);
static PyObject* vectorToPython(const Vector& vector);
static PyObject* valueToPython(const Value& value); static PyObject* valueToPython(const Value& value);
/** /**
\brief Create an instance of Entity \brief Create an instance of Entity
*/ */
...@@ -142,6 +150,23 @@ namespace dynamicgraph { ...@@ -142,6 +150,23 @@ namespace dynamicgraph {
return Py_BuildValue(""); return Py_BuildValue("");
} }
void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* tuple)
{
if (PyTuple_Size(tuple) != m.size2()) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"lines of matrix have different sizes.");
}
for (unsigned int iCol=0; iCol < m.size2(); iCol++) {
PyObject* pyDouble = PyTuple_GetItem(tuple, iCol);
if (!PyFloat_Check(pyDouble)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"element of matrix should be "
"a floating point number.");
}
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
}
}
Value pythonToValue(PyObject* pyObject, Value pythonToValue(PyObject* pyObject,
const Value::Type& valueType) const Value::Type& valueType)
{ {
...@@ -151,6 +176,13 @@ namespace dynamicgraph { ...@@ -151,6 +176,13 @@ namespace dynamicgraph {
float fvalue; float fvalue;
double dvalue; double dvalue;
std::string svalue; std::string svalue;
Vector v;
Matrix m;
unsigned int nCols;
unsigned int size;
PyObject* row;
unsigned int nRows;
FILE* file;
switch (valueType) { switch (valueType) {
case (Value::BOOL) : case (Value::BOOL) :
...@@ -197,12 +229,89 @@ namespace dynamicgraph { ...@@ -197,12 +229,89 @@ namespace dynamicgraph {
svalue = PyString_AsString(pyObject); svalue = PyString_AsString(pyObject);
return Value(svalue); return Value(svalue);
break; break;
case (Value::VECTOR) :
// Check that argument is a tuple
if (!PyTuple_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"vector");
}
size = PyTuple_Size(pyObject);
v.resize(size);
for (unsigned int i=0; i<size; i++) {
PyObject* pyDouble = PyTuple_GetItem(pyObject, i);
if (!PyFloat_Check(pyDouble)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"element of vector should be a floating "
"point number.");
}
v[i] = PyFloat_AsDouble(pyDouble);
}
return Value(v);
break;
case (Value::MATRIX) :
// Check that argument is a tuple
if (!PyTuple_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"matrix");
}
nRows = PyTuple_Size(pyObject);
if (nRows == 0) {
return Value(Matrix(0,0));
}
row = PyTuple_GetItem(pyObject, 0);
if (!PyTuple_Check(row)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"matrix");
}
nCols = PyTuple_Size(row);
file = fopen("/home/florent/tmp/python", "w");
PyObject_Print(row, file, Py_PRINT_RAW);
fclose(file);
m.resize(nRows, nCols);
fillMatrixRow(m, 0, row);
for (unsigned int iRow=1; iRow<nRows; iRow++) {
row = PyTuple_GetItem(pyObject, iRow);
if (!PyTuple_Check(row)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"matrix");
fillMatrixRow(m, iRow, row);
}
}
return Value(m);
break;
default:
std::cerr << "Only int, double and string are supported."
<< std::endl;
} }
std::cerr << "Only int, double and string are supported."
<< std::endl;
return Value(); return Value();
} }
PyObject* vectorToPython(const Vector& vector)
{
PyObject* tuple = PyTuple_New(vector.size());
for (unsigned int index = 0; index < vector.size(); index++) {
PyObject* pyDouble = PyFloat_FromDouble(vector[index]);
PyTuple_SET_ITEM(tuple, index, pyDouble);
}
return tuple;
}
PyObject* matrixToPython(const Matrix& matrix)
{
PyObject* tuple = PyTuple_New(matrix.size1());
for (unsigned int iRow = 0; iRow < matrix.size1(); iRow++) {
PyObject* row = PyTuple_New(matrix.size2());
for (unsigned iCol=0; iCol < matrix.size2(); iCol++) {
PyObject* pyDouble = PyFloat_FromDouble(matrix(iRow, iCol));
PyTuple_SET_ITEM(row, iCol, pyDouble);
}
PyTuple_SET_ITEM(tuple, iRow, row);
}
return tuple;
}
PyObject* valueToPython(const Value& value) PyObject* valueToPython(const Value& value)
{ {
bool boolValue; bool boolValue;
...@@ -211,6 +320,8 @@ namespace dynamicgraph { ...@@ -211,6 +320,8 @@ namespace dynamicgraph {
float floatValue; float floatValue;
double doubleValue; double doubleValue;
std::string stringValue; std::string stringValue;
Vector vectorValue;
Matrix matrixValue;
switch(value.type()) { switch(value.type()) {
case (Value::BOOL) : case (Value::BOOL) :
...@@ -234,6 +345,14 @@ namespace dynamicgraph { ...@@ -234,6 +345,14 @@ namespace dynamicgraph {
case (Value::STRING) : case (Value::STRING) :
stringValue = (std::string) value.value(); stringValue = (std::string) value.value();
return Py_BuildValue("s", stringValue.c_str()); return Py_BuildValue("s", stringValue.c_str());
case (Value::VECTOR) :
vectorValue = value.value();
return vectorToPython(vectorValue);
case (Value::MATRIX) :
matrixValue = value.value();
return matrixToPython(matrixValue);
default:
return Py_BuildValue("");
} }
return Py_BuildValue(""); return Py_BuildValue("");
} }
...@@ -299,11 +418,6 @@ namespace dynamicgraph { ...@@ -299,11 +418,6 @@ namespace dynamicgraph {
return NULL; return NULL;
} }
} }
std::cout << "executeCommand:"<< std::endl;
for (unsigned int i=0; i<valueVector.size(); i++) {
std::cout << " value[" << i << "]=("
<< valueVector[i] << ")" << std::endl;
}
command->setParameterValues(valueVector); command->setParameterValues(valueVector);
try { try {
Value result = command->execute(); Value result = command->execute();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment