gtsam_points
Loading...
Searching...
No Matches
pointcloud_buffer.hpp
Go to the documentation of this file.
1#ifndef GLK_POINTCLOUD_BUFFER_HPP
2#define GLK_POINTCLOUD_BUFFER_HPP
3
4#include <atomic>
5#include <memory>
6#include <vector>
7#include <iostream>
8#include <Eigen/Dense>
9#include <glk/drawable.hpp>
10#include <glk/colormap.hpp>
11#include <glk/api_export.hpp>
12
13namespace glk {
14
16 std::string attribute_name;
17 int dim;
18 int stride;
20};
21
23public:
24 using Ptr = std::shared_ptr<PointCloudBuffer>;
25
26 PointCloudBuffer(int stride, int num_points);
27 PointCloudBuffer(const float* data, int stride, int num_points);
28
29 // Eigen utility constructors
30 PointCloudBuffer(const Eigen::Matrix<float, 3, -1>& points);
31 PointCloudBuffer(const Eigen::Matrix<double, 3, -1>& points);
32
33 template <typename Scalar, int Dim, typename Allocator>
34 PointCloudBuffer(const std::vector<Eigen::Matrix<Scalar, Dim, 1>, Allocator>& points);
35
36 PointCloudBuffer(const Eigen::Vector3f* points, int num_points);
37 PointCloudBuffer(const Eigen::Vector4f* points, int num_points);
38 PointCloudBuffer(const Eigen::Vector3d* points, int num_points);
39 PointCloudBuffer(const Eigen::Vector4d* points, int num_points);
40
41 virtual ~PointCloudBuffer() override;
42
43 // Add point attribute methods
44 template <int N, typename Allocator>
45 void add_normals(const std::vector<Eigen::Matrix<float, N, 1>, Allocator>& normals);
46 template <int N, typename Allocator>
47 void add_normals(const std::vector<Eigen::Matrix<double, N, 1>, Allocator>& normals);
48 template <int N>
49 void add_normals(const Eigen::Matrix<float, N, 1>* normals, int num_points);
50 template <int N>
51 void add_normals(const Eigen::Matrix<double, N, 1>* normals, int num_points);
52
53 template <typename Scalar, typename Allocator>
54 void add_color(const std::vector<Eigen::Matrix<Scalar, 4, 1>, Allocator>& colors);
55 void add_color(const Eigen::Vector4f* colors, int num_points);
56 void add_color(const Eigen::Vector4d* colors, int num_points);
57
58 void add_intensity(glk::COLORMAP colormap, const std::vector<float>& intensities, float scale = 1.0f);
59 void add_intensity(glk::COLORMAP colormap, const std::vector<double>& intensities, float scale = 1.0f);
60 void add_intensity(glk::COLORMAP colormap, const float* intensities, const int num_points, float scale = 1.0f);
61 void add_intensity(glk::COLORMAP colormap, const double* intensities, const int num_points, float scale = 1.0f);
62
63 void add_normals(const float* data, int stride, int num_points);
64 void add_color(const float* data, int stride, int num_points);
65 void add_intensity(glk::COLORMAP colormap, const float* data, int stride, int num_points, float scale = 1.0f);
66
67 void set_colormap_buffer(const std::string& attribute_name);
68 void add_colormap(std::vector<float>& cmap, float scale = 1.0f);
69 void add_colormap(std::vector<double>& cmap, float scale = 1.0);
70 void add_colormap(const float* data, int stride, int num_points, float scale = 1.0f);
71
72 void add_buffer(const std::string& attribute_name, const std::vector<float>& data);
73 void add_buffer(const std::string& attribute_name, const std::vector<double>& data);
74 template <typename Scalar, int D, typename Allocator>
75 void add_buffer(const std::string& attribute_name, const std::vector<Eigen::Matrix<Scalar, D, 1>, Allocator>& data);
76 void add_buffer(const std::string& attribute_name, int dim, const float* data, int stride, int num_points);
77
78 // Partial attribute update methods (User must ensure that stride and dim are matched with existing attribute)
79 void update_buffer_with_indices(GLuint buffer, const float* data, int stride, const unsigned int* indices, int num_indices);
80 void update_buffer_with_indices(const std::string& attribute_name, int dim, const float* data, int stride, const unsigned int* indices, int num_indices);
81
82 template <typename Scalar, int D, typename Allocator>
83 void update_points_with_indices(const std::vector<Eigen::Matrix<Scalar, D, 1>, Allocator>& points, const std::vector<unsigned int>& indices);
84 void update_points_with_indices(const Eigen::Vector3f* points, const unsigned int* indices, int num_indices);
85 void update_points_with_indices(const Eigen::Vector4f* points, const unsigned int* indices, int num_indices);
86 void update_points_with_indices(const Eigen::Vector3d* points, const unsigned int* indices, int num_indices);
87 void update_points_with_indices(const Eigen::Vector4d* points, const unsigned int* indices, int num_indices);
88 void update_points_with_indices(const float* data, int stride, const unsigned int* indices, int num_indices);
89
90 template <typename Scalar, typename Allocator>
91 void update_color_with_indices(const std::vector<Eigen::Matrix<Scalar, 4, 1>, Allocator>& colors, const std::vector<unsigned int>& indices);
92 void update_color_with_indices(const Eigen::Vector4f* colors, const unsigned int* indices, int num_indices);
93 void update_color_with_indices(const Eigen::Vector4d* colors, const unsigned int* indices, int num_indices);
94
97
98 void bind(glk::GLSLShader& shader) const;
99 void unbind(glk::GLSLShader& shader) const;
100
101 virtual void draw(glk::GLSLShader& shader) const override;
102
103 GLuint vba_id() const;
104 GLuint vbo_id() const;
105 GLuint ebo_id() const;
106
107 int get_aux_size() const;
108 const AuxBufferData& get_aux_buffer(int i) const;
109
110 int size() const { return num_points; }
111
112 size_t memory_usage() const;
113
114private:
115 mutable std::atomic_uint rendering_count;
116 int points_rendering_budget;
117
118 GLuint vao;
119 GLuint vbo;
120 GLuint ebo;
121 int stride;
122 int num_points;
123
124 GLuint cmap_bo; // buffer object for colormap attribute
125
126 std::vector<AuxBufferData> aux_buffers;
127};
128
129// template methods
130template <typename Scalar, int Dim, typename Allocator>
131PointCloudBuffer::PointCloudBuffer(const std::vector<Eigen::Matrix<Scalar, Dim, 1>, Allocator>& points) : PointCloudBuffer(points.data(), points.size()) {}
132
133template <int N, typename Allocator>
134void PointCloudBuffer::add_normals(const std::vector<Eigen::Matrix<float, N, 1>, Allocator>& normals) {
135 add_normals(normals[0].data(), sizeof(float) * N, normals.size());
136}
137
138template <int N, typename Allocator>
139void PointCloudBuffer::add_normals(const std::vector<Eigen::Matrix<double, N, 1>, Allocator>& normals) {
140 std::vector<Eigen::Vector3f> normals_f(normals.size());
141 std::transform(normals.begin(), normals.end(), normals_f.begin(), [](const Eigen::Matrix<double, N, 1>& n) { return n.template head<3>().template cast<float>(); });
142 add_normals(normals_f[0].data(), sizeof(Eigen::Vector3f), normals_f.size());
143}
144
145template <int N>
146void PointCloudBuffer::add_normals(const Eigen::Matrix<float, N, 1>* normals, int num_points) {
147 add_normals(normals->data(), sizeof(float) * N, num_points);
148}
149
150template <int N>
151void PointCloudBuffer::add_normals(const Eigen::Matrix<double, N, 1>* normals, int num_points) {
152 std::vector<Eigen::Matrix<float, N, 1>> normals_f(num_points);
153 std::transform(normals, normals + num_points, normals_f.begin(), [](const Eigen::Matrix<double, N, 1>& p) { return p.template cast<float>(); });
155}
156
157template <typename Scalar, typename Allocator>
158void PointCloudBuffer::add_color(const std::vector<Eigen::Matrix<Scalar, 4, 1>, Allocator>& colors) {
159 add_color(colors.data(), colors.size());
160}
161
162template <typename Scalar, int D, typename Allocator>
163void PointCloudBuffer::add_buffer(const std::string& attribute_name, const std::vector<Eigen::Matrix<Scalar, D, 1>, Allocator>& data) {
164 if constexpr (std::is_same<Scalar, float>::value) {
165 add_buffer(attribute_name, D, data[0].data(), sizeof(float) * D, data.size());
166 } else {
167 std::vector<Eigen::Matrix<float, D, 1>> data_f(data.size());
168 std::transform(data.begin(), data.end(), data_f.begin(), [](const Eigen::Matrix<double, D, 1>& p) { return p.template cast<float>(); });
169 add_buffer(attribute_name, D, data_f.data(), sizeof(float) * D, data_f.size());
170 }
171}
172
173template <typename Scalar, int D, typename Allocator>
174void PointCloudBuffer::update_points_with_indices(const std::vector<Eigen::Matrix<Scalar, D, 1>, Allocator>& points, const std::vector<unsigned int>& indices) {
175 if (points.size() != indices.size()) {
176 std::cerr << "error: points.size() != indices.size() (" << points.size() << " vs " << indices.size() << ")" << std::endl;
177 }
178 update_points_with_indices(points.data(), indices.data(), indices.size());
179}
180
181template <typename Scalar, typename Allocator>
182void PointCloudBuffer::update_color_with_indices(const std::vector<Eigen::Matrix<Scalar, 4, 1>, Allocator>& colors, const std::vector<unsigned int>& indices) {
183 if (colors.size() != indices.size()) {
184 std::cerr << "error: colors.size() != indices.size() (" << colors.size() << " vs " << indices.size() << ")" << std::endl;
185 }
186 update_color_with_indices(colors.data(), indices.data(), indices.size());
187}
188
189} // namespace glk
190
191#endif
Definition drawable.hpp:12
Definition glsl_shader.hpp:20
Definition pointcloud_buffer.hpp:22
GLuint vbo_id() const
PointCloudBuffer(const Eigen::Vector3f *points, int num_points)
PointCloudBuffer(const Eigen::Vector3d *points, int num_points)
std::shared_ptr< PointCloudBuffer > Ptr
Definition pointcloud_buffer.hpp:24
PointCloudBuffer(const Eigen::Matrix< double, 3, -1 > &points)
virtual ~PointCloudBuffer() override
PointCloudBuffer(const float *data, int stride, int num_points)
void add_normals(const std::vector< Eigen::Matrix< float, N, 1 >, Allocator > &normals)
Definition pointcloud_buffer.hpp:134
void add_buffer(const std::string &attribute_name, const std::vector< double > &data)
int get_aux_size() const
const AuxBufferData & get_aux_buffer(int i) const
void add_intensity(glk::COLORMAP colormap, const float *intensities, const int num_points, float scale=1.0f)
void disable_partial_rendering()
void update_points_with_indices(const std::vector< Eigen::Matrix< Scalar, D, 1 >, Allocator > &points, const std::vector< unsigned int > &indices)
Definition pointcloud_buffer.hpp:174
void add_color(const std::vector< Eigen::Matrix< Scalar, 4, 1 >, Allocator > &colors)
Definition pointcloud_buffer.hpp:158
void update_points_with_indices(const float *data, int stride, const unsigned int *indices, int num_indices)
void update_color_with_indices(const std::vector< Eigen::Matrix< Scalar, 4, 1 >, Allocator > &colors, const std::vector< unsigned int > &indices)
Definition pointcloud_buffer.hpp:182
PointCloudBuffer(const Eigen::Vector4f *points, int num_points)
PointCloudBuffer(const Eigen::Matrix< float, 3, -1 > &points)
PointCloudBuffer(int stride, int num_points)
int size() const
Definition pointcloud_buffer.hpp:110
size_t memory_usage() const
void update_buffer_with_indices(GLuint buffer, const float *data, int stride, const unsigned int *indices, int num_indices)
PointCloudBuffer(const Eigen::Vector4d *points, int num_points)
void add_buffer(const std::string &attribute_name, int dim, const float *data, int stride, int num_points)
void add_color(const float *data, int stride, int num_points)
virtual void draw(glk::GLSLShader &shader) const override
void update_points_with_indices(const Eigen::Vector3d *points, const unsigned int *indices, int num_indices)
void update_color_with_indices(const Eigen::Vector4f *colors, const unsigned int *indices, int num_indices)
void add_intensity(glk::COLORMAP colormap, const double *intensities, const int num_points, float scale=1.0f)
GLuint vba_id() const
void bind(glk::GLSLShader &shader) const
void add_intensity(glk::COLORMAP colormap, const float *data, int stride, int num_points, float scale=1.0f)
void add_intensity(glk::COLORMAP colormap, const std::vector< float > &intensities, float scale=1.0f)
void update_points_with_indices(const Eigen::Vector3f *points, const unsigned int *indices, int num_indices)
void enable_partial_rendering(int points_budget=8192 *5)
void set_colormap_buffer(const std::string &attribute_name)
void add_normals(const float *data, int stride, int num_points)
void add_color(const Eigen::Vector4f *colors, int num_points)
void update_points_with_indices(const Eigen::Vector4f *points, const unsigned int *indices, int num_indices)
void add_intensity(glk::COLORMAP colormap, const std::vector< double > &intensities, float scale=1.0f)
void add_buffer(const std::string &attribute_name, const std::vector< float > &data)
void unbind(glk::GLSLShader &shader) const
void add_color(const Eigen::Vector4d *colors, int num_points)
void update_buffer_with_indices(const std::string &attribute_name, int dim, const float *data, int stride, const unsigned int *indices, int num_indices)
void update_color_with_indices(const Eigen::Vector4d *colors, const unsigned int *indices, int num_indices)
void add_colormap(std::vector< float > &cmap, float scale=1.0f)
void add_colormap(std::vector< double > &cmap, float scale=1.0)
void add_colormap(const float *data, int stride, int num_points, float scale=1.0f)
GLuint ebo_id() const
void update_points_with_indices(const Eigen::Vector4d *points, const unsigned int *indices, int num_indices)
Definition async_buffer_copy.hpp:6
COLORMAP
Definition colormap.hpp:9
std::enable_if_t< needs_aligned_allocator< T >::value, std::shared_ptr< T > > make_shared(Args &&... args)
Definition make_shared.hpp:20
Eigen::Vector4i colormap(COLORMAP type, int x)
Definition pointcloud_buffer.hpp:15
std::string attribute_name
Definition pointcloud_buffer.hpp:16
int stride
Definition pointcloud_buffer.hpp:18
int dim
Definition pointcloud_buffer.hpp:17
GLuint buffer
Definition pointcloud_buffer.hpp:19