-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathhex.hpp
More file actions
734 lines (643 loc) · 17.7 KB
/
Copy pathhex.hpp
File metadata and controls
734 lines (643 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
* @file liblava/util/hex.hpp
* @brief Hex point, cell and grid
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
* @see https://www.redblobgames.com/grids/hexagons/
*/
#pragma once
#include "liblava/core/types.hpp"
#include <cmath>
#include <numbers>
#include <unordered_map>
namespace lava {
/**
* @brief Hex point
*/
struct hex_point {
/// List of hex points
using list = std::vector<hex_point>;
/// X coordinate
r32 x{};
/// Y coordinate
r32 y{};
};
/**
* @brief Hex cell
*/
struct hex_cell {
/// List of hex cells
using list = std::vector<hex_cell>;
/// Q axis
i32 q{};
/// R axis
i32 r{};
/// S axis
i32 s{};
/**
* @brief Compare operator
*/
auto operator<=>(hex_cell const&) const = default;
/// Hex pair (Q and R)
using pair = std::pair<i32, i32>;
/// Map of hex cells
using map = std::unordered_map<pair, index, pair_hash>;
/**
* @brief Get the pair
* @return pair Hex pair
*/
inline pair to_pair() const {
return {q, r};
}
/**
* @brief Add hex cell
* @param cell Another hex cell
*/
inline void add(hex_cell const& cell) {
*this = {q + cell.q,
r + cell.r,
s + cell.s};
}
/**
* @brief Substract hex cell
* @param cell Another hex cell
*/
inline void substract(hex_cell const& cell) {
*this = {q - cell.q,
r - cell.r,
s - cell.s};
}
/**
* @brief Scale the hex cell
* @param factor Scaling factor
*/
inline void scale(i32 factor) {
*this = {q * factor,
r * factor,
s * factor};
}
/**
* @brief Rotate to left
*/
inline void rotate_left() {
*this = {-s, -q, -r};
}
/**
* @brief Rotate to right
*/
inline void rotate_right() {
*this = {-r, -s, -q};
}
};
/**
* @brief Get S axis from Q and R axes
* @param q Q axis
* @param r R axis
* @return i32 S axis
*/
inline i32 hex_get_s(i32 q, i32 r) {
return -q - r;
}
/**
* @brief Get hex cell from pair
* @param pair Hex pair
* @return hex_cell Hex cell
*/
inline hex_cell hex_cell_from_pair(hex_cell::pair const& pair) {
return {pair.first, pair.second,
hex_get_s(pair.first, pair.second)};
}
/**
* @brief Hex fractional cell
*/
struct hex_fractional_cell {
/// Q axis
r32 q{};
/// R axis
r32 r{};
/// S axis
r32 s{};
};
/// Hex fractional cell
using hex_frac_cell = hex_fractional_cell;
/**
* @brief Check if hex cell is valid
* @param cell Hex cell to check
* @return Hex cell is valid or not
*/
inline bool hex_is_valid(hex_cell const& cell) {
return cell.q + cell.r + cell.s == 0;
}
/**
* @brief Hex offset coordinates
*/
struct hex_offset_coord {
/// Column coordinate
i32 col{};
/// Row coordinate
i32 row{};
};
/// Hex doubled coordinates
using hex_doubled_coord = hex_offset_coord;
/**
* @brief Hex orientation
*/
struct hex_orientation {
/// F0 value
r32 f0{};
/// F1 value
r32 f1{};
/// F2 value
r32 f2{};
/// F3 value
r32 f3{};
/// B0 value
r32 b0{};
/// B1 value
r32 b1{};
/// B2 value
r32 b2{};
/// B3 value
r32 b3{};
/// Start angle
r32 start_angle{};
};
/**
* @brief Hex layout
*/
struct hex_layout {
/// Hex orientation
hex_orientation orientation;
/// Hex origin
hex_point origin;
/// Hex size
hex_point size;
};
/// List of hex directions
hex_cell::list const hex_directions{
{1, 0, -1},
{1, -1, 0},
{0, -1, 1},
{-1, 0, 1},
{-1, 1, 0},
{0, 1, -1}};
/**
* @brief Get the hex cell from direction
* @param direction Direction index
* @return hex_cell Hex cell
*/
inline hex_cell hex_direction(index direction) {
return hex_directions[direction];
}
/**
* @brief Get the neighbor of hex cell by direction
* @param cell Target hex cell
* @param direction Direction index
* @return hex_cell Neighbor hex cell
*/
inline hex_cell hex_neighbor(hex_cell const& cell,
index direction) {
auto neighbor = cell;
neighbor.add(hex_direction(direction));
return neighbor;
}
/// List of hex diagonals
hex_cell::list const hex_diagonals{
{2, -1, -1},
{-1, -2, 1},
{-1, -1, 2},
{-2, 1, 1},
{-1, 2, -1},
{1, 1, -2}};
/**
* @brief Get the diagonal from direction
* @param direction Direction index
* @return hex_cell Hex cell
*/
inline hex_cell hex_diagonal(index direction) {
return hex_diagonals[direction];
}
/**
* @brief Get the diagonal neighbor of hex cell by direction
* @param cell Target hex cell
* @param direction Direction index
* @return hex_cell Diagonal neighbor hex cell
*/
inline hex_cell hex_diagonal_neighbor(hex_cell const& cell,
index direction) {
auto neighbor = cell;
neighbor.add(hex_diagonal(direction));
return neighbor;
}
/**
* @brief Get the length of hex cell
* @param cell Target hex cell
* @return i32 Length of hex cell
*/
inline i32 hex_length(hex_cell const& cell) {
return to_i32(std::abs(cell.q)
+ std::abs(cell.r)
+ std::abs(cell.s) / 2);
}
/**
* @brief Get the distance between 2 hex cells
* @param a Source hex cell
* @param b Target hex cell
* @return i32 Distance
*/
inline i32 hex_distance(hex_cell const& a,
hex_cell const& b) {
auto dist = a;
dist.substract(b);
return hex_length(dist);
}
/**
* @brief Round a fractional cell to hex cell
* @param cell Target fractional cell
* @return hex_cell Rounded hex cell
*/
inline hex_cell hex_round(hex_frac_cell const& cell) {
auto qi = to_i32(std::round(cell.q));
auto ri = to_i32(std::round(cell.r));
auto si = to_i32(std::round(cell.s));
auto const q_diff = std::abs(qi - cell.q);
auto const r_diff = std::abs(ri - cell.r);
auto const s_diff = std::abs(si - cell.s);
if ((q_diff > r_diff) && (q_diff > s_diff))
qi = -ri - si;
else if (r_diff > s_diff)
ri = -qi - si;
else
si = -qi - ri;
return {qi, ri, si};
}
/**
* @brief Get the linear interpolation between 2 hex cells
* @param a Source fractional hex cell
* @param b Target fractional hex cell
* @param t Factor
* @return hex_frac_cell Fractional hex cell
*/
inline hex_frac_cell hex_lerp(hex_frac_cell const& a,
hex_frac_cell const& b,
r32 t) {
return {
a.q * (1.f - t) + b.q * t,
a.r * (1.f - t) + b.r * t,
a.s * (1.f - t) + b.s * t};
}
/**
* @brief Get the line between 2 hex cells
* @param a Source hex cell
* @param b Target hex cell
* @return hex_cell::list List of hex cells
*/
inline hex_cell::list hex_line(hex_cell const& a,
hex_cell const& b) {
auto const a_nudge = hex_frac_cell{a.q + 0.000001f,
a.r + 0.000001f,
a.s - 0.000002f};
auto const b_nudge = hex_frac_cell{b.q + 0.000001f,
b.r + 0.000001f,
b.s - 0.000002f};
ui32 const n = hex_distance(a, b);
auto const step = 1.f / std::max(n, 1u);
hex_cell::list results;
for (auto i = 0u; i <= n; ++i)
results.push_back(hex_round(hex_lerp(a_nudge,
b_nudge,
step * i)));
return results;
}
/**
* @brief Hex offsets
*/
enum class hex_offset : i32 {
odd = -1,
even = 1
};
/**
* @brief Get the Q offset from hex cube
* @param offset Hex offset
* @param cell Hex cell
* @return hex_offset_coord Hex offset coordinates
*/
inline hex_offset_coord hex_q_offset_from_cube(hex_offset offset,
hex_cell const& cell) {
auto const& col = cell.q;
auto const row = cell.r
+ to_i32((cell.q + (i32)offset * (cell.q & 1)) / 2);
return {col, row};
}
/**
* @brief Get the Q offset to hex cube
* @param offset Hex offset
* @param coord Hex offset coordinates
* @return hex_cell Hex cell
*/
inline hex_cell hex_q_offset_to_cube(hex_offset offset,
hex_offset_coord const& coord) {
auto const& q = coord.col;
auto const r = coord.row
- to_i32((coord.col + (i32)offset * (coord.col & 1)) / 2);
auto const s = -q - r;
return {q, r, s};
}
/**
* @brief Get the R offset from hex cube
* @param offset Hex offset
* @param cell Hex cell
* @return hex_offset_coord Hex offset coordinates
*/
inline hex_offset_coord hex_r_offset_from_cube(hex_offset offset,
hex_cell const& cell) {
auto const col = cell.q
+ to_i32((cell.r + (i32)offset * (cell.r & 1)) / 2);
auto const& row = cell.r;
return {col, row};
}
/**
* @brief Get the R offset to hex cube
* @param offset Hex offset
* @param coord Hex offset coordinates
* @return hex_cell Hex cell
*/
inline hex_cell hex_r_offset_to_cube(hex_offset offset,
hex_offset_coord const& coord) {
auto const q = coord.col
- to_i32((coord.row + (i32)offset * (coord.row & 1)) / 2);
auto const& r = coord.row;
auto const s = -q - r;
return {q, r, s};
}
/**
* @brief Get the Q doubled from hex cube
* @param cell Hex cell
* @return hex_doubled_coord Hex doubled coordinates
*/
inline hex_doubled_coord hex_q_doubled_from_cube(hex_cell const& cell) {
auto const& col = cell.q;
auto const row = 2 * cell.r + cell.q;
return {col, row};
}
/**
* @brief Get the Q doubled to hex cube
* @param coord Hex doubled coordinates
* @return hex_cell Hex cell
*/
inline hex_cell hex_q_doubled_to_cube(hex_doubled_coord const& coord) {
auto const& q = coord.col;
auto const r = to_i32((coord.row - coord.col) / 2);
auto const s = -q - r;
return {q, r, s};
}
/**
* @brief Get the R offset from hex cube
* @param cell Hex cell
* @return hex_doubled_coord Hex doubled coordinates
*/
inline hex_doubled_coord hex_r_doubled_from_cube(hex_cell const& cell) {
auto const col = 2 * cell.q + cell.r;
auto const& row = cell.r;
return {col, row};
}
/**
* @brief Get the R doubled to hex cube
* @param coord Hex doubled coordinates
* @return hex_cell Hex cell
*/
inline hex_cell hex_r_doubled_to_cube(hex_doubled_coord const& coord) {
auto const q = to_i32((coord.col - coord.row) / 2);
auto const& r = coord.row;
auto const s = -q - r;
return {q, r, s};
}
/// Hex point Y layout
hex_orientation const hex_layout_point_y = {
std::sqrt(3.f),
std::sqrt(3.f) / 2.f,
0.f,
3.f / 2.f,
std::sqrt(3.f) / 3.f,
-1.f / 3.f,
0.f,
2.f / 3.f,
0.5f};
/// Hex flat layout
hex_orientation const hex_layout_flat = {
3.f / 2.f,
0.f,
std::sqrt(3.f) / 2.f,
std::sqrt(3.f),
2.f / 3.f,
0.f,
-1.f / 3.f,
std::sqrt(3.f) / 3.f,
0.f};
/**
* @brief Convert the hex cell to pixel
* @param layout Hex layout
* @param cell Hex cell
* @return hex_point Hex point
*/
inline hex_point hex_to_pixel(hex_layout const& layout,
hex_cell const& cell) {
auto const& m = layout.orientation;
auto const& size = layout.size;
auto const& origin = layout.origin;
auto const x = (m.f0 * cell.q + m.f1 * cell.r) * size.x;
auto const y = (m.f2 * cell.q + m.f3 * cell.r) * size.y;
return {x + origin.x, y + origin.y};
}
/**
* @brief Convert the hex point to cell
* @param layout Hex layout
* @param p Hex point
* @return hex_frac_cell Hex fractional cell
*/
inline hex_frac_cell hex_pixel_to_cell(hex_layout const& layout,
hex_point const& p) {
auto const& m = layout.orientation;
auto const& size = layout.size;
auto const& origin = layout.origin;
auto const pt = hex_point{(p.x - origin.x) / size.x,
(p.y - origin.y) / size.y};
auto const q = m.b0 * pt.x + m.b1 * pt.y;
auto const r = m.b2 * pt.x + m.b3 * pt.y;
return {q, r, -q - r};
}
/**
* @brief Get the hex corner offset
* @param layout Hex layout
* @param corner Corner
* @return hex_point Hex point
*/
inline hex_point hex_corner_offset(hex_layout const& layout,
i32 corner) {
auto const& m = layout.orientation;
auto const& size = layout.size;
auto const angle = 2.
* std::numbers::pi_v<r32> * (m.start_angle - corner)
/ 6.f;
return {size.x * (r32)std::cos(angle),
size.y * (r32)std::sin(angle)};
}
/**
* @brief Get the hex polygon corners
* @param layout Hex layout
* @param cell Hex cell
* @return hex_point::list List of hex points
*/
inline hex_point::list hex_polygon_corners(hex_layout const& layout,
hex_cell const& cell) {
hex_point::list corners = {};
auto const center = hex_to_pixel(layout, cell);
for (int i = 0u; i < 6; ++i) {
auto const offset = hex_corner_offset(layout, i);
corners.push_back({center.x + offset.x,
center.y + offset.y});
}
return corners;
}
/**
* @brief Get the hex point by corner
* @param center Center hex point
* @param size Size of hex cell
* @param corner Corner
* @return hex_point Hex point
*/
inline hex_point hex_get_corner(hex_point const& center,
r32 size,
ui32 corner) {
auto const angle_deg = 60 * corner - 30;
auto const angle_rad = std::numbers::pi_v<r32> / 180 * angle_deg;
return {
center.x + size * std::cos(angle_rad),
center.y + size * std::sin(angle_rad)};
}
/**
* @brief Hex cardinal directions
*/
enum class hex_cardinal_direction : index {
NE = 0,
E,
SE,
SW,
W,
NW
};
/**
* @brief Convert hex cardinal direction to string
* @param direction Hex cardinal direction
* @return string String representation
*/
inline string to_string(hex_cardinal_direction direction) {
switch (direction) {
case hex_cardinal_direction::NE: {
return "Northeast";
}
case hex_cardinal_direction::E: {
return "East";
}
case hex_cardinal_direction::SE: {
return "Southeast";
}
case hex_cardinal_direction::SW: {
return "Southwest";
}
case hex_cardinal_direction::W: {
return "West";
}
case hex_cardinal_direction::NW: {
return "Northwest";
}
}
}
/// List of hex cardinal directions
hex_cell::list const hex_cardinal_directions{
{1, 0, -1},
{0, 1, -1},
{-1, 1, 0},
{-1, 0, 1},
{0, -1, 1},
{1, -1, 0}};
/**
* @brief Get the hex cell from cardinal direction
* @param direction Hex cardinal direction
* @return hex_cell Hex cell
*/
inline hex_cell hex_get(hex_cardinal_direction direction) {
return hex_cardinal_directions[(index)direction];
}
/**
* @brief Get the opposite cardinal direction
* @param direction Hex cardinal direction
* @return hex_cardinal_direction Hex cardinal direction
*/
inline hex_cardinal_direction hex_opposite(hex_cardinal_direction direction) {
if ((index)direction < 3)
return hex_cardinal_direction((i32)direction + 3);
else
return hex_cardinal_direction((i32)direction - 3);
}
/// Hex inner radius factor = sqrt(3) / 2
constexpr r32 const hex_inner_radius_factor = 0.866025404f;
/// Hex default outer radius
constexpr r32 const hex_default_outer_radius = 1.f;
/**
* @brief Get the hex inner radius from outer radius
* @param outer_radius Hex outer radius
* @return r32 Hex inner radius
*/
inline r32 hex_calculate_inner_radius(r32 outer_radius) {
return outer_radius * hex_inner_radius_factor;
}
/**
* @brief Hex grid
*/
struct hex_grid {
/// Hex inner radius
r32 inner_radius = 0.f;
/// Hex outer radius
r32 outer_radius = hex_default_outer_radius;
/// Hex layout
hex_layout layout;
/**
* @brief Construct a new hex grid
* @param radius Hex outer radius
*/
hex_grid(r32 radius = hex_default_outer_radius)
: outer_radius(radius) {
update();
}
/**
* @brief Update the hex grid
* @param orientation Hex orientation
*/
void update(hex_orientation orientation = hex_layout_point_y) {
inner_radius = hex_calculate_inner_radius(outer_radius);
layout = {
orientation,
{},
{outer_radius, outer_radius}};
}
/**
* @brief Find the hex cell from X and Y coordinates
* @param x X coordinate
* @param y Y coordinate
* @return hex_cell Hex cell
*/
hex_cell find(r32 x, r32 y) const {
return hex_round(hex_pixel_to_cell(layout,
{x, y}));
}
/**
* @brief Get the hex point from hex cell
* @param cell Hex cell
* @return hex_point Hex point
*/
hex_point to_pixel(hex_cell const& cell) const {
return hex_to_pixel(layout, cell);
}
};
} // namespace lava