Skip to content

Commit

Permalink
Occupancy grid can create cubes for all the occupied voxels
Browse files Browse the repository at this point in the history
  • Loading branch information
RaduAlexandru committed May 24, 2023
1 parent 73f59aa commit 783c41f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/permuto_sdf/OccupancyGrid.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include "permuto_sdf/RaySamplesPacked.cuh"

namespace easy_pbr{
class Mesh;
}


class OccupancyGrid{
public:
Expand All @@ -29,6 +33,7 @@ public:


torch::Tensor compute_grid_points(const bool randomize_position); //makes point at the position where the values lives. So actually it return the corners of the cubes because the values live at the corners
std::shared_ptr<easy_pbr::Mesh> create_cubes_for_occupied_voxels(); //makes one cube for each occupied voxel, useful for visualization
std::tuple<torch::Tensor,torch::Tensor> compute_random_sample_of_grid_points(const int nr_of_voxels_to_select, const bool randomize_position);
RaySamplesPacked compute_samples_in_occupied_regions(const torch::Tensor& ray_origins, const torch::Tensor& ray_dirs, const torch::Tensor& ray_t_entry, const torch::Tensor& ray_t_exit, const float min_dist_between_samples, const int max_nr_samples_per_ray, const bool jitter_samples); //goes through the occupies regions and creates samples
RaySamplesPacked compute_first_sample_start_of_occupied_regions(const torch::Tensor& ray_origins, const torch::Tensor& ray_dirs, const torch::Tensor& ray_t_entry, const torch::Tensor& ray_t_exit); //creates one sample only at the beggining of the occupied space, useful for sphere tracing
Expand Down
61 changes: 61 additions & 0 deletions src/OccupancyGrid.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

//my stuff
#include "permuto_sdf/OccupancyGridGPU.cuh"
#include "easy_pbr/Mesh.h"


using torch::Tensor;
Expand Down Expand Up @@ -115,6 +116,66 @@ torch::Tensor OccupancyGrid::compute_grid_points(const bool randomize_position){
return grid_points;
}

std::shared_ptr<easy_pbr::Mesh> OccupancyGrid::create_cubes_for_occupied_voxels(){

//get the positions of the centers of the voxels
torch::Tensor grid_centers=compute_grid_points(false);
Eigen::MatrixXf grid_center_eigen=tensor2eigen(grid_centers);

//get the occupancy for each voxels
torch::Tensor grid_occupancy=get_grid_occupancy();
grid_occupancy=grid_occupancy.view({-1,1});
Eigen::MatrixXf grid_occupancy_eigen=tensor2eigen(grid_occupancy.to(torch::kFloat32));

//make a cube of the size of a voxel
// std::shared_ptr<easy_pbr::Mesh> cube=easy_pbr::Mesh::create();
float voxel_size=m_grid_extent/m_nr_voxels_per_dim;
// cube->create_box(voxel_size, voxel_size, voxel_size);

// VLOG(1) << "starting loop";

//for every occupied voxel, we put a cube there
std::vector<std::shared_ptr<easy_pbr::Mesh>> cubes_list;
for(int i=0; i<get_nr_voxels(); i++){
float occupancy=grid_occupancy_eigen(i,0);
Eigen::Vector3f pos=grid_center_eigen.row(i);

//if it's occupied, create a cube at this position
if(occupancy>0.5){
// std::shared_ptr<easy_pbr::Mesh> cube_at_pos=std::make_shared<easy_pbr::Mesh>(cube->clone());
std::shared_ptr<easy_pbr::Mesh> cube_at_pos=easy_pbr::Mesh::create();
cube_at_pos->create_box(voxel_size, voxel_size, voxel_size);
//move at position
cube_at_pos->translate_model_matrix(pos.cast<double>());
cube_at_pos->apply_model_matrix_to_cpu(true);

// std::cout << "cube to eb added is " << *(cube_at_pos) << std::endl;

cubes_list.push_back(cube_at_pos);
// std::cout <<"push cube" << std::endl;
// exit(1);
// std::cout << "first_cube is " << *(cubes_list[0]) << std::endl;
}
}
// std::cout << "first_cube is " << *(cubes_list[0]) << std::endl;




std::shared_ptr<easy_pbr::Mesh> mesh_cubes=easy_pbr::Mesh::create();
mesh_cubes->add(cubes_list);

mesh_cubes->m_vis.m_show_mesh=false;
mesh_cubes->m_vis.m_show_wireframe=true;

// VLOG(1) << "return";
std::cout << "nr cubes " << cubes_list.size() << std::endl;
// std::cout << " mesh_cubes is " << *mesh_cubes << std::endl;

return mesh_cubes;
}


std::tuple<torch::Tensor,torch::Tensor> OccupancyGrid::compute_random_sample_of_grid_points(const int nr_voxels_to_select, const bool randomize_position){

int nr_voxels_total=get_nr_voxels();
Expand Down
2 changes: 2 additions & 0 deletions src/PyBridge.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "permuto_sdf/RaySamplesPacked.cuh"
#include "permuto_sdf/TrainParams.h"
#include "permuto_sdf/NGPGui.h"
#include "easy_pbr/Mesh.h"

#include "easy_pbr/Viewer.h"

Expand Down Expand Up @@ -67,6 +68,7 @@ PYBIND11_MODULE(permuto_sdf, m) {
.def("get_nr_voxels", &OccupancyGrid::get_nr_voxels )
.def("get_nr_voxels_per_dim", &OccupancyGrid::get_nr_voxels_per_dim )
.def("compute_grid_points", &OccupancyGrid::compute_grid_points )
.def("create_cubes_for_occupied_voxels", &OccupancyGrid::create_cubes_for_occupied_voxels )
.def("compute_random_sample_of_grid_points", &OccupancyGrid::compute_random_sample_of_grid_points )
.def("check_occupancy", &OccupancyGrid::check_occupancy )
.def("update_with_density", &OccupancyGrid::update_with_density )
Expand Down

0 comments on commit 783c41f

Please sign in to comment.