Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_triangular_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
56#ifndef TPL_CA_TRIANGULAR_LATTICE_H
57#define TPL_CA_TRIANGULAR_LATTICE_H
58
59#include <array>
60#include <cstddef>
61#include <ostream>
62#include <sstream>
63#include <string>
64#include <utility>
65
66#include <ah-errors.H>
67
68#include <ca-traits.H>
69#include <tpl_ca_concepts.H>
70#include <tpl_ca_lattice.H>
71
72namespace Aleph {
73namespace CA {
74
85{
88
89 friend constexpr bool operator==(const Tri_Coord & a, const Tri_Coord & b) noexcept
90 {
91 return a.i == b.i and a.j == b.j;
92 }
93 friend constexpr bool operator!=(const Tri_Coord & a, const Tri_Coord & b) noexcept { return not(a == b); }
94};
95
97[[nodiscard]] inline constexpr int tri_parity(const Tri_Coord & c) noexcept
98{
99 using unsigned_t = std::make_unsigned_t<ca_index_t>;
100 return static_cast<int>((static_cast<unsigned_t>(c.i) + static_cast<unsigned_t>(c.j)) & 1u);
101}
102
104[[nodiscard]] inline constexpr bool tri_is_up(const Tri_Coord & c) noexcept
105{
106 return tri_parity(c) == 0;
107}
108
110[[nodiscard]] inline constexpr bool tri_is_down(const Tri_Coord & c) noexcept
111{
112 return tri_parity(c) == 1;
113}
114
127[[nodiscard]] inline std::array<double, 2> tri_pixel_centre(const Tri_Coord & c,
128 const double side) noexcept
129{
130 constexpr double sqrt3 = 1.7320508075688772;
131 const double height = side * sqrt3 / 2.0;
132 const double x = (static_cast<double>(c.j) + 1.0) * (side / 2.0);
133 const double y_base = static_cast<double>(c.i) * height + height / 2.0;
134 const double y = tri_is_up(c) ? y_base - height / 6.0 : y_base + height / 6.0;
135 return {x, y};
136}
137
149template <typename Storage, typename Boundary = OpenBoundary>
150class Triangular_Lattice : public Lattice<Storage, Boundary>
151{
152 static_assert(Storage::rank == 2, "Triangular_Lattice requires a 2D storage");
153
154public:
159 static constexpr std::size_t rank = base_type::rank;
160
161 using base_type::base_type;
162
164 [[nodiscard]] ca_size_t rows() const noexcept { return this->size(0); }
165
167 [[nodiscard]] ca_size_t cols() const noexcept { return this->size(1); }
168
171 {
172 return this->at(coord_type{c.i, c.j});
173 }
174
176 void set_tri(Tri_Coord c, const state_type &v)
177 {
178 this->set(coord_type{c.i, c.j}, v);
179 }
180
183 {
184 return this->at_safe(coord_type{c.i, c.j});
185 }
186};
187
201template <typename Lattice, typename Palette>
202inline void render_triangular_lattice_tikz(std::ostream &os,
203 const Lattice &lat,
205 const double side = 1.0)
206{
207 os << "% Aleph::CA::render_triangular_lattice_tikz\n";
208 os << "\\begin{tikzpicture}[x=1cm, y=-1cm]\n";
209 for (ca_size_t i = 0; i < lat.rows(); ++i)
210 for (ca_size_t j = 0; j < lat.cols(); ++j)
211 {
212 const Tri_Coord c{static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)};
213 const auto px = tri_pixel_centre(c, side);
214 const std::string colour = palette(lat.at_tri(c));
215 const double rotation = tri_is_up(c) ? 0.0 : 180.0;
216 os << " \\node[regular polygon, regular polygon sides=3, draw, fill=" << colour
217 << ", inner sep=0pt, minimum size=" << side
218 << "cm, rotate=" << rotation << "] at (" << px[0] << ", " << px[1] << ") {};\n";
219 }
220 os << "\\end{tikzpicture}\n";
221}
222
226template <typename Lattice, typename Palette>
229 double side = 1.0)
230{
231 std::ostringstream oss;
232 render_triangular_lattice_tikz(oss, lat, std::forward<Palette>(palette), side);
233 return oss.str();
234}
235
236} // namespace CA
237} // namespace Aleph
238
239#endif // TPL_CA_TRIANGULAR_LATTICE_H
Exception handling system with formatted messages for Aleph-w.
Common typedefs and tag types for the Cellular Automata module.
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.
Triangular lattice with parity helpers over a 2D storage.
void set_tri(Tri_Coord c, const state_type &v)
Write cell value at triangular coords (i, j) (strict).
typename base_type::state_type state_type
typename base_type::extents_type extents_type
typename base_type::coord_type coord_type
state_type at_tri_safe(Tri_Coord c) const
Boundary-aware read at triangular coords.
state_type at_tri(Tri_Coord c) const
ca_size_t cols() const noexcept
Number of columns (axis 1).
static constexpr std::size_t rank
ca_size_t rows() const noexcept
< inherit constructors
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 int tri_parity(const Tri_Coord &c) noexcept
std::array< double, 2 > tri_pixel_centre(const Tri_Coord &c, const double side) noexcept
Cartesian centre of a triangle in (i, j) units.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
constexpr bool tri_is_down(const Tri_Coord &c) noexcept
constexpr bool tri_is_up(const Tri_Coord &c) noexcept
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
void render_triangular_lattice_tikz(std::ostream &os, const Lattice &lat, Palette &&palette, const double side=1.0)
Render a triangular frame as a TikZ picture.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Coordinates of a triangular cell as (i, j) integers.
friend constexpr bool operator!=(const Tri_Coord &a, const Tri_Coord &b) noexcept
friend constexpr bool operator==(const Tri_Coord &a, const Tri_Coord &b) noexcept
C++20 concepts for the Cellular Automata module.
Cellular automata lattice with pluggable boundary policies.