cpp-tutorial-3-pointcloud.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
5 // librealsense tutorial #3 - Point cloud generation //
7 
8 // First include the librealsense C++ header file
9 #include <librealsense/rs.hpp>
10 #include <cstdio>
11 
12 // Also include GLFW to allow for graphical display
13 #define GLFW_INCLUDE_GLU
14 #include <GLFW/glfw3.h>
15 
16 double yaw, pitch, lastX, lastY; int ml;
17 static void on_mouse_button(GLFWwindow * win, int button, int action, int mods)
18 {
19  if(button == GLFW_MOUSE_BUTTON_LEFT) ml = action == GLFW_PRESS;
20 }
21 static double clamp(double val, double lo, double hi) { return val < lo ? lo : val > hi ? hi : val; }
22 static void on_cursor_pos(GLFWwindow * win, double x, double y)
23 {
24  if(ml)
25  {
26  yaw = clamp(yaw - (x - lastX), -120, 120);
27  pitch = clamp(pitch + (y - lastY), -80, 80);
28  }
29  lastX = x;
30  lastY = y;
31 }
32 
33 int main() try
34 {
35  // Turn on logging. We can separately enable logging to console or to file, and use different severity filters for each.
37  //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
38 
39  // Create a context object. This object owns the handles to all connected realsense devices.
41  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
42  if(ctx.get_device_count() == 0) return EXIT_FAILURE;
43 
44  // This tutorial will access only a single device, but it is trivial to extend to multiple devices
45  rs::device * dev = ctx.get_device(0);
46  printf("\nUsing device 0, an %s\n", dev->get_name());
47  printf(" Serial number: %s\n", dev->get_serial());
48  printf(" Firmware version: %s\n", dev->get_firmware_version());
49 
50  // Configure depth and color to run with the device's preferred settings
53  dev->start();
54 
55  // Open a GLFW window to display our output
56  glfwInit();
57  GLFWwindow * win = glfwCreateWindow(1280, 960, "librealsense tutorial #3", nullptr, nullptr);
61  while(!glfwWindowShouldClose(win))
62  {
63  // Wait for new frame data
65  dev->wait_for_frames();
66 
67  // Retrieve our images
68  const uint16_t * depth_image = (const uint16_t *)dev->get_frame_data(rs::stream::depth);
69  const uint8_t * color_image = (const uint8_t *)dev->get_frame_data(rs::stream::color);
70 
71  // Retrieve camera parameters for mapping between depth and color
75  float scale = dev->get_depth_scale();
76 
77  // Set up a perspective transform in a space that we can rotate by clicking and dragging the mouse
78  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79  glMatrixMode(GL_PROJECTION);
80  glLoadIdentity();
81  gluPerspective(60, (float)1280/960, 0.01f, 20.0f);
82  glMatrixMode(GL_MODELVIEW);
83  glLoadIdentity();
84  gluLookAt(0,0,0, 0,0,1, 0,-1,0);
85  glTranslatef(0,0,+0.5f);
86  glRotated(pitch, 1, 0, 0);
87  glRotated(yaw, 0, 1, 0);
88  glTranslatef(0,0,-0.5f);
89 
90  // We will render our depth data as a set of points in 3D space
91  glPointSize(2);
92  glEnable(GL_DEPTH_TEST);
93  glBegin(GL_POINTS);
94 
95  for(int dy=0; dy<depth_intrin.height; ++dy)
96  {
97  for(int dx=0; dx<depth_intrin.width; ++dx)
98  {
99  // Retrieve the 16-bit depth value and map it into a depth in meters
100  uint16_t depth_value = depth_image[dy * depth_intrin.width + dx];
101  float depth_in_meters = depth_value * scale;
102 
103  // Skip over pixels with a depth value of zero, which is used to indicate no data
104  if(depth_value == 0) continue;
105 
106  // Map from pixel coordinates in the depth image to pixel coordinates in the color image
107  rs::float2 depth_pixel = {(float)dx, (float)dy};
108  rs::float3 depth_point = depth_intrin.deproject(depth_pixel, depth_in_meters);
109  rs::float3 color_point = depth_to_color.transform(depth_point);
110  rs::float2 color_pixel = color_intrin.project(color_point);
111 
112  // Use the color from the nearest color pixel, or pure white if this point falls outside the color image
113  const int cx = (int)std::round(color_pixel.x), cy = (int)std::round(color_pixel.y);
114  if(cx < 0 || cy < 0 || cx >= color_intrin.width || cy >= color_intrin.height)
115  {
116  glColor3ub(255, 255, 255);
117  }
118  else
119  {
120  glColor3ubv(color_image + (cy * color_intrin.width + cx) * 3);
121  }
122 
123  // Emit a vertex at the 3D location of this depth pixel
124  glVertex3f(depth_point.x, depth_point.y, depth_point.z);
125  }
126  }
127  glEnd();
128 
129  glfwSwapBuffers(win);
130  }
131 
132  return EXIT_SUCCESS;
133 }
134 catch(const rs::error & e)
135 {
136  // Method calls against librealsense objects may throw exceptions of type rs::error
137  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
138  printf(" %s\n", e.what());
139  return EXIT_FAILURE;
140 }
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
Provides convenience methods relating to devices.
Definition: rs.hpp:567
intrinsics get_stream_intrinsics(stream stream) const
Retrieves intrinsic camera parameters for specific stream.
Definition: rs.hpp:788
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
static void on_cursor_pos(GLFWwindow *win, double x, double y)
const std::string & get_failed_args() const
Definition: rs.hpp:315
float y
Definition: rs.hpp:248
extrinsics get_extrinsics(stream from_stream, stream to_stream) const
Retrieves extrinsic transformation between viewpoints of two different streams.
Definition: rs.hpp:630
rs_error * e
Cross-stream extrinsics: encode the topology describing how the different devices are connected...
Definition: rs.hpp:281
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:114
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9820
Video stream intrinsics.
Definition: rs.hpp:252
GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow *window, GLFWmousebuttonfun cbfun)
Sets the mouse button callback.
Definition: input.c:482
#define GLFW_MOUSE_BUTTON_LEFT
Definition: glfw3.h:432
Exposes librealsense functionality for C++ compilers.
static void on_mouse_button(GLFWwindow *win, int button, int action, int mods)
const void * get_frame_data(stream stream) const
Retrieves contents of latest frame on a stream.
Definition: rs.hpp:1050
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
float x
Definition: rs.hpp:248
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
float y
Definition: rs.hpp:249
Context.
Definition: rs.hpp:319
GLfloat f
Definition: glext.h:1868
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:544
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:531
GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow *window, GLFWcursorposfun cbfun)
Sets the cursor position callback.
Definition: input.c:491
auto ctx
int height
Definition: rs.h:303
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
int width
Definition: rs.h:302
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:116
float z
Definition: rs.hpp:249
rs_device * dev
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
float get_depth_scale() const
Retrieves mapping between units of depth image and meters.
Definition: rs.hpp:653
float x
Definition: rs.hpp:249
float3 deproject(const float2 &pixel, float depth) const
Definition: rs.hpp:263
device * get_device(int index)
Definition: rs.hpp:354
float3 transform(const float3 &point) const
Definition: rs.hpp:284
const std::string & get_failed_function() const
Definition: rs.hpp:314
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:232
GLint GLint GLint GLint GLint x
Definition: glext.h:114
float2 project(const float3 &point) const
Definition: rs.hpp:267
void wait_for_frames()
Blocks until new frames are available.
Definition: rs.hpp:985
int get_device_count() const
Definition: rs.hpp:343
GLuint GLfloat * val
Definition: glext.h:1490
static double clamp(double val, double lo, double hi)
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:406


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17