CARLA
Loading...
Searching...
No Matches
test_camera_intrinsics.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
10
11#include <cmath>
12
13namespace {
14constexpr double kEps = 1e-6;
15} // namespace
16
17TEST(CameraIntrinsics, hd_90deg_fov_focal_length) {
18 // 1920x1080 with 90deg horizontal FOV: fx = 1920 / (2 * tan(45deg)) = 960.
19 const auto k = carla::ros2::ComputeIntrinsics(1920u, 1080u, 90.0f);
20 EXPECT_NEAR(k.fx, 960.0, kEps);
21 EXPECT_NEAR(k.fy, 960.0, kEps);
22 EXPECT_NEAR(k.cx, 960.0, kEps);
23 EXPECT_NEAR(k.cy, 540.0, kEps);
24}
25
26TEST(CameraIntrinsics, square_60deg_fov) {
27 // 800x800 with 60deg FOV: fx = 800 / (2 * tan(30deg)) ~ 692.82.
28 const auto k = carla::ros2::ComputeIntrinsics(800u, 800u, 60.0f);
29 const double expected_fx = 800.0 / (2.0 * std::tan(M_PI / 6.0));
30 EXPECT_NEAR(k.fx, expected_fx, kEps);
31 EXPECT_NEAR(k.fy, k.fx, kEps);
32 EXPECT_NEAR(k.cx, 400.0, kEps);
33 EXPECT_NEAR(k.cy, 400.0, kEps);
34}
35
36TEST(CameraIntrinsics, regression_guard_against_buggy_precedence) {
37 // The pre-86ebadcee spelling computed fx = width / (2 * tan(fov) * M_PI / 360),
38 // applying the degree-to-radian conversion to tan(fov) instead of to fov.
39 // For fov=90 that returned ~0.06; the corrected form returns 960. Guard against
40 // a regression to the buggy spelling.
41 const auto k = carla::ros2::ComputeIntrinsics(1920u, 1080u, 90.0f);
42 ASSERT_GT(k.fx, 100.0);
43}
44
45TEST(CameraIntrinsics, narrow_fov) {
46 // 1024x768 with 30deg horizontal FOV.
47 const auto k = carla::ros2::ComputeIntrinsics(1024u, 768u, 30.0f);
48 const double expected_fx = 1024.0 / (2.0 * std::tan(M_PI / 12.0));
49 EXPECT_NEAR(k.fx, expected_fx, kEps);
50 EXPECT_NEAR(k.cy, 384.0, kEps);
51}
52
53TEST(CameraIntrinsics, principal_point_always_at_image_centre) {
54 for (float fov : {30.0f, 60.0f, 90.0f, 120.0f, 150.0f}) {
55 const auto k = carla::ros2::ComputeIntrinsics(640u, 480u, fov);
56 EXPECT_NEAR(k.cx, 320.0, kEps);
57 EXPECT_NEAR(k.cy, 240.0, kEps);
58 }
59}
CameraIntrinsics ComputeIntrinsics(uint32_t width, uint32_t height, float fov_degrees) noexcept
TEST(CameraIntrinsics, hd_90deg_fov_focal_length)