Porytiles
Loading...
Searching...
No Matches
canonical_pixel_tile.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <vector>
6
9
10namespace porytiles {
11
37template <SupportsTransparency PixelType>
38class CanonicalPixelTile : public PixelTile<PixelType> {
39 public:
59 explicit CanonicalPixelTile(const PixelTile<PixelType> &tile) : PixelTile<PixelType>{}
60 {
61 // Helper struct to store candidate tiles with their flip flags
62 struct Candidate {
63 PixelTile<PixelType> flipped_tile;
64 bool h_flip;
65 bool v_flip;
66
67 auto operator<=>(const Candidate &other) const
68 {
69 return flipped_tile <=> other.flipped_tile;
70 }
71 };
72
73 std::array flips = {
74 std::pair{false, false}, std::pair{false, true}, std::pair{true, false}, std::pair{true, true}};
75
76 std::vector<Candidate> candidates;
77 candidates.reserve(4);
78
79 for (const auto &[h, v] : flips) {
80 candidates.push_back({tile.flip(h, v), h, v});
81 }
82
83 auto min_candidate = *std::min_element(candidates.begin(), candidates.end());
84
85 // Assign the canonical tile data
86 *static_cast<PixelTile<PixelType> *>(this) = min_candidate.flipped_tile;
87 h_flip_ = min_candidate.h_flip;
88 v_flip_ = min_candidate.v_flip;
89 }
90
110 CanonicalPixelTile(const PixelTile<PixelType> &tile, const PixelType &extrinsic_transparency)
111 requires requires(const PixelType &p) { p.is_transparent(p); }
113 {
114 struct Candidate {
115 PixelTile<PixelType> flipped_tile;
116 bool h_flip;
117 bool v_flip;
118 };
119
120 std::array flips = {
121 std::pair{false, false}, std::pair{false, true}, std::pair{true, false}, std::pair{true, true}};
122
123 std::vector<Candidate> candidates;
124 candidates.reserve(4);
125 for (const auto &[h, v] : flips) {
126 candidates.push_back({tile.flip(h, v), h, v});
127 }
128
129 auto min_it = std::min_element(
130 candidates.begin(), candidates.end(), [&extrinsic_transparency](const Candidate &a, const Candidate &b) {
131 return PixelTile<PixelType>::cross_et_compare(
132 a.flipped_tile, extrinsic_transparency, b.flipped_tile, extrinsic_transparency) < 0;
133 });
134
135 *static_cast<PixelTile<PixelType> *>(this) = min_it->flipped_tile;
136 h_flip_ = min_it->h_flip;
137 v_flip_ = min_it->v_flip;
138 }
139
153 auto operator<=>(const CanonicalPixelTile &other) const = default;
154
164 [[nodiscard]] bool h_flip() const
165 {
166 return h_flip_;
167 }
168
178 [[nodiscard]] bool v_flip() const
179 {
180 return v_flip_;
181 }
182
183 private:
184 bool h_flip_;
185 bool v_flip_;
186};
187
188} // namespace porytiles
A PixelTile representation that stores the canonical (lexicographically minimal) orientation among al...
bool h_flip() const
Returns the horizontal flip flag.
bool v_flip() const
Returns the vertical flip flag.
CanonicalPixelTile(const PixelTile< PixelType > &tile, const PixelType &extrinsic_transparency)
Canonicalizes the input tile's orientation under cross-ET strict weak ordering.
CanonicalPixelTile(const PixelTile< PixelType > &tile)
Constructs a CanonicalPixelTile by finding the canonical orientation of the input tile.
auto operator<=>(const CanonicalPixelTile &other) const =default
Three-way comparison operator that compares all fields in lexicographic order.
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.