rs-software-device.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
6 #include "example.hpp"
7 
8 #define STB_IMAGE_WRITE_IMPLEMENTATION
9 #include <stb_image_write.h>
10 #include <int-rs-splash.hpp>
11 
12 #define STB_IMAGE_IMPLEMENTATION
13 #include <stb_image.h>
14 
15 const int W = 640;
16 const int H = 480;
17 const int BPP = 2;
18 
20 {
21  int x, y, bpp;
22  std::vector<uint8_t> frame;
23 };
24 
26 {
27 public:
29  {
30  depth_frame.x = W;
31  depth_frame.y = H;
32  depth_frame.bpp = BPP;
33 
35 
36  std::vector<uint8_t> pixels_depth(depth_frame.x * depth_frame.y * depth_frame.bpp, 0);
37  depth_frame.frame = std::move(pixels_depth);
38 
39  auto realsense_logo = stbi_load_from_memory(splash, (int)splash_size, &color_frame.x, &color_frame.y, &color_frame.bpp, false);
40 
41  std::vector<uint8_t> pixels_color(color_frame.x * color_frame.y * color_frame.bpp, 0);
42 
43  memcpy(pixels_color.data(), realsense_logo, color_frame.x*color_frame.y * 4);
44 
45  for (auto i = 0; i< color_frame.y; i++)
46  for (auto j = 0; j < color_frame.x * 4; j += 4)
47  {
48  if (pixels_color.data()[i*color_frame.x * 4 + j] == 0)
49  {
50  pixels_color.data()[i*color_frame.x * 4 + j] = 22;
51  pixels_color.data()[i*color_frame.x * 4 + j + 1] = 115;
52  pixels_color.data()[i*color_frame.x * 4 + j + 2] = 185;
53  }
54  }
55  color_frame.frame = std::move(pixels_color);
56  }
57 
59  {
60  return color_frame;
61  }
62 
64  {
65  draw_text(50, 50, "This point-cloud is generated from a synthetic device:");
66 
68  if (now - last > std::chrono::milliseconds(1))
69  {
70  app_state.yaw -= 1;
71  wave_base += 0.1f;
72  last = now;
73 
74  for (int i = 0; i < depth_frame.y; i++)
75  {
76  for (int j = 0; j < depth_frame.x; j++)
77  {
78  auto d = 2 + 0.1 * (1 + sin(wave_base + j / 50.f));
79  ((uint16_t*)depth_frame.frame.data())[i*depth_frame.x + j] = (int)(d * 0xff);
80  }
81  }
82  }
83  return depth_frame;
84  }
85 
87  {
89  (float)color_frame.x / 2, (float)color_frame.y / 2,
90  (float)color_frame.x / 2, (float)color_frame.y / 2,
91  RS2_DISTORTION_BROWN_CONRADY ,{ 0,0,0,0,0 } };
92 
93  return intrinsics;
94  }
95 
97  {
99  (float)depth_frame.x / 2, (float)depth_frame.y / 2,
100  (float)depth_frame.x , (float)depth_frame.y ,
101  RS2_DISTORTION_BROWN_CONRADY ,{ 0,0,0,0,0 } };
102 
103  return intrinsics;
104  }
105 
106 private:
109 
110  std::chrono::high_resolution_clock::time_point last;
111  float wave_base = 0.f;
112 };
113 
114 int main(int argc, char * argv[]) try
115 {
116  window app(1280, 1500, "RealSense Capture Example");
117  glfw_state app_state;
118  register_glfw_callbacks(app, app_state);
119  rs2::colorizer color_map; // Save colorized depth for preview
120 
123  int frame_number = 0;
124 
126 
127  auto texture = app_data.get_synthetic_texture();
128 
131 
132  //==================================================//
133  // Declare Software-Only Device //
134  //==================================================//
135 
136  rs2::software_device dev; // Create software-only device
137 
138  auto depth_sensor = dev.add_sensor("Depth"); // Define single sensor
139  auto color_sensor = dev.add_sensor("Color"); // Define single sensor
140 
141  auto depth_stream = depth_sensor.add_video_stream({ RS2_STREAM_DEPTH, 0, 0,
142  W, H, 60, BPP,
143  RS2_FORMAT_Z16, depth_intrinsics });
144 
145  depth_sensor.add_read_only_option(RS2_OPTION_DEPTH_UNITS, 0.001f);
146 
147 
148  auto color_stream = color_sensor.add_video_stream({ RS2_STREAM_COLOR, 0, 1, texture.x,
149  texture.y, 60, texture.bpp,
150  RS2_FORMAT_RGBA8, color_intrinsics });
151 
154 
155  depth_sensor.open(depth_stream);
156  color_sensor.open(color_stream);
157 
158  depth_sensor.start(sync);
159  color_sensor.start(sync);
160 
161  depth_stream.register_extrinsics_to(color_stream, { { 1,0,0,0,1,0,0,0,1 },{ 0,0,0 } });
162 
163  while (app) // Application still alive?
164  {
165  synthetic_frame& depth_frame = app_data.get_synthetic_depth(app_state);
166 
167  depth_sensor.on_video_frame({ depth_frame.frame.data(), // Frame pixels from capture API
168  [](void*) {}, // Custom deleter (if required)
169  depth_frame.x*depth_frame.bpp, depth_frame.bpp, // Stride and Bytes-per-pixel
170  (rs2_time_t)frame_number * 16, RS2_TIMESTAMP_DOMAIN_HARDWARE_CLOCK, frame_number, // Timestamp, Frame# for potential sync services
171  depth_stream });
172 
173 
174  color_sensor.on_video_frame({ texture.frame.data(), // Frame pixels from capture API
175  [](void*) {}, // Custom deleter (if required)
176  texture.x*texture.bpp, texture.bpp, // Stride and Bytes-per-pixel
177  (rs2_time_t)frame_number * 16, RS2_TIMESTAMP_DOMAIN_HARDWARE_CLOCK, frame_number, // Timestamp, Frame# for potential sync services
178  color_stream });
179 
180  ++frame_number;
181 
182  rs2::frameset fset = sync.wait_for_frames();
185 
186  if (depth && color)
187  {
188  if (auto as_depth = depth.as<rs2::depth_frame>())
189  points = pc.calculate(as_depth);
190  pc.map_to(color);
191 
192  // Upload the color frame to OpenGL
193  app_state.tex.upload(color);
194  }
195  draw_pointcloud(app.width(), app.height(), app_state, points);
196  }
197 
198  return EXIT_SUCCESS;
199 }
200 catch (const rs2::error & e)
201 {
202  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
203  return EXIT_FAILURE;
204 }
205 catch (const std::exception& e)
206 {
207  std::cerr << e.what() << std::endl;
208  return EXIT_FAILURE;
209 }
210 
211 
212 
synthetic_frame depth_frame
static uint8_t splash[]
synthetic_frame & get_synthetic_depth(glfw_state &app_state)
double yaw
Definition: example.hpp:875
GLuint color
GLint GLint GLsizei GLsizei GLsizei depth
STBIDEF stbi_uc * stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
unsigned short uint16_t
Definition: stdint.h:79
void map_to(frame mapped)
d
Definition: rmse.py:171
software_sensor add_sensor(std::string name)
e
Definition: rmse.py:177
The texture class.
Definition: example.hpp:402
frameset wait_for_frames(unsigned int timeout_ms=5000) const
frame first_or_default(rs2_stream s, rs2_format f=RS2_FORMAT_ANY) const
Definition: rs_frame.hpp:978
const int H
synthetic_frame & get_synthetic_texture()
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
GLdouble f
const int W
synthetic_frame color_frame
float width() const
Definition: example.hpp:627
void upload(const rs2::video_frame &frame)
Definition: example.hpp:406
rs2_intrinsics create_texture_intrinsics()
points calculate(frame depth) const
void create_matcher(rs2_matchers matcher)
GLint j
std::vector< uint8_t > frame
static size_t splash_size
dictionary intrinsics
Definition: t265_stereo.py:142
float height() const
Definition: example.hpp:628
int main(int argc, char *argv[])
void draw_pointcloud(float width, float height, glfw_state &app_state, rs2::points &points)
Definition: example.hpp:886
rs2_intrinsics create_depth_intrinsics()
void draw_text(int x, int y, const char *text)
Definition: example.hpp:109
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
Video stream intrinsics.
Definition: rs_types.h:58
std::ostream & cerr()
int i
double rs2_time_t
Definition: rs_types.h:300
void register_glfw_callbacks(window &app, glfw_state &app_state)
Definition: example.hpp:1037
texture tex
Definition: example.hpp:882
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
const int BPP
GLdouble GLdouble GLint GLint const GLdouble * points
T as() const
Definition: rs_frame.hpp:580
std::chrono::high_resolution_clock::time_point last


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