ANYbotics / grid_map

Universal grid map library for mobile robotic mapping
BSD 3-Clause "New" or "Revised" License
2.67k stars 808 forks source link

zero copy in GridMapMsgHelpers.hpp #225

Open williamhaarhoff opened 4 years ago

williamhaarhoff commented 4 years ago

I am unable to zero copy from a gridmap message type into the corresponding eigen matrix using the grid_map::multiArrayMessageMapToMatrixEigen() function in GridMapMessageHelpers.hpp.

Here is code to reproduce this:

// create a minimal gridmap
grid_map::GridMap map({"foo"});
map.setGeometry(grid_map::Length(1.0, 1.0), 0.2, grid_map::Position(0.0, 0.0));

// for convenience generate a valid gridmap message
grid_map_msgs::GridMap msg;
grid_map::GridMapRosConverter converter;
converter.toMessage(map, msg);

// attempt to zero copy msg into a matrix
msg.data[0].data[0] = 1.f;  // mark data at this index
grid_map::Matrix mat;
if(!grid_map::multiArrayMessageMapToMatrixEigen(msg.data[0], mat))
{
    return -1;
}

// verify the addresses of the data
std::cout<<"msg value:"<<msg.data[0].data[0]<<std::endl;
std::cout<<"mat value: "<<mat(0, 0)<<std::endl;

std::cout<<"msg address" <<&msg.data[0].data[0]<<std::endl;
std::cout<<"mat address" <<&mat(0, 0)<<std::endl;

The output of which was:

msg value:1
mat value: 1
msg address0x55d9e2d209b0
mat address0x55d9e2d208e0
maximilianwulf commented 4 years ago

Hi @williamhaarhoff, thank you for the issue, we will have a look.