libvisiontransfer  10.0.0
open3d_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2022 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 
16 #define _USE_MATH_DEFINES
17 #include <cmath>
18 
19 // Open3D headers must be included before visiontransfer!
20 #include <open3d/Open3D.h>
21 #include <visiontransfer/deviceenumeration.h>
22 #include <visiontransfer/imagetransfer.h>
23 #include <visiontransfer/imageset.h>
24 #include <visiontransfer/reconstruct3d.h>
25 #include <iostream>
26 #include <exception>
27 #include <memory>
28 #include <stdio.h>
29 
30 #ifdef _MSC_VER
31 // Visual studio does not come with snprintf
32 #define snprintf _snprintf_s
33 #endif
34 
35 using namespace visiontransfer;
36 
37 int main(int argc, char** argv) {
38  try {
39  if(argc != 2) {
40  std::cout
41  << "Usage: " << argv[0] << " MODE" << std::endl
42  << std::endl
43  << "Available modes:" << std::endl
44  << "0: Display RGBD image" << std::endl
45  << "1: Display 3d pointcloud" << std::endl;
46  return 1;
47  }
48  bool displayPointcloud = (bool)atoi(argv[1]);
49 
50  // Search for Nerian stereo devices
51  DeviceEnumeration deviceEnum;
52  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
53  if(devices.size() == 0) {
54  std::cout << "No devices discovered!" << std::endl;
55  return -1;
56  }
57 
58  // Create an image transfer object that receives data from
59  // the first detected device
60  ImageTransfer imageTransfer(devices[0]);
61 
62  // Initialize objects that will be needed later
63  ImageSet imageSet;
64  Reconstruct3D recon3d;
65  open3d::geometry::AxisAlignedBoundingBox bbox(Eigen::Vector3d(-10, -10, 0),
66  Eigen::Vector3d(10, 10, 5));
67  std::shared_ptr<open3d::geometry::PointCloud> cloud(new open3d::geometry::PointCloud);
68  std::shared_ptr<open3d::geometry::RGBDImage> rgbdImage(new open3d::geometry::RGBDImage);
69 
70  // Start viewing
71  open3d::visualization::Visualizer vis;
72  vis.CreateVisualizerWindow("Nerian 3D viewer", 1280, 768);
73  vis.PrintVisualizerHelp();
74  vis.GetRenderOption().SetPointSize(2);
75 
76  // Keep receiving new frames in a loop
77  while(true) {
78  // Grab frame
79  while(!imageTransfer.receiveImageSet(imageSet)) {
80  if(!vis.PollEvents()) {
81  return 0;
82  }
83  }
84 
85  if(displayPointcloud) {
86  // Crop pointcloud
87  (*cloud) = *recon3d.createOpen3DCloud(imageSet, true)->Crop(bbox);
88 
89  // Rotate for visualization
90  cloud->Transform(Eigen::Affine3d(Eigen::AngleAxis<double>(M_PI, Eigen::Vector3d(1.0, 0.0, 0.0))).matrix());
91  } else {
92  // Get RGBD Frame
93  (*rgbdImage) = *recon3d.createOpen3DImageRGBD(imageSet);
94  }
95 
96  // Visualize
97  if(!vis.HasGeometry()) {
98  if(displayPointcloud) {
99  vis.AddGeometry(cloud);
100  } else {
101  vis.AddGeometry(rgbdImage);
102  }
103  }
104 
105  vis.UpdateGeometry();
106  if(!vis.PollEvents()) {
107  return 0;
108  }
109 
110  vis.UpdateRender();
111  }
112 
113  } catch(const std::exception& ex) {
114  std::cerr << "Exception occurred: " << ex.what() << std::endl;
115  }
116 
117  return 0;
118 }
Transforms a disparity map into a set of 3D points.
Definition: reconstruct3d.h:35
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:36
A set of one to three images, but usually two (the left camera image and the disparity map)...
Definition: imageset.h:38
Allows for the discovery of devices in the network.
Nerian Vision Technologies