52#include <gtest/gtest.h>
70static_assert(LatticeLike<Hex_2D>);
76TEST(CAHexCoord, AxialToCubeSatisfiesXYZZeroSum)
82 EXPECT_EQ(cube.x + cube.y + cube.z, 0);
87TEST(CAHexCoord, AxialOffsetEvenRRoundTrip)
97TEST(CAHexCoord, AxialOffsetOddRRoundTrip)
107TEST(CAHexCoord, HexDistanceMatchesNeighbourhoodRadius)
113 const Hex_Axial n{origin.
q + off[0], origin.r + off[1]};
121TEST(CAHexCoord, PixelCentresAreUniquePerCell)
123 std::set<std::pair<long long, long long>> seen;
128 const auto key = std::pair<long long, long long>(
129 static_cast<long long>(px[0] * 1e6),
130 static_cast<long long>(px[1] * 1e6));
131 EXPECT_TRUE(seen.insert(key).second)
132 <<
"duplicate pixel for (q=" << q <<
", r=" <<
r <<
")";
140TEST(CAHexLattice, AxialAccessorsReadAndWrite)
149 EXPECT_EQ(lat.at_axial(
Hex_Axial{q, r}), q +
r * 10);
152TEST(CAHexLattice, OffsetAccessorsRoundTripEvenR)
158 EXPECT_EQ(lat.at_axial(
Hex_Axial{1, 1}), 7);
161TEST(CAHexLattice, BoundaryAwareAtSafeOnOpenBoundary)
164 EXPECT_EQ(lat.at_axial_safe(
Hex_Axial{-1, 0}), 0);
165 EXPECT_EQ(lat.at_axial_safe(
Hex_Axial{0, 0}), 5);
174struct Bays_B2_S34_Functor
176 template <
typename State>
177 [[nodiscard]]
constexpr State operator()(
const State ¤t,
178 const std::size_t alive)
const noexcept
180 const bool alive_now =
current != State{};
181 const bool next_alive = (alive_now
and (alive == 3 or alive == 4))
182 or (not alive_now
and alive == 2);
183 return next_alive ?
static_cast<State
>(1) : static_cast<State>(0);
189TEST(CAHexLatticeEngine, OneIsolatedCellDiesByB2S34)
201 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{2, 2}), 0);
205 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{q, r}), 0);
208TEST(CAHexLatticeEngine, PairOfAdjacentCellsBirthsThirdCell)
226 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{2, 2}), 0);
227 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{3, 2}), 0);
230 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{3, 1}), 1);
231 EXPECT_EQ(eng.frame().at_axial(
Hex_Axial{2, 3}), 1);
238TEST(CAHexLatticeTikz, ProducesTikzPictureWithExpectedFragments)
243 lat, [](
int v) {
return v != 0 ? std::string(
"black") : std::string(
"white"); });
244 EXPECT_NE(
out.find(
"\\begin{tikzpicture}"), std::string::npos);
245 EXPECT_NE(
out.find(
"\\end{tikzpicture}"), std::string::npos);
246 EXPECT_NE(
out.find(
"regular polygon sides=6"), std::string::npos);
247 EXPECT_NE(
out.find(
"fill=black"), std::string::npos);
248 EXPECT_NE(
out.find(
"fill=white"), std::string::npos);
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
Hexagonal lattice with axial accessors over a 2D storage.
void set_axial(Hex_Axial a, const state_type &v)
Write cell value at axial coords (q, r) (strict).
void set_offset_even_r(Hex_Offset o, const state_type &v)
Convenience: write using the even-r offset convention.
Six-neighbour hex pattern in axial coordinates over a 2D lattice.
static constexpr std::array< Offset_Vec< 2 >, 6 > offsets
The six axial-coordinate offsets of the hex neighborhood.
Rule whose next state depends on (current, alive_count).
Synchronous double-buffered engine.
void run(const std::size_t steps)
Run several synchronous steps.
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.
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).
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.
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Axial integer coordinates (q, r) of a hex cell.
Offset coordinates (col, row) for visualisation.
Out-of-range neighbours behave as if the lattice ended.
The lattice wraps around on every axis.
C++20 concepts for the Cellular Automata module.
Synchronous double-buffered engine for cellular automata.
Hexagonal lattice with axial / offset / cube coordinate conversions and a TikZ-compatible pixel mappi...
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).