The open source OpenXR runtime
1// Copyright 2023-2024, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Testing UV to tangent values.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 */
8
9#include "catch_amalgamated.hpp"
10
11#include "math/m_mathinclude.h"
12#include "render/render_interface.h"
13
14
15#define QUARTER_PI (M_PI / 4)
16#define SIXTH_PI (M_PI / 6)
17#define MARGIN (0.000001)
18
19static inline xrt_normalized_rect
20rect(float x, float y, float w, float h)
21{
22 xrt_normalized_rect rect = {x, y, w, h};
23 return rect;
24}
25
26static inline void
27check(xrt_normalized_rect &result, xrt_normalized_rect &&truth)
28{
29 REQUIRE_THAT(result.x, Catch::Matchers::WithinAbs(truth.x, MARGIN));
30 REQUIRE_THAT(result.y, Catch::Matchers::WithinAbs(truth.y, MARGIN));
31 REQUIRE_THAT(result.w, Catch::Matchers::WithinAbs(truth.w, MARGIN));
32 REQUIRE_THAT(result.h, Catch::Matchers::WithinAbs(truth.h, MARGIN));
33}
34
35#define CAPUTER_FOV(FOV) CAPTURE(FOV.angle_up, FOV.angle_down, FOV.angle_left, FOV.angle_right);
36
37
38TEST_CASE("render_calc_uv_to_tangent_lengths_rect")
39{
40 // check assumptions
41 REQUIRE_THAT(tan(QUARTER_PI), Catch::Matchers::WithinAbs(1.0, MARGIN));
42
43 SECTION("45_degrees")
44 {
45 struct xrt_fov f45 = XRT_STRUCT_INIT;
46 f45.angle_down = -QUARTER_PI;
47 f45.angle_up = QUARTER_PI;
48 f45.angle_left = -QUARTER_PI;
49 f45.angle_right = QUARTER_PI;
50
51 SECTION("normal")
52 {
53 CAPTURE(f45.angle_up, f45.angle_down, f45.angle_left, f45.angle_right);
54 struct xrt_normalized_rect result;
55 render_calc_uv_to_tangent_lengths_rect(&f45, &result);
56
57 /*
58 * We expect a fov of 45 degrees in all angles to have tangets
59 * of 1 or -1. In order to transform uv [0 .. 1] to [-1 .. 1]
60 * we need to have a width of 2 and a offset of -1.
61 */
62 check(result, rect(-1, -1, 2, 2));
63 }
64
65 SECTION("flipped_vertical")
66 {
67 f45.angle_down = -f45.angle_down;
68 f45.angle_up = -f45.angle_up;
69
70 CAPUTER_FOV(f45);
71 struct xrt_normalized_rect result;
72 render_calc_uv_to_tangent_lengths_rect(&f45, &result);
73
74 // We expect the same values as normal but with y and h negated.
75 check(result, rect(-1, 1, 2, -2));
76 }
77
78 SECTION("flipped_horizontal")
79 {
80 f45.angle_left = -f45.angle_left;
81 f45.angle_right = -f45.angle_right;
82
83 CAPUTER_FOV(f45);
84 struct xrt_normalized_rect result;
85 render_calc_uv_to_tangent_lengths_rect(&f45, &result);
86
87 // We expect the same values as normal but with x and w negated.
88 check(result, rect(1, -1, -2, 2));
89 }
90 }
91
92
93 SECTION("30_degrees")
94 {
95 struct xrt_fov f30 = XRT_STRUCT_INIT;
96 f30.angle_down = -SIXTH_PI;
97 f30.angle_up = SIXTH_PI;
98 f30.angle_left = -SIXTH_PI;
99 f30.angle_right = SIXTH_PI;
100
101 CAPUTER_FOV(f30);
102 struct xrt_normalized_rect result;
103 render_calc_uv_to_tangent_lengths_rect(&f30, &result);
104
105 float t = tan(SIXTH_PI);
106 float t2 = t * 2;
107
108 // We expect the offset to be -tan(PI/6) and the lengths to be tan(PI/6) * 2.
109 check(result, rect(-t, -t, t2, t2));
110 }
111}