Skip to content

Commit

Permalink
Cleanup data types used for indeces and sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
traversaro committed Nov 18, 2020
1 parent 2b9e971 commit 87f73db
Show file tree
Hide file tree
Showing 31 changed files with 420 additions and 411 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The minimum required CMake version to configure and compile iDynTree is now 3.16 (https://github.com/robotology/idyntree/pull/732).
- The Python package name of the SWIG bindings changed from `iDynTree` to `idyntree.bindings` (https://github.com/robotology/idyntree/pull/733, https://github.com/robotology/idyntree/pull/735). To continue referring to iDynTree classes as `iDynTree.<ClassName>`, you can change your `import iDynTree` statements to `import idyntree.bindings as iDynTree`. Otherwise, you can use `import idyntree.bindings` to refer them as `idyntree.bindings.<ClassName>`.
- Improve the use of `const` keyword in `KinDynComputations`(https://github.com/robotology/idyntree/pull/736).
- Cleanup size and indices attributes. For consistency with std and Eigen, all sizes and indices have been changed to use std::size_t for unsigned quantities and std::ptrdiff_t for signed quantities. The only exception is the index stored in the triplets of the iDynTree::SparseMatrix data structure, that have been left defined to int for compatibility with Eigen (https://github.com/robotology/idyntree/pull/767).

### Removed
- Remove the CMake option IDYNTREE_USES_KDL and all the classes available when enabling it. They were deprecated in iDynTree 1.0 .
Expand Down
11 changes: 7 additions & 4 deletions src/core/include/iDynTree/Core/EigenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#ifndef IDYNTREE_EIGEN_HELPERS_H
#define IDYNTREE_EIGEN_HELPERS_H

#include <cstddef>

#include <Eigen/Dense>
#include <iDynTree/Core/VectorDynSize.h>
#include <iDynTree/Core/VectorFixSize.h>
Expand Down Expand Up @@ -113,8 +115,8 @@ toEigen(const MatrixView<const double>& mat)
// - inner_stride = 2 = number of rows of A
// - outer_stride = 1

const int innerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? mat.rows() : 1;
const int outerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? 1 : mat.cols();
const std::ptrdiff_t innerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? mat.rows() : 1;
const std::ptrdiff_t outerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? 1 : mat.cols();

return Eigen::Map<const MatrixRowMajor, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>>(
mat.data(),
Expand Down Expand Up @@ -162,8 +164,8 @@ toEigen(const MatrixView<double>& mat)
// - inner_stride = 2 = number of rows of A
// - outer_stride = 1

const int innerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? mat.rows() : 1;
const int outerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? 1 : mat.cols();
const std::ptrdiff_t innerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? mat.rows() : 1;
const std::ptrdiff_t outerStride = (mat.storageOrder() == MatrixStorageOrdering::ColumnMajor) ? 1 : mat.cols();

return Eigen::Map<MatrixRowMajor, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>>(
mat.data(),
Expand Down Expand Up @@ -405,3 +407,4 @@ inline void setSubVector(VectorDynSize& vec,
}

#endif /* IDYNTREE_EIGEN_HELPERS_H */

31 changes: 16 additions & 15 deletions src/core/include/iDynTree/Core/MatrixDynSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace iDynTree
* element corresponding to row and col, using row
* major ordering.
*/
unsigned int rawIndexRowMajor(int row, int col) const;
std::size_t rawIndexRowMajor(std::size_t row, std::size_t col) const;

/**
* Return the raw index in the data vector of the
Expand All @@ -42,12 +42,12 @@ namespace iDynTree
* this function is used just in the fillColMajorBuffer
* method.
*/
unsigned int rawIndexColMajor(int row, int col) const;
std::size_t rawIndexColMajor(std::size_t row, std::size_t col) const;

/**
* Set the capacity of the vector, resizing the buffer pointed by m_data.
*/
void changeCapacityAndCopyData(const unsigned int _newCapacity);
void changeCapacityAndCopyData(const std::size_t _newCapacity);

protected:
/**
Expand All @@ -58,13 +58,13 @@ namespace iDynTree
* \warning this class stores data using the row major order
*/
double * m_data;
unsigned int m_rows;
unsigned int m_cols;
std::size_t m_rows;
std::size_t m_cols;

/**
* The buffer to which m_data is pointing is m_capacity*sizeof(double).
*/
unsigned int m_capacity;
std::size_t m_capacity;

public:
/**
Expand All @@ -80,7 +80,7 @@ namespace iDynTree
*
* \warning performs dynamic memory allocation operations
*/
MatrixDynSize(unsigned int _rows, unsigned int _cols);
MatrixDynSize(std::size_t _rows, std::size_t _cols);

/**
* Constructor from a C-style matrix.
Expand All @@ -89,7 +89,7 @@ namespace iDynTree
* \warning this class stores data using the row major order
* \warning performs dynamic memory allocation operations
*/
MatrixDynSize(const double * in_data, const unsigned int in_rows, const unsigned int in_cols);
MatrixDynSize(const double * in_data, const std::size_t in_rows, const std::size_t in_cols);

/**
* Copy constructor
Expand Down Expand Up @@ -141,12 +141,12 @@ namespace iDynTree
* In doubt, don't use them and rely on more high level functions.
*/
///@{
double operator()(const unsigned int row, const unsigned int col) const;
double& operator()(const unsigned int row, const unsigned int col);
double getVal(const unsigned int row, const unsigned int col) const;
bool setVal(const unsigned int row, const unsigned int col, const double new_el);
unsigned int rows() const;
unsigned int cols() const;
double operator()(const std::size_t row, const std::size_t col) const;
double& operator()(const std::size_t row, const std::size_t col);
double getVal(const std::size_t row, const std::size_t col) const;
bool setVal(const std::size_t row, const std::size_t col, const double new_el);
std::size_t rows() const;
std::size_t cols() const;
///@}

/**
Expand Down Expand Up @@ -178,7 +178,7 @@ namespace iDynTree
*
* \warning performs dynamic memory allocation operations if newRows*newCols > capacity()
*/
void resize(const unsigned int _newRows, const unsigned int _newCols);
void resize(const std::size_t _newRows, const std::size_t _newCols);

/**
* Increase the capacity of the matrix, preserving old content.
Expand Down Expand Up @@ -251,3 +251,4 @@ namespace iDynTree
}

#endif /* IDYNTREE_MATRIX_DYN_SIZE_H */

59 changes: 30 additions & 29 deletions src/core/include/iDynTree/Core/MatrixFixSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace iDynTree
* element corresponding to row and col, using row
* major ordering.
*/
unsigned int rawIndexRowMajor(int row, int col) const;
std::size_t rawIndexRowMajor(std::size_t row, std::size_t col) const;

/**
* Return the raw index in the data vector of the
Expand All @@ -48,7 +48,7 @@ namespace iDynTree
* this function is used just in the fillColMajorBuffer
* method.
*/
unsigned int rawIndexColMajor(int row, int col) const;
std::size_t rawIndexColMajor(std::size_t row, std::size_t col) const;

protected:
/**
Expand All @@ -75,7 +75,7 @@ namespace iDynTree
*
* \warning this class stores data using the row major order
*/
MatrixFixSize(const double * in_data, const unsigned int in_rows, const unsigned int in_cols);
MatrixFixSize(const double * in_data, const std::size_t in_rows, const std::size_t in_cols);

/**
* Constructor from a MatrixView
Expand All @@ -90,12 +90,12 @@ namespace iDynTree
*
*/
///@{
double operator()(const unsigned int row, const unsigned int col) const;
double& operator()(const unsigned int row, const unsigned int col);
double getVal(const unsigned int row, const unsigned int col) const;
bool setVal(const unsigned int row, const unsigned int col, const double new_el);
unsigned int rows() const;
unsigned int cols() const;
double operator()(const std::size_t row, const std::size_t col) const;
double& operator()(const std::size_t row, const std::size_t col);
double getVal(const std::size_t row, const std::size_t col) const;
bool setVal(const std::size_t row, const std::size_t col, const double new_el);
std::size_t rows() const;
std::size_t cols() const;
///@}

MatrixFixSize & operator=(iDynTree::MatrixView<const double> mat);
Expand Down Expand Up @@ -180,8 +180,8 @@ namespace iDynTree

template<unsigned int nRows, unsigned int nCols>
MatrixFixSize<nRows,nCols>::MatrixFixSize(const double* in_data,
const unsigned int in_rows,
const unsigned int in_cols)
const std::size_t in_rows,
const std::size_t in_cols)
{
if( in_rows != nRows ||
in_cols != nCols )
Expand All @@ -206,9 +206,9 @@ namespace iDynTree
}
else
{
for(unsigned int row=0; row < nRows; row++ )
for(std::size_t row=0; row < nRows; row++ )
{
for(unsigned int col=0; col < nCols; col++ )
for(std::size_t col=0; col < nCols; col++ )
{
this->m_data[rawIndexRowMajor(row,col)] = other(row, col);
}
Expand All @@ -219,23 +219,23 @@ namespace iDynTree
template<unsigned int nRows, unsigned int nCols>
void MatrixFixSize<nRows,nCols>::zero()
{
for(unsigned int row=0; row < this->rows(); row++ )
for(std::size_t row=0; row < this->rows(); row++ )
{
for(unsigned int col=0; col < this->cols(); col++ )
for(std::size_t col=0; col < this->cols(); col++ )
{
this->m_data[rawIndexRowMajor(row,col)] = 0.0;
}
}
}

template<unsigned int nRows, unsigned int nCols>
unsigned int MatrixFixSize<nRows,nCols>::rows() const
std::size_t MatrixFixSize<nRows,nCols>::rows() const
{
return nRows;
}

template<unsigned int nRows, unsigned int nCols>
unsigned int MatrixFixSize<nRows,nCols>::cols() const
std::size_t MatrixFixSize<nRows,nCols>::cols() const
{
return nCols;
}
Expand All @@ -257,9 +257,9 @@ namespace iDynTree
assert(nCols == mat.cols());
assert(nRows == mat.rows());

for(unsigned int i = 0; i < nRows; i++)
for(std::size_t i = 0; i < nRows; i++)
{
for(unsigned int j = 0; j < nCols; j++)
for(std::size_t j = 0; j < nCols; j++)
{
this->m_data[this->rawIndexRowMajor(i,j)] = mat(i, j);
}
Expand All @@ -268,23 +268,23 @@ namespace iDynTree
}

template<unsigned int nRows, unsigned int nCols>
double& MatrixFixSize<nRows,nCols>::operator()(const unsigned int row, const unsigned int col)
double& MatrixFixSize<nRows,nCols>::operator()(const std::size_t row, const std::size_t col)
{
assert(row < nRows);
assert(col < nCols);
return this->m_data[rawIndexRowMajor(row,col)];
}

template<unsigned int nRows, unsigned int nCols>
double MatrixFixSize<nRows,nCols>::operator()(const unsigned int row, const unsigned int col) const
double MatrixFixSize<nRows,nCols>::operator()(const std::size_t row, const std::size_t col) const
{
assert(row < nRows);
assert(col < nCols);
return this->m_data[rawIndexRowMajor(row,col)];
}

template<unsigned int nRows, unsigned int nCols>
double MatrixFixSize<nRows,nCols>::getVal(const unsigned int row, const unsigned int col) const
double MatrixFixSize<nRows,nCols>::getVal(const std::size_t row, const std::size_t col) const
{
if( row >= this->rows() ||
col >= this->cols() )
Expand All @@ -297,7 +297,7 @@ namespace iDynTree
}

template<unsigned int nRows, unsigned int nCols>
bool MatrixFixSize<nRows,nCols>::setVal(const unsigned int row, const unsigned int col, const double new_el)
bool MatrixFixSize<nRows,nCols>::setVal(const std::size_t row, const std::size_t col, const double new_el)
{
if( row >= this->rows() ||
col >= this->cols() )
Expand All @@ -321,9 +321,9 @@ namespace iDynTree
template<unsigned int nRows, unsigned int nCols>
void MatrixFixSize<nRows,nCols>::fillColMajorBuffer(double* colMajorBuf) const
{
for(unsigned int row = 0; row < this->rows(); row++ )
for(std::size_t row = 0; row < this->rows(); row++ )
{
for(unsigned int col = 0; col < this->cols(); col++ )
for(std::size_t col = 0; col < this->cols(); col++ )
{
colMajorBuf[this->rawIndexColMajor(row,col)] =
this->m_data[this->rawIndexRowMajor(row,col)];
Expand All @@ -332,13 +332,13 @@ namespace iDynTree
}

template<unsigned int nRows, unsigned int nCols>
unsigned int MatrixFixSize<nRows,nCols>::rawIndexRowMajor(int row, int col) const
std::size_t MatrixFixSize<nRows,nCols>::rawIndexRowMajor(std::size_t row, std::size_t col) const
{
return (nCols*row + col);
}

template<unsigned int nRows, unsigned int nCols>
unsigned int MatrixFixSize<nRows,nCols>::rawIndexColMajor(int row, int col) const
std::size_t MatrixFixSize<nRows,nCols>::rawIndexColMajor(std::size_t row, std::size_t col) const
{
return (row + nRows*col);
}
Expand All @@ -348,9 +348,9 @@ namespace iDynTree
{
std::stringstream ss;

for(unsigned int row=0; row < this->rows(); row++ )
for(std::size_t row=0; row < this->rows(); row++ )
{
for(unsigned int col=0; col < this->cols(); col++ )
for(std::size_t col=0; col < this->cols(); col++ )
{
ss << this->m_data[this->rawIndexRowMajor(row,col)] << " ";
}
Expand Down Expand Up @@ -383,3 +383,4 @@ namespace iDynTree
}

#endif /* IDYNTREE_MATRIX_FIX_SIZE_H */

Loading

0 comments on commit 87f73db

Please sign in to comment.