Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
rsutil.h
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 #ifndef LIBREALSENSE_RSUTIL2_H
5 #define LIBREALSENSE_RSUTIL2_H
6 
7 #include "h/rs_types.h"
8 #include "h/rs_sensor.h"
9 #include "h/rs_frame.h"
10 #include "rs.h"
11 #include "assert.h"
12 #include <stdlib.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <math.h>
16 
17 /* Given a point in 3D space, compute the corresponding pixel coordinates in an image with no distortion or forward distortion coefficients produced by the same camera */
18 static void rs2_project_point_to_pixel(float pixel[2], const struct rs2_intrinsics * intrin, const float point[3])
19 {
20  //assert(intrin->model != RS2_DISTORTION_INVERSE_BROWN_CONRADY); // Cannot project to an inverse-distorted image
21 
22  float x = point[0] / point[2], y = point[1] / point[2];
23 
25  {
26 
27  float r2 = x*x + y*y;
28  float f = 1 + intrin->coeffs[0]*r2 + intrin->coeffs[1]*r2*r2 + intrin->coeffs[4]*r2*r2*r2;
29  x *= f;
30  y *= f;
31  float dx = x + 2*intrin->coeffs[2]*x*y + intrin->coeffs[3]*(r2 + 2*x*x);
32  float dy = y + 2*intrin->coeffs[3]*x*y + intrin->coeffs[2]*(r2 + 2*y*y);
33  x = dx;
34  y = dy;
35  }
36  if (intrin->model == RS2_DISTORTION_FTHETA)
37  {
38  float r = sqrtf(x*x + y*y);
39  float rd = (float)(1.0f / intrin->coeffs[0] * atan(2 * r* tan(intrin->coeffs[0] / 2.0f)));
40  x *= rd / r;
41  y *= rd / r;
42  }
43 
44  pixel[0] = x * intrin->fx + intrin->ppx;
45  pixel[1] = y * intrin->fy + intrin->ppy;
46 }
47 
48 /* Given pixel coordinates and depth in an image with no distortion or inverse distortion coefficients, compute the corresponding point in 3D space relative to the same camera */
49 static void rs2_deproject_pixel_to_point(float point[3], const struct rs2_intrinsics * intrin, const float pixel[2], float depth)
50 {
51  assert(intrin->model != RS2_DISTORTION_MODIFIED_BROWN_CONRADY); // Cannot deproject from a forward-distorted image
52  assert(intrin->model != RS2_DISTORTION_FTHETA); // Cannot deproject to an ftheta image
53  //assert(intrin->model != RS2_DISTORTION_BROWN_CONRADY); // Cannot deproject to an brown conrady model
54 
55  float x = (pixel[0] - intrin->ppx) / intrin->fx;
56  float y = (pixel[1] - intrin->ppy) / intrin->fy;
58  {
59  float r2 = x*x + y*y;
60  float f = 1 + intrin->coeffs[0]*r2 + intrin->coeffs[1]*r2*r2 + intrin->coeffs[4]*r2*r2*r2;
61  float ux = x*f + 2*intrin->coeffs[2]*x*y + intrin->coeffs[3]*(r2 + 2*x*x);
62  float uy = y*f + 2*intrin->coeffs[3]*x*y + intrin->coeffs[2]*(r2 + 2*y*y);
63  x = ux;
64  y = uy;
65  }
66  point[0] = depth * x;
67  point[1] = depth * y;
68  point[2] = depth;
69 }
70 
71 /* Transform 3D coordinates relative to one sensor to 3D coordinates relative to another viewpoint */
72 static void rs2_transform_point_to_point(float to_point[3], const struct rs2_extrinsics * extrin, const float from_point[3])
73 {
74  to_point[0] = extrin->rotation[0] * from_point[0] + extrin->rotation[3] * from_point[1] + extrin->rotation[6] * from_point[2] + extrin->translation[0];
75  to_point[1] = extrin->rotation[1] * from_point[0] + extrin->rotation[4] * from_point[1] + extrin->rotation[7] * from_point[2] + extrin->translation[1];
76  to_point[2] = extrin->rotation[2] * from_point[0] + extrin->rotation[5] * from_point[1] + extrin->rotation[8] * from_point[2] + extrin->translation[2];
77 }
78 
79 /* Calculate horizontal and vertical feild of view, based on video intrinsics */
80 static void rs2_fov(const struct rs2_intrinsics * intrin, float to_fov[2])
81 {
82  to_fov[0] = (atan2f(intrin->ppx + 0.5f, intrin->fx) + atan2f(intrin->width - (intrin->ppx + 0.5f), intrin->fx)) * 57.2957795f;
83  to_fov[1] = (atan2f(intrin->ppy + 0.5f, intrin->fy) + atan2f(intrin->height - (intrin->ppy + 0.5f), intrin->fy)) * 57.2957795f;
84 }
85 
86 static void next_pixel_in_line(float curr[2], const float start[2], const float end[2])
87 {
88  float line_slope = (end[1] - start[1]) / (end[0] - start[0]);
89  if (fabs(end[0] - curr[0]) > fabs(end[1] - curr[1]))
90  {
91  curr[0] = end[0] > curr[0] ? curr[0] + 1 : curr[0] - 1;
92  curr[1] = end[1] - line_slope * (end[0] - curr[0]);
93  }
94  else
95  {
96  curr[1] = end[1] > curr[1] ? curr[1] + 1 : curr[1] - 1;
97  curr[0] = end[0] - ((end[1] + curr[1]) / line_slope);
98  }
99 }
100 
101 static bool is_pixel_in_line(const float curr[2], const float start[2], const float end[2])
102 {
103  return ((end[0] >= start[0] && end[0] >= curr[0] && curr[0] >= start[0]) || (end[0] <= start[0] && end[0] <= curr[0] && curr[0] <= start[0])) &&
104  ((end[1] >= start[1] && end[1] >= curr[1] && curr[1] >= start[1]) || (end[1] <= start[1] && end[1] <= curr[1] && curr[1] <= start[1]));
105 }
106 
107 static void adjust_2D_point_to_boundary(float p[2], int width, int height)
108 {
109  if (p[0] < 0) p[0] = 0;
110  if (p[0] > width) p[0] = (float)width;
111  if (p[1] < 0) p[1] = 0;
112  if (p[1] > height) p[1] = (float)height;
113 }
114 
115 /* Find projected pixel with unknown depth search along line. */
116 static void rs2_project_color_pixel_to_depth_pixel(float to_pixel[2],
117  const uint16_t* data, float depth_scale,
118  float depth_min, float depth_max,
119  const struct rs2_intrinsics* depth_intrin,
120  const struct rs2_intrinsics* color_intrin,
121  const struct rs2_extrinsics* color_to_depth,
122  const struct rs2_extrinsics* depth_to_color,
123  const float from_pixel[2])
124 {
125  //Find line start pixel
126  float start_pixel[2] = { 0 }, min_point[3] = { 0 }, min_transformed_point[3] = { 0 };
127  rs2_deproject_pixel_to_point(min_point, color_intrin, from_pixel, depth_min);
128  rs2_transform_point_to_point(min_transformed_point, color_to_depth, min_point);
129  rs2_project_point_to_pixel(start_pixel, depth_intrin, min_transformed_point);
130  adjust_2D_point_to_boundary(start_pixel, depth_intrin->width, depth_intrin->height);
131 
132  //Find line end depth pixel
133  float end_pixel[2] = { 0 }, max_point[3] = { 0 }, max_transformed_point[3] = { 0 };
134  rs2_deproject_pixel_to_point(max_point, color_intrin, from_pixel, depth_max);
135  rs2_transform_point_to_point(max_transformed_point, color_to_depth, max_point);
136  rs2_project_point_to_pixel(end_pixel, depth_intrin, max_transformed_point);
137  adjust_2D_point_to_boundary(end_pixel, depth_intrin->width, depth_intrin->height);
138 
139  //search along line for the depth pixel that it's projected pixel is the closest to the input pixel
140  float min_dist = -1;
141  for (float p[2] = { start_pixel[0], start_pixel[1] }; is_pixel_in_line(p, start_pixel, end_pixel); next_pixel_in_line(p, start_pixel, end_pixel))
142  {
143  float depth = depth_scale * data[(int)p[1] * depth_intrin->width + (int)p[0]];
144  if (depth == 0)
145  continue;
146 
147  float projected_pixel[2] = { 0 }, point[3] = { 0 }, transformed_point[3] = { 0 };
148  rs2_deproject_pixel_to_point(point, depth_intrin, p, depth);
149  rs2_transform_point_to_point(transformed_point, depth_to_color, point);
150  rs2_project_point_to_pixel(projected_pixel, color_intrin, transformed_point);
151 
152  float new_dist = pow((projected_pixel[1] - from_pixel[1]), 2) + pow((projected_pixel[0] - from_pixel[0]), 2);
153  if (new_dist < min_dist || min_dist < 0)
154  {
155  min_dist = new_dist;
156  to_pixel[0] = p[0];
157  to_pixel[1] = p[1];
158  }
159  }
160 }
161 
162 #endif
Definition: rs_types.h:47
float translation[3]
Definition: rs_sensor.h:85
float coeffs[5]
Definition: rs_types.h:65
Exposes librealsense functionality for C compilers.
float rotation[9]
Definition: rs_sensor.h:84
float ppx
Definition: rs_types.h:60
Exposes RealSense frame functionality for C compilers.
Exposes RealSense structs.
Definition: rs_types.h:49
int width
Definition: rs_types.h:58
Exposes RealSense sensor functionality for C compilers.
Definition: rs_types.h:48
Cross-stream extrinsics: encode the topology describing how the different devices are connected.
Definition: rs_sensor.h:82
rs2_distortion model
Definition: rs_types.h:64
float fy
Definition: rs_types.h:63
float fx
Definition: rs_types.h:62
Video stream intrinsics.
Definition: rs_types.h:56
int height
Definition: rs_types.h:59
float ppy
Definition: rs_types.h:61