android/jni/utils.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include <jni.h>
5 #include <memory>
6 #include "error.h"
7 #include "../../../include/librealsense2/rs.hpp"
8 #include "../../../include/librealsense2/rsutil.h"
9 
10 // helper method for retrieving rs2_intrinsics from intrinsic object
11 rs2_intrinsics intrinsic_jobject2rs(JNIEnv *env, jobject intrinsic) {
12  jclass intrinsic_class = env->GetObjectClass(intrinsic);
13  jfieldID width_field = env->GetFieldID(intrinsic_class, "mWidth", "I");
14  jfieldID height_field = env->GetFieldID(intrinsic_class, "mHeight", "I");
15  jfieldID ppx_field = env->GetFieldID(intrinsic_class, "mPpx", "F");
16  jfieldID ppy_field = env->GetFieldID(intrinsic_class, "mPpy", "F");
17  jfieldID fx_field = env->GetFieldID(intrinsic_class, "mFx", "F");
18  jfieldID fy_field = env->GetFieldID(intrinsic_class, "mFy", "F");
19  jfieldID model_field = env->GetFieldID(intrinsic_class, "mModelValue", "I");
20  jfieldID coeff_field = env->GetFieldID(intrinsic_class, "mCoeffs", "[F");
22  intrinsics.width = env->GetIntField(intrinsic, width_field);
23  intrinsics.height = env->GetIntField(intrinsic, height_field);
24  intrinsics.ppx = env->GetFloatField(intrinsic, ppx_field);
25  intrinsics.ppy = env->GetFloatField(intrinsic, ppy_field);
26  intrinsics.fx = env->GetFloatField(intrinsic, fx_field);
27  intrinsics.fy = env->GetFloatField(intrinsic, fy_field);
28  intrinsics.model = (rs2_distortion)env->GetIntField(intrinsic, model_field);
29 
30  jobject coeffsObject = env->GetObjectField(intrinsic, coeff_field);
31  jfloatArray* coeffsArray = reinterpret_cast<jfloatArray *>(&coeffsObject);
32  jfloat * coeffs = env->GetFloatArrayElements(*coeffsArray, NULL);
33  memcpy(intrinsics.coeffs, coeffs, 5 * sizeof(float));
34  env->ReleaseFloatArrayElements(*coeffsArray, coeffs, 0);
35 
36  return intrinsics;
37 }
38 
39 // helper method for retrieving rs2_extrinsics from extrinsic object
40 rs2_extrinsics extrinsic_jobject2rs(JNIEnv *env, jobject extrinsic) {
42  jclass extrinsic_class = env->GetObjectClass(extrinsic);
43  //fill rotation
44  jfieldID rotation_field = env->GetFieldID(extrinsic_class, "mRotation", "[F");
45  jobject rotationObject = env->GetObjectField(extrinsic, rotation_field);
46  jfloatArray* rotationArray = reinterpret_cast<jfloatArray *>(&rotationObject);
47  jfloat * rotation = env->GetFloatArrayElements(*rotationArray, NULL);
48  memcpy(extrinsics.rotation, rotation, 9 * sizeof(float));
49  env->ReleaseFloatArrayElements(*rotationArray, rotation, 0);
50  //fill translation
51  jfieldID translation_field = env->GetFieldID(extrinsic_class, "mTranslation", "[F");
52  jobject translationObject = env->GetObjectField(extrinsic, translation_field);
53  jfloatArray* translationArray = reinterpret_cast<jfloatArray *>(&translationObject);
54  jfloat * translation = env->GetFloatArrayElements(*translationArray, NULL);
55  memcpy(extrinsics.translation, translation, 3 * sizeof(float));
56  env->ReleaseFloatArrayElements(*translationArray, translation, 0);
57 
58  return extrinsics;
59 }
60 
61 extern "C" JNIEXPORT void JNICALL
63  jobject pixel_2D, jobject intrinsic,
64  jobject point_3D) {
65  // retrieving float[3] from point_3D
66  jclass point_3D_class = env->GetObjectClass(point_3D);
67  jfieldID point_x_field = env->GetFieldID(point_3D_class, "mX", "F");
68  jfieldID point_y_field = env->GetFieldID(point_3D_class, "mY", "F");
69  jfieldID point_z_field = env->GetFieldID(point_3D_class, "mZ", "F");
70  float point[3];
71  point[0] = env->GetFloatField(point_3D, point_x_field);
72  point[1] = env->GetFloatField(point_3D, point_y_field);
73  point[2] = env->GetFloatField(point_3D, point_z_field);
74 
75  // retrieving rs2_intrinsics from intrinsic object
77 
78  // preparing struct to be filled by API function
79  float pixel[2] = {0.f, 0.f};
80 
81  rs2_project_point_to_pixel(pixel, &intrinsics, point);
82 
83  // returning pixel into pixel_2D struct
84  jclass pixel_2D_class = env->GetObjectClass(pixel_2D);
85  jfieldID pixel_x_field = env->GetFieldID(pixel_2D_class, "mX", "F");
86  jfieldID pixel_y_field = env->GetFieldID(pixel_2D_class, "mY", "F");
87  env->SetFloatField(pixel_2D, pixel_x_field, pixel[0]);
88  env->SetFloatField(pixel_2D, pixel_y_field, pixel[1]);
89 }
90 
91 extern "C" JNIEXPORT void JNICALL
93  jobject point_3D, jobject intrinsic,
94  jobject pixel_2D, jfloat depth) {
95 
96  // retrieving float[2] from pixel_2D
97  jclass pixel_2D_class = env->GetObjectClass(pixel_2D);
98  jfieldID pixel_x_field = env->GetFieldID(pixel_2D_class, "mX", "F");
99  jfieldID pixel_y_field = env->GetFieldID(pixel_2D_class, "mY", "F");
100  float pixel[2];
101  pixel[0] = env->GetFloatField(pixel_2D, pixel_x_field);
102  pixel[1] = env->GetFloatField(pixel_2D, pixel_y_field);
103 
104  // retrieving rs2_intrinsics from intrinsic object
106 
107  // preparing struct to be filled by API function
108  float point[3] = {0.f, 0.f, 0.f};
109 
110  rs2_deproject_pixel_to_point(point, &intrinsics, pixel, depth);
111 
112  // returning point into point_3D struct
113  jclass point_3D_class = env->GetObjectClass(point_3D);
114  jfieldID point_x_field = env->GetFieldID(point_3D_class, "mX", "F");
115  jfieldID point_y_field = env->GetFieldID(point_3D_class, "mY", "F");
116  jfieldID point_z_field = env->GetFieldID(point_3D_class, "mZ", "F");
117  env->SetFloatField(point_3D, point_x_field, point[0]);
118  env->SetFloatField(point_3D, point_y_field, point[1]);
119  env->SetFloatField(point_3D, point_z_field, point[2]);
120 }
121 
122 extern "C" JNIEXPORT void JNICALL
124  jobject to_point_3D, jobject extrinsic,
125  jobject from_point_3D) {
126  // retrieving float[3] from from_point_3D
127  jclass point_3D_class = env->GetObjectClass(from_point_3D);
128  jfieldID point_x_field = env->GetFieldID(point_3D_class, "mX", "F");
129  jfieldID point_y_field = env->GetFieldID(point_3D_class, "mY", "F");
130  jfieldID point_z_field = env->GetFieldID(point_3D_class, "mZ", "F");
131  float from_point[3];
132  from_point[0] = env->GetFloatField(from_point_3D, point_x_field);
133  from_point[1] = env->GetFloatField(from_point_3D, point_y_field);
134  from_point[2] = env->GetFloatField(from_point_3D, point_z_field);
135 
136  // retrieving rs2_extrinsics from extrinsic object
138 
139  // preparing struct to be filled by API function
140  float to_point[3] = {0.f, 0.f, 0.f};
141 
142  //api call
143  rs2_transform_point_to_point(to_point, &extrinsics, from_point);
144 
145  // returning point into point_3D struct
146  jclass to_point_3D_class = env->GetObjectClass(to_point_3D);
147  jfieldID to_point_x_field = env->GetFieldID(to_point_3D_class, "mX", "F");
148  jfieldID to_point_y_field = env->GetFieldID(to_point_3D_class, "mY", "F");
149  jfieldID to_point_z_field = env->GetFieldID(to_point_3D_class, "mZ", "F");
150  env->SetFloatField(to_point_3D, to_point_x_field, to_point[0]);
151  env->SetFloatField(to_point_3D, to_point_y_field, to_point[1]);
152  env->SetFloatField(to_point_3D, to_point_z_field, to_point[2]);
153 }
154 
155 extern "C" JNIEXPORT void JNICALL
157  jobject fov, jobject intrinsic) {
158  // retrieving rs2_intrinsics from intrinsic object
160 
161  float fov_cpp[2] = {0.f, 0.f};
162  rs2_fov(&intrinsics, fov_cpp);
163 
164  // returning fov into fov struct
165  jclass fov_class = env->GetObjectClass(fov);
166  jfieldID fov_x_field = env->GetFieldID(fov_class, "mFovX", "F");
167  jfieldID fov_y_field = env->GetFieldID(fov_class, "mFovY", "F");
168  env->SetFloatField(fov, fov_x_field, fov_cpp[0]);
169  env->SetFloatField(fov, fov_y_field, fov_cpp[1]);
170 }
171 
172 extern "C" JNIEXPORT void JNICALL
174  jobject toPixel, jlong depthFrameHandle, jfloat depthScale,
175  jfloat depthMin, jfloat depthMax,
176  jobject depthIntrinsic, jobject otherIntrinsic,
177  jobject otherToDepth, jobject depthToOther,
178  jobject fromPixel) {
179 
180 
181  // preparing struct to be filled by API function
182  float to_pixel[2] = {0.f, 0.f};
183 
184  // preparing depth frame data
185  rs2_frame* depthFrame = reinterpret_cast<rs2_frame*>(depthFrameHandle);
186  rs2_error* e = nullptr;
187  const uint16_t* depthFrameData = (const uint16_t*)rs2_get_frame_data(depthFrame, &e);
188  // retrieving intrinsics objects
190  rs2_intrinsics other_intrinsics = intrinsic_jobject2rs(env, otherIntrinsic);
191  // retrieving extrinsics objects
192  rs2_extrinsics other_to_depth_extrinsics = extrinsic_jobject2rs(env, otherToDepth);
193  rs2_extrinsics depth_to_other_extrinsics = extrinsic_jobject2rs(env, depthToOther);
194  // retrieving float[2] from fromPixel
195  jclass from_pixel_class = env->GetObjectClass(fromPixel);
196  jfieldID from_pixel_x_field = env->GetFieldID(from_pixel_class, "mX", "F");
197  jfieldID from_pixel_y_field = env->GetFieldID(from_pixel_class, "mY", "F");
198  float from_pixel[2];
199  from_pixel[0] = env->GetFloatField(fromPixel, from_pixel_x_field);
200  from_pixel[1] = env->GetFloatField(fromPixel, from_pixel_y_field);
201 
202  // api call
203  rs2_project_color_pixel_to_depth_pixel(to_pixel, depthFrameData, depthScale, depthMin, depthMax,
204  &depth_intrinsics, &other_intrinsics, &other_to_depth_extrinsics, &depth_to_other_extrinsics, from_pixel);
205 
206  // returning pixel into pixel_2D struct
207  jclass to_pixel_class = env->GetObjectClass(toPixel);
208  jfieldID to_pixel_x_field = env->GetFieldID(to_pixel_class, "mX", "F");
209  jfieldID to_pixel_y_field = env->GetFieldID(to_pixel_class, "mY", "F");
210  env->SetFloatField(toPixel, to_pixel_x_field, to_pixel[0]);
211  env->SetFloatField(toPixel, to_pixel_y_field, to_pixel[1]);
212 }
static void rs2_transform_point_to_point(float to_point[3], const struct rs2_extrinsics *extrin, const float from_point[3])
Definition: rsutil.h:168
::realsense_legacy_msgs::extrinsics_< std::allocator< void > > extrinsics
Definition: extrinsics.h:59
JNIEXPORT void JNICALL Java_com_intel_realsense_librealsense_Utils_nDeprojectPixelToPoint(JNIEnv *env, jclass type, jobject point_3D, jobject intrinsic, jobject pixel_2D, jfloat depth)
rs2_distortion
Distortion model: defines how pixel coordinates should be mapped to sensor coordinates.
Definition: rs_types.h:45
float translation[3]
Definition: rs_sensor.h:99
static void rs2_fov(const struct rs2_intrinsics *intrin, float to_fov[2])
Definition: rsutil.h:176
GLint GLint GLsizei GLsizei GLsizei depth
unsigned short uint16_t
Definition: stdint.h:79
float coeffs[5]
Definition: rs_types.h:67
JNIEXPORT void JNICALL Java_com_intel_realsense_librealsense_Utils_nProject2dPixelToDepthPixel(JNIEnv *env, jclass type, jobject toPixel, jlong depthFrameHandle, jfloat depthScale, jfloat depthMin, jfloat depthMax, jobject depthIntrinsic, jobject otherIntrinsic, jobject otherToDepth, jobject depthToOther, jobject fromPixel)
e
Definition: rmse.py:177
point
Definition: rmse.py:166
float rotation[9]
Definition: rs_sensor.h:98
GLint GLenum GLint const GLfloat * coeffs
Definition: glext.h:10577
static void rs2_deproject_pixel_to_point(float point[3], const struct rs2_intrinsics *intrin, const float pixel[2], float depth)
Definition: rsutil.h:83
rs2_intrinsics intrinsic_jobject2rs(JNIEnv *env, jobject intrinsic)
JNIEXPORT void JNICALL Java_com_intel_realsense_librealsense_Utils_nProjectPointToPixel(JNIEnv *env, jclass type, jobject pixel_2D, jobject intrinsic, jobject point_3D)
dictionary intrinsics
Definition: t265_stereo.py:142
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: rs_sensor.h:96
JNIEXPORT void JNICALL Java_com_intel_realsense_librealsense_Utils_nGetFov(JNIEnv *env, jclass type, jobject fov, jobject intrinsic)
JNIEXPORT void JNICALL Java_com_intel_realsense_librealsense_Utils_nTransformPointToPoint(JNIEnv *env, jclass type, jobject to_point_3D, jobject extrinsic, jobject from_point_3D)
rs2_distortion model
Definition: rs_types.h:66
static void rs2_project_point_to_pixel(float pixel[2], const struct rs2_intrinsics *intrin, const float point[3])
Definition: rsutil.h:19
GLenum type
Video stream intrinsics.
Definition: rs_types.h:58
#define NULL
Definition: tinycthread.c:47
static void rs2_project_color_pixel_to_depth_pixel(float to_pixel[2], const uint16_t *data, float depth_scale, float depth_min, float depth_max, const struct rs2_intrinsics *depth_intrin, const struct rs2_intrinsics *color_intrin, const struct rs2_extrinsics *color_to_depth, const struct rs2_extrinsics *depth_to_color, const float from_pixel[2])
Definition: rsutil.h:212
float f[2]
rs2_extrinsics extrinsic_jobject2rs(JNIEnv *env, jobject extrinsic)
struct rs2_frame rs2_frame
Definition: rs_types.h:261
const void * rs2_get_frame_data(const rs2_frame *frame, rs2_error **error)
Definition: rs.cpp:940


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