Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_hex_lattice.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
66#ifndef TPL_CA_HEX_LATTICE_H
67#define TPL_CA_HEX_LATTICE_H
68
69#include <array>
70#include <cmath>
71#include <cstddef>
72#include <ostream>
73#include <sstream>
74#include <string>
75
76#include <ah-errors.H>
77
78#include <ca-traits.H>
79#include <tpl_ca_concepts.H>
80#include <tpl_ca_lattice.H>
81
82namespace Aleph {
83namespace CA {
84
96{
99
100 friend constexpr bool operator==(Hex_Axial a, Hex_Axial b) noexcept
101 {
102 return a.q == b.q and a.r == b.r;
103 }
104 friend constexpr bool operator!=(Hex_Axial a, Hex_Axial b) noexcept { return not(a == b); }
105};
106
115{
118
119 friend constexpr bool operator==(Hex_Offset a, Hex_Offset b) noexcept
120 {
121 return a.col == b.col and a.row == b.row;
122 }
123 friend constexpr bool operator!=(Hex_Offset a, Hex_Offset b) noexcept { return not(a == b); }
124};
125
134{
138
139 friend constexpr bool operator==(Hex_Cube a, Hex_Cube b) noexcept
140 {
141 return a.x == b.x and a.y == b.y and a.z == b.z;
142 }
143 friend constexpr bool operator!=(Hex_Cube a, Hex_Cube b) noexcept { return not(a == b); }
144};
145
146// ---------------------------------------------------------------------------
147// Coordinate conversions.
148// ---------------------------------------------------------------------------
149
151[[nodiscard]] inline constexpr Hex_Offset axial_to_offset_even_r(Hex_Axial a) noexcept
152{
153 // even-r: col = q + (r + (r & 1)) / 2
154 const ca_index_t col = a.q + (a.r + (a.r & 1)) / 2;
155 return Hex_Offset{col, a.r};
156}
157
160{
161 const ca_index_t q = o.col - (o.row + (o.row & 1)) / 2;
162 return Hex_Axial{q, o.row};
163}
164
166[[nodiscard]] inline constexpr Hex_Offset axial_to_offset_odd_r(Hex_Axial a) noexcept
167{
168 const ca_index_t col = a.q + (a.r - (a.r & 1)) / 2;
169 return Hex_Offset{col, a.r};
170}
171
174{
175 const ca_index_t q = o.col - (o.row - (o.row & 1)) / 2;
176 return Hex_Axial{q, o.row};
177}
178
180[[nodiscard]] inline constexpr Hex_Cube axial_to_cube(Hex_Axial a) noexcept
181{
182 const ca_index_t x = a.q;
183 const ca_index_t z = a.r;
184 return Hex_Cube{x, -x - z, z};
185}
186
188[[nodiscard]] inline constexpr Hex_Axial cube_to_axial(Hex_Cube c) noexcept
189{
190 return Hex_Axial{c.x, c.z};
191}
192
198[[nodiscard]] inline constexpr ca_size_t hex_distance(Hex_Axial a, Hex_Axial b) noexcept
199{
200 const Hex_Cube ca = axial_to_cube(a);
201 const Hex_Cube cb = axial_to_cube(b);
202 auto absv = [](ca_index_t v) noexcept -> ca_index_t { return v < 0 ? -v : v; };
203 const ca_index_t d = absv(ca.x - cb.x) + absv(ca.y - cb.y) + absv(ca.z - cb.z);
204 return static_cast<ca_size_t>(d / 2);
205}
206
217[[nodiscard]] inline std::array<double, 2> axial_to_pixel_pointy(Hex_Axial a,
218 double radius) noexcept
219{
220 const double sqrt3 = 1.7320508075688772;
221 const double x = radius * (sqrt3 * static_cast<double>(a.q)
222 + (sqrt3 / 2.0) * static_cast<double>(a.r));
223 const double y = radius * (3.0 / 2.0) * static_cast<double>(a.r);
224 return {x, y};
225}
226
235[[nodiscard]] inline std::array<double, 2> axial_to_pixel_flat(Hex_Axial a,
236 double radius) noexcept
237{
238 const double sqrt3 = 1.7320508075688772;
239 const double x = radius * (3.0 / 2.0) * static_cast<double>(a.q);
240 const double y = radius * (sqrt3 * static_cast<double>(a.r)
241 + (sqrt3 / 2.0) * static_cast<double>(a.q));
242 return {x, y};
243}
244
260template <typename Storage, typename Boundary = OpenBoundary>
261class Hex_Lattice : public Lattice<Storage, Boundary>
262{
263 static_assert(Storage::rank == 2, "Hex_Lattice requires a 2D storage");
264
265public:
270 static constexpr std::size_t rank = base_type::rank;
271
272 using base_type::base_type;
273
275 [[nodiscard]] ca_size_t rows() const noexcept { return this->size(0); }
276
278 [[nodiscard]] ca_size_t cols() const noexcept { return this->size(1); }
279
282 {
283 return this->at(coord_type{a.r, a.q});
284 }
285
288 {
289 this->set(coord_type{a.r, a.q}, v);
290 }
291
294 {
295 return this->at_safe(coord_type{a.r, a.q});
296 }
297
303
309};
310
329template <typename Lattice, typename Palette>
330inline void render_hex_lattice_tikz(std::ostream &os,
331 const Lattice &lat,
333 double radius = 0.5,
334 bool pointy_top = true)
335{
336 os << "% Aleph::CA::render_hex_lattice_tikz\n";
337 os << "\\begin{tikzpicture}[x=1cm, y=-1cm]\n";
338 for (ca_size_t r = 0; r < lat.rows(); ++r)
339 for (ca_size_t q = 0; q < lat.cols(); ++q)
340 {
341 const Hex_Axial a{static_cast<ca_index_t>(q), static_cast<ca_index_t>(r)};
342 const auto px
343 = pointy_top ? axial_to_pixel_pointy(a, radius) : axial_to_pixel_flat(a, radius);
344 const std::string colour = palette(lat.at_axial(a));
345 const double rotation = pointy_top ? 30.0 : 0.0;
346 os << " \\node[regular polygon, regular polygon sides=6, draw, fill="
347 << colour << ", inner sep=0pt, minimum size=" << (2 * radius)
348 << "cm, rotate=" << rotation << "] at (" << px[0] << ", " << px[1] << ") {};\n";
349 }
350 os << "\\end{tikzpicture}\n";
351}
352
356template <typename Lattice, typename Palette>
357[[nodiscard]] inline std::string render_hex_lattice_tikz(const Lattice &lat,
359 double radius = 0.5,
360 bool pointy_top = true)
361{
362 std::ostringstream oss;
363 render_hex_lattice_tikz(oss, lat, std::forward<Palette>(palette), radius, pointy_top);
364 return oss.str();
365}
366
367} // namespace CA
368} // namespace Aleph
369
370#endif // TPL_CA_HEX_LATTICE_H
Exception handling system with formatted messages for Aleph-w.
size_t size_t col
Definition ca-c-api.h:116
Common typedefs and tag types for the Cellular Automata module.
Hexagonal lattice with axial accessors over a 2D storage.
static constexpr std::size_t rank
void set_axial(Hex_Axial a, const state_type &v)
Write cell value at axial coords (q, r) (strict).
typename base_type::extents_type extents_type
ca_size_t cols() const noexcept
Number of columns per row (axis 1, the q axis).
typename base_type::coord_type coord_type
ca_size_t rows() const noexcept
< inherit constructors
typename base_type::state_type state_type
void set_offset_even_r(Hex_Offset o, const state_type &v)
Convenience: write using the even-r offset convention.
state_type at_offset_even_r(Hex_Offset o) const
Convenience: read using the even-r offset convention.
state_type at_axial(Hex_Axial a) const
state_type at_axial_safe(Hex_Axial a) const
Boundary-aware read at axial coords.
Lattice that adds boundary-aware access on top of a storage.
state_type at_safe(coord_type c) const
Boundary-aware read.
void set(const coord_type &c, const state_type &v)
Strict write: throws if c is out of range.
typename Storage::state_type state_type
typename Storage::extents_type extents_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
static mpfr_t y
Definition mpfr_mul_d.c:3
constexpr Hex_Offset axial_to_offset_even_r(Hex_Axial a) noexcept
Axial -> offset (even-r convention: even rows are shifted right).
constexpr Hex_Axial cube_to_axial(Hex_Cube c) noexcept
Cube -> axial.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< double, 2 > axial_to_pixel_pointy(Hex_Axial a, double radius) noexcept
Pointy-top pixel coordinates of an axial cell.
std::array< double, 2 > axial_to_pixel_flat(Hex_Axial a, double radius) noexcept
Flat-top pixel coordinates of an axial cell.
void render_hex_lattice_tikz(std::ostream &os, const Lattice &lat, Palette &&palette, double radius=0.5, bool pointy_top=true)
Render a hex frame as a stand-alone TikZ picture.
constexpr Hex_Offset axial_to_offset_odd_r(Hex_Axial a) noexcept
Axial -> offset (odd-r convention: odd rows are shifted right).
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
constexpr Hex_Axial offset_to_axial_even_r(Hex_Offset o) noexcept
Offset (even-r) -> axial.
constexpr ca_size_t hex_distance(Hex_Axial a, Hex_Axial b) noexcept
Hex distance between two axial coordinates.
constexpr Hex_Cube axial_to_cube(Hex_Axial a) noexcept
Axial -> cube.
constexpr Hex_Axial offset_to_axial_odd_r(Hex_Offset o) noexcept
Offset (odd-r) -> axial.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Axial integer coordinates (q, r) of a hex cell.
friend constexpr bool operator!=(Hex_Axial a, Hex_Axial b) noexcept
ca_index_t q
column index
ca_index_t r
row index
friend constexpr bool operator==(Hex_Axial a, Hex_Axial b) noexcept
Cube coordinates (x, y, z) with the constraint x+y+z = 0.
friend constexpr bool operator!=(Hex_Cube a, Hex_Cube b) noexcept
friend constexpr bool operator==(Hex_Cube a, Hex_Cube b) noexcept
Offset coordinates (col, row) for visualisation.
friend constexpr bool operator!=(Hex_Offset a, Hex_Offset b) noexcept
friend constexpr bool operator==(Hex_Offset a, Hex_Offset b) noexcept
gsl_rng * r
C++20 concepts for the Cellular Automata module.
Cellular automata lattice with pluggable boundary policies.