huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
/*
 * image_projector.cpp - Calculate 2D image projective matrix
 *
 *  Copyright (c) 2017 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Zong Wei <wei.zong@intel.com>
 */
 
#include "image_projector.h"
 
namespace XCam {
 
ImageProjector::ImageProjector (CalibrationParams &params)
    : _calib_params (params)
{
    set_camera_intrinsics(
        params.focal_x,
        params.focal_y,
        params.offset_x,
        params.offset_y,
        params.skew);
}
 
ImageProjector::ImageProjector (
    double focal_x,
    double focal_y,
    double offset_x,
    double offset_y,
    double skew)
{
    set_camera_intrinsics(
        focal_x,
        focal_y,
        offset_x,
        offset_y,
        skew);
}
 
Quaternd
ImageProjector::interp_orientation (
    int64_t frame_ts,
    const std::vector<Vec4d> &orientation,
    const std::vector<int64_t> &orient_ts,
    int& index)
{
    if (orientation.empty () || orient_ts.empty ()) {
        return Quaternd ();
    }
 
    int count = orient_ts.size ();
    if (count == 1) {
        return Quaternd(orientation[0]);
    }
 
    int i = index;
    XCAM_ASSERT(0 <= i && i < count);
 
    while (i >= 0 && orient_ts[i] > frame_ts) {
        i--;
    }
    if (i < 0) return Quaternd (orientation[0]);
 
    while (i + 1 < count && orient_ts[i + 1] < frame_ts) {
        i++;
    }
    if (i >= count) return Quaternd (orientation[count - 1]);
 
    index = i;
 
    double weight_start = (orient_ts[i + 1] - frame_ts) / (orient_ts[i + 1] - orient_ts[i]);
    double weight_end = 1.0f - weight_start;
    XCAM_ASSERT (weight_start >= 0 && weight_start <= 1.0);
    XCAM_ASSERT (weight_end >= 0 && weight_end <= 1.0);
 
    return Quaternd (orientation[i] * weight_start + orientation[i + 1] * weight_end);
    //return Quaternd (quat[i]).slerp(weight_start, Quaternd (quat[i + 1]));
}
 
// rotate coordinate system keeps the handedness of original coordinate system unchanged
//
// axis_to_x: defines the axis of the new cooridinate system that
//    coincide with the X axis of the original coordinate system.
// axis_to_y: defines the axis of the new cooridinate system that
//    coincide with the Y axis of the original coordinate system.
//
Mat3d
ImageProjector::rotate_coordinate_system (
    CoordinateAxisType axis_to_x,
    CoordinateAxisType axis_to_y)
{
    Mat3d t_mat;
    if (axis_to_x == AXIS_X && axis_to_y == AXIS_MINUS_Z) {
        t_mat = Mat3d (Vec3d (1, 0, 0),
                       Vec3d (0, 0, -1),
                       Vec3d (0, 1, 0));
    } else if (axis_to_x == AXIS_X && axis_to_y == AXIS_MINUS_Y) {
        t_mat = Mat3d (Vec3d (1, 0, 0),
                       Vec3d (0, -1, 0),
                       Vec3d (0, 0, -1));
    } else if (axis_to_x == AXIS_X && axis_to_y == AXIS_Z) {
        t_mat = Mat3d (Vec3d (1, 0, 0),
                       Vec3d (0, 0, 1),
                       Vec3d (0, -1, 0));
    } else if (axis_to_x == AXIS_MINUS_Z && axis_to_y == AXIS_Y) {
        t_mat = Mat3d (Vec3d (0, 0, -1),
                       Vec3d (0, 1, 0),
                       Vec3d (1, 0, 0));
    } else if (axis_to_x == AXIS_MINUS_X && axis_to_y == AXIS_Y) {
        t_mat = Mat3d (Vec3d (-1, 0, 0),
                       Vec3d (0, 1, 0),
                       Vec3d (0, 0, -1));
    } else if (axis_to_x == AXIS_Z && axis_to_y == AXIS_Y) {
        t_mat = Mat3d (Vec3d (0, 0, 1),
                       Vec3d (0, 1, 0),
                       Vec3d (-1, 0, 0));
    } else if (axis_to_x == AXIS_MINUS_Y && axis_to_y == AXIS_X) {
        t_mat = Mat3d (Vec3d (0, -1, 0),
                       Vec3d (1, 0, 0),
                       Vec3d (0, 0, 1));
    } else if (axis_to_x == AXIS_MINUS_X && axis_to_y == AXIS_MINUS_Y) {
        t_mat = Mat3d (Vec3d (-1, 0, 0),
                       Vec3d (0, -1, 0),
                       Vec3d (0, 0, 1));
    } else if (axis_to_x == AXIS_Y && axis_to_y == AXIS_MINUS_X) {
        t_mat = Mat3d (Vec3d (0, 1, 0),
                       Vec3d (-1, 0, 0),
                       Vec3d (0, 0, 1));
    } else  {
        t_mat = Mat3d ();
    }
    return t_mat;
}
 
// mirror coordinate system will change the handedness of original coordinate system
//
// axis_mirror: defines the axis that coordinate system mirror on
//
Mat3d
ImageProjector::mirror_coordinate_system (CoordinateAxisType axis_mirror)
{
    Mat3d t_mat;
 
    switch (axis_mirror) {
    case AXIS_X:
    case AXIS_MINUS_X:
        t_mat = Mat3d (Vec3d (-1, 0, 0),
                       Vec3d (0, 1, 0),
                       Vec3d (0, 0, 1));
        break;
    case AXIS_Y:
    case AXIS_MINUS_Y:
        t_mat = Mat3d (Vec3d (1, 0, 0),
                       Vec3d (0, -1, 0),
                       Vec3d (0, 0, 1));
        break;
    case AXIS_Z:
    case AXIS_MINUS_Z:
        t_mat = Mat3d (Vec3d (1, 0, 0),
                       Vec3d (0, 1, 0),
                       Vec3d (0, 0, -1));
        break;
    default:
        t_mat = Mat3d ();
        break;
    }
 
    return t_mat;
}
 
// transform coordinate system will change the handedness of original coordinate system
//
// axis_to_x: defines the axis of the new cooridinate system that
//    coincide with the X axis of the original coordinate system.
// axis_to_y: defines the axis of the new cooridinate system that
//    coincide with the Y axis of the original coordinate system.
// axis_mirror: defines the axis that coordinate system mirror on
Mat3d
ImageProjector::transform_coordinate_system (CoordinateSystemConv &transform)
{
    return mirror_coordinate_system (transform.axis_mirror) *
           rotate_coordinate_system (transform.axis_to_x, transform.axis_to_y);
}
 
Mat3d
ImageProjector::align_coordinate_system (
    CoordinateSystemConv &world_to_device,
    Mat3d &extrinsics,
    CoordinateSystemConv &device_to_image)
{
    return transform_coordinate_system (world_to_device)
           * extrinsics
           * transform_coordinate_system (device_to_image);
}
 
XCamReturn
ImageProjector::set_sensor_calibration (CalibrationParams &params)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
 
    _calib_params = params;
    set_camera_intrinsics (
        params.focal_x,
        params.focal_y,
        params.offset_x,
        params.offset_y,
        params.skew);
 
    return ret;
}
 
XCamReturn
ImageProjector::set_camera_intrinsics (
    double focal_x,
    double focal_y,
    double offset_x,
    double offset_y,
    double skew)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
 
    _intrinsics = Mat3d (Vec3d (focal_x, skew, offset_x),
                         Vec3d (0, focal_y, offset_y),
                         Vec3d (0, 0, 1));
 
    XCAM_LOG_DEBUG("Intrinsic Matrix(3x3) \n");
    XCAM_LOG_DEBUG("intrinsic = [ %lf, %lf, %lf ; %lf, %lf, %lf ; %lf, %lf, %lf ] \n",
                   _intrinsics(0, 0), _intrinsics(0, 1), _intrinsics(0, 2),
                   _intrinsics(1, 0), _intrinsics(1, 1), _intrinsics(1, 2),
                   _intrinsics(2, 0), _intrinsics(2, 1), _intrinsics(2, 2));
    return ret;
}
 
Mat3d
ImageProjector::calc_camera_extrinsics (
    const int64_t frame_ts,
    const std::vector<int64_t> &pose_ts,
    const std::vector<Vec4d> &orientation,
    const std::vector<Vec3d> &translation)
{
    if (pose_ts.empty () || orientation.empty () || translation.empty ()) {
        return Mat3d ();
    }
 
    int index = 0;
    const double ts = frame_ts + _calib_params.gyro_delay;
    Quaternd quat = interp_orientation (ts, orientation, pose_ts, index) +
                    Quaternd (_calib_params.gyro_drift);
 
    Mat3d extrinsics = quat.rotation_matrix ();
 
    XCAM_LOG_DEBUG("Extrinsic Matrix(3x3) \n");
    XCAM_LOG_DEBUG("extrinsic = [ %lf, %lf, %lf; %lf, %lf, %lf; %lf, %lf, %lf ] \n",
                   extrinsics(0, 0), extrinsics(0, 1), extrinsics(0, 2),
                   extrinsics(1, 0), extrinsics(1, 1), extrinsics(1, 2),
                   extrinsics(2, 0), extrinsics(2, 1), extrinsics(2, 2));
 
    return extrinsics;
}
 
Mat3d
ImageProjector::calc_camera_extrinsics (
    const int64_t frame_ts,
    DevicePoseList &pose_list)
{
    if (pose_list.empty ()) {
        return Mat3d ();
    }
 
    int index = 0;
 
    std::vector<Vec4d> orientation;
    std::vector<int64_t> orient_ts;
    std::vector<Vec3d> translation;
 
    for (DevicePoseList::iterator iter = pose_list.begin (); iter != pose_list.end (); ++iter)
    {
        SmartPtr<DevicePose> pose = *iter;
 
        orientation.push_back (Vec4d (pose->orientation[0],
                                      pose->orientation[1],
                                      pose->orientation[2],
                                      pose->orientation[3]));
 
        orient_ts.push_back (pose->timestamp);
 
        translation.push_back (Vec3d (pose->translation[0],
                                      pose->translation[1],
                                      pose->translation[2]));
 
    }
 
    const int64_t ts = frame_ts + _calib_params.gyro_delay;
    Quaternd quat = interp_orientation (ts, orientation, orient_ts, index) +
                    Quaternd (_calib_params.gyro_drift);
 
    Mat3d extrinsics = quat.rotation_matrix ();
 
    XCAM_LOG_DEBUG("Extrinsic Matrix(3x3) \n");
    XCAM_LOG_DEBUG("extrinsic = [ %lf, %lf, %lf; %lf, %lf, %lf; %lf, %lf, %lf ] \n",
                   extrinsics(0, 0), extrinsics(0, 1), extrinsics(0, 2),
                   extrinsics(1, 0), extrinsics(1, 1), extrinsics(1, 2),
                   extrinsics(2, 0), extrinsics(2, 1), extrinsics(2, 2));
 
    return extrinsics;
}
 
Mat3d
ImageProjector::calc_projective (
    Mat3d &extrinsic0,
    Mat3d &extrinsic1)
{
    Mat3d intrinsic = get_camera_intrinsics ();
 
    return intrinsic * extrinsic0 * extrinsic1.transpose () * intrinsic.inverse ();
}
 
}