Skip to content

Commit

Permalink
Added support for Rotation::operator() via __getitem__ and __setitem_…
Browse files Browse the repository at this point in the history
…_ (changes c++ accessor () to [] in python)
  • Loading branch information
Alessio Rocchi committed Feb 14, 2017
1 parent d1f6a06 commit f680f8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ R3 = Rotation(*R_numpy)
v_numpy = v.ndarray()
v5 = Vector(*v_numpy)
v.x() # get 0th element of v
v.x(1.0) # set 0th element of v
R[0,0] # equivalent to R(0,0) in c++
R[0,0] = 1.0 # equivalent to R(0,0) = 1.0 in c++
```
25 changes: 25 additions & 0 deletions pyKDL.i
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ namespace KDL
}
}

%typemap(in) (int matrix_i_row, int matrix_i_col) {
if (!PyTuple_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Error: expecting a tuple (m[1,2] is equivalent to m[(1,2)])");
return NULL;
}

if (PyTuple_Size($input) != 2 ) {
PyErr_SetString(PyExc_ValueError, "Matrix elements are accessed by using two integers m[i_row,i_col]");
return NULL;
}

$1 = (int)PyInt_AsLong(PyTuple_GetItem($input,0)); /* int i */
$2 = (int)PyInt_AsLong(PyTuple_GetItem($input,1)); /* int j */
};
%ignore KDL::Rotation::data;
%rename(_set) KDL::Rotation::operator()(int i, int j);
%rename(_get) KDL::Rotation::operator()(int i, int j) const;
%rename(assign) KDL::Rotation::operator=(const Rotation& arg);
%extend KDL::Rotation {
Vector __mul__(const Vector& v) const {
Expand All @@ -99,6 +115,15 @@ namespace KDL
npy_intp dims[] = {3, 3};
return PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, (void*)$self->data);
}

void __setitem__(int matrix_i_row, int matrix_i_col, double value) {
$self->operator ()(matrix_i_row,matrix_i_col) = value;
}

double __getitem__(int matrix_i_row, int matrix_i_col) {
return $self->operator ()(matrix_i_row,matrix_i_col);
}

}

%rename(assign) KDL::Frame::operator=(const Frame& arg);
Expand Down

0 comments on commit f680f8b

Please sign in to comment.