libvisiontransfer  10.6.0
open3d_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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 using namespace visiontransfer;
31 
32 int main(int argc, char** argv) {
33  try {
34  if(argc != 2) {
35  std::cout
36  << "Usage: " << argv[0] << " MODE" << std::endl
37  << std::endl
38  << "Available modes:" << std::endl
39  << "0: Display RGBD image" << std::endl
40  << "1: Display 3d pointcloud" << std::endl;
41  return 1;
42  }
43  bool displayPointcloud = (bool)atoi(argv[1]);
44 
45  // Search for Nerian stereo devices
46  DeviceEnumeration deviceEnum;
47  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
48  if(devices.size() == 0) {
49  std::cout << "No devices discovered!" << std::endl;
50  return -1;
51  }
52 
53  // Create an image transfer object that receives data from
54  // the first detected device
55  ImageTransfer imageTransfer(devices[0]);
56 
57  // Initialize objects that will be needed later
58  ImageSet imageSet;
59  Reconstruct3D recon3d;
60  open3d::geometry::AxisAlignedBoundingBox bbox(Eigen::Vector3d(-10, -10, 0),
61  Eigen::Vector3d(10, 10, 5));
62  std::shared_ptr<open3d::geometry::PointCloud> cloud(new open3d::geometry::PointCloud);
63  std::shared_ptr<open3d::geometry::RGBDImage> rgbdImage(new open3d::geometry::RGBDImage);
64 
65  // Start viewing
66  open3d::visualization::Visualizer vis;
67  vis.CreateVisualizerWindow("Nerian 3D viewer", 1280, 768);
68  vis.PrintVisualizerHelp();
69  vis.GetRenderOption().SetPointSize(2);
70 
71  // Keep receiving new frames in a loop
72  while(true) {
73  // Grab frame
74  while(!imageTransfer.receiveImageSet(imageSet)) {
75  if(!vis.PollEvents()) {
76  return 0;
77  }
78  }
79 
80  if(displayPointcloud) {
81  // Crop pointcloud
82  (*cloud) = *recon3d.createOpen3DCloud(imageSet)->Crop(bbox);
83 
84  // Rotate for visualization
85  cloud->Transform(Eigen::Affine3d(Eigen::AngleAxis<double>(M_PI, Eigen::Vector3d(1.0, 0.0, 0.0))).matrix());
86  } else {
87  // Get RGBD Frame
88  (*rgbdImage) = *recon3d.createOpen3DImageRGBD(imageSet);
89  }
90 
91  // Visualize
92  if(!vis.HasGeometry()) {
93  if(displayPointcloud) {
94  vis.AddGeometry(cloud);
95  } else {
96  vis.AddGeometry(rgbdImage);
97  }
98  }
99 
100  vis.UpdateGeometry();
101  if(!vis.PollEvents()) {
102  return 0;
103  }
104 
105  vis.UpdateRender();
106  }
107 
108  } catch(const std::exception& ex) {
109  std::cerr << "Exception occurred: " << ex.what() << std::endl;
110  }
111 
112  return 0;
113 }
visiontransfer::DeviceEnumeration::discoverDevices
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Definition: deviceenumeration.h:66
visiontransfer::DeviceEnumeration
Allows for the discovery of devices in the network.
Definition: deviceenumeration.h:42
visiontransfer::ImageSet
A set of one to three images, but usually two (the left camera image and the disparity map)....
Definition: imageset.h:50
visiontransfer::Reconstruct3D
Transforms a disparity map into a set of 3D points.
Definition: reconstruct3d.h:47
visiontransfer::ImageTransfer
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:52
Allied Vision