16#include <glm/vec2.hpp>
40 if (n <= 0)
return 0.f;
44 return (
static_cast<float>(i) + 0.5f) /
static_cast<float>(n);
46 return static_cast<float>(i) /
static_cast<float>(n);
48 return (n > 1) ?
static_cast<float>(i) /
static_cast<float>(n - 1) : 0.f;
61 if (n <= 1)
return 0.f;
66 idx = u *
static_cast<float>(n) - 0.5f;
71 return std::clamp(idx, 0.f,
static_cast<float>(n - 1));
79 const glm::ivec2 &tile_shape_with_halo,
80 const glm::ivec2 &global_shape,
83 const float du = (global_shape.x > 0) ? (domain_uv.u_max - domain_uv.u_min) /
84 static_cast<float>(global_shape.x)
86 const float dv = (global_shape.y > 0) ? (domain_uv.v_max - domain_uv.v_min) /
87 static_cast<float>(global_shape.y)
90 return {domain_uv.u_min +
static_cast<float>(global_offset.x) * du,
92 static_cast<float>(global_offset.x + tile_shape_with_halo.x) * du,
93 domain_uv.v_min +
static_cast<float>(global_offset.y) * dv,
95 static_cast<float>(global_offset.y + tile_shape_with_halo.y) *
float index_to_uv(int i, int n, GridRegistration reg=GridRegistration::CellCenter)
Maps a discrete index i in [0, n) to a UV coordinate in [0, 1].
Definition grid_mapping.hpp:36
GridRegistration
Grid registration policy for mapping discrete indices to [0, 1].
Definition grid_mapping.hpp:27
@ Node
Vertex-centered at i/(N-1), pinning endpoints 0 and 1.
Definition grid_mapping.hpp:30
@ CellCorner
Left corner of cell i at i/N.
Definition grid_mapping.hpp:29
@ CellCenter
Cell i covers [i/N, (i+1)/N], center at (i + 0.5)/N.
Definition grid_mapping.hpp:28
UvRect tile_uv_rect(const glm::ivec2 &global_offset, const glm::ivec2 &tile_shape_with_halo, const glm::ivec2 &global_shape, const UvRect &domain_uv=UvRect::unit())
Computes the exact UV rectangle of a tile (including halo) within a domain of global_shape.
Definition grid_mapping.hpp:78
float uv_to_continuous_index(float u, int n, GridRegistration reg=GridRegistration::CellCenter)
Maps a UV coordinate in [0, 1] to a continuous index in [0, n - 1].
Definition grid_mapping.hpp:56
Axis-aligned rectangle in normalized [0, 1]^2 domain/UV space.
Definition uv_rect.hpp:30
static constexpr UvRect unit()
Definition uv_rect.hpp:46
Normalized domain/UV rectangle [u_min, u_max] x [v_min, v_max].