rs-tracking-and-depth.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
5 #include "example.hpp" // Include short list of convenience functions for rendering
6 
7 #include <algorithm> // std::min, std::max
8 #include <fstream> // std::ifstream
9 
10 // Helper functions
11 void register_glfw_callbacks(window& app, glfw_state& app_state);
12 
13 float detR(float H[16]) {
14  return H[0]*(H[5]*H[10]-H[9]*H[6]) - H[4]*(H[1]*H[10]-H[2]*H[9]) + H[8]*(H[1]*H[6]-H[5]*H[2]);
15 }
16 
17 int main(int argc, char * argv[]) try
18 {
19  // Create a simple OpenGL window for rendering:
20  window app(1280, 720, "RealSense Tracking and Depth Example");
21  // Construct an object to manage view state
22  glfw_state app_state;
23  // register callbacks to allow manipulation of the pointcloud
24  register_glfw_callbacks(app, app_state);
25 
26  // Declare pointcloud object, for calculating pointclouds and texture mappings
28  // We want the points object to be persistent so we can display the last cloud when a frame drops
30  // store pose and timestamp
31  rs2::pose_frame pose_frame(nullptr);
32  std::vector<rs2_vector> trajectory;
33 
34  rs2::context ctx; // Create librealsense context for managing devices
35  std::map<std::string, rs2::colorizer> colorizers; // Declare map from device serial number to colorizer (utility class to convert depth data RGB colorspace)
36  std::vector<rs2::pipeline> pipelines;
37 
38  // Capture serial numbers before opening streaming
39  std::vector<std::string> serials;
40  for (auto&& dev : ctx.query_devices())
41  serials.push_back(dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));
42 
43 
44  // Start a streaming pipe per each connected device
45  for (auto&& serial : serials)
46  {
47  rs2::pipeline pipe(ctx);
49  cfg.enable_device(serial);
50  pipe.start(cfg);
51  pipelines.emplace_back(pipe);
52  // Map from each device's serial number to a different colorizer
53  colorizers[serial] = rs2::colorizer();
54  }
55 
56  // extrinsics
57  // depth w.r.t. tracking (column-major)
58  float H_t265_d400[16] = {1, 0, 0, 0,
59  0,-1, 0, 0,
60  0, 0,-1, 0,
61  0, 0, 0, 1};
62  std::string fn = "./H_t265_d400.cfg";
63  std::ifstream ifs(fn);
64  if (!ifs.is_open()) {
65  std::cerr << "Couldn't open " << fn << std::endl;
66  return -1;
67  }
68  else {
69  for (int i = 0; i < 3; i++) {
70  for (int j = 0; j < 4; j++) {
71  ifs >> H_t265_d400[i+4*j]; // row-major to column-major
72  }
73  }
74  }
75  float det = detR(H_t265_d400);
76  if (fabs(1-det) > 1e-6) {
77  std::cerr << "Invalid homogeneous transformation matrix input (det != 1)" << std::endl;
78  return -1;
79  }
80 
81  while (app) // Application still alive?
82  {
83  for (auto &&pipe : pipelines) // loop over pipelines
84  {
85  // Wait for the next set of frames from the camera
86  auto frames = pipe.wait_for_frames();
87 
88 
89  auto color = frames.get_color_frame();
90 
91  // For cameras that don't have RGB sensor, we'll map the pointcloud to infrared instead of color
92  if (!color)
93  color = frames.get_infrared_frame();
94 
95  // Tell pointcloud object to map to this color frame
96  if (color)
97  pc.map_to(color);
98 
99  auto depth = frames.get_depth_frame();
100 
101  // Generate the pointcloud and texture mappings
102  if (depth)
103  points = pc.calculate(depth);
104 
105  // Upload the color frame to OpenGL
106  if (color)
107  app_state.tex.upload(color);
108 
109 
110  // pose
111  auto pose = frames.get_pose_frame();
112  if (pose) {
113  pose_frame = pose;
114 
115  // Print the x, y, z values of the translation, relative to initial position
116  auto pose_data = pose.get_pose_data();
117  std::cout << "\r" << "Device Position: " << std::setprecision(3) << std::fixed << pose_data.translation.x << " " << pose_data.translation.y << " " << pose_data.translation.z << " (meters)";
118 
119  // add new point in the trajectory (if motion large enough to reduce size of traj. vector)
120  if (trajectory.size() == 0)
121  trajectory.push_back(pose_data.translation);
122  else {
123  rs2_vector prev = trajectory.back();
124  rs2_vector curr = pose_data.translation;
125  if (sqrt(pow((curr.x - prev.x), 2) + pow((curr.y - prev.y), 2) + pow((curr.z - prev.z), 2)) > 0.002)
126  trajectory.push_back(pose_data.translation);
127  }
128  }
129  }
130 
131  // Draw the pointcloud
132  if (points && pose_frame) {
133  rs2_pose pose = pose_frame.get_pose_data();
134  draw_pointcloud_wrt_world(app.width(), app.height(), app_state, points, pose, H_t265_d400, trajectory);
135  }
136  }
137 
138  return EXIT_SUCCESS;
139 }
140 catch (const rs2::error & e)
141 {
142  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
143  return EXIT_FAILURE;
144 }
145 catch (const std::exception & e)
146 {
147  std::cerr << e.what() << std::endl;
148  return EXIT_FAILURE;
149 }
float z
Definition: rs_types.h:131
GLuint color
device_list query_devices() const
Definition: rs_context.hpp:112
GLint GLint GLsizei GLsizei GLsizei depth
void map_to(frame mapped)
GLsizei const GLchar *const * string
void enable_device(const std::string &serial)
int main(int argc, char *argv[])
e
Definition: rmse.py:177
rs2_pose get_pose_data() const
Definition: rs_frame.hpp:933
::realsense_legacy_msgs::pose_< std::allocator< void > > pose
Definition: pose.h:88
const int H
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
float y
Definition: rs_types.h:131
std::ostream & cout()
void register_glfw_callbacks(window &app, glfw_state &app_state)
Definition: example.hpp:1037
float width() const
Definition: example.hpp:627
void upload(const rs2::video_frame &frame)
Definition: example.hpp:406
points calculate(frame depth) const
GLint j
float x
Definition: rs_types.h:131
float height() const
Definition: example.hpp:628
3D vector in Euclidean coordinate space
Definition: rs_types.h:129
std::ostream & cerr()
int i
pipeline_profile start()
const GLuint * pipelines
Definition: glext.h:1893
float detR(float H[16])
texture tex
Definition: example.hpp:882
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
void draw_pointcloud_wrt_world(float width, float height, glfw_state &app_state, rs2::points &points, rs2_pose &pose, float H_t265_d400[16], std::vector< rs2_vector > &trajectory)
Definition: example.hpp:952
GLdouble GLdouble GLint GLint const GLdouble * points


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40