This repository was archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathcalibration.c
More file actions
804 lines (671 loc) · 24.3 KB
/
calibration.c
File metadata and controls
804 lines (671 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// This library
#include <k4ainternal/calibration.h>
// Dependent libraries
#include <k4ainternal/common.h>
#include <cJSON.h>
#include <locale.h> //cJSON.h need this set correctly.
// System dependencies
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#define READ_RETRY_ALLOC_INCREASE (5 * 1024)
#define READ_RETRY_BASE_ALLOCATION (10 * 1024)
#define MAX_READ_RETRIES (10)
typedef struct _INTRINSIC_TYPE_TO_STRING_MAPPER
{
k4a_calibration_model_type_t type_e;
char *type_s;
} intrinsic_type_to_string_mapper_t;
static intrinsic_type_to_string_mapper_t intrinsic_type_mapper[] =
{ { K4A_CALIBRATION_LENS_DISTORTION_MODEL_THETA, "CALIBRATION_LensDistortionModelTheta" },
{ K4A_CALIBRATION_LENS_DISTORTION_MODEL_POLYNOMIAL_3K, "CALIBRATION_LensDistortionModelPolynomial3K" },
{ K4A_CALIBRATION_LENS_DISTORTION_MODEL_RATIONAL_6KT, "CALIBRATION_LensDistortionModelRational6KT" },
{ K4A_CALIBRATION_LENS_DISTORTION_MODEL_BROWN_CONRADY, "CALIBRATION_LensDistortionModelBrownConrady" } };
typedef struct _calibration_context_t
{
depthmcu_t depthmcu;
size_t json_size;
char *json; // string representation of JSON file
k4a_calibration_camera_t depth_calibration;
k4a_calibration_camera_t color_calibration;
k4a_calibration_imu_t gyro_calibration;
k4a_calibration_imu_t accel_calibration;
} calibration_context_t;
K4A_DECLARE_CONTEXT(calibration_t, calibration_context_t);
static k4a_result_t fill_array_of_floats(cJSON *json, float *data, unsigned int length)
{
k4a_result_t result;
unsigned int elements_to_read = 0;
result = K4A_RESULT_FROM_BOOL(cJSON_IsArray(json) == true);
if (K4A_SUCCEEDED(result))
{
elements_to_read = (unsigned int)cJSON_GetArraySize(json);
result = K4A_RESULT_FROM_BOOL(elements_to_read <= length); // unexpected - too many element to read
elements_to_read = elements_to_read <= length ? elements_to_read : length;
}
if (K4A_SUCCEEDED(result))
{
unsigned int x = 0;
cJSON *element = NULL;
cJSON_ArrayForEach(element, json)
{
result = K4A_RESULT_FROM_BOOL(element != NULL);
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(element) == true);
}
if (K4A_SUCCEEDED(result))
{
data[x++] = (float)element->valuedouble;
}
if (K4A_FAILED(result) || x >= elements_to_read)
{
break;
}
}
}
return result;
}
static k4a_result_t fill_rotation_matrix(cJSON *rt, k4a_calibration_extrinsics_t *extrinsics)
{
k4a_result_t result;
cJSON *rotation = NULL;
cJSON *translation = NULL;
rotation = cJSON_GetObjectItem(rt, "Rotation");
result = K4A_RESULT_FROM_BOOL(rotation != NULL);
if (K4A_SUCCEEDED(result))
{
translation = cJSON_GetObjectItem(rt, "Translation");
result = K4A_RESULT_FROM_BOOL(translation != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(rotation, extrinsics->rotation, COUNTOF(extrinsics->rotation));
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(translation, extrinsics->translation, COUNTOF(extrinsics->translation));
// raw calibration stores extrinsics in meters. convert to mm to align with k4a depth resolution.
for (unsigned int i = 0; i < COUNTOF(extrinsics->translation); i++)
{
extrinsics->translation[i] *= 1000.f;
}
}
return result;
}
static k4a_result_t fill_intrinsics(cJSON *intrinsics, k4a_calibration_intrinsics_t *intrinsic_data)
{
k4a_result_t result;
cJSON *count;
cJSON *type = NULL;
cJSON *parameters = NULL;
unsigned int parameter_count = 0;
count = cJSON_GetObjectItem(intrinsics, "ModelParameterCount");
result = K4A_RESULT_FROM_BOOL(count != NULL);
if (K4A_SUCCEEDED(result))
{
type = cJSON_GetObjectItem(intrinsics, "ModelType");
result = K4A_RESULT_FROM_BOOL(type != NULL);
}
if (K4A_SUCCEEDED(result))
{
parameters = cJSON_GetObjectItem(intrinsics, "ModelParameters");
result = K4A_RESULT_FROM_BOOL(parameters != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(count) == true);
}
if (K4A_SUCCEEDED(result))
{
const unsigned int max_size = COUNTOF(intrinsic_data->parameters.v);
parameter_count = (unsigned int)count->valueint;
result = K4A_RESULT_FROM_BOOL(parameter_count <= max_size);
parameter_count = parameter_count <= max_size ? parameter_count : max_size;
intrinsic_data->parameter_count = parameter_count;
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(parameters, intrinsic_data->parameters.v, parameter_count);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsString(type) == true);
}
if (K4A_SUCCEEDED(result))
{
intrinsic_data->type = K4A_CALIBRATION_LENS_DISTORTION_MODEL_UNKNOWN;
for (unsigned int x = 0; x < COUNTOF(intrinsic_type_mapper); x++)
{
if (strcmp(type->valuestring, intrinsic_type_mapper[x].type_s) == 0)
{
intrinsic_data->type = intrinsic_type_mapper[x].type_e;
break;
}
}
result = K4A_RESULT_FROM_BOOL(intrinsic_data->type != K4A_CALIBRATION_LENS_DISTORTION_MODEL_UNKNOWN);
}
return result;
}
static k4a_result_t fill_in_camera_cal_data(k4a_calibration_camera_t *cal, cJSON *camera)
{
k4a_result_t result;
cJSON *rotation = NULL;
cJSON *intrinsics = NULL;
cJSON *height = NULL;
cJSON *width = NULL;
cJSON *radius = NULL;
intrinsics = cJSON_GetObjectItem(camera, "Intrinsics");
result = K4A_RESULT_FROM_BOOL(intrinsics != NULL);
if (K4A_SUCCEEDED(result))
{
rotation = cJSON_GetObjectItem(camera, "Rt");
result = K4A_RESULT_FROM_BOOL(rotation != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = fill_rotation_matrix(rotation, &cal->extrinsics);
}
if (K4A_SUCCEEDED(result))
{
result = fill_intrinsics(intrinsics, &cal->intrinsics);
}
if (K4A_SUCCEEDED(result))
{
height = cJSON_GetObjectItem(camera, "SensorHeight");
result = K4A_RESULT_FROM_BOOL(height != NULL);
}
if (K4A_SUCCEEDED(result))
{
width = cJSON_GetObjectItem(camera, "SensorWidth");
result = K4A_RESULT_FROM_BOOL(width != NULL);
}
if (K4A_SUCCEEDED(result))
{
radius = cJSON_GetObjectItem(camera, "MetricRadius");
result = K4A_RESULT_FROM_BOOL(radius != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(height) == true);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(width) == true);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(radius) == true);
}
if (K4A_SUCCEEDED(result))
{
cal->resolution_width = width->valueint;
cal->resolution_height = height->valueint;
cal->metric_radius = (float)radius->valuedouble;
// metric_radius equals 0 means that calibration failed to estimate this parameter
if (cal->metric_radius <= 0.0001f)
{
cal->metric_radius = 1.7f; // default value corresponds to ~120 degree FoV
}
}
return result;
}
static k4a_result_t fill_in_imu_cal_data(k4a_calibration_imu_t *cal, cJSON *inertial_sensor)
{
k4a_result_t result;
cJSON *bias = NULL;
cJSON *bias_uncertainty = NULL;
cJSON *mixing_matrix = NULL;
cJSON *model_type_mask = NULL;
cJSON *noise = NULL;
cJSON *rotation = NULL;
cJSON *second_order_scaling = NULL;
cJSON *temperature_bounds = NULL;
cJSON *temperature = NULL;
bias = cJSON_GetObjectItem(inertial_sensor, "BiasTemperatureModel");
result = K4A_RESULT_FROM_BOOL(bias != NULL);
if (K4A_SUCCEEDED(result))
{
bias_uncertainty = cJSON_GetObjectItem(inertial_sensor, "BiasUncertainty");
result = K4A_RESULT_FROM_BOOL(bias_uncertainty != NULL);
}
if (K4A_SUCCEEDED(result))
{
mixing_matrix = cJSON_GetObjectItem(inertial_sensor, "MixingMatrixTemperatureModel");
result = K4A_RESULT_FROM_BOOL(mixing_matrix != NULL);
}
if (K4A_SUCCEEDED(result))
{
model_type_mask = cJSON_GetObjectItem(inertial_sensor, "ModelTypeMask");
result = K4A_RESULT_FROM_BOOL(model_type_mask != NULL);
}
if (K4A_SUCCEEDED(result))
{
noise = cJSON_GetObjectItem(inertial_sensor, "Noise");
result = K4A_RESULT_FROM_BOOL(noise != NULL);
}
if (K4A_SUCCEEDED(result))
{
rotation = cJSON_GetObjectItem(inertial_sensor, "Rt");
result = K4A_RESULT_FROM_BOOL(rotation != NULL);
}
if (K4A_SUCCEEDED(result))
{
second_order_scaling = cJSON_GetObjectItem(inertial_sensor, "SecondOrderScaling");
result = K4A_RESULT_FROM_BOOL(second_order_scaling != NULL);
}
if (K4A_SUCCEEDED(result))
{
temperature_bounds = cJSON_GetObjectItem(inertial_sensor, "TemperatureBounds");
result = K4A_RESULT_FROM_BOOL(temperature_bounds != NULL);
}
if (K4A_SUCCEEDED(result))
{
temperature = cJSON_GetObjectItem(inertial_sensor, "TemperatureC");
result = K4A_RESULT_FROM_BOOL(temperature != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(bias, cal->bias_temperature_model, COUNTOF(cal->bias_temperature_model));
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(bias_uncertainty, cal->bias_uncertainty, COUNTOF(cal->bias_uncertainty));
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(mixing_matrix,
cal->mixing_matrix_temperature_model,
COUNTOF(cal->mixing_matrix_temperature_model));
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(model_type_mask) == true);
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(noise, cal->noise, COUNTOF(cal->noise));
}
if (K4A_SUCCEEDED(result))
{
result = fill_rotation_matrix(rotation, &cal->depth_to_imu);
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(second_order_scaling,
cal->second_order_scaling,
COUNTOF(cal->second_order_scaling));
}
if (K4A_SUCCEEDED(result))
{
result = fill_array_of_floats(temperature_bounds, cal->temperature_bounds, COUNTOF(cal->temperature_bounds));
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsNumber(temperature) == true);
}
if (K4A_SUCCEEDED(result))
{
cal->temperature_in_c = (float)temperature->valuedouble;
cal->model_type_mask = model_type_mask->valuedouble;
}
return result;
}
static k4a_result_t get_camera_calibration(char *json, k4a_calibration_camera_t *cal, char *location)
{
k4a_result_t result;
bool found = false;
cJSON *main_object = cJSON_Parse(json);
if (main_object == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_ERROR("cJSON_Parse error %s", error_ptr);
}
return K4A_RESULT_FAILED;
}
cJSON *cal_info = NULL;
cal_info = cJSON_GetObjectItem(main_object, "CalibrationInformation");
result = K4A_RESULT_FROM_BOOL(cal_info != NULL);
cJSON *cameras = NULL;
if (K4A_SUCCEEDED(result))
{
cameras = cJSON_GetObjectItem(cal_info, "Cameras");
result = K4A_RESULT_FROM_BOOL(cameras != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsArray(cameras));
}
if (K4A_SUCCEEDED(result))
{
cJSON *camera = NULL;
cJSON_ArrayForEach(camera, cameras)
{
result = K4A_RESULT_FROM_BOOL(camera != NULL);
cJSON *obj_location;
obj_location = cJSON_GetObjectItem(camera, "Location");
result = K4A_RESULT_FROM_BOOL(obj_location != NULL);
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsString(obj_location));
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(obj_location->valuestring != NULL);
}
if (K4A_SUCCEEDED(result))
{
if (strcmp(obj_location->valuestring, location) == 0)
{
result = fill_in_camera_cal_data(cal, camera);
found = true;
break;
}
else
{
continue;
}
}
if (K4A_FAILED(result))
{
break;
}
}
result = K4A_RESULT_FROM_BOOL(found == true);
}
cJSON_Delete(main_object);
return result;
}
static k4a_result_t get_imu_calibration(char *json, k4a_calibration_imu_t *cal, char *sensor_type)
{
k4a_result_t result;
bool found = false;
cJSON *main_object = cJSON_Parse(json);
if (main_object == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_ERROR("cJSON_Parse error %s", error_ptr);
}
return K4A_RESULT_FAILED;
}
cJSON *cal_info = NULL;
cal_info = cJSON_GetObjectItem(main_object, "CalibrationInformation");
result = K4A_RESULT_FROM_BOOL(cal_info != NULL);
cJSON *inertial_sensors = NULL;
if (K4A_SUCCEEDED(result))
{
inertial_sensors = cJSON_GetObjectItem(cal_info, "InertialSensors");
result = K4A_RESULT_FROM_BOOL(inertial_sensors != NULL);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsArray(inertial_sensors));
}
if (K4A_SUCCEEDED(result))
{
cJSON *inertial_sensor = NULL;
cJSON_ArrayForEach(inertial_sensor, inertial_sensors)
{
result = K4A_RESULT_FROM_BOOL(inertial_sensor != NULL);
cJSON *obj_sensor_type;
obj_sensor_type = cJSON_GetObjectItem(inertial_sensor, "SensorType");
result = K4A_RESULT_FROM_BOOL(obj_sensor_type != NULL);
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(cJSON_IsString(obj_sensor_type));
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(obj_sensor_type->valuestring != NULL);
}
if (K4A_SUCCEEDED(result))
{
if (strcmp(obj_sensor_type->valuestring, sensor_type) == 0)
{
result = fill_in_imu_cal_data(cal, inertial_sensor);
found = true;
break;
}
else
{
continue;
}
}
if (K4A_FAILED(result))
{
break;
}
}
result = K4A_RESULT_FROM_BOOL(found == true);
}
// recusively frees children
cJSON_Delete(main_object);
return result;
}
static k4a_result_t read_extrinsic_calibration(calibration_context_t *calibration)
{
size_t json_size;
char *json;
k4a_result_t result;
int tries = 0;
json_size = READ_RETRY_BASE_ALLOCATION;
if (calibration->json_size != 0)
{
json_size = calibration->json_size;
}
do
{
size_t bytes_read = 0;
json = malloc(json_size);
result = K4A_RESULT_FROM_BOOL(json != NULL);
if (K4A_SUCCEEDED(result))
{
result = depthmcu_get_extrinsic_calibration(calibration->depthmcu, json, json_size, &bytes_read);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(bytes_read < json_size);
}
if (K4A_SUCCEEDED(result))
{
json[bytes_read] = '\0'; // NULL terminate the json calibration, which is ASCII text
calibration->json = json;
calibration->json_size = bytes_read + 1; // ++ for NULL
}
else
{
// failed
free(json);
json_size += READ_RETRY_ALLOC_INCREASE;
tries++;
}
} while (K4A_FAILED(result) && tries < MAX_READ_RETRIES);
return result;
}
k4a_result_t calibration_create(depthmcu_t depthmcu, calibration_t *calibration_handle)
{
calibration_context_t *calibration;
k4a_result_t result;
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, depthmcu == NULL);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, calibration_handle == NULL);
calibration = calibration_t_create(calibration_handle);
result = K4A_RESULT_FROM_BOOL(calibration != NULL);
if (K4A_SUCCEEDED(result))
{
calibration->depthmcu = depthmcu;
result = read_extrinsic_calibration(calibration);
}
if (K4A_SUCCEEDED(result))
{
result = calibration_create_from_raw(calibration->json,
calibration->json_size,
&calibration->depth_calibration,
&calibration->color_calibration,
&calibration->gyro_calibration,
&calibration->accel_calibration);
}
if (K4A_FAILED(result) && *calibration_handle != NULL)
{
calibration_destroy(*calibration_handle);
*calibration_handle = NULL;
result = K4A_RESULT_FAILED;
}
return result;
}
k4a_result_t calibration_create_from_raw(char *raw_calibration,
size_t raw_calibration_size,
k4a_calibration_camera_t *depth_calibration,
k4a_calibration_camera_t *color_calibration,
k4a_calibration_imu_t *gyro_calibration,
k4a_calibration_imu_t *accel_calibration)
{
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, raw_calibration == NULL);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, !(strnlen(raw_calibration, raw_calibration_size) <= raw_calibration_size));
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED,
depth_calibration == NULL && color_calibration == NULL && gyro_calibration == NULL &&
accel_calibration == NULL);
k4a_result_t result = K4A_RESULT_SUCCEEDED;
#ifdef _WIN32
int previous_thread_locale = -1;
if (K4A_SUCCEEDED(result))
{
previous_thread_locale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
result = K4A_RESULT_FROM_BOOL(previous_thread_locale == _ENABLE_PER_THREAD_LOCALE ||
previous_thread_locale == _DISABLE_PER_THREAD_LOCALE);
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL(setlocale(LC_ALL, "C") != NULL);
}
#else // NOT _WIN32
locale_t thread_locale = newlocale(LC_ALL_MASK, "C", (locale_t)0);
locale_t previous_locale = uselocale(thread_locale);
#endif
if (K4A_SUCCEEDED(result) && depth_calibration != NULL)
{
result = get_camera_calibration(raw_calibration, depth_calibration, "CALIBRATION_CameraLocationD0");
}
if (K4A_SUCCEEDED(result) && color_calibration != NULL)
{
result = get_camera_calibration(raw_calibration, color_calibration, "CALIBRATION_CameraLocationPV0");
}
if (K4A_SUCCEEDED(result) && gyro_calibration != NULL)
{
result = get_imu_calibration(raw_calibration, gyro_calibration, "CALIBRATION_InertialSensorType_Gyro");
}
if (K4A_SUCCEEDED(result) && accel_calibration != NULL)
{
result = get_imu_calibration(raw_calibration,
accel_calibration,
"CALIBRATION_InertialSensorType_Accelerometer");
}
#ifdef _WIN32
if (previous_thread_locale == _ENABLE_PER_THREAD_LOCALE || previous_thread_locale == _DISABLE_PER_THREAD_LOCALE)
{
if (K4A_FAILED(K4A_RESULT_FROM_BOOL(_configthreadlocale(previous_thread_locale) != -1)))
{
// Only set result to failed, don't let this call succeed and clear a failure that might have happened
// already.
result = K4A_RESULT_FAILED;
}
}
#else // NOT _WIN32
if ((previous_locale != NULL) && (K4A_FAILED(K4A_RESULT_FROM_BOOL(uselocale(previous_locale) != NULL))))
{
// Only set result to failed, don't let this call succeed and clear a failure that might have happened
// already.
result = K4A_RESULT_FAILED;
}
if (thread_locale)
{
freelocale(thread_locale);
}
#endif
return result;
}
void calibration_destroy(calibration_t calibration_handle)
{
calibration_context_t *calibration;
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, calibration_t, calibration_handle);
calibration = calibration_t_get_context(calibration_handle);
if (calibration->json)
{
free(calibration->json);
}
calibration_t_destroy(calibration_handle);
}
k4a_result_t calibration_get_camera(calibration_t calibration_handle,
k4a_calibration_type_t type,
k4a_calibration_camera_t *cal_data)
{
calibration_context_t *calibration;
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, calibration_t, calibration_handle);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, type != K4A_CALIBRATION_TYPE_DEPTH && type != K4A_CALIBRATION_TYPE_COLOR);
calibration = calibration_t_get_context(calibration_handle);
if (type == K4A_CALIBRATION_TYPE_DEPTH)
{
memcpy(cal_data, &calibration->depth_calibration, sizeof(k4a_calibration_camera_t));
}
else // if (type == K4A_CALIBRATION_TYPE_COLOR)
{
assert(type == K4A_CALIBRATION_TYPE_COLOR);
memcpy(cal_data, &calibration->color_calibration, sizeof(k4a_calibration_camera_t));
}
return K4A_RESULT_SUCCEEDED;
}
k4a_result_t calibration_get_imu(calibration_t calibration_handle,
k4a_calibration_type_t type,
k4a_calibration_imu_t *cal_data)
{
calibration_context_t *calibration;
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, calibration_t, calibration_handle);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, type != K4A_CALIBRATION_TYPE_GYRO && type != K4A_CALIBRATION_TYPE_ACCEL);
calibration = calibration_t_get_context(calibration_handle);
if (type == K4A_CALIBRATION_TYPE_GYRO)
{
memcpy(cal_data, &calibration->gyro_calibration, sizeof(k4a_calibration_imu_t));
}
else // if (type == K4A_CALIBRATION_TYPE_ACCEL)
{
assert(type == K4A_CALIBRATION_TYPE_ACCEL);
memcpy(cal_data, &calibration->accel_calibration, sizeof(k4a_calibration_imu_t));
}
return K4A_RESULT_SUCCEEDED;
}
k4a_buffer_result_t calibration_get_raw_data(calibration_t calibration_handle, uint8_t *data, size_t *data_size)
{
calibration_context_t *calibration;
k4a_buffer_result_t bresult = K4A_BUFFER_RESULT_FAILED;
RETURN_VALUE_IF_HANDLE_INVALID(K4A_BUFFER_RESULT_FAILED, calibration_t, calibration_handle);
RETURN_VALUE_IF_ARG(K4A_BUFFER_RESULT_FAILED, data_size == NULL);
calibration = calibration_t_get_context(calibration_handle);
if (data == NULL)
{
(*data_size) = calibration->json_size;
bresult = K4A_BUFFER_RESULT_TOO_SMALL;
}
else
{
k4a_result_t result;
result = K4A_RESULT_FROM_BOOL(*data_size >= calibration->json_size);
if (K4A_SUCCEEDED(result))
{
memcpy(data, calibration->json, calibration->json_size);
*data_size = calibration->json_size;
bresult = K4A_BUFFER_RESULT_SUCCEEDED;
}
else
{
*data_size = calibration->json_size;
bresult = K4A_BUFFER_RESULT_TOO_SMALL;
}
}
return bresult;
}