LibMultiSense/PointCloudUtility/PointCloudUtility.cc
Go to the documentation of this file.
1 
37 #ifdef WIN32
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN 1
40 #endif
41 
42 #include <windows.h>
43 #include <winsock2.h>
44 #else
45 #include <unistd.h>
46 #endif
47 
48 #include <chrono>
49 #include <csignal>
50 #include <iostream>
51 
52 #include <MultiSense/MultiSenseChannel.hh>
54 
55 #include "getopt/getopt.h"
56 
57 namespace lms = multisense;
58 
59 namespace
60 {
61 
62 volatile bool done = false;
63 
64 void usage(const char *name)
65 {
66  std::cerr << "USAGE: " << name << " [<options>]" << std::endl;
67  std::cerr << "Where <options> are:" << std::endl;
68  std::cerr << "\t-a <current_address> : CURRENT IPV4 address (default=10.66.171.21)" << std::endl;
69  std::cerr << "\t-m <mtu> : MTU to use to communicate with the camera (default=1500)" << std::endl;
70  std::cerr << "\t-r <max-range> : Current max range from the camera for points to be included (default=50m)" << std::endl;
71  std::cerr << "\t-c : Flag to colorize the point clouds with the aux image" << std::endl;
72  exit(1);
73 }
74 
75 #ifdef WIN32
76 BOOL WINAPI signal_handler(DWORD dwCtrlType)
77 {
78  (void) dwCtrlType;
79  done = true;
80  return TRUE;
81 }
82 #else
83 void signal_handler(int sig)
84 {
85  (void) sig;
86  done = true;
87 }
88 #endif
89 
90 }
91 
92 int main(int argc, char** argv)
93 {
94 #if WIN32
95  SetConsoleCtrlHandler (signal_handler, TRUE);
96 #else
97  signal(SIGINT, signal_handler);
98 #endif
99 
100  std::string ip_address = "10.66.171.21";
101  int16_t mtu = 1500;
102  double max_range = 50.0;
103  bool color = false;
104 
105  int c;
106  while(-1 != (c = getopt(argc, argv, "a:m:r:c")))
107  {
108  switch(c)
109  {
110  case 'a': ip_address = std::string(optarg); break;
111  case 'm': mtu = static_cast<uint16_t>(atoi(optarg)); break;
112  case 'r': max_range = std::stod(optarg); break;
113  case 'c': color = true; break;
114  default: usage(*argv); break;
115  }
116  }
117 
118  const auto channel = lms::Channel::create(lms::Channel::Config{ip_address, mtu});
119  if (!channel)
120  {
121  std::cerr << "Failed to create channel" << std::endl;;
122  return 1;
123  }
124 
125  //
126  // QuerySet dynamic config from the camera
127  //
128  auto config = channel->get_config();
129  config.frames_per_second = 10.0;
130  if (const auto status = channel->set_config(config); status != lms::Status::OK)
131  {
132  std::cerr << "Cannot set config" << std::endl;
133  return 1;
134  }
135 
136  //
137  // If our camera has an aux color camera, and we want to colorize our point clouds with the aux image,
138  // color
139  //
140  const auto info = channel->get_info();
141  const auto color_stream = (color && info.device.has_aux_camera()) ?
142  lms::DataSource::AUX_RECTIFIED_RAW :
143  lms::DataSource::LEFT_RECTIFIED_RAW;
144 
145  //
146  // Start a single image stream
147  //
148  if (const auto status = channel->start_streams({color_stream,
149  lms::DataSource::LEFT_DISPARITY_RAW}); status != lms::Status::OK)
150  {
151  std::cerr << "Cannot start streams: " << lms::to_string(status) << std::endl;
152  return 1;
153  }
154 
155  while(!done)
156  {
157  if (const auto image_frame = channel->get_next_image_frame(); image_frame)
158  {
159  if (color_stream == lms::DataSource::LEFT_RECTIFIED_RAW)
160  {
161  if (const auto point_cloud = lms::create_color_pointcloud<uint8_t>(image_frame.value(),
162  max_range,
163  color_stream,
164  lms::DataSource::LEFT_DISPARITY_RAW); point_cloud)
165  {
166  std::cout << "Saving pointcloud for frame id: " << image_frame->frame_id << std::endl;
167  lms::write_pointcloud_ply(point_cloud.value(), std::to_string(image_frame->frame_id) + ".ply");
168  }
169  }
170  else
171  {
172  using ColorT = std::array<uint8_t, 3>;
173  if (const auto bgr = create_bgr_image(image_frame.value(), color_stream); bgr)
174  {
175  const auto &disparity = image_frame->get_image(lms::DataSource::LEFT_DISPARITY_RAW);
176  if (const auto point_cloud = lms::create_color_pointcloud<ColorT>(disparity,
177  bgr,
178  max_range,
179  image_frame->calibration); point_cloud)
180  {
181  std::cout << "Saving aux colorized pointcloud for frame id: " << image_frame->frame_id << std::endl;
182  lms::write_pointcloud_ply(point_cloud.value(), std::to_string(image_frame->frame_id) + ".ply");
183  }
184  }
185  }
186  }
187  }
188 
189  channel->stop_streams({lms::DataSource::ALL});
190 
191  return 0;
192 }
usage
static void usage()
Definition: FirmwareUpdateUtility.cc:51
multisense::create_bgr_image
std::optional< Image > create_bgr_image(const ImageFrame &frame, const DataSource &output_source)
Convert a YCbCr420 luma + chroma image into a BGR color image.
Definition: utilities.cc:340
getopt.h
getopt
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
MultiSenseUtilities.hh
multisense::write_pointcloud_ply
MULTISENSE_API bool write_pointcloud_ply(const PointCloud< Color > &point_cloud, const std::filesystem::path &path)
Write a point cloud to a ASCII ply file.
Definition: MultiSenseUtilities.hh:276
multisense::Channel::create
static std::unique_ptr< Channel > create(const Config &config, const ChannelImplementation &impl=ChannelImplementation::LEGACY)
Factory create function which allows for switching between different channel implementations.
Definition: factory.cc:42
multisense
Definition: factory.cc:39
main
int main(int argc, char **argv)
Definition: LibMultiSense/PointCloudUtility/PointCloudUtility.cc:92
signal_handler
static void signal_handler(int signal)
Definition: FirmwareUpdateUtility.cc:190
optarg
char * optarg
Definition: getopt.c:29
multisense::to_string
std::string to_string(const Status &status)
Convert a status object to a user readable string.
Definition: utilities.cc:137


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:09