4 #ifndef LIBREALSENSE_RSUTIL2_H
5 #define LIBREALSENSE_RSUTIL2_H
19 static void rs2_project_point_to_pixel(
float pixel[2],
const struct rs2_intrinsics * intrin,
const float point[3])
21 float x = point[0] / point[2], y = point[1] / point[2];
28 float f = 1 + intrin->
coeffs[0]*r2 + intrin->
coeffs[1]*r2*r2 + intrin->
coeffs[4]*r2*r2*r2;
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);
39 float r2 = x * x + y * y;
40 float f = 1 + intrin->
coeffs[0] * r2 + intrin->
coeffs[1] * r2*r2 + intrin->
coeffs[4] * r2*r2*r2;
45 float dx = xf + 2 * intrin->
coeffs[2] * x*y + intrin->
coeffs[3] * (r2 + 2 * x*x);
46 float dy = yf + 2 * intrin->
coeffs[3] * x*y + intrin->
coeffs[2] * (r2 + 2 * y*y);
54 float r = sqrtf(x*x + y*y);
59 float rd = (float)(1.0f / intrin->
coeffs[0] * atan(2 * r* tan(intrin->
coeffs[0] / 2.0f)));
65 float r = sqrtf(x*x + y*y);
70 float theta = atan(r);
71 float theta2 = theta*theta;
72 float series = 1 + theta2*(intrin->
coeffs[0] + theta2*(intrin->
coeffs[1] + theta2*(intrin->
coeffs[2] + theta2*intrin->
coeffs[3])));
73 float rd = theta*series;
78 pixel[0] = x * intrin->
fx + intrin->
ppx;
79 pixel[1] = y * intrin->
fy + intrin->
ppy;
83 static void rs2_deproject_pixel_to_point(
float point[3],
const struct rs2_intrinsics * intrin,
const float pixel[2],
float depth)
88 float x = (pixel[0] - intrin->
ppx) / intrin->
fx;
89 float y = (pixel[1] - intrin->
ppy) / intrin->
fy;
93 float f = 1 + intrin->
coeffs[0]*r2 + intrin->
coeffs[1]*r2*r2 + intrin->
coeffs[4]*r2*r2*r2;
94 float ux = x*f + 2*intrin->
coeffs[2]*x*y + intrin->
coeffs[3]*(r2 + 2*x*x);
95 float uy = y*f + 2*intrin->
coeffs[3]*x*y + intrin->
coeffs[2]*(r2 + 2*y*y);
101 float rd = sqrtf(x*x + y*y);
102 if (rd < FLT_EPSILON)
108 float theta2 = rd*rd;
109 for (
int i = 0; i < 4; i++)
111 float f = theta*(1 + theta2*(intrin->
coeffs[0] + theta2*(intrin->
coeffs[1] + theta2*(intrin->
coeffs[2] + theta2*intrin->
coeffs[3])))) - rd;
112 if (fabs(f) < FLT_EPSILON)
116 float df = 1 + theta2*(3 * intrin->
coeffs[0] + theta2*(5 * intrin->
coeffs[1] + theta2*(7 * intrin->
coeffs[2] + 9 * theta2*intrin->
coeffs[3])));
118 theta2 = theta*theta;
120 float r = tan(theta);
126 float rd = sqrtf(x*x + y*y);
127 if (rd < FLT_EPSILON)
131 float r = (float)(tan(intrin->
coeffs[0] * rd) / atan(2 * tan(intrin->
coeffs[0] / 2.0f)));
136 point[0] = depth * x;
137 point[1] = depth * y;
142 static void rs2_transform_point_to_point(
float to_point[3],
const struct rs2_extrinsics * extrin,
const float from_point[3])
150 static void rs2_fov(
const struct rs2_intrinsics * intrin,
float to_fov[2])
152 to_fov[0] = (atan2f(intrin->
ppx + 0.5f, intrin->
fx) + atan2f(intrin->
width - (intrin->
ppx + 0.5f), intrin->
fx)) * 57.2957795f;
153 to_fov[1] = (atan2f(intrin->
ppy + 0.5f, intrin->
fy) + atan2f(intrin->
height - (intrin->
ppy + 0.5f), intrin->
fy)) * 57.2957795f;
156 static void next_pixel_in_line(
float curr[2],
const float start[2],
const float end[2])
158 float line_slope = (end[1] - start[1]) / (end[0] - start[0]);
159 if (fabs(end[0] - curr[0]) > fabs(end[1] - curr[1]))
161 curr[0] = end[0] > curr[0] ? curr[0] + 1 : curr[0] - 1;
162 curr[1] = end[1] - line_slope * (end[0] - curr[0]);
166 curr[1] = end[1] > curr[1] ? curr[1] + 1 : curr[1] - 1;
167 curr[0] = end[0] - ((end[1] + curr[1]) / line_slope);
171 static bool is_pixel_in_line(
const float curr[2],
const float start[2],
const float end[2])
173 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])) &&
174 ((end[1] >= start[1] && end[1] >= curr[1] && curr[1] >= start[1]) || (end[1] <= start[1] && end[1] <= curr[1] && curr[1] <= start[1]));
177 static void adjust_2D_point_to_boundary(
float p[2],
int width,
int height)
179 if (p[0] < 0) p[0] = 0;
180 if (p[0] > width) p[0] = (float)width;
181 if (p[1] < 0) p[1] = 0;
182 if (p[1] > height) p[1] = (float)height;
186 static void rs2_project_color_pixel_to_depth_pixel(
float to_pixel[2],
187 const uint16_t* data,
float depth_scale,
188 float depth_min,
float depth_max,
193 const float from_pixel[2])
196 float start_pixel[2] = { 0 }, min_point[3] = { 0 }, min_transformed_point[3] = { 0 };
197 rs2_deproject_pixel_to_point(min_point, color_intrin, from_pixel, depth_min);
198 rs2_transform_point_to_point(min_transformed_point, color_to_depth, min_point);
199 rs2_project_point_to_pixel(start_pixel, depth_intrin, min_transformed_point);
200 adjust_2D_point_to_boundary(start_pixel, depth_intrin->
width, depth_intrin->
height);
203 float end_pixel[2] = { 0 }, max_point[3] = { 0 }, max_transformed_point[3] = { 0 };
204 rs2_deproject_pixel_to_point(max_point, color_intrin, from_pixel, depth_max);
205 rs2_transform_point_to_point(max_transformed_point, color_to_depth, max_point);
206 rs2_project_point_to_pixel(end_pixel, depth_intrin, max_transformed_point);
207 adjust_2D_point_to_boundary(end_pixel, depth_intrin->
width, depth_intrin->
height);
211 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))
213 float depth = depth_scale * data[(int)p[1] * depth_intrin->
width + (
int)p[0]];
217 float projected_pixel[2] = { 0 }, point[3] = { 0 }, transformed_point[3] = { 0 };
218 rs2_deproject_pixel_to_point(point, depth_intrin, p, depth);
219 rs2_transform_point_to_point(transformed_point, depth_to_color, point);
220 rs2_project_point_to_pixel(projected_pixel, color_intrin, transformed_point);
222 float new_dist = pow((projected_pixel[1] - from_pixel[1]), 2) + pow((projected_pixel[0] - from_pixel[0]), 2);
223 if (new_dist < min_dist || min_dist < 0)
Exposes librealsense functionality for C compilers.
Exposes RealSense frame functionality for C compilers.
Exposes RealSense sensor functionality for C compilers.
Exposes RealSense structs.
@ RS2_DISTORTION_KANNALA_BRANDT4
Definition: rs_types.h:52
@ RS2_DISTORTION_MODIFIED_BROWN_CONRADY
Definition: rs_types.h:48
@ RS2_DISTORTION_BROWN_CONRADY
Definition: rs_types.h:51
@ RS2_DISTORTION_INVERSE_BROWN_CONRADY
Definition: rs_types.h:49
@ RS2_DISTORTION_FTHETA
Definition: rs_types.h:50
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented.
Definition: rs_sensor.h:96
float translation[3]
Definition: rs_sensor.h:98
float rotation[9]
Definition: rs_sensor.h:97
Video stream intrinsics.
Definition: rs_types.h:59
int height
Definition: rs_types.h:61
float fx
Definition: rs_types.h:64
float fy
Definition: rs_types.h:65
float ppx
Definition: rs_types.h:62
rs2_distortion model
Definition: rs_types.h:66
int width
Definition: rs_types.h:60
float ppy
Definition: rs_types.h:63
float coeffs[5]
Definition: rs_types.h:67