Point cloud visualizer does not work properly - c++

I run the point cloud visualizer simpleVis from the code given in the official docu (see
here.)
Below you find a shortened version of the code.
#include <iostream>
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
using namespace std::chrono_literals;
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// --------------------------------------------
// -----Open 3D viewer and add point cloud-----
// --------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
// --------------
// -----Main-----
// --------------
int
main (int argc, char** argv)
{
// ------------------------------------
// -----Create example point cloud-----
// ------------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
std::cout << "Generating example point clouds.\n\n";
// We're going to make an ellipse extruded along the z-axis. The colour for
// the XYZRGB cloud will gradually go from red to green to blue.
std::uint8_t r(255), g(15), b(15);
for (float z(-1.0); z <= 1.0; z += 0.05)
{
for (float angle(0.0); angle <= 360.0; angle += 5.0)
{
pcl::PointXYZ basic_point;
basic_point.x = 0.5 * std::cos (pcl::deg2rad(angle));
basic_point.y = sinf (pcl::deg2rad(angle));
basic_point.z = z;
basic_cloud_ptr->points.push_back(basic_point);
pcl::PointXYZRGB point;
point.x = basic_point.x;
point.y = basic_point.y;
point.z = basic_point.z;
std::uint32_t rgb = (static_cast<std::uint32_t>(r) << 16 |
static_cast<std::uint32_t>(g) << 8 | static_cast<std::uint32_t>(b));
point.rgb = *reinterpret_cast<float*>(&rgb);
point_cloud_ptr->points.push_back (point);
}
if (z < 0.0)
{
r -= 12;
g += 12;
}
else
{
g -= 12;
b += 12;
}
}
basic_cloud_ptr->width = basic_cloud_ptr->size ();
basic_cloud_ptr->height = 1;
point_cloud_ptr->width = point_cloud_ptr->size ();
point_cloud_ptr->height = 1;
pcl::visualization::PCLVisualizer::Ptr viewer;
viewer = simpleVis(basic_cloud_ptr);
while (!viewer->wasStopped ())
{
viewer->resetCamera();
viewer->spinOnce (100);
std::this_thread::sleep_for(100ms);
}
}
Consequently, a window pops up which should visualize the point cloud.
However, I only see green, red and black areas as in the image below.
Then I press r and the following appears:
As far as I understand it, the appearing object is the point cloud.
Now, if I try to move it around. The point cloud "smears" as shown the image below.
I figured the following out:
If I run the simpler cloud viewer as described
here.
The visualisation works properly.
In the last few months, I tried to run a Python library for point cloud visualization that also uses
PCL under the hood. The problem also appears there.
Do you have any idea what the problem is?

This is a deep zoom-in of the meeting of the axes at (0,0,0).
To see more of the point cloud, you need to zoom-out (roll the mouse wheel).
Zoom-out may be limited depending on the definition of focus and the point of view.
Pressing the 'f' key when the mouse cursor hovers over the center will usually take you far enough away to zoom out.
Once you have a reasonable view, you can press 'c' to get (in the stdout) the camera parameters and set them in the code.

Related

How to correctly use VTK ConstrainedDelaunay2D?

I've started from the VTK ConstrainedDelaunay2D example and added my own points:
#include <vtkSmartPointer.h>
#include <vtkDelaunay2D.h>
#include <vtkCellArray.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolygon.h>
#include <vtkMath.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkNamedColors.h>
#include <vtkVersionMacros.h> // For version macros
int main(int, char *[])
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
int ptsHeight = 400;
std::vector<std::vector<int>> pts{ {166, 127},{103, 220},{166, 190},{174, 291},{189, 226},{227, 282},{213, 187},{242, 105},{196, 131},{182, 83} };
for (size_t i = 0; i < pts.size(); i++)
{
// !important: flip y
int x = pts[i][0];
int y = ptsHeight - pts[i][1];
points->InsertNextPoint(x, y, 0);
}
vtkSmartPointer<vtkPolyData> aPolyData = vtkSmartPointer<vtkPolyData>::New();
aPolyData->SetPoints(points);
// Create a cell array to store the polygon in
vtkSmartPointer<vtkCellArray> aCellArray = vtkSmartPointer<vtkCellArray>::New();
// Define a polygonal hole with a clockwise polygon
vtkSmartPointer<vtkPolygon> aPolygon = vtkSmartPointer<vtkPolygon>::New();
for (unsigned int i = 0; i < pts.size(); i++)
{
aPolygon->GetPointIds()->InsertNextId(i);
}
aCellArray->InsertNextCell(aPolygon);
// Create a polydata to store the boundary. The points must be the
// same as the points we will triangulate.
vtkSmartPointer<vtkPolyData> boundary =
vtkSmartPointer<vtkPolyData>::New();
boundary->SetPoints(aPolyData->GetPoints());
boundary->SetPolys(aCellArray);
// Triangulate the grid points
vtkSmartPointer<vtkDelaunay2D> delaunay =
vtkSmartPointer<vtkDelaunay2D>::New();
delaunay->SetInputData(aPolyData);
delaunay->SetSourceData(boundary);
// Visualize
vtkSmartPointer<vtkPolyDataMapper> meshMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
meshMapper->SetInputConnection(delaunay->GetOutputPort());
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkActor> meshActor =
vtkSmartPointer<vtkActor>::New();
meshActor->SetMapper(meshMapper);
meshActor->GetProperty()->EdgeVisibilityOn();
meshActor->GetProperty()->SetEdgeColor(colors->GetColor3d("Peacock").GetData());
meshActor->GetProperty()->SetInterpolationToFlat();
meshActor->GetProperty()->SetBackfaceCulling(true);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(meshActor);
//renderer->AddActor(boundaryActor);
renderer->SetBackground(colors->GetColor3d("Mint").GetData());
// Render and interact
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
I'm experiencing two issues:
I get different results if I flip the Y coordinates: why is that ?
Why are there faces pointing in the wrong direction (flipped normal / wrong winding )?
Here's what I mean by the 1st issue:
If I don't flip the Y coordinates I get this:
I get the same effect if I don't flip the Y axis but insert the boundary polygon in reverse order:
for (unsigned int i = 0; i < pts.size(); i++)
{
aPolygon->GetPointIds()->InsertNextId(pts.size() - 1 - i);
}
I don't think I fully understand how the boundary/constraint works.
I thoght that the same points should produce the same triangulation wether the vertices are flipped vertically or not. (I suspect the order of indices changes then ?)
Regarding the second issue (unpredictable flipped faces) I'm not sure what the best way forward is. I had a look at the vtkDelaunay2D class and couldn't find anything related.
(I've tried setting projection plane mode to VTK_DELAUNAY_XY_PLANE, but it didn't seem to affect the output)
I've also tried to use vtkPolyDataNormals but got no output:
vtkSmartPointer<vtkPolyDataNormals> normalGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
normalGenerator->SetInputData(delaunay->GetOutput());
normalGenerator->ComputePointNormalsOff();
normalGenerator->ComputeCellNormalsOn();
normalGenerator->FlipNormalsOn();
normalGenerator->Update();
(normalGenerator's output has 0 cells and points)
Is there a way to compute constrained delaunay triangulation for a list of 2d points and ensure all the faces point the same way ? (If so, how ? Would it be possible to do this with the vtkDelaunay2D class alone or is it necessary to use other filters?)
Any hints/tips are more than welcome :)
I'm using VTK 8.2 by the way.
the flipping in y effectively reverses the faces orientation (what is clockwise becomes anti-clockwise, like in a mirror).
I'm not sure I can reproduce your example above. A quick test in python seems to give the expected behavior, maybe you can start from this and map it to your c++ version:
import vedo
pts = [
[166, 127],
[103, 220],
[166, 190],
[174, 291],
[189, 226],
[227, 282],
[213, 187],
[242, 105],
[196, 131],
[182, 83],
]
ids = [[2,4,6], [0,2,8]] # faces to erase by pt-index (clockwise)
dly = vedo.delaunay2D(pts, mode='xy', boundaries=ids)
dly.c('grey5').lc('red4').lw(2)
labels = vedo.Points(pts).labels('id').z(1)
vedo.show(labels, dly, axes=1)

drawing bezier paths with cairo

I need to draw a collection of Bezier paths with the help of Cairo in C++.
So far I can draw a single path.
But I need to draw many paths consisting of multiple bezier curves and lines.
I load collection of paths from the external dictionaty generated by python.
Here, for this minimal example I introduce a path by the variable path0.
It is a vector, consisting of 2 subvectors. The first one represents a bezier spline, while the second one is a simple straight line.
The first subvector naturally has 4 components, while the second one has just 2.
Here is the example of my code.
#include <iostream>
#include <vector>
#include "cairo.h"
#include <string>
int main(int argc, char* argv[])
{
cairo_surface_t* surface;
cairo_t* cr;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 1000);
cr = cairo_create(surface);
cairo_scale(cr, 1, 1);
cairo_set_line_width(cr, 1);
cairo_set_source_rgb(cr, 0, 0, 0);
// Here goes the svg-path. It is a bezier curve and unconnected line.
std::vector<std::vector<std::vector<double>>> path0 = { {{82.801,-204.48},{66.241,-204.48},{52.561,-214.56},{52.561,-235.44}},{{277.921,-192.24},{277.921,-173.5}} };
// Here I put the initial point in the middle of the canvas.
double x0 = 300+path0[0][0][0];
double y0 = 700+ path0[0][0][1];
cairo_move_to(cr, x0, y0);
// Parsing path
for (int i = 0; i < path0.size(); i++) {
if (path0[i].size() == 4) {
cairo_rel_curve_to(cr, path0[i][1][0], path0[i][1][1], path0[i][2][0], path0[i][2][1], path0[i][3][0], path0[i][3][1]);
cairo_stroke(cr);
}
if (path0[i].size() == 2) {
cairo_rel_line_to(cr, symbol[0][i][1][0], symbol[0][i][1][1]);
cairo_stroke(cr);
}
}
cairo_surface_write_to_png(surface, "stroke.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
The point is it I see only one curve on the canvas. Probably, the second one is outside.
Could you help me to understand what I'm doing wrong?
Yaroslav.
Could you clarify what you are expecting to see? With the following change, I get something like two lines, but I am not sure it is what you want to see.
--- t.cpp.orig 2020-10-13 17:00:12.404871474 +0200
+++ t.cpp 2020-10-13 17:04:20.573101739 +0200
## -29,13 +29,12 ## int main(int argc, char* argv[])
for (int i = 0; i < path0.size(); i++) {
if (path0[i].size() == 4) {
cairo_rel_curve_to(cr, path0[i][1][0], path0[i][1][1], path0[i][2][0], path0[i][2][1], path0[i][3][0], path0[i][3][1]);
- cairo_stroke(cr);
}
if (path0[i].size() == 2) {
- cairo_rel_line_to(cr, symbol[0][i][1][0], symbol[0][i][1][1]);
- cairo_stroke(cr);
+ cairo_rel_line_to(cr, path0[i][1][0], path0[i][1][1]);
}
}
+ cairo_stroke(cr);
cairo_surface_write_to_png(surface, "stroke.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
Result is:
What happens in the above program is:
First you call cairo_move_to(cr, 382.8, 495.5). This decides where the following curve begins.
Then there is call to cairo_rel_curve_to(). This adds a curve from the current point to something like 382.8+52.5, 495.5-235.4. The other two points describe how the curve is bent.
Next there is a call to cairo_rel_line_to(cr, 277.9, -173.5);.
In my version, this adds a line segment starting from the end of the curve.
In your version, the call to cairo_stroke() deleted the path. Since there is no current point, the line_to does not do anything (well, it sets the current point for following drawing commands).

Intel RealSense Depth Camera D435i noises and mounds

Intel RealSense Depth Camera D435i.
I try to capture an image and save it as stl format.
I use this project provided by Intel to achieve this task.
https://github.com/IntelRealSense/librealsense/releases/download/v2.29.0/Intel.RealSense.SDK.exe
In the solution there is an application named PointCloud.
I modified a little the application to have a clear image.
But even with the basic code, the result is not very satisfying.
I capture a smooth surface but there are many little bumps on result.
I don't know if the problem comes from the SDK or from the camera.
I check the result in MeshLab which is a great 3D tool.
Any idea ?
The result (a smooth table surface) :
Here is my code C++ (I added some filters only but without filters I have the same problem) :
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include "example.hpp" // Include short list of convenience functions for rendering
#include <algorithm> // std::min, std::max
#include <iostream>
#include <Windows.h>
#include <imgui.h>
#include "imgui_impl_glfw.h"
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include "tchar.h"
// Helper functions
void register_glfw_callbacks(window& app, glfw_state& app_state);
int main(int argc, char * argv[]) try
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
// Create a simple OpenGL window for rendering:
window app(1280, 720, "Capron 3D");
ImGui_ImplGlfw_Init(app, false);
bool capture = false;
HWND hWnd;
hWnd = FindWindow(NULL, _T("Capron 3D"));
ShowWindow(hWnd, SW_MAXIMIZE);
// Construct an object to manage view state
glfw_state app_state;
// register callbacks to allow manipulation of the pointcloud
register_glfw_callbacks(app, app_state);
app_state.yaw = 3.29;
app_state.pitch = 0;
// Declare pointcloud object, for calculating pointclouds and texture mappings
rs2::pointcloud pc;
// We want the points object to be persistent so we can display the last cloud when a frame drops
rs2::points points;
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Start streaming with default recommended configuration
pipe.start();
rs2::decimation_filter dec_filter;
rs2::spatial_filter spat_filter;
rs2::threshold_filter thres_filter;
rs2::temporal_filter temp_filter;
float w = static_cast<float>(app.width());
float h = static_cast<float>(app.height());
while (app) // Application still alive?
{
static const int flags = ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove;
ImGui_ImplGlfw_NewFrame(1);
ImGui::SetNextWindowSize({ app.width(), app.height() });
ImGui::Begin("app", nullptr, flags);
// Set options for the ImGui buttons
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, { 1, 1, 1, 1 });
ImGui::PushStyleColor(ImGuiCol_Button, { 36 / 255.f, 44 / 255.f, 51 / 255.f, 1 });
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, { 40 / 255.f, 170 / 255.f, 90 / 255.f, 1 });
ImGui::PushStyleColor(ImGuiCol_ButtonActive, { 36 / 255.f, 44 / 255.f, 51 / 255.f, 1 });
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 12);
ImGui::SetCursorPos({ 10, 10 });
if (ImGui::Button("Capturer", { 100, 50 }))
{
capture = true;
}
// Wait for the next set of frames from the camera
auto frames = pipe.wait_for_frames();
auto color = frames.get_color_frame();
// For cameras that don't have RGB sensor, we'll map the pointcloud to infrared instead of color
if (!color)
color = frames.get_infrared_frame();
// Tell pointcloud object to map to this color frame
pc.map_to(color);
auto depth = frames.get_depth_frame();
/*spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, 50);
depth = spat_filter.process(depth);*/
spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, 1);
depth = spat_filter.process(depth);
spat_filter.set_option(RS2_OPTION_HOLES_FILL, 2);
depth = spat_filter.process(depth);
//temp_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, 1);
//depth = temp_filter.process(depth);
// Generate the pointcloud and texture mappings
points = pc.calculate(depth);
// Upload the color frame to OpenGL
app_state.tex.upload(color);
thres_filter.set_option(RS2_OPTION_MIN_DISTANCE, 0);
depth = thres_filter.process(depth);
// Draw the pointcloud
draw_pointcloud(int(w) / 2, int(h) / 2, app_state, points);
if (capture)
{
points.export_to_ply("My3DFolder\\new.ply", depth);
return EXIT_SUCCESS;
}
ImGui::PopStyleColor(4);
ImGui::PopStyleVar();
ImGui::End();
ImGui::Render();
}
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
MessageBox(0, "Erreur connexion RealSense. Veuillez vérifier votre caméra 3D.", "Capron Podologie", 0);
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
I found the answer,
I was using HolesFill filter.
//These lines
spat_filter.set_option(RS2_OPTION_HOLES_FILL, 2);
depth = spat_filter.process(depth);
And the holes fill algorithm is prédictif. It creates points but the coordinates of these points are not exactly correct. The second parameter of the spat_filter.set_option is between 1 and 5. More I increade this parameter, more noised is the result.
If I remove these lines, I have a clearer result.
But this time I have many holes on the result.

PCL: PCLvisualizer multiple point clouds (XYZ) in same viewport with different colours

I have a vector of point clouds of spatial points (XYZ only) which I am trying to display in the same window with different colours. I am using custom colour handlers in the PCLvisualizer.
Unfortunately the colours aren't different in the viewer on display however, but all the points are displayed in the same colour (the second one). It seems like the colour is changed for all the previous points in the viewer each time a new point cloud is added.
Here is my code:
pcl::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor (0, 0, 0);
std::stringstream cloud_name;
int counter(0);
pcl::RGB rgb_color;
for (auto curr_cloud : clouds_vector)
{
rgb_color = pcl::GlasbeyLUT::at(counter);
++counter;
cloud_name.str("");
cloud_name << "Cloud " << counter;
if (counter < 5)
{
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> first_color(curr_cloud, rgb_color.r, rgb_color.g, rgb_color.b);
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, first_color, cloud_name.str());
}
else
{
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> second_color(curr_cloud, rgb_color.r, rgb_color.g, rgb_color.b);
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, second_color, cloud_name.str());
}
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 8, cloud_name.str());
}
Any ideas why this might be and what to change?
Many thanks to you all in advance!
EDIT: I have tried around a little, and the problem doesn't seem to be the way I'm visualising the point clouds itself (above described way is working with freshly created clouds with random values.
In my use case I am extracting multiple point clouds with the help of their indices from a single point cloud. If I do that they still all hold the same colour upon visualisation. I still can't figure out why that's the case though and am happy for any suggestions. Here is the full code with the extraction (for the benefit of a minimal example with a randomly created mother point cloud from which the extraction is taking place):
PointCloudT::Ptr cloud (new PointCloudT);
// 100 points in point cloud, random coordinates
cloud->resize(100);
for (PointCloudT::iterator cloud_it (cloud->begin()); cloud_it != cloud->end(); ++cloud_it) {
cloud_it->x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_it->y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_it->z = 1024 * rand () / (RAND_MAX + 1.0f);
}
// Create index vectors containing 20 indices each
std::vector <pcl::PointIndices> point_indices_vector;
int offset(20);
pcl::PointIndices indices;
for (int i=0;i<5;++i) {
for (int j=0;j<20;++j) {
indices.indices.push_back(j + i * offset);
}
point_indices_vector.push_back(indices);
indices.indices.clear(); // <----- THIS WAS MISSING
}
// Create extraction object to copy points from 100 strong cloud to 20-point clouds
pcl::ExtractIndices<PointT> extract;
extract.setInputCloud (cloud);
extract.setNegative (false);
// Extract points and copy them to clouds vector
std::vector<PointCloudT::Ptr, Eigen::aligned_allocator<PointCloudT::Ptr> > clouds_vector;
PointCloudT::Ptr curr_segment_cloud;
for (auto curr_index_vector : point_indices_vector) {
curr_segment_cloud.reset (new PointCloudT);
// Copy points of current line segment from source cloud into current segment cloud
extract.setIndices(boost::make_shared<const pcl::PointIndices> (curr_index_vector));
extract.filter (*curr_segment_cloud);
// Push back point cloud into return vector
clouds_vector.push_back(curr_segment_cloud);
}
// Create viewer
pcl::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor (0, 0, 0);
// Visualize point clouds from clouds vector
std::stringstream cloud_name;
int counter(0);
pcl::RGB rgb;
for (auto curr_cloud : clouds_vector) {
++counter;
cloud_name.str("");
cloud_name << "Cloud " << counter;
// Generate unique colour
rgb = pcl::GlasbeyLUT::at(counter);
// Create colour handle
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> colour_handle(curr_cloud, rgb.r, rgb.g, rgb.b);
// Add points to viewer and set parameters
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, colour_handle, cloud_name.str());
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 8, cloud_name.str());
}
// Keep viewer running
while (!viewer.wasStopped()) {
viewer.spinOnce(100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
Problem solved!
So if anybody wants to use the code... both of the snippets above are now working. (I found the mistake and corrected it in the above code snippet).
I'm embarrassed to say that I simply forgot to clear my extraction indices vector before extracting the points. So the last cloud was the full cloud again and was plotting over all of the before added points making them invisible. This way all the points seemed the same colour. o.O

Why does QPainter::drawPoint draw a horizontal line segment?

I'm trying to draw a 3-pixel large point with QPainter. But the following code instead draws a horizontal line with width of 3 pixels.
#include <QPainter>
#include <QImage>
int main()
{
const int w=1000, h=1000;
QImage img(w, h, QImage::Format_RGBX8888);
{
QPainter p(&img);
p.fillRect(0,0,w,h,Qt::black);
p.scale(w,h);
p.setPen(QPen(Qt::red, 3./w, Qt::SolidLine, Qt::RoundCap));
p.drawPoint(QPointF(0.1,0.1));
}
img.save("test.png");
}
Here's the top left corner of the resulting image:
I am expecting to get a point which is red circle, or at least a square — but instead I get this line segment. If I comment out p.scale(w,h) and draw the point with width 3 (instead of 3./w) at position (100,100), then I get a small mostly symmetric 3-pixel in height and width point.
What's going on? Why do I get a line segment instead of point as expected? And how to fix it, without resorting to drawing an ellipse or to avoiding QPainter::scale?
I'm using Qt 5.10.0 on Linux x86 with g++ 5.5.0. The same happens on Qt 5.5.1.
It appears that QPaintEngineEx::drawPoints renders points as line segments of length 1/63.. See the following code from qtbase/src/gui/painting/qpaintengineex.cpp in the Qt sources:
void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
{
QPen pen = state()->pen;
if (pen.capStyle() == Qt::FlatCap)
pen.setCapStyle(Qt::SquareCap);
if (pen.brush().isOpaque()) {
while (pointCount > 0) {
int count = qMin(pointCount, 16);
qreal pts[64];
int oset = -1;
for (int i=0; i<count; ++i) {
pts[++oset] = points[i].x();
pts[++oset] = points[i].y();
pts[++oset] = points[i].x() + 1/63.;
pts[++oset] = points[i].y();
}
QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
stroke(path, pen);
pointCount -= 16;
points += 16;
}
} else {
for (int i=0; i<pointCount; ++i) {
qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + qreal(1/63.), points[i].y() };
QVectorPath path(pts, 2, 0);
stroke(path, pen);
}
}
}
Notice the pts[++oset] = points[i].x() + 1/63.; line in the opaque brush branch. This is the second vertex of the path — shifted with respect to the desired position of the point.
This explains why the line extends to the right of the position requested and why it depends on the scale. So, it seems the code in the OP isn't wrong for an ideal QPainter implementation, but just has come across a Qt bug (be it in the implementation of the method or in its documentation).
So the conclusion: one has to work around this problem by either using different scale, or drawing ellipses, or drawing line segments with much smaller lengths than what QPainter::drawPoints does.
I've reported this as QTBUG-70409.
Though I have not been able to pin point why exactly the problem occurs, I have got relatively close to the solution. The problem lies with scaling. I did a lots of trial and error with different scaling and width of point with the below code.
const int w=500, h=500;
const int scale = 100;
float xPos = 250;
float yPos = 250;
float widthF = 5;
QImage img(w, h, QImage::Format_RGBX8888);
{
QPainter p(&img);
p.setRenderHints(QPainter::Antialiasing);
p.fillRect(0,0,w,h,Qt::black);
p.scale(scale, scale);
p.setPen(QPen(Qt::red, widthF/(scale), Qt::SolidLine, Qt::RoundCap));
p.drawPoint(QPointF(xPos/scale, yPos/scale));
}
img.save("test.png");
The above code produces the image
My observations are
1) Due to high scaling, the point (which is just 3 pixel wide) is not able to scale proportionally at lower width (width of point), if you set width as something like 30 the round shape is visible.
2) If you want to keep the width of point low then you have to decrease the scaling.
Sadly I can not explain why at high scaling it is not expanding proportionally.