FireSTARR
Loading...
Searching...
No Matches
Location.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_LOCATION_H
3#define FS_LOCATION_H
4#include "stdafx.h"
5#include "StrictType.h"
6#include "Util.h"
7namespace fs
8{
9constexpr auto CELL_CENTER = static_cast<InnerSize>(0.5);
10// Number of bits to use for storing one coordinate of Position data
11static constexpr HashSize XYBits = std::bit_width<HashSize>(MAX_HEIGHT - 1);
12static_assert(pow_int<XYBits, size_t>(2) == MAX_HEIGHT);
13static_assert(pow_int<XYBits, size_t>(2) == MAX_WIDTH);
14static constexpr HashSize ColumnMask = bit_mask<XYBits, HashSize>();
15// // Number of bits to use for storing Position data
16// static constexpr HashSize PositionBits = XYBits * 2;
17// // Hash mask for bits being used for Position data
18// static constexpr Topo HashMask = bit_mask<PositionBits, Topo>();
19// static_assert(HashMask >= static_cast<size_t>(MAX_WIDTH) * MAX_HEIGHT - 1);
20// static_assert(HashMask <= std::numeric_limits<HashSize>::max());
21// HACK: use same invalid value as Idx versions
22// decimal cell position in the X direction
23struct XPos : public StrictType<XPos, units::Unitless, XYSize, std::numeric_limits<Idx>::max()>
24{
25 using StrictType::StrictType;
26 auto operator<=>(const XPos& rhs) const = default;
27};
28// decimal cell position in the Y direction
29struct YPos : public StrictType<YPos, units::Unitless, XYSize, std::numeric_limits<Idx>::max()>
30{
31 using StrictType::StrictType;
32 auto operator<=>(const YPos& rhs) const = default;
33};
34struct XIdx : public StrictType<XIdx, units::Unitless, Idx, std::numeric_limits<Idx>::max()>
35{
36 using StrictType::StrictType;
37 explicit constexpr XIdx(const XPos& x) : XIdx(static_cast<Idx>(x.value)) { }
38 auto operator<=>(const XIdx& rhs) const = default;
39};
40struct YIdx : public StrictType<YIdx, units::Unitless, Idx, std::numeric_limits<Idx>::max()>
41{
42 using StrictType::StrictType;
43 explicit constexpr YIdx(const YPos& y) : YIdx(static_cast<Idx>(y.value)) { }
44 auto operator<=>(const YIdx& rhs) const = default;
45};
46// HACK: don't actually have bounds for now
47// The position within the Environment that a spreading point has.
48struct XYPos
49{
50 XPos x{};
51 YPos y{};
52 static constexpr XYPos Invalid() noexcept { return XYPos{XPos::Invalid(), YPos::Invalid()}; }
53 auto operator<=>(const XYPos& rhs) const = default;
54};
55// a point that is within a set of min/max bounds
56template <class S, int XMin, int XMax, int YMin, int YMax>
57struct BoundedPoint;
58// Offset from a position
59using Offset = BoundedPoint<XYSize, -1, 1, -1, 1>;
60// The position within the Environment that a spreading point has.
62template <class S, int XMin, int XMax, int YMin, int YMax>
64{
65 S x{XMin - 1};
66 S y{YMin - 1};
67 auto operator==(const BoundedPoint& rhs) const noexcept { return x == rhs.x && y == rhs.y; }
68 auto operator<=>(const BoundedPoint& rhs) const noexcept
69 {
70 if (auto cmp = x <=> rhs.x; 0 != cmp)
71 {
72 return cmp;
73 }
74 return y <=> rhs.y;
75 }
77 static constexpr auto INVALID_X = XMin - 1;
78 static constexpr auto INVALID_Y = YMin - 1;
79};
80struct XYIdx
81{
82public:
83 // access value for format
84 constexpr inline Idx x_value() const noexcept { return x.value; }
85 constexpr inline Idx y_value() const noexcept { return y.value; }
86 constexpr XYIdx() = default;
87 explicit constexpr XYIdx(const XIdx& x0, const YIdx& y0) : x{x0}, y{y0}
88 {
89#ifdef DEBUG_POINTS
90 // HACK: don't use logging since breaks constexpr
91 if (-1 >= x.value || MAX_WIDTH <= x.value || -1 >= y.value || MAX_HEIGHT <= y.value)
92 {
93 cout << std::format("xy out of bounds: ({}, {})", x.value, y.value);
94 exit(-1);
95 }
96#endif
97 }
98 template <class T>
99 explicit constexpr XYIdx(const T& x0, const T& y0)
100 : XYIdx(XIdx{static_cast<Idx>(x0)}, YIdx{static_cast<Idx>(y0)})
101 { }
102 template <class S, int XMin, int XMax, int YMin, int YMax>
103 explicit constexpr XYIdx(const BoundedPoint<S, XMin, XMax, YMin, YMax>& pt) noexcept
104 : XYIdx(pt.x, pt.y)
105 { }
106 explicit constexpr XYIdx(const XYPos& xy) noexcept : XYIdx(XIdx{xy.x}, YIdx{xy.y}) { }
107 constexpr auto operator==(const XYIdx& rhs) const noexcept { return x == rhs.x && y == rhs.y; }
108 constexpr auto operator<=>(const XYIdx& rhs) const noexcept
109 {
110 // maintain same order as hash was, regardless of how actual object is stored
111 if (const auto cmp = y <=> rhs.y; 0 != cmp)
112 {
113 return cmp;
114 }
115 return x <=> rhs.x;
116 }
117 XYPos operator+(const XYPos& rhs) const noexcept
118 {
119 return XYPos{XPos{rhs.x.value + x.value}, YPos{rhs.y.value + y.value}};
120 }
121 XYPos center() const noexcept { return *this + XYPos{XPos{CELL_CENTER}, YPos{CELL_CENTER}}; }
122 XYIdx operator+(const XYIdx& rhs) const noexcept { return XYIdx{x + rhs.x, y + rhs.y}; }
123 XYIdx operator+(const XIdx& rhs) const noexcept { return XYIdx{x + rhs, y}; }
124 XYIdx operator+(const YIdx& rhs) const noexcept { return XYIdx{x, y + rhs}; }
133 inline constexpr CellIndex relativeIndex(const XYIdx& rhs) const noexcept
134 {
135 constexpr CellIndex DIRECTIONS[9] = {
136 DIRECTION_SW,
137 DIRECTION_S,
138 DIRECTION_SE,
139 DIRECTION_W,
140 DIRECTION_NONE,
141 DIRECTION_E,
142 DIRECTION_NW,
143 DIRECTION_N,
144 DIRECTION_NE
145 };
146 return DIRECTIONS[((x - rhs.x).value + 1) + 3 * ((y - rhs.y).value + 1)];
147 }
148 XIdx x{};
149 YIdx y{};
150};
151inline XYPos operator+(const XYPos& lhs, const XYIdx& rhs) noexcept { return rhs + lhs; }
152static inline constexpr std::pair<XIdx, YIdx> hash_to_xy(const XYIdx& xy) noexcept
153{
154 return {xy.x, xy.y};
155}
156static inline constexpr std::pair<Idx, Idx> hash_to_xy_value(const XYIdx& xy) noexcept
157{
158 return {xy.x_value(), xy.y_value()};
159}
160static inline constexpr size_t to_index(const XYIdx& xy) noexcept
161{
162 // NOTE: do this here since fixed grid is the only reason for this?
163 return (static_cast<HashSize>(xy.y_value()) << XYBits) + static_cast<HashSize>(xy.x_value());
164}
173inline constexpr CellIndex relativeIndex(const XYIdx& src, const XYIdx& dst) noexcept
174{
175 return src.relativeIndex(dst);
176}
177}
178#endif
Definition Location.h:64
A wrapper around a ValueType to ensure correct types are used.
Definition StrictType.h:34
Definition Location.h:35
Definition Location.h:24
Definition Location.h:81
constexpr CellIndex relativeIndex(const XYIdx &rhs) const noexcept
Definition Location.h:133
Definition Location.h:49
Definition Location.h:41
Definition Location.h:30