Skip to content

Commit 5aa1a1d

Browse files
committed
add proc gen algorithms
1 parent b81500b commit 5aa1a1d

File tree

2 files changed

+255
-0
lines changed

2 files changed

+255
-0
lines changed

include/gf2/core/ProcGen.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023 Julien Bernard
3+
#ifndef GF_PROC_GEN_H
4+
#define GF_PROC_GEN_H
5+
6+
#include <vector>
7+
8+
#include "Heightmap.h"
9+
#include "Random.h"
10+
#include "Span.h"
11+
#include "Vec2.h"
12+
13+
namespace gf {
14+
15+
GF_CORE_API std::vector<Vec2F> midpoint_displacement_1d(Vec2F p0, Vec2F p1, Random& random, int iterations, Vec2F direction, float initial_factor = 1.0f, float reduction_factor = 0.5f);
16+
17+
GF_CORE_API std::vector<Vec2F> midpoint_displacement_1d(Vec2F p0, Vec2F p1, Random& random, int iterations, float initial_factor = 1.0f, float reduction_factor = 0.5f);
18+
19+
GF_CORE_API Heightmap midpoint_displacement_2d(Vec2I size, Random& random, Span<const double> initial_values = nullptr);
20+
21+
GF_CORE_API Heightmap diamond_square_2d(Vec2I size, Random& random, Span<const double> initial_values = nullptr);
22+
23+
}
24+
25+
#endif // GF_PROC_GEN_H

library/core/ProcGen.cc

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023 Julien Bernard
3+
4+
#include <gf2/core/ProcGen.h>
5+
6+
namespace gf {
7+
8+
/*
9+
* Midpoint Displacement 1D
10+
*/
11+
12+
GF_CORE_API std::vector<Vec2F> midpoint_displacement_1d(Vec2F p0, Vec2F p1, Random& random, int iterations, Vec2F direction, float initial_factor, float reduction_factor)
13+
{
14+
direction = initial_factor * gf::euclidean_distance(p0, p1) * gf::normalize(direction);
15+
16+
std::vector<Vec2F> points;
17+
18+
const std::size_t size = static_cast<std::size_t>(1) << iterations;
19+
const std::size_t count = size + 1;
20+
points.resize(count);
21+
points[0] = p0;
22+
points[count - 1] = p1;
23+
24+
std::size_t step = size / 2;
25+
26+
while (step > 0) {
27+
for (std::size_t i = step; i < size; i += 2 * step) {
28+
assert(i - step < count);
29+
const Vec2F prev = points[i - step];
30+
assert(i + step < count);
31+
const Vec2F next = points[i + step];
32+
33+
Vec2F middle = (prev + next) / 2;
34+
middle += random.compute_uniform_float(-0.5f, +0.5f) * direction;
35+
36+
points[i] = middle;
37+
}
38+
39+
direction *= reduction_factor;
40+
step /= 2;
41+
}
42+
43+
return points;
44+
}
45+
46+
GF_CORE_API std::vector<Vec2F> midpoint_displacement_1d(Vec2F p0, Vec2F p1, Random& random, int iterations, float initial_factor, float reduction_factor)
47+
{
48+
return midpoint_displacement_1d(p0, p1, random, iterations, gf::perp(p1 - p0), initial_factor, reduction_factor);
49+
}
50+
51+
/*
52+
* Midpoint Displacement 2D
53+
*/
54+
55+
namespace {
56+
57+
int compute_power_of_two_size(Vec2I size) {
58+
int actual_size = 1;
59+
60+
while (actual_size + 1 < size.h || actual_size + 1 < size.w) {
61+
actual_size = actual_size * 2;
62+
}
63+
64+
return actual_size;
65+
}
66+
67+
void initialize_corners(Heightmap& map, Span<const double> initial_values, int d) {
68+
if (initial_values.size() == 0) {
69+
map.set_value({ 0, 0 }, 0.0);
70+
map.set_value({ 0, d }, 0.0);
71+
map.set_value({ d, d }, 0.0);
72+
map.set_value({ d, 0 }, 0.0);
73+
} else if (initial_values.size() < 4) {
74+
map.set_value({ 0, 0 }, initial_values[0]);
75+
map.set_value({ 0, d }, initial_values[0]);
76+
map.set_value({ d, d }, initial_values[0]);
77+
map.set_value({ d, 0 }, initial_values[0]);
78+
} else {
79+
map.set_value({ 0, 0 }, initial_values[0]);
80+
map.set_value({ 0, d }, initial_values[1]);
81+
map.set_value({ d, d }, initial_values[2]);
82+
map.set_value({ d, 0 }, initial_values[3]);
83+
}
84+
}
85+
86+
} // anonymous namespace
87+
88+
GF_CORE_API Heightmap midpoint_displacement_2d(Vec2I size, Random& random, Span<const double> initial_values)
89+
{
90+
int actual_size = compute_power_of_two_size(size);
91+
92+
int d = actual_size;
93+
actual_size = actual_size + 1;
94+
95+
Heightmap map({ actual_size, actual_size });
96+
initialize_corners(map, initial_values, d);
97+
98+
while (d >= 2) {
99+
const int d2 = d / 2;
100+
101+
std::uniform_real_distribution<double> dist(-static_cast<double>(d), static_cast<double>(d));
102+
103+
for (int y = d2; y < actual_size; y += d) {
104+
for (int x = d2; x < actual_size; x += d) {
105+
const double ne = map.value({ x - d2, y - d2 });
106+
const double nw = map.value({ x - d2, y + d2 });
107+
const double se = map.value({ x + d2, y - d2 });
108+
const double sw = map.value({ x + d2, y + d2 });
109+
110+
// center
111+
const double center = (ne + nw + se + sw) / 4;
112+
map.set_value({ x, y }, center + dist(random.engine()));
113+
114+
// north
115+
const double north = (ne + nw) / 2;
116+
map.set_value({ x - d2, y }, north + dist(random.engine()));
117+
118+
// south
119+
const double south = (se + sw) / 2;
120+
map.set_value({ x + d2, y }, south + dist(random.engine()));
121+
122+
// east
123+
const double east = (ne + se) / 2;
124+
map.set_value({ x, y - d2 }, east + dist(random.engine()));
125+
126+
// west
127+
const double west = (nw + sw) / 2;
128+
map.set_value({ x, y + d2 }, west + dist(random.engine()));
129+
}
130+
}
131+
132+
d = d2;
133+
}
134+
135+
const Vec2I offset = (actual_size - size) / 2;
136+
return map.sub_map(RectI::from_position_size(offset, size));
137+
}
138+
139+
/*
140+
* Diamond-Square
141+
*/
142+
143+
namespace {
144+
145+
void diamond(Heightmap& map, Random& random, Vec2I pos, int d) {
146+
// clang-format off
147+
const double value = (map.value({ pos.x - d, pos.y - d })
148+
+ map.value({ pos.x - d, pos.y + d })
149+
+ map.value({ pos.x + d, pos.y - d })
150+
+ map.value({ pos.x + d, pos.y + d })) / 4;
151+
// clang-format on
152+
153+
const double noise = random.compute_uniform_float(-static_cast<double>(d), +static_cast<double>(d));
154+
map.set_value(pos, value + noise);
155+
}
156+
157+
void square(Heightmap& map, Random& random, Vec2I pos, int d) {
158+
const Vec2I size = map.size();
159+
160+
double value = 0.0;
161+
int n = 0;
162+
163+
if (pos.x >= d) {
164+
value += map.value({ pos.x - d, pos.y });
165+
++n;
166+
}
167+
168+
if (pos.x + d < size.w) {
169+
value += map.value({ pos.x + d, pos.y });
170+
++n;
171+
}
172+
173+
if (pos.y >= d) {
174+
value += map.value({ pos.x, pos.y - d });
175+
++n;
176+
}
177+
178+
if (pos.y + d < size.h) {
179+
value += map.value({ pos.x, pos.y + d });
180+
++n;
181+
}
182+
183+
assert(n > 0);
184+
value = value / n;
185+
186+
const double noise = random.compute_uniform_float(-static_cast<double>(d), static_cast<double>(d));
187+
map.set_value(pos, value + noise);
188+
}
189+
190+
} // anonymous namespace
191+
192+
GF_CORE_API Heightmap diamond_square_2d(Vec2I size, Random& random, Span<const double> initial_values)
193+
{
194+
int actual_size = compute_power_of_two_size(size);
195+
196+
int d = actual_size;
197+
actual_size = actual_size + 1;
198+
199+
Heightmap map({ actual_size, actual_size });
200+
initialize_corners(map, initial_values, d);
201+
202+
while (d >= 2) {
203+
int d2 = d / 2;
204+
205+
for (int y = d2; y < actual_size; y += d) {
206+
for (int x = d2; x < actual_size; x += d) {
207+
diamond(map, random, { x, y }, d2);
208+
}
209+
}
210+
211+
for (int y = 0; y < actual_size; y += d) {
212+
for (int x = d2; x < actual_size; x += d) {
213+
square(map, random, { x, y }, d2);
214+
}
215+
}
216+
217+
for (int y = d2; y < actual_size; y += d) {
218+
for (int x = 0; x < actual_size; x += d) {
219+
square(map, random, { x, y }, d2);
220+
}
221+
}
222+
223+
d = d2;
224+
}
225+
226+
const Vec2I offset = (actual_size - size) / 2;
227+
return map.sub_map(RectI::from_position_size(offset, size));
228+
}
229+
230+
}

0 commit comments

Comments
 (0)