cpp-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 
4 #include <librealsense/rs.hpp>
5 #include "example.hpp"
6 
7 #include <chrono>
8 #include <vector>
9 #include <sstream>
10 #include <iostream>
11 #include <algorithm>
12 
13 inline void glVertex(const rs::float3 & vertex) { glVertex3fv(&vertex.x); }
14 inline void glTexCoord(const rs::float2 & tex_coord) { glTexCoord2fv(&tex_coord.x); }
15 
16 struct state { double yaw, pitch, lastX, lastY; bool ml; std::vector<rs::stream> tex_streams; int index; rs::device * dev; };
17 
18 int main(int argc, char * argv[]) try
19 {
21  //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
22 
24  if(ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
25  rs::device & dev = *ctx.get_device(0);
26 
31  dev.start();
32 
33  state app_state = {0, 0, 0, 0, false, {rs::stream::color, rs::stream::depth, rs::stream::infrared}, 0, &dev};
34  if(dev.is_stream_enabled(rs::stream::infrared2)) app_state.tex_streams.push_back(rs::stream::infrared2);
35 
36  glfwInit();
37  std::ostringstream ss; ss << "CPP Point Cloud Example (" << dev.get_name() << ")";
38  GLFWwindow * win = glfwCreateWindow(640, 480, ss.str().c_str(), 0, 0);
39  glfwSetWindowUserPointer(win, &app_state);
40 
41  glfwSetMouseButtonCallback(win, [](GLFWwindow * win, int button, int action, int mods)
42  {
43  auto s = (state *)glfwGetWindowUserPointer(win);
44  if(button == GLFW_MOUSE_BUTTON_LEFT) s->ml = action == GLFW_PRESS;
45  if(button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) s->index = (s->index+1) % s->tex_streams.size();
46  });
47 
48  glfwSetCursorPosCallback(win, [](GLFWwindow * win, double x, double y)
49  {
50  auto s = (state *)glfwGetWindowUserPointer(win);
51  if(s->ml)
52  {
53  s->yaw -= (x - s->lastX);
54  s->yaw = std::max(s->yaw, -120.0);
55  s->yaw = std::min(s->yaw, +120.0);
56  s->pitch += (y - s->lastY);
57  s->pitch = std::max(s->pitch, -80.0);
58  s->pitch = std::min(s->pitch, +80.0);
59  }
60  s->lastX = x;
61  s->lastY = y;
62  });
63 
64  glfwSetKeyCallback(win, [](GLFWwindow * win, int key, int scancode, int action, int mods)
65  {
66  auto s = (state *)glfwGetWindowUserPointer(win);
67  if (action == GLFW_RELEASE)
68  {
69  if (key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(win, 1);
70  else if (key == GLFW_KEY_F1)
71  {
72  if (!s->dev->is_streaming()) s->dev->start();
73  }
74  else if (key == GLFW_KEY_F2)
75  {
76  if (s->dev->is_streaming()) s->dev->stop();
77  }
78  }
79  });
80 
82  texture_buffer tex;
83 
84  int frames = 0; float time = 0, fps = 0;
85  auto t0 = std::chrono::high_resolution_clock::now();
86  while (!glfwWindowShouldClose(win))
87  {
89  if(dev.is_streaming()) dev.wait_for_frames();
90 
91  auto t1 = std::chrono::high_resolution_clock::now();
92  time += std::chrono::duration<float>(t1-t0).count();
93  t0 = t1;
94  ++frames;
95  if(time > 0.5f)
96  {
97  fps = frames / time;
98  frames = 0;
99  time = 0;
100  }
101 
102  const rs::stream tex_stream = app_state.tex_streams[app_state.index];
103  const rs::extrinsics extrin = dev.get_extrinsics(rs::stream::depth, tex_stream);
104  const rs::intrinsics depth_intrin = dev.get_stream_intrinsics(rs::stream::depth);
105  const rs::intrinsics tex_intrin = dev.get_stream_intrinsics(tex_stream);
106  bool identical = depth_intrin == tex_intrin && extrin.is_identity();
107 
108  glPushAttrib(GL_ALL_ATTRIB_BITS);
109 
110  tex.upload(dev, tex_stream);
111 
112  int width, height;
113  glfwGetFramebufferSize(win, &width, &height);
114  glViewport(0, 0, width, height);
115  glClearColor(52.0f/255, 72.f/255, 94.0f/255.0f, 1);
116  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117 
118  glMatrixMode(GL_PROJECTION);
119  glPushMatrix();
120  gluPerspective(60, (float)width/height, 0.01f, 20.0f);
121 
122  glMatrixMode(GL_MODELVIEW);
123  glPushMatrix();
124  gluLookAt(0,0,0, 0,0,1, 0,-1,0);
125 
126  glTranslatef(0,0,+0.5f);
127  glRotated(app_state.pitch, 1, 0, 0);
128  glRotated(app_state.yaw, 0, 1, 0);
129  glTranslatef(0,0,-0.5f);
130 
131  glPointSize((float)width/640);
132  glEnable(GL_DEPTH_TEST);
133  glEnable(GL_TEXTURE_2D);
134  glBindTexture(GL_TEXTURE_2D, tex.get_gl_handle());
135  glBegin(GL_POINTS);
136 
137  auto points = reinterpret_cast<const rs::float3 *>(dev.get_frame_data(rs::stream::points));
138  auto depth = reinterpret_cast<const uint16_t *>(dev.get_frame_data(rs::stream::depth));
139 
140  for(int y=0; y<depth_intrin.height; ++y)
141  {
142  for(int x=0; x<depth_intrin.width; ++x)
143  {
144  if(points->z) //if(uint16_t d = *depth++)
145  {
146  //const rs::float3 point = depth_intrin.deproject({static_cast<float>(x),static_cast<float>(y)}, d*depth_scale);
147  glTexCoord(identical ? tex_intrin.pixel_to_texcoord({static_cast<float>(x),static_cast<float>(y)}) : tex_intrin.project_to_texcoord(extrin.transform(*points)));
148  glVertex(*points);
149  }
150  ++points;
151  }
152  }
153  glEnd();
154  glPopMatrix();
155  glMatrixMode(GL_PROJECTION);
156  glPopMatrix();
157  glPopAttrib();
158 
159  glfwGetWindowSize(win, &width, &height);
160  glPushAttrib(GL_ALL_ATTRIB_BITS);
161  glPushMatrix();
162  glOrtho(0, width, height, 0, -1, +1);
163 
164  std::ostringstream ss; ss << dev.get_name() << " (" << app_state.tex_streams[app_state.index] << ")";
165  draw_text((width-get_text_width(ss.str().c_str()))/2, height-20, ss.str().c_str());
166 
167  ss.str(""); ss << fps << " FPS";
168  draw_text(20, 40, ss.str().c_str());
169  glPopMatrix();
170 
171  glfwSwapBuffers(win);
172  }
173 
174  glfwDestroyWindow(win);
175  glfwTerminate();
176  return EXIT_SUCCESS;
177 }
178 catch(const rs::error & e)
179 {
180  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
181  return EXIT_FAILURE;
182 }
183 catch(const std::exception & e)
184 {
185  std::cerr << e.what() << std::endl;
186  return EXIT_FAILURE;
187 }
float2 project_to_texcoord(const float3 &point) const
Definition: rs.hpp:268
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
Provides convenience methods relating to devices.
Definition: rs.hpp:567
double lastX
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
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:456
const std::string & get_failed_args() const
Definition: rs.hpp:315
int main(int argc, char *argv[])
void glTexCoord(const rs::float2 &tex_coord)
rs_error * e
GLint GLint GLsizei GLsizei height
Definition: glext.h:112
Cross-stream extrinsics: encode the topology describing how the different devices are connected...
Definition: rs.hpp:281
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:114
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glext.h:8966
#define GLFW_MOUSE_BUTTON_RIGHT
Definition: glfw3.h:433
GLFWAPI void * glfwGetWindowUserPointer(GLFWwindow *window)
Returns the user pointer of the specified window.
Definition: window.c:612
double lastY
Video stream intrinsics.
Definition: rs.hpp:252
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glext.h:8966
GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow *window, GLFWmousebuttonfun cbfun)
Sets the mouse button callback.
Definition: input.c:482
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:112
float2 pixel_to_texcoord(const float2 &pixel) const
Definition: rs.hpp:259
#define GLFW_MOUSE_BUTTON_LEFT
Definition: glfw3.h:432
Exposes librealsense functionality for C++ compilers.
#define GLFW_KEY_F2
Definition: glfw3.h:341
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
bool is_identity() const
Definition: rs.hpp:283
float x
Definition: rs.hpp:248
#define GLFW_RELEASE
The key or mouse button was released.
Definition: glfw3.h:225
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
GLFWAPI void glfwSetWindowUserPointer(GLFWwindow *window, void *pointer)
Sets the user pointer of the specified window.
Definition: window.c:605
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
GLuint GLuint GLsizei count
Definition: glext.h:111
Context.
Definition: rs.hpp:319
GLfloat f
Definition: glext.h:1868
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:321
#define GLFW_KEY_F1
Definition: glfw3.h:340
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 get_text_width(const char *text)
Definition: example.hpp:41
int height
Definition: rs.h:303
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:413
int width
Definition: rs.h:302
void glVertex(const rs::float3 &vertex)
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
double yaw
GLdouble s
Definition: glext.h:231
double pitch
GLint GLint GLsizei width
Definition: glext.h:112
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:369
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:484
void draw_text(int x, int y, const char *text)
Definition: example.hpp:46
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:458
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
float x
Definition: rs.hpp:249
GLuint get_gl_handle() const
Definition: example.hpp:71
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
void upload(const void *data, int width, int height, rs::format format, int stride=0)
Definition: example.hpp:73
rs::device * dev
std::vector< rs::stream > tex_streams
int get_device_count() const
Definition: rs.hpp:343
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