Porytiles
Loading...
Searching...
No Matches
canonical_shape_tile.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <compare>
6#include <vector>
7
9
10namespace porytiles2 {
11
43template <typename PixelType>
44class CanonicalShapeTile : public ShapeTile<PixelType> {
45 public:
66 explicit CanonicalShapeTile(const ShapeTile<PixelType> &tile) : ShapeTile<PixelType>{}
67 {
68 // Helper struct to store candidate tiles with their flip flags
69 struct Candidate {
70 ShapeTile<PixelType> flipped_tile;
71 bool h_flip;
72 bool v_flip;
73
74 bool operator<(const Candidate &other) const
75 {
76 return ShapeTile<PixelType>::compare_shape_only(flipped_tile, other.flipped_tile);
77 }
78 };
79
80 std::array flips = {
81 std::pair{false, false}, std::pair{false, true}, std::pair{true, false}, std::pair{true, true}};
82
83 std::vector<Candidate> candidates;
84 candidates.reserve(4);
85
86 for (const auto &[h, v] : flips) {
87 candidates.push_back({tile.flip(h, v), h, v});
88 }
89
90 auto min_candidate = *std::min_element(candidates.begin(), candidates.end());
91
92 // Assign the canonical tile data
93 *static_cast<ShapeTile<PixelType> *>(this) = min_candidate.flipped_tile;
94 h_flip_ = min_candidate.h_flip;
95 v_flip_ = min_candidate.v_flip;
96 }
97
114 auto operator<=>(const CanonicalShapeTile &other) const = default;
115
125 [[nodiscard]] bool h_flip() const
126 {
127 return h_flip_;
128 }
129
139 [[nodiscard]] bool v_flip() const
140 {
141 return v_flip_;
142 }
143
144 private:
145 bool h_flip_;
146 bool v_flip_;
147};
148
149} // namespace porytiles2
A ShapeTile representation that stores the canonical (lexicographically minimal) orientation among al...
bool h_flip() const
Returns the horizontal flip flag.
auto operator<=>(const CanonicalShapeTile &other) const =default
Three-way comparison operator that compares all fields.
CanonicalShapeTile(const ShapeTile< PixelType > &tile)
Constructs a CanonicalShapeTile by finding the canonical orientation of the input tile.
bool v_flip() const
Returns the vertical flip flag.
An 8x8 tile backed by mask-based storage that maps shape regions to pixel values.