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
//! Transformation utilities.
//!
//! Some space definitions to keep in mind:
//!
//! Texture coordinates:
//! * origin top left
//! * full texture ([left; right], [top; bottom]):
//!     ([0; 1], [0; 1])
//!
//! NDC:
//! * origin center
//! * full screen ([left; right], [top; bottom]):
//!     ([-1; 1], [1; -1])
//!
//! Pixel coordinates:
//! * origin top left
//! * full screen ([left; right], [top; bottom]):
//!      ([0; `screen_extent.x`], [0; `screen_extent.y`])

use crate::rect::RectF32;

/// Transforms texture coordinates to normalized device coordinates (NDC).
#[inline]
pub fn ndc_from_texcoord(texcoord: glam::Vec2) -> glam::Vec2 {
    glam::vec2(texcoord.x * 2.0 - 1.0, 1.0 - texcoord.y * 2.0)
}

/// Transforms texture coordinates to normalized device coordinates (NDC).
#[inline]
pub fn ndc_from_pixel(pixel_coord: glam::Vec2, screen_extent: glam::UVec2) -> glam::Vec2 {
    glam::vec2(
        pixel_coord.x / screen_extent.x as f32 * 2.0 - 1.0,
        1.0 - pixel_coord.y / screen_extent.y as f32 * 2.0,
    )
}

/// Defines a transformation from a rectangular region of interest into a rectangular target region.
///
/// This is "pan and scan".
///
/// Transforms the range of `region_of_interest` to the range of `region`.
#[derive(Clone, Debug)]
pub struct RectTransform {
    /// The region of the scene that should be visible.
    pub region_of_interest: RectF32,

    /// The full scene.
    pub region: RectF32,
}

impl RectTransform {
    /// No-op rect transform that transforms from a unit rectangle to a unit rectangle.
    pub const IDENTITY: Self = Self {
        region_of_interest: RectF32::UNIT,
        region: RectF32::UNIT,
    };

    /// Computes a transformation matrix that applies the rect transform to the NDC space.
    ///
    /// This matrix is expected to be the left most transformation in the vertex transformation chain.
    /// It causes the area described by `region_of_interest` to be mapped to the area described by `region`.
    /// Meaning, that `region` represents the full screen of the NDC space.
    ///
    /// This means that only the relation of the rectangles in `RectTransform` is important.
    /// Scaling or moving both rectangles by the same amount does not change the result.
    pub fn to_ndc_scale_and_translation(&self) -> glam::Mat4 {
        // It's easier to think in texcoord space, and then transform to NDC.
        // This texcoord rect specifies the portion of the screen that should become the entire range of the NDC screen.
        let texcoord_rect = RectF32 {
            min: (self.region_of_interest.min - self.region.min) / self.region.extent,
            extent: self.region_of_interest.extent / self.region.extent,
        };
        let texcoord_rect_min = texcoord_rect.min;
        let texcoord_rect_max = texcoord_rect.max();

        // y axis is flipped in NDC, therefore we need to flip the y axis of the rect.
        let rect_min_ndc = ndc_from_texcoord(glam::vec2(texcoord_rect_min.x, texcoord_rect_max.y));
        let rect_max_ndc = ndc_from_texcoord(glam::vec2(texcoord_rect_max.x, texcoord_rect_min.y));

        let scale = 2.0 / (rect_max_ndc - rect_min_ndc);
        let translation = -0.5 * (rect_min_ndc + rect_max_ndc);

        glam::Mat4::from_scale(scale.extend(1.0))
            * glam::Mat4::from_translation(translation.extend(0.0))
    }

    pub fn scale(&self) -> glam::Vec2 {
        self.region_of_interest.extent / self.region.extent
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn to_ndc_scale_and_translation() {
        let region = RectF32 {
            min: glam::vec2(1.0, 1.0),
            extent: glam::vec2(2.0, 3.0),
        };

        // Identity
        {
            let rect_transform = RectTransform {
                region_of_interest: region,
                region,
            };
            let identity = rect_transform.to_ndc_scale_and_translation();
            assert_eq!(identity, glam::Mat4::IDENTITY);
        }

        // Scale
        {
            let scale_factor = glam::vec2(2.0, 0.25);

            let rect_transform = RectTransform {
                region_of_interest: RectF32 {
                    // Move the roi to the middle of the region.
                    min: region.center() - region.extent * scale_factor * 0.5,
                    extent: region.extent * scale_factor,
                },
                region,
            };
            let scale = rect_transform.to_ndc_scale_and_translation();
            assert_eq!(
                scale,
                glam::Mat4::from_scale(1.0 / scale_factor.extend(1.0))
            );
            assert_eq!(rect_transform.scale(), scale_factor);
        }

        // Translation
        {
            let translation_vec = glam::vec2(1.0, 2.0);

            let rect_transform = RectTransform {
                region_of_interest: RectF32 {
                    min: region.min + translation_vec * region.extent,
                    extent: region.extent,
                },
                region,
            };
            let translation = rect_transform.to_ndc_scale_and_translation();
            assert_eq!(
                translation,
                glam::Mat4::from_translation(
                    glam::vec3(-translation_vec.x, translation_vec.y, 0.0) * 2.0
                )
            );
            assert_eq!(rect_transform.scale(), glam::Vec2::ONE);
        }

        // Scale + translation
        {
            let scale_factor = glam::vec2(2.0, 0.25);
            let translation_vec = glam::vec2(1.0, 2.0);

            let rect_transform = RectTransform {
                region_of_interest: RectF32 {
                    // Move the roi to the middle of the region and then apply translation
                    min: region.center() - region.extent * scale_factor * 0.5
                        + translation_vec * region.extent,
                    extent: region.extent * scale_factor,
                },
                region,
            };
            let scale_and_translation = rect_transform.to_ndc_scale_and_translation();
            assert_eq!(
                scale_and_translation,
                glam::Mat4::from_scale(1.0 / scale_factor.extend(1.0))
                    * glam::Mat4::from_translation(
                        glam::vec3(-translation_vec.x, translation_vec.y, 0.0) * 2.0
                    )
            );
            assert_eq!(rect_transform.scale(), scale_factor);
        }
    }
}