facontidavide / Bonxai

Fast, hierarchical, sparse Voxel Grid
Mozilla Public License 2.0
643 stars 53 forks source link

Not able to use move assignment on VoxelGrid? #26

Open ErykHalicki opened 5 months ago

ErykHalicki commented 5 months ago

TLDR: Is there a way to use a move assignment operator with VoxelGrid?

Hey there!

First of all I'd like to say this project is incredible, it has spend up the 3d mapping systems of the robotics project I'm working on significantly.

Also I'm kind of new to c++ so please bear with me.

I wasn't able to find a visualisation tool for the project, so I threw together a janky one using openGL, and I ran into a minor issue/question while I was making it. Here's the github repo.

After making the initial visualiser, I wanted to see my serialized file update real time, so I tried adding a function that would deserialise the map file on a regular interval and assign the new deserialized map to the visualizer "global" map so that it could be drawn.

The issue I ran into was with the assignment/move operation.

When I tried compiling this:

void updateGridFromFile(std::string inputFileName){
    std::ifstream inputFile(inputFileName, std::ios::binary);
    if (!inputFile.is_open()) {
        std::cerr << "Error: Unable to open file " << inputFileName << std::endl;
    }

    // Read the header of the file to obtain information about the voxel grid
    char header[256];
    inputFile.getline(header, 256);
    Bonxai::HeaderInfo info = Bonxai::GetHeaderInfo(header);

    // Deserialize the voxel grid from the file
    auto g = Bonxai::Deserialize<float>(inputFile, info);
    inputFile.close();
    grid=std::move(g);
}

I would get the following error message:

/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/src/visualizer.cpp: In function ‘void updateGridFromFile(std::string)’:
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/src/visualizer.cpp:240:21: error: use of deleted function ‘Bonxai::VoxelGrid<float>& Bonxai::VoxelGrid<float>::operator=(Bonxai::VoxelGrid<float>&&)’
  240 |     grid=std::move(g);
      |                     ^
In file included from /home/eryk/Desktop/Cascade/ros2_ws/src/mapping/src/visualizer.cpp:4:
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: note: ‘Bonxai::VoxelGrid<float>& Bonxai::VoxelGrid<float>::operator=(Bonxai::VoxelGrid<float>&&)’ is implicitly deleted because the default definition would be ill-formed:
  267 | class VoxelGrid
      |       ^~~~~~~~~
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const uint32_t Bonxai::VoxelGrid<float>::INNER_BITS’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const uint32_t Bonxai::VoxelGrid<float>::LEAF_BITS’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const uint32_t Bonxai::VoxelGrid<float>::Log2N’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const double Bonxai::VoxelGrid<float>::resolution’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const double Bonxai::VoxelGrid<float>::inv_resolution’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const uint32_t Bonxai::VoxelGrid<float>::INNER_MASK’, cannot use default assignment operator
/home/eryk/Desktop/Cascade/ros2_ws/src/mapping/include/bonxai/bonxai.hpp:267:7: error: non-static const member ‘const uint32_t Bonxai::VoxelGrid<float>::LEAF_MASK’, cannot use default assignment operator
gmake[2]: *** [CMakeFiles/visualizer.dir/build.make:76: CMakeFiles/visualizer.dir/src/visualizer.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:167: CMakeFiles/visualizer.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2

So I just removed the const modifier from the listed variables and now its working fine.

So I was mainly wondering if there was a better way to move data from my deserialized grid to my global grid.

Also, is it a bad idea to remove the const modifier like I did? Is there a reason there is no move assignment operator defined for VoxelGrid?

Thanks in advance!!

arjo129 commented 3 months ago

I ran into the same issue as you. Also curious, is there a reason why move constructors are not provided? I notice that some of the properties of VoxelGrid are const. I've got a PR (#31) that gives a potential implementation.