HighMap library (C++)
Loading...
Searching...
No Matches
grid_mapping.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#include <cmath>
15
16#include <glm/vec2.hpp>
17
19
20namespace hmap
21{
22
32
36inline float index_to_uv(int i,
37 int n,
39{
40 if (n <= 0) return 0.f;
41 switch (reg)
42 {
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;
49 }
50 return 0.f;
51}
52
57 float u,
58 int n,
60{
61 if (n <= 1) return 0.f;
62 float idx = 0.f;
63 switch (reg)
64 {
66 idx = u * static_cast<float>(n) - 0.5f;
67 break;
68 case GridRegistration::CellCorner: idx = u * static_cast<float>(n); break;
69 case GridRegistration::Node: idx = u * static_cast<float>(n - 1); break;
70 }
71 return std::clamp(idx, 0.f, static_cast<float>(n - 1));
72}
73
78inline UvRect tile_uv_rect(const glm::ivec2 &global_offset,
79 const glm::ivec2 &tile_shape_with_halo,
80 const glm::ivec2 &global_shape,
81 const UvRect &domain_uv = UvRect::unit())
82{
83 const float du = (global_shape.x > 0) ? (domain_uv.u_max - domain_uv.u_min) /
84 static_cast<float>(global_shape.x)
85 : 0.f;
86 const float dv = (global_shape.y > 0) ? (domain_uv.v_max - domain_uv.v_min) /
87 static_cast<float>(global_shape.y)
88 : 0.f;
89
90 return {domain_uv.u_min + static_cast<float>(global_offset.x) * du,
91 domain_uv.u_min +
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,
94 domain_uv.v_min +
95 static_cast<float>(global_offset.y + tile_shape_with_halo.y) *
96 dv};
97}
98
99} // namespace hmap
Definition opencv.hpp:20
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].