Intel® RealSense™ Cross Platform API  2.13.0
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 "assert.h"
10 
11 #include <math.h>
12 
13 
14 /* 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 */
15 static void rs2_project_point_to_pixel(float pixel[2], const struct rs2_intrinsics * intrin, const float point[3])
16 {
17  //assert(intrin->model != RS2_DISTORTION_INVERSE_BROWN_CONRADY); // Cannot project to an inverse-distorted image
18 
19  float x = point[0] / point[2], y = point[1] / point[2];
20 
22  {
23 
24  float r2 = x*x + y*y;
25  float f = 1 + intrin->coeffs[0]*r2 + intrin->coeffs[1]*r2*r2 + intrin->coeffs[4]*r2*r2*r2;
26  x *= f;
27  y *= f;
28  float dx = x + 2*intrin->coeffs[2]*x*y + intrin->coeffs[3]*(r2 + 2*x*x);
29  float dy = y + 2*intrin->coeffs[3]*x*y + intrin->coeffs[2]*(r2 + 2*y*y);
30  x = dx;
31  y = dy;
32  }
33  if (intrin->model == RS2_DISTORTION_FTHETA)
34  {
35  float r = sqrtf(x*x + y*y);
36  float rd = (float)(1.0f / intrin->coeffs[0] * atan(2 * r* tan(intrin->coeffs[0] / 2.0f)));
37  x *= rd / r;
38  y *= rd / r;
39  }
40 
41  pixel[0] = x * intrin->fx + intrin->ppx;
42  pixel[1] = y * intrin->fy + intrin->ppy;
43 }
44 
45 /* 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 */
46 static void rs2_deproject_pixel_to_point(float point[3], const struct rs2_intrinsics * intrin, const float pixel[2], float depth)
47 {
48  assert(intrin->model != RS2_DISTORTION_MODIFIED_BROWN_CONRADY); // Cannot deproject from a forward-distorted image
49  assert(intrin->model != RS2_DISTORTION_FTHETA); // Cannot deproject to an ftheta image
50  //assert(intrin->model != RS2_DISTORTION_BROWN_CONRADY); // Cannot deproject to an brown conrady model
51 
52  float x = (pixel[0] - intrin->ppx) / intrin->fx;
53  float y = (pixel[1] - intrin->ppy) / intrin->fy;
55  {
56  float r2 = x*x + y*y;
57  float f = 1 + intrin->coeffs[0]*r2 + intrin->coeffs[1]*r2*r2 + intrin->coeffs[4]*r2*r2*r2;
58  float ux = x*f + 2*intrin->coeffs[2]*x*y + intrin->coeffs[3]*(r2 + 2*x*x);
59  float uy = y*f + 2*intrin->coeffs[3]*x*y + intrin->coeffs[2]*(r2 + 2*y*y);
60  x = ux;
61  y = uy;
62  }
63  point[0] = depth * x;
64  point[1] = depth * y;
65  point[2] = depth;
66 }
67 
68 /* Transform 3D coordinates relative to one sensor to 3D coordinates relative to another viewpoint */
69 static void rs2_transform_point_to_point(float to_point[3], const struct rs2_extrinsics * extrin, const float from_point[3])
70 {
71  to_point[0] = extrin->rotation[0] * from_point[0] + extrin->rotation[3] * from_point[1] + extrin->rotation[6] * from_point[2] + extrin->translation[0];
72  to_point[1] = extrin->rotation[1] * from_point[0] + extrin->rotation[4] * from_point[1] + extrin->rotation[7] * from_point[2] + extrin->translation[1];
73  to_point[2] = extrin->rotation[2] * from_point[0] + extrin->rotation[5] * from_point[1] + extrin->rotation[8] * from_point[2] + extrin->translation[2];
74 }
75 
76 /* Calculate horizontal and vertical feild of view, based on video intrinsics */
77 static void rs2_fov(const struct rs2_intrinsics * intrin, float to_fov[2])
78 {
79  to_fov[0] = (atan2f(intrin->ppx + 0.5f, intrin->fx) + atan2f(intrin->width - (intrin->ppx + 0.5f), intrin->fx)) * 57.2957795f;
80  to_fov[1] = (atan2f(intrin->ppy + 0.5f, intrin->fy) + atan2f(intrin->height - (intrin->ppy + 0.5f), intrin->fy)) * 57.2957795f;
81 }
82 
83 #endif
Definition: rs_types.h:47
float translation[3]
Definition: rs_sensor.h:85
float coeffs[5]
Definition: rs_types.h:65
float rotation[9]
Definition: rs_sensor.h:84
float ppx
Definition: rs_types.h:60
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