moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
mesh_filter_base.hpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Suat Gedikli */
36
37#pragma once
38
39#include <cstdint>
40#include <map>
44#include <Eigen/Geometry> // for Isometry3d
45#include <queue>
46#include <thread>
47#include <condition_variable>
48#include <mutex>
49
50// forward declarations
51namespace shapes
52{
53class Mesh;
54}
55
56namespace mesh_filter
57{
58MOVEIT_CLASS_FORWARD(Job); // Defines JobPtr, ConstPtr, WeakPtr... etc
59MOVEIT_CLASS_FORWARD(GLMesh); // Defines GLMeshPtr, ConstPtr, WeakPtr... etc
60
61typedef unsigned int MeshHandle;
62typedef uint32_t LabelType;
63
65{
66 // inner types and typedefs
67public:
68 typedef std::function<bool(MeshHandle, Eigen::Isometry3d&)> TransformCallback;
69 // \todo @suat: to avoid a few comparisons, it would be much nicer if background = 14 and shadow = 15 (near/far clip
70 // can be anything below that)
71 // this would allow me to do a single comparison instead of 3, in the code i write
72 enum
73 {
75 SHADOW = 1,
79 };
80
81public:
89 MeshFilterBase(const TransformCallback& transform_callback, const SensorModel::Parameters& sensor_parameters,
90 const std::string& render_vertex_shader = "", const std::string& render_fragment_shader = "",
91 const std::string& filter_vertex_shader = "", const std::string& filter_fragment_shader = "");
92
95
103 MeshHandle addMesh(const shapes::Mesh& mesh);
104
110 void removeMesh(MeshHandle mesh_handle);
111
118 void filter(const void* sensor_data, GLushort type, bool wait = false) const;
119
128 void getFilteredLabels(LabelType* labels) const;
129
135 void getFilteredDepth(float* depth) const;
136
146 void getModelLabels(LabelType* labels) const;
147
153 void getModelDepth(float* depth) const;
154
162 void setShadowThreshold(float threshold);
163
169 void setTransformCallback(const TransformCallback& transform_callback);
170
176 void setPaddingScale(float scale);
177
182 void setPaddingOffset(float offset);
183
184protected:
188 void initialize(const std::string& render_vertex_shader, const std::string& render_fragment_shader,
189 const std::string& filter_vertex_shader, const std::string& filter_fragment_shader);
190
194 void deInitialize();
195
199 void run(const std::string& render_vertex_shader, const std::string& render_fragment_shader,
200 const std::string& filter_vertex_shader, const std::string& filter_fragment_shader);
201
207 void doFilter(const void* sensor_data, const int encoding) const;
208
214 void addMeshHelper(MeshHandle handle, const shapes::Mesh& cmesh);
215
220 bool removeMeshHelper(MeshHandle handle);
221
226 void addJob(const JobPtr& job) const;
227
234 void setSize(unsigned int width, unsigned int height);
235
237 std::map<MeshHandle, GLMeshPtr> meshes_;
238
240 SensorModel::ParametersPtr sensor_parameters_;
241
244
248
250 std::thread filter_thread_;
251
253 mutable std::condition_variable jobs_condition_;
254
256 mutable std::mutex jobs_mutex_;
257
259 mutable std::queue<JobPtr> jobs_queue_;
260
262 mutable std::mutex meshes_mutex_;
263
265 mutable std::mutex transform_callback_mutex_;
266
268 bool stop_;
269
271 GLRendererPtr mesh_renderer_;
272
274 GLRendererPtr depth_filter_;
275
277 GLuint canvas_;
278
281
284
287
290
293
296};
297} // namespace mesh_filter
#define MOVEIT_CLASS_FORWARD(C)
GLMesh represents a mesh from geometric_shapes for rendering in GL frame buffers.
Definition gl_mesh.hpp:59
This class is used to execute functions within the thread that holds the OpenGL context.
void setTransformCallback(const TransformCallback &transform_callback)
set the callback for retrieving transformations for each mesh.
std::mutex transform_callback_mutex_
mutex for synchronization of setting/calling transform_callback_
void doFilter(const void *sensor_data, const int encoding) const
the filter method that does the magic
void getModelLabels(LabelType *labels) const
retrieves the labels of the rendered model
std::map< MeshHandle, GLMeshPtr > meshes_
storage for meshed to be filtered
void filter(const void *sensor_data, GLushort type, bool wait=false) const
label/remove pixels from input depth-image
SensorModel::ParametersPtr sensor_parameters_
the parameters of the used sensor model
GLuint sensor_depth_texture_
handle depth texture from sensor data
GLuint shadow_threshold_location_
handle to GLSL location of shadow threshold
MeshFilterBase(const TransformCallback &transform_callback, const SensorModel::Parameters &sensor_parameters, const std::string &render_vertex_shader="", const std::string &render_fragment_shader="", const std::string &filter_vertex_shader="", const std::string &filter_fragment_shader="")
Constructor.
bool removeMeshHelper(MeshHandle handle)
used within a Job to allow the main thread removing meshes
std::queue< JobPtr > jobs_queue_
OpenGL job queue that need to be processed by the worker thread.
void run(const std::string &render_vertex_shader, const std::string &render_fragment_shader, const std::string &filter_vertex_shader, const std::string &filter_fragment_shader)
filtering thread
bool stop_
indicates whether the filtering loop should stop
void addMeshHelper(MeshHandle handle, const shapes::Mesh &cmesh)
used within a Job to allow the main thread adding meshes
float padding_offset_
padding offset
GLRendererPtr depth_filter_
second pass renderer for filtering the results of first pass
void removeMesh(MeshHandle mesh_handle)
removes a mesh given by its handle
GLRendererPtr mesh_renderer_
first pass renderer for rendering the mesh
void initialize(const std::string &render_vertex_shader, const std::string &render_fragment_shader, const std::string &filter_vertex_shader, const std::string &filter_fragment_shader)
initializes OpenGL related things as well as renderers
void addJob(const JobPtr &job) const
add a Job for the main thread that needs to be executed there
MeshHandle next_handle_
next handle to be used for next mesh that is added
void setShadowThreshold(float threshold)
set the shadow threshold. points that are further away than the rendered model are filtered out....
MeshHandle min_handle_
Handle values below this are all taken (this variable is used for more efficient computation of next_...
MeshHandle addMesh(const shapes::Mesh &mesh)
adds a mesh to the filter object.
TransformCallback transform_callback_
callback function for retrieving the mesh transformations
std::function< bool(MeshHandle, Eigen::Isometry3d &)> TransformCallback
void setPaddingOffset(float offset)
set the offset component of padding. This value is added to the scaled sensor-specific constant compo...
void getModelDepth(float *depth) const
retrieves the depth values of the rendered model
void getFilteredLabels(LabelType *labels) const
retrieves the labels of the input data
std::thread filter_thread_
the filtering thread that also holds the OpenGL context
void getFilteredDepth(float *depth) const
retrieves the filtered depth values
void setPaddingScale(float scale)
set the scale component of padding used to multiply with sensor-specific padding coefficients to get ...
void setSize(unsigned int width, unsigned int height)
sets the size of the frame buffers
std::mutex meshes_mutex_
mutex for synchronization of updating filtered meshes
std::condition_variable jobs_condition_
condition variable to notify the filtering thread if a new image arrived
std::mutex jobs_mutex_
mutex required for synchronization of condition states
float shadow_threshold_
threshold for shadowed pixels vs. filtered pixels
GLuint canvas_
canvas element (screen-filling quad) for second pass
Abstract Interface defining Sensor Parameters.
unsigned int MeshHandle