HighMap library (C++)
Loading...
Searching...
No Matches
grids.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 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
9
10#pragma once
11#include <array>
12#include <vector>
13
14#include "highmap/algebra.hpp"
15#include "highmap/array.hpp"
16
17#include <numbers>
18
19namespace hmap
20{
21
22// ==========================================================================
23// Neighborhoods
24// ==========================================================================
25
26namespace neighborhood
27{
28
30{
31 glm::ivec2 offset;
32 float distance;
34};
35
37{
38 glm::ivec2 offset;
39 float distance;
41 float weight;
42};
43
44// 4-neighborhood (Von Neumann: N, E, S, W)
45inline constexpr std::array<Neighbor, 4> VON_NEUMANN_4 = {{
46 {{0, 1}, 1.0f, 1.0f},
47 {{1, 0}, 1.0f, 1.0f},
48 {{0, -1}, 1.0f, 1.0f},
49 {{-1, 0}, 1.0f, 1.0f},
50}};
51
52// Moore neighborhood (orthogonal first, then diagonals)
53// 6 2 8
54// 1 . 4
55// 5 3 7
56// clang-format off
57inline constexpr std::array<Neighbor, 8> MOORE_8 = {{
58 {{-1, 0}, 1.0f, 1.0f},
59 {{0, 1}, 1.0f, 1.0f},
60 {{0, -1}, 1.0f, 1.0f},
61 {{1, 0}, 1.0f, 1.0f},
62 {{-1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
63 {{-1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
64 {{1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
65 {{1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
66}};
67// clang-format on
68
69// D8 Flow direction neighborhood (counter-clockwise / standard D8 code
70// ordering):
71// 5 6 7
72// 4 . 0
73// 3 2 1
74// clang-format off
75inline constexpr std::array<Neighbor, 8> D8_FLOW = {{
76 {{1, 0}, 1.0f, 1.0f},
77 {{1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
78 {{0, -1}, 1.0f, 1.0f},
79 {{-1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
80 {{-1, 0}, 1.0f, 1.0f},
81 {{-1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
82 {{0, 1}, 1.0f, 1.0f},
83 {{1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
84}};
85// clang-format on
86
87// D-infinity Flow neighborhood (with Quinn effective contour length weights)
88// 5 1 7
89// 0 . 3
90// 4 2 6
91// clang-format off
92inline constexpr std::array<NeighborWeighted, 8> DINF_8 = {{
93 {{-1, 0}, 1.0f, 1.0f, 0.500f},
94 {{0, 1}, 1.0f, 1.0f, 0.500f},
95 {{0, -1}, 1.0f, 1.0f, 0.500f},
96 {{1, 0}, 1.0f, 1.0f, 0.500f},
97 {{-1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>, 0.354f},
98 {{-1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>, 0.354f},
99 {{1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>, 0.354f},
100 {{1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>, 0.354f},
101}};
102// clang-format on
103
104// Geomorphons 8-neighborhood (ray search pattern)
105// 7 0 1
106// 6 . 2
107// 5 4 3
108// clang-format off
109inline constexpr std::array<Neighbor, 8> GEOMORPHON_8 = {{
110 {{-1, 0}, 1.0f, 1.0f},
111 {{-1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
112 {{0, 1}, 1.0f, 1.0f},
113 {{1, 1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
114 {{1, 0}, 1.0f, 1.0f},
115 {{1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
116 {{0, -1}, 1.0f, 1.0f},
117 {{-1, -1}, std::numbers::sqrt2_v<float>, 1.0f / std::numbers::sqrt2_v<float>},
118}};
119// clang-format on
120
121} // namespace neighborhood
122
123// ==========================================================================
124// Functions
125// ==========================================================================
126
141int convert_length_to_pixel(float x,
142 int nx,
143 bool lim_inf = true,
144 bool lim_sup = false,
145 float scale = 1.f);
146
155void grid_xy_vector(std::vector<float> &x,
156 std::vector<float> &y,
157 glm::ivec2 shape,
158 const UvRect &uv_rect = UvRect::unit(),
159 bool endpoint = false);
160
168void rescale_grid_from_unit_square_to_bbox(std::vector<float> &x,
169 std::vector<float> &y,
170 const UvRect &uv_rect);
171
172} // namespace hmap
Header file defining basic vector and matrix manipulation classes.
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition grids.hpp:27
constexpr std::array< Neighbor, 8 > GEOMORPHON_8
Definition grids.hpp:109
constexpr std::array< NeighborWeighted, 8 > DINF_8
Definition grids.hpp:92
constexpr std::array< Neighbor, 8 > MOORE_8
Definition grids.hpp:57
constexpr std::array< Neighbor, 8 > D8_FLOW
Definition grids.hpp:75
constexpr std::array< Neighbor, 4 > VON_NEUMANN_4
Definition grids.hpp:45
Definition opencv.hpp:20
void rescale_grid_from_unit_square_to_bbox(std::vector< float > &x, std::vector< float > &y, const UvRect &uv_rect)
Rescale 1D grid vectors from unit square [0, 1] to target UV rectangle.
Definition grid.cpp:134
void grid_xy_vector(std::vector< float > &x, std::vector< float > &y, glm::ivec2 shape, const UvRect &uv_rect=UvRect::unit(), bool endpoint=false)
Return x and y coordinates of a regular grid, as two 1D vectors.
Definition grid.cpp:117
Cloud scale(const Cloud &cloud, glm::vec2 scale, glm::vec2 center={0.5f, 0.5f})
Scales the point coordinates in a cloud relative to a center point.
Definition cloud_functions.cpp:163
int convert_length_to_pixel(float x, int nx, bool lim_inf=true, bool lim_sup=false, float scale=1.f)
Converts a length value to a pixel index in a discretized space.
Definition grid.cpp:17
static constexpr UvRect unit()
Definition uv_rect.hpp:46
float weight
Definition grids.hpp:41
float distance
Definition grids.hpp:39
float inv_distance
Definition grids.hpp:40
glm::ivec2 offset
Definition grids.hpp:38
Definition grids.hpp:30
float inv_distance
Definition grids.hpp:33
glm::ivec2 offset
Definition grids.hpp:31
float distance
Definition grids.hpp:32