aboutsummaryrefslogtreecommitdiff
path: root/xcore/xcam_utils.cpp
blob: 246be930bf1bc85b4e1aef83f2a80422d7c93586 (plain)
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
/*
 * xcam_utils.h - xcam utilities
 *
 *  Copyright (c) 2014-2015 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: Wind Yuan <feng.yuan@intel.com>
 * Author: Zong Wei <wei.zong@intel.com>
 * Author: Junkai Wu <junkai.wu@intel.com>
 * Author: Yinhang Liu <yinhangx.liu@intel.com>
 */

#include "xcam_utils.h"
#include "video_buffer.h"
#include "image_file_handle.h"

namespace XCam {

static float
transform_bowl_coord_to_image_x (
    const float bowl_x, const float bowl_y,
    const uint32_t img_width)
{
    float offset_radian = (bowl_x < 0.0f) ? XCAM_PI : ((bowl_y >= 0.0f) ? 2.0f * XCAM_PI : 0.0f);
    float arctan_radian = (bowl_x != 0.0f) ? atan (-bowl_y / bowl_x) : ((bowl_y >= 0.0f) ? -XCAM_PI / 2.0f : XCAM_PI / 2.0f);

    float img_x = arctan_radian + offset_radian;
    img_x *= img_width / (2.0f * XCAM_PI);
    return XCAM_CLAMP (img_x, 0.0f, img_width - 1.0f);
}

static float
transform_bowl_coord_to_image_y (
    const BowlDataConfig &config,
    const float bowl_x, const float bowl_y, const float bowl_z,
    const uint32_t img_height)
{
    float wall_image_height = config.wall_height / (config.wall_height + config.ground_length) * img_height;
    float ground_image_height = img_height - wall_image_height;
    float img_y = 0.0f;

    if (bowl_z > 0.0f) {
        img_y = (config.wall_height - bowl_z) * wall_image_height / config.wall_height;
        img_y = XCAM_CLAMP (img_y, 0.0f, wall_image_height - 1.0f);
    } else {
        float max_semimajor = config.b *
                              sqrt (1 - config.center_z * config.center_z / (config.c * config.c));
        float min_semimajor = max_semimajor - config.ground_length;
        XCAM_ASSERT (min_semimajor >= 0);
        XCAM_ASSERT (max_semimajor > min_semimajor);
        float step = ground_image_height / (max_semimajor - min_semimajor);

        float axis_ratio = config.a / config.b;
        float cur_semimajor = sqrt (bowl_x * bowl_x + bowl_y * bowl_y * axis_ratio * axis_ratio) / axis_ratio;
        cur_semimajor = XCAM_CLAMP (cur_semimajor, min_semimajor, max_semimajor);

        img_y = (max_semimajor - cur_semimajor) * step + wall_image_height;
        img_y = XCAM_CLAMP (img_y, wall_image_height, img_height - 1.0f);
    }
    return img_y;
}

PointFloat2 bowl_view_coords_to_image (
    const BowlDataConfig &config,
    const PointFloat3 &bowl_pos,
    const uint32_t img_width, const uint32_t img_height)
{
    PointFloat2 img_pos;
    img_pos.x = transform_bowl_coord_to_image_x (bowl_pos.x, bowl_pos.y, img_width);
    img_pos.y = transform_bowl_coord_to_image_y (config, bowl_pos.x, bowl_pos.y, bowl_pos.z, img_height);

    return img_pos;
}

PointFloat3 bowl_view_image_to_world (
    const BowlDataConfig &config,
    const uint32_t img_width, const uint32_t img_height,
    const PointFloat2 &img_pos)
{
    PointFloat3 world;
    float angle;

    float a = config.a;
    float b = config.b;
    float c = config.c;

    float wall_image_height = config.wall_height / (config.wall_height + config.ground_length) * img_height;
    float ground_image_height = img_height - wall_image_height;

    float z_step = config.wall_height / wall_image_height;
    float angle_step = fabs(config.angle_end - config.angle_start) / img_width;

    if(img_pos.y < wall_image_height) {
        world.z = config.wall_height - img_pos.y * z_step; // TODO world.z
        angle = degree2radian (config.angle_start + img_pos.x * angle_step);
        float r2 = 1 - (world.z - config.center_z) * (world.z - config.center_z) / (c * c);

        if(XCAM_DOUBLE_EQUAL_AROUND (angle, XCAM_PI / 2)) {
            world.x = 0.0f;
            world.y = -sqrt(r2 * b * b);
        } else if (XCAM_DOUBLE_EQUAL_AROUND (angle, XCAM_PI * 3 / 2)) {
            world.x = 0.0f;
            world.y = sqrt(r2 * b * b);
        } else if((angle < XCAM_PI / 2) || (angle > XCAM_PI * 3 / 2)) {
            world.x = sqrt(r2 * a * a * b * b / (b * b + a * a * tan(angle) * tan(angle)));
            world.y = -world.x * tan(angle);
        } else {
            world.x = -sqrt(r2 * a * a * b * b / (b * b + a * a * tan(angle) * tan(angle)));
            world.y = -world.x * tan(angle);
        }
    } else {
        a = a * sqrt(1 - config.center_z * config.center_z / (c * c));
        b = b * sqrt(1 - config.center_z * config.center_z / (c * c));

        float ratio_ab = b / a;
        float step_b = config.ground_length / ground_image_height;

        b = b - (img_pos.y - wall_image_height) * step_b;
        a = b / ratio_ab;

        angle = degree2radian (config.angle_start + img_pos.x * angle_step);

        if(XCAM_DOUBLE_EQUAL_AROUND (angle, XCAM_PI / 2)) {
            world.x = 0.0f;
            world.y = -b;
        } else if (XCAM_DOUBLE_EQUAL_AROUND (angle, XCAM_PI * 3 / 2)) {
            world.x = 0.0f;
            world.y = b;
        } else if((angle < XCAM_PI / 2) || (angle > XCAM_PI * 3 / 2)) {
            world.x = a * b / sqrt(b * b + a * a * tan(angle) * tan(angle));
            world.y = -world.x * tan(angle);
        } else {
            world.x = -a * b / sqrt(b * b + a * a * tan(angle) * tan(angle));
            world.y = -world.x * tan(angle);
        }
        world.z = 0.0f;
    }

    return world;
}

void centralize_bowl_coord_from_cameras (
    ExtrinsicParameter &front_cam, ExtrinsicParameter &right_cam,
    ExtrinsicParameter &rear_cam, ExtrinsicParameter &left_cam,
    PointFloat3 &bowl_coord_offset)
{
    bowl_coord_offset.x = (front_cam.trans_x + rear_cam.trans_x) / 2.0f;
    bowl_coord_offset.y = (right_cam.trans_y + left_cam.trans_y) / 2.0f;
    bowl_coord_offset.z = 0.0f;

    front_cam.trans_x -= bowl_coord_offset.x;
    front_cam.trans_y -= bowl_coord_offset.y;

    right_cam.trans_x -= bowl_coord_offset.x;
    right_cam.trans_y -= bowl_coord_offset.y;

    rear_cam.trans_x -= bowl_coord_offset.x;
    rear_cam.trans_y -= bowl_coord_offset.y;

    left_cam.trans_x -= bowl_coord_offset.x;
    left_cam.trans_y -= bowl_coord_offset.y;
}

double
linear_interpolate_p2 (
    double value_start, double value_end,
    double ref_start, double ref_end,
    double ref_curr)
{
    double weight_start = 0;
    double weight_end = 0;
    double dist_start = 0;
    double dist_end = 0;
    double dist_sum = 0;
    double value = 0;

    dist_start = abs(ref_curr - ref_start);
    dist_end = abs(ref_end - ref_curr);
    dist_sum = dist_start + dist_end;

    if (dist_start == 0) {
        weight_start = 10000000.0;
    } else {
        weight_start = (dist_sum / dist_start);
    }

    if (dist_end == 0) {
        weight_end = 10000000.0;
    } else {
        weight_end = (dist_sum / dist_end);
    }

    value = (value_start * weight_start + value_end * weight_end) / (weight_start + weight_end);
    return value;
}

double
linear_interpolate_p4(
    double value_lt, double value_rt,
    double value_lb, double value_rb,
    double ref_lt_x, double ref_rt_x,
    double ref_lb_x, double ref_rb_x,
    double ref_lt_y, double ref_rt_y,
    double ref_lb_y, double ref_rb_y,
    double ref_curr_x, double ref_curr_y)
{
    double weight_lt = 0;
    double weight_rt = 0;
    double weight_lb = 0;
    double weight_rb = 0;
    double dist_lt = 0;
    double dist_rt = 0;
    double dist_lb = 0;
    double dist_rb = 0;
    double dist_sum = 0;
    double value = 0;

    dist_lt = abs(ref_curr_x - ref_lt_x) + abs(ref_curr_y - ref_lt_y);
    dist_rt = abs(ref_curr_x - ref_rt_x) + abs(ref_curr_y - ref_rt_y);
    dist_lb = abs(ref_curr_x - ref_lb_x) + abs(ref_curr_y - ref_lb_y);
    dist_rb = abs(ref_curr_x - ref_rb_x) + abs(ref_curr_y - ref_rb_y);
    dist_sum = dist_lt + dist_rt + dist_lb + dist_rb;

    if (dist_lt == 0) {
        weight_lt = 10000000.0;
    } else {
        weight_lt = (dist_sum / dist_lt);
    }
    if (dist_rt == 0) {
        weight_rt = 10000000.0;
    } else {
        weight_rt = (dist_sum / dist_rt);
    }
    if (dist_lb == 0) {
        weight_lb = 10000000.0;
    } else {
        weight_lb = (dist_sum / dist_lb);
    }
    if (dist_rb == 0) {
        weight_rb = 10000000.0;
    } else {
        weight_rb = (dist_sum / dist_rt);
    }

    value = floor ((value_lt * weight_lt + value_rt * weight_rt + value_lb * weight_lb + value_rb * weight_rb) /
                   (weight_lt + weight_rt + weight_lb + weight_rb) + 0.5);
    return value;
}

void
get_gauss_table (uint32_t radius, float sigma, std::vector<float> &table, bool normalize)
{
    uint32_t i;
    uint32_t scale = radius * 2 + 1;
    float dis = 0.0f, sum = 1.0f;

    //XCAM_ASSERT (scale < 512);
    table.resize (scale);
    table[radius] = 1.0f;

    for (i = 0; i < radius; i++)  {
        dis = (i - radius) * (i - radius);
        table[i] = table[scale - i - 1] = exp(-dis / (2.0f * sigma * sigma));
        sum += table[i] * 2.0f;
    }

    if (!normalize)
        return;

    for(i = 0; i < scale; i++)
        table[i] /= sum;
}

void
dump_buf_perfix_path (const SmartPtr<VideoBuffer> buf, const char *prefix_name)
{
    char file_name[256];
    XCAM_ASSERT (prefix_name);
    XCAM_ASSERT (buf.ptr ());

    const VideoBufferInfo &info = buf->get_video_info ();
    snprintf (
        file_name, 256, "%s-%dx%d.%s",
        prefix_name, info.width, info.height, xcam_fourcc_to_string (info.format));

    dump_video_buf (buf, file_name);
}

bool
dump_video_buf (const SmartPtr<VideoBuffer> buf, const char *file_name)
{
    ImageFileHandle file;
    XCAM_ASSERT (file_name);

    XCamReturn ret = file.open (file_name, "wb");
    XCAM_FAIL_RETURN (
        ERROR, xcam_ret_is_ok (ret), false,
        "dump buffer failed when open file: %s", file_name);

    ret = file.write_buf (buf);
    XCAM_FAIL_RETURN (
        ERROR, xcam_ret_is_ok (ret), false,
        "dump buffer to file: %s failed", file_name);

    return true;
}

}