Skip to content

Commit

Permalink
Use the newer viewer everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
niosus committed Feb 4, 2020
1 parent 69f3506 commit 9a77346
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 142 deletions.
6 changes: 4 additions & 2 deletions examples/simple_nodes/CMakeLists.txt
Expand Up @@ -6,7 +6,8 @@ target_link_libraries(show_objects_kitti
projections
ground_remove
folder_reader
visualization
viewer
drawable
${MY_QT_LIBRARIES}
${Boost_LIBRARIES}
${PCL_LIBRARIES}
Expand All @@ -20,7 +21,8 @@ target_link_libraries(show_objects_moosmann
projections
ground_remove
folder_reader
visualization
viewer
drawable
${MY_QT_LIBRARIES}
${Boost_LIBRARIES}
${PCL_LIBRARIES}
Expand Down
15 changes: 10 additions & 5 deletions examples/simple_nodes/show_objects_kitti.cpp
Expand Up @@ -25,12 +25,13 @@

#include "ground_removal/depth_ground_remover.h"
#include "projections/projection_params.h"
#include "qt/drawables/drawable_cloud.h"
#include "qt/drawables/object_painter.h"
#include "utils/cloud.h"
#include "utils/folder_reader.h"
#include "utils/radians.h"
#include "utils/timer.h"
#include "utils/velodyne_utils.h"
#include "visualization/visualizer.h"

#include "tclap/CmdLine.h"

Expand All @@ -39,7 +40,7 @@ using std::string;
using namespace depth_clustering;

void ReadData(const Radians& angle_tollerance, const string& in_path,
Visualizer* visualizer) {
Viewer* visualizer) {
// delay reading for one second to allow GUI to load
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// now load the data
Expand All @@ -63,16 +64,20 @@ void ReadData(const Radians& angle_tollerance, const string& in_path,
angle_tollerance, min_cluster_size, max_cluster_size);
clusterer.SetDiffType(DiffFactory::DiffType::ANGLES);

ObjectPainter object_painter{visualizer,
ObjectPainter::OutlineType::kPolygon3d};

depth_ground_remover.AddClient(&clusterer);
clusterer.AddClient(visualizer->object_clouds_client());
clusterer.AddClient(&object_painter);

fprintf(stderr, "INFO: everything initialized\n");

for (auto path : cloud_reader.GetAllFilePaths()) {
time_utils::Timer timer;
auto cloud = ReadKittiCloud(path);
cloud->InitProjection(*proj_params_ptr);
visualizer->OnNewObjectReceived(*cloud, 0);
visualizer->Clear();
visualizer->AddDrawable(DrawableCloud::FromCloud(cloud));
depth_ground_remover.OnNewObjectReceived(*cloud, 0);

uint max_wait_time = 100;
Expand Down Expand Up @@ -108,7 +113,7 @@ int main(int argc, char* argv[]) {

QApplication application(argc, argv);
// visualizer should be created from a gui thread
Visualizer visualizer;
Viewer visualizer;
visualizer.show();

// create and run loader thread
Expand Down
16 changes: 11 additions & 5 deletions examples/simple_nodes/show_objects_moosmann.cpp
Expand Up @@ -25,12 +25,14 @@

#include "ground_removal/depth_ground_remover.h"
#include "projections/projection_params.h"
#include "qt/drawables/object_painter.h"
#include "qt/drawables/drawable_cloud.h"
#include "qt/viewer/viewer.h"
#include "utils/cloud.h"
#include "utils/folder_reader.h"
#include "utils/radians.h"
#include "utils/timer.h"
#include "utils/velodyne_utils.h"
#include "visualization/visualizer.h"

#include "tclap/CmdLine.h"

Expand All @@ -40,7 +42,7 @@ using std::to_string;
using namespace depth_clustering;

void ReadData(const Radians& angle_tollerance, const string& in_path,
Visualizer* visualizer) {
Viewer* visualizer) {
// delay reading for one second to allow GUI to load
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// now load the data
Expand All @@ -65,14 +67,18 @@ void ReadData(const Radians& angle_tollerance, const string& in_path,
angle_tollerance, min_cluster_size, max_cluster_size);
clusterer.SetDiffType(DiffFactory::DiffType::ANGLES);

ObjectPainter object_painter{visualizer,
ObjectPainter::OutlineType::kPolygon3d};

depth_ground_remover.AddClient(&clusterer);
clusterer.AddClient(visualizer->object_clouds_client());
clusterer.AddClient(&object_painter);

for (const auto& path : image_reader.GetAllFilePaths()) {
auto depth_image = MatFromDepthPng(path);
auto cloud_ptr = Cloud::FromImage(depth_image, *proj_params_ptr);
time_utils::Timer timer;
visualizer->OnNewObjectReceived(*cloud_ptr, 0);
visualizer->Clear();
visualizer->AddDrawable(DrawableCloud::FromCloud(cloud_ptr));
depth_ground_remover.OnNewObjectReceived(*cloud_ptr, 0);
auto current_millis = timer.measure(time_utils::Timer::Units::Milli);
fprintf(stderr, "INFO: It took %lu ms to process and show everything.\n",
Expand Down Expand Up @@ -108,7 +114,7 @@ int main(int argc, char* argv[]) {

QApplication application(argc, argv);
// visualizer should be created from a gui thread
Visualizer visualizer;
Viewer visualizer;
visualizer.show();

// create and run loader thread
Expand Down
2 changes: 1 addition & 1 deletion src/clusterers/image_based_clusterer.h
Expand Up @@ -88,7 +88,7 @@ class ImageBasedClusterer : public AbstractClusterer {
* @param[in] cloud The cloud to cluster
* @param[in] sender_id The sender identifier
*/
void OnNewObjectReceived(const Cloud& cloud, const int sender_id) override {
void OnNewObjectReceived(const Cloud& cloud, int) override {
// generate a projection from a point cloud
if (!cloud.projection_ptr()) {
fprintf(stderr, "ERROR: projection not initialized in cloud.\n");
Expand Down
2 changes: 2 additions & 0 deletions src/qt/drawables/CMakeLists.txt
@@ -1,7 +1,9 @@
add_library(drawable SHARED
drawable_cloud.cpp
object_painter.cpp
drawable_polygon3d.cpp
drawable_cube.cpp)
target_link_libraries(drawable
${OpenCV_LIBS}
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY})
47 changes: 0 additions & 47 deletions src/qt/drawables/bbox_painter.h

This file was deleted.

87 changes: 87 additions & 0 deletions src/qt/drawables/object_painter.cpp
@@ -0,0 +1,87 @@
// Copyright Igor Bogoslavskyi, year 2017.
// In case of any problems with the code please contact me.
// Email: igor.bogoslavskyi@uni-bonn.de.

#include "./object_painter.h"

namespace depth_clustering {

void ObjectPainter::OnNewObjectReceived(
const std::unordered_map<uint16_t, Cloud>& clouds, int) {
if (!viewer_) {
return;
}
Timer timer;
for (const auto& kv : clouds) {
const auto& cluster = kv.second;
Drawable::UniquePtr drawable{nullptr};
switch (outline_type_) {
case OutlineType::kBox:
drawable = CreateDrawableCube(cluster);
break;
case OutlineType::kPolygon3d:
drawable = CreateDrawablePolygon3d(cluster);
break;
}
if (drawable) {
viewer_->AddDrawable(std::move(drawable));
}
}
fprintf(stderr, "[TIMING]: Adding all boxes took %lu us\n",
timer.measure(Timer::Units::Micro));
viewer_->update();
fprintf(stderr, "[TIMING]: Viewer updated in %lu us\n",
timer.measure(Timer::Units::Micro));
}

Drawable::UniquePtr ObjectPainter::CreateDrawableCube(
const depth_clustering::Cloud& cloud) {
Eigen::Vector3f center = Eigen::Vector3f::Zero();
Eigen::Vector3f extent = Eigen::Vector3f::Zero();
Eigen::Vector3f max_point(std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::lowest());
Eigen::Vector3f min_point(std::numeric_limits<float>::max(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::max());
for (const auto& point : cloud.points()) {
center = center + point.AsEigenVector();
min_point << std::min(min_point.x(), point.x()),
std::min(min_point.y(), point.y()), std::min(min_point.z(), point.z());
max_point << std::max(max_point.x(), point.x()),
std::max(max_point.y(), point.y()), std::max(max_point.z(), point.z());
}
center /= cloud.size();
if (min_point.x() < max_point.x()) {
extent = max_point - min_point;
}
return DrawableCube::Create(center, extent);
}

Drawable::UniquePtr ObjectPainter::CreateDrawablePolygon3d(
const depth_clustering::Cloud& cloud) {
float min_z{std::numeric_limits<float>::max()};
float max_z{std::numeric_limits<float>::lowest()};
std::vector<cv::Point2f> cv_points;
cv_points.reserve(cloud.size());
std::vector<int> hull_indices;
for (const auto& point : cloud.points()) {
cv_points.emplace_back(cv::Point2f{point.x(), point.y()});
min_z = std::min(min_z, point.z());
max_z = std::max(max_z, point.z());
}
cv::convexHull(cv_points, hull_indices);
DrawablePolygon3d::AlignedEigenVectors hull;
hull.reserve(cloud.size());
for (int index : hull_indices) {
const auto& cv_point = cv_points[index];
hull.emplace_back(cv_point.x, cv_point.y, min_z);
}
const float diff_z = max_z - min_z;
if (diff_z < 0.3) {
return nullptr;
}
return DrawablePolygon3d::Create(hull, diff_z);
}

} // namespace depth_clustering
85 changes: 7 additions & 78 deletions src/qt/drawables/object_painter.h
Expand Up @@ -31,89 +31,18 @@ class ObjectPainter
public:
enum class OutlineType { kBox, kPolygon3d };

void OnNewObjectReceived(const std::unordered_map<uint16_t, Cloud>& clouds,
int) override {
if (!viewer_) {
return;
}
Timer timer;
for (const auto& kv : clouds) {
const auto& cluster = kv.second;
Drawable::UniquePtr drawable{nullptr};
switch (outline_type_) {
case OutlineType::kBox:
drawable = CreateDrawableCube(cluster);
break;
case OutlineType::kPolygon3d:
drawable = CreateDrawablePolygon3d(cluster);
break;
}
if (drawable) {
viewer_->AddDrawable(std::move(drawable));
}
}
fprintf(stderr, "[TIMING]: Adding all boxes took %lu us\n",
timer.measure(Timer::Units::Micro));
viewer_->update();
fprintf(stderr, "[TIMING]: Viewer updated in %lu us\n",
timer.measure(Timer::Units::Micro));
}

explicit ObjectPainter(Viewer* viewer, OutlineType outline_type)
: viewer_{viewer}, outline_type_{outline_type} {}

void OnNewObjectReceived(const std::unordered_map<uint16_t, Cloud>& clouds,
int id) override;

private:
Drawable::UniquePtr CreateDrawableCube(
const depth_clustering::Cloud& cloud) const {
Eigen::Vector3f center = Eigen::Vector3f::Zero();
Eigen::Vector3f extent = Eigen::Vector3f::Zero();
Eigen::Vector3f max_point(std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::lowest());
Eigen::Vector3f min_point(std::numeric_limits<float>::max(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::max());
for (const auto& point : cloud.points()) {
center = center + point.AsEigenVector();
min_point << std::min(min_point.x(), point.x()),
std::min(min_point.y(), point.y()),
std::min(min_point.z(), point.z());
max_point << std::max(max_point.x(), point.x()),
std::max(max_point.y(), point.y()),
std::max(max_point.z(), point.z());
}
center /= cloud.size();
if (min_point.x() < max_point.x()) {
extent = max_point - min_point;
}
return DrawableCube::Create(center, extent);
}
static Drawable::UniquePtr CreateDrawableCube(
const depth_clustering::Cloud& cloud);

Drawable::UniquePtr CreateDrawablePolygon3d(
const depth_clustering::Cloud& cloud) const {
float min_z{std::numeric_limits<float>::max()};
float max_z{std::numeric_limits<float>::lowest()};
std::vector<cv::Point2f> cv_points;
cv_points.reserve(cloud.size());
std::vector<int> hull_indices;
for (const auto& point : cloud.points()) {
cv_points.emplace_back(cv::Point2f{point.x(), point.y()});
min_z = std::min(min_z, point.z());
max_z = std::max(max_z, point.z());
}
cv::convexHull(cv_points, hull_indices);
DrawablePolygon3d::AlignedEigenVectors hull;
hull.reserve(cloud.size());
for (int index : hull_indices) {
const auto& cv_point = cv_points[index];
hull.emplace_back(cv_point.x, cv_point.y, min_z);
}
const float diff_z = max_z - min_z;
if (diff_z < 0.3) {
return nullptr;
}
return DrawablePolygon3d::Create(hull, diff_z);
}
static Drawable::UniquePtr CreateDrawablePolygon3d(
const depth_clustering::Cloud& cloud);

Viewer* viewer_{nullptr};
OutlineType outline_type_{OutlineType::kBox};
Expand Down

0 comments on commit 9a77346

Please sign in to comment.