66#ifndef TPL_CA_HEX_LATTICE_H
67#define TPL_CA_HEX_LATTICE_H
102 return a.q == b.q
and a.r == b.r;
121 return a.col == b.col
and a.row == b.row;
141 return a.x == b.x
and a.y == b.y
and a.z == b.z;
161 const ca_index_t q = o.col - (o.row + (o.row & 1)) / 2;
175 const ca_index_t q = o.col - (o.row - (o.row & 1)) / 2;
203 const ca_index_t d = absv(ca.
x - cb.
x) + absv(ca.
y - cb.
y) + absv(ca.
z - cb.
z);
218 double radius)
noexcept
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);
236 double radius)
noexcept
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));
260template <
typename Storage,
typename Boundary = OpenBoundary>
263 static_assert(Storage::rank == 2,
"Hex_Lattice requires a 2D storage");
272 using base_type::base_type;
329template <
typename Lattice,
typename Palette>
334 bool pointy_top =
true)
336 os <<
"% Aleph::CA::render_hex_lattice_tikz\n";
337 os <<
"\\begin{tikzpicture}[x=1cm, y=-1cm]\n";
339 for (
ca_size_t q = 0; q < lat.cols(); ++q)
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";
350 os <<
"\\end{tikzpicture}\n";
356template <
typename Lattice,
typename Palette>
360 bool pointy_top =
true)
362 std::ostringstream oss;
Exception handling system with formatted messages for Aleph-w.
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.
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.
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.
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.
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
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
C++20 concepts for the Cellular Automata module.
Cellular automata lattice with pluggable boundary policies.