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 porytiles2 {
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
104 auto operator<=>(const CanonicalPixelTile &other) const = default;
105
115 [[nodiscard]] bool h_flip() const
116 {
117 return h_flip_;
118 }
119
129 [[nodiscard]] bool v_flip() const
130 {
131 return v_flip_;
132 }
133
134 private:
135 bool h_flip_;
136 bool v_flip_;
137};
138
139} // namespace porytiles2
A PixelTile representation that stores the canonical (lexicographically minimal) orientation among al...
bool v_flip() const
Returns the vertical flip flag.
CanonicalPixelTile(const PixelTile< PixelType > &tile)
Constructs a CanonicalPixelTile by finding the canonical orientation of the input tile.
bool h_flip() const
Returns the horizontal flip flag.
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.