HighMap library (C++)
Loading...
Searching...
No Matches
uv_rect.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
11#pragma once
12
13#include <algorithm>
14
15#include <glm/vec2.hpp>
16#include <glm/vec4.hpp>
17
19
20namespace hmap
21{
22
29struct UvRect
30{
31 float u_min = 0.f;
32 float u_max = 1.f;
33 float v_min = 0.f;
34 float v_max = 1.f;
35
36 constexpr UvRect() = default;
37 constexpr UvRect(float u0, float u1, float v0, float v1)
38 : u_min(u0), u_max(u1), v_min(v0), v_max(v1)
39 {
40 }
41 constexpr UvRect(const glm::vec4 &v)
42 : u_min(v.x), u_max(v.y), v_min(v.z), v_max(v.w)
43 {
44 }
45
46 static constexpr UvRect unit()
47 {
48 return {0.f, 1.f, 0.f, 1.f};
49 }
50
51 static constexpr UvRect from_vec4(const glm::vec4 &v)
52 {
53 return {v.x, v.y, v.z, v.w};
54 }
55
56 constexpr glm::vec4 to_vec4() const
57 {
58 return {u_min, u_max, v_min, v_max};
59 }
60
61 constexpr BBox2D to_bbox2d() const
62 {
63 return BBox2D(glm::vec2(u_min, v_min), glm::vec2(u_max, v_max));
64 }
65
66 static constexpr UvRect from_bbox2d(const BBox2D &bbox)
67 {
68 return UvRect(bbox.min.x, bbox.max.x, bbox.min.y, bbox.max.y);
69 }
70
71 constexpr glm::vec2 size() const
72 {
73 return {u_max - u_min, v_max - v_min};
74 }
75
76 constexpr bool contains(float u, float v) const
77 {
78 return u >= u_min && u <= u_max && v >= v_min && v <= v_max;
79 }
80
81 constexpr glm::vec2 lerp(float rx, float ry) const
82 {
83 return {u_min + rx * (u_max - u_min), v_min + ry * (v_max - v_min)};
84 }
85
86 constexpr glm::vec2 normalize(float u, float v) const
87 {
88 const float du = u_max - u_min;
89 const float dv = v_max - v_min;
90 return {(du != 0.f) ? (u - u_min) / du : 0.f,
91 (dv != 0.f) ? (v - v_min) / dv : 0.f};
92 }
93};
94
95} // namespace hmap
Axis-aligned 2D bounding box in world coordinates.
Definition opencv.hpp:20
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25
glm::vec2 min
Definition bbox2d.hpp:26
glm::vec2 max
Definition bbox2d.hpp:27
constexpr glm::vec4 to_vec4() const
Definition uv_rect.hpp:56
static constexpr UvRect unit()
Definition uv_rect.hpp:46
float u_max
Definition uv_rect.hpp:32
constexpr BBox2D to_bbox2d() const
Definition uv_rect.hpp:61
constexpr glm::vec2 normalize(float u, float v) const
Definition uv_rect.hpp:86
constexpr bool contains(float u, float v) const
Definition uv_rect.hpp:76
constexpr glm::vec2 lerp(float rx, float ry) const
Definition uv_rect.hpp:81
constexpr UvRect(float u0, float u1, float v0, float v1)
Definition uv_rect.hpp:37
static constexpr UvRect from_bbox2d(const BBox2D &bbox)
Definition uv_rect.hpp:66
float v_max
Definition uv_rect.hpp:34
constexpr UvRect(const glm::vec4 &v)
Definition uv_rect.hpp:41
constexpr UvRect()=default
float u_min
Definition uv_rect.hpp:31
static constexpr UvRect from_vec4(const glm::vec4 &v)
Definition uv_rect.hpp:51
float v_min
Definition uv_rect.hpp:33
constexpr glm::vec2 size() const
Definition uv_rect.hpp:71