pcl-converter.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "pcl-converter.hpp"
  2. // Get RGB values based on normals - texcoords, normals value [u v]
  3. std::tuple<uint8_t, uint8_t, uint8_t> PCLConverter::get_texcolor(rs2::video_frame texture, rs2::texture_coordinate texcoords)
  4. {
  5. const int w = texture.get_width(), h = texture.get_height();
  6. // convert normals [u v] to basic coords [x y]
  7. int x = std::min(std::max(int(texcoords.u * w + .5f), 0), w - 1);
  8. int y = std::min(std::max(int(texcoords.v * h + .5f), 0), h - 1);
  9. int idx = x * texture.get_bytes_per_pixel() + y * texture.get_stride_in_bytes();
  10. const auto texture_data = reinterpret_cast<const uint8_t *>(texture.get_data());
  11. return std::tuple<uint8_t, uint8_t, uint8_t>(texture_data[idx], texture_data[idx + 1], texture_data[idx + 2]);
  12. }
  13. PtrCloud PCLConverter::points_to_pcl(const rs2::points &points, const rs2::video_frame &color)
  14. {
  15. auto sp = points.get_profile().as<rs2::video_stream_profile>();
  16. PtrCloud cloud(new PointCloudRGB);
  17. // Config of PCL Cloud object
  18. cloud->width = static_cast<uint32_t>(sp.width());
  19. cloud->height = static_cast<uint32_t>(sp.height());
  20. cloud->is_dense = false;
  21. cloud->points.resize(points.size());
  22. auto tex_coords = points.get_texture_coordinates();
  23. auto vertices = points.get_vertices();
  24. // Iterating through all points and setting XYZ coordinates
  25. // and RGB values
  26. for (int i = 0; i < points.size(); ++i)
  27. {
  28. cloud->points[i].x = vertices[i].x;
  29. cloud->points[i].y = vertices[i].y;
  30. cloud->points[i].z = vertices[i].z;
  31. std::tuple<uint8_t, uint8_t, uint8_t> current_color;
  32. current_color = get_texcolor(color, tex_coords[i]);
  33. // Reversed order- 2-1-0 because of BGR (Blue, Green, Red) model used in camera
  34. cloud->points[i].r = std::get<2>(current_color);
  35. cloud->points[i].g = std::get<1>(current_color);
  36. cloud->points[i].b = std::get<0>(current_color);
  37. }
  38. return cloud;
  39. }
  40. void PCLConverter::convert_and_save_ply(const std::string &fileName, const rs2::points &points, const rs2::video_frame &color)
  41. {
  42. auto cloud = points_to_pcl(points, color);
  43. pcl::io::savePLYFile(fileName, *cloud);
  44. }