0

In Open3D, with C++, how to animate 3D point cloud: same question that this one in python Open3d: How to update point cloud during window running? but can't get this to work.

The following code (inspired from github repo example examples/cpp/PointCloud.cpp) compile, run (the viewer and point appears), but animation is not rendered

>> cat ../examples/cpp/PtCloud.cpp 
#include <Eigen/Dense>
#include <iostream>
#include <memory>

#include "open3d/Open3D.h"

#include <unistd.h> // sleep

using namespace open3d;

void PrintPointCloud(const std::shared_ptr<geometry::PointCloud> &pointcloud) {
    using namespace open3d;

    bool pointcloud_has_normal = pointcloud->HasNormals();
    utility::LogInfo("Pointcloud has %d points.",
                     (int)pointcloud->points_.size());

    Eigen::Vector3d min_bound = pointcloud->GetMinBound();
    Eigen::Vector3d max_bound = pointcloud->GetMaxBound();
    utility::LogInfo(
            "Bounding box is: ({:.4f}, {:.4f}, {:.4f}) - ({:.4f}, {:.4f}, "
            "{:.4f})",
            min_bound(0), min_bound(1), min_bound(2), max_bound(0),
            max_bound(1), max_bound(2));

    for (size_t i = 0; i < pointcloud->points_.size(); i++) {
        if (pointcloud_has_normal) {
            const Eigen::Vector3d &point = pointcloud->points_[i];
            const Eigen::Vector3d &normal = pointcloud->normals_[i];
            utility::LogInfo("{:.6f} {:.6f} {:.6f} {:.6f} {:.6f} {:.6f}",
                             point(0), point(1), point(2), normal(0), normal(1),
                             normal(2));
        } else {
            const Eigen::Vector3d &point = pointcloud->points_[i];
            utility::LogInfo("{:.6f} {:.6f} {:.6f}", point(0), point(1),
                             point(2));
        }
    }
    utility::LogInfo("End of the list.");
}

std::shared_ptr<geometry::PointCloud> pointcloud(new geometry::PointCloud);

bool AnimatePointCloud(visualization::Visualizer * visualizer) {
    if (!visualizer) return false;

    for (int i = 0; i < 10; i++) {
         sleep(2);
         pointcloud->points_.push_back(Eigen::Vector3d(0.0, 0.0, i+1.0));
         pointcloud->points_.push_back(Eigen::Vector3d(1.0, 0.0, i+1.0));
         pointcloud->points_.push_back(Eigen::Vector3d(0.0, 1.0, i+1.0));
         PrintPointCloud(pointcloud);
         visualizer->UpdateGeometry(pointcloud);
         visualizer->PollEvents();
         visualizer->UpdateRender();
         visualizer->Run();
    }
    return true;
}

int main(int argc, char *argv[]) {
    // 1. test basic pointcloud functions.

    pointcloud->points_.push_back(Eigen::Vector3d(0.0, 0.0, 0.0));
    pointcloud->points_.push_back(Eigen::Vector3d(1.0, 0.0, 0.0));
    pointcloud->points_.push_back(Eigen::Vector3d(0.0, 1.0, 0.0));
    PrintPointCloud(pointcloud);

    // 2. test pointcloud visualization

    visualization::Visualizer visualizer;
    visualizer.CreateVisualizerWindow("Open3D", 1600, 900);
    visualizer.AddGeometry(pointcloud);
    visualizer.RegisterAnimationCallback(AnimatePointCloud);
    visualizer.Run();
    visualizer.DestroyVisualizerWindow();

    return 0;
}

The code is supposed to animate points that stack up in animated way: the viewer seems to freeze. Didn't find any C++ example. What's wrong?

fghoussen
  • 393
  • 3
  • 16

0 Answers0