FireSTARR
Loading...
Searching...
No Matches
Cell.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_CELL_H
3#define FS_CELL_H
4#include "stdafx.h"
5#include "Util.h"
6namespace fs
7{
8using SpreadKey = uint32_t;
12class Cell
13{
14protected:
18 SpreadKey data_;
19
20public:
25 [[nodiscard]] constexpr SpreadKey fullHash() const { return data_; }
30 explicit constexpr Cell(const SpreadKey hash) noexcept : data_{hash} { }
38 [[nodiscard]] static constexpr SpreadKey hashCell(
39 const SlopeSize slope,
40 const AspectSize aspect,
41 const FuelCodeSize& fuel
42 ) noexcept
43 {
44 // HACK: so we can call and set it all invalid if anything is
45 // if any are invalid then they all should be
46 const auto do_hash_cell = [](const SlopeSize s, const AspectSize a, const FuelCodeSize f) {
47 return static_cast<SpreadKey>(f) << FuelShift | static_cast<SpreadKey>(s) << SlopeShift
48 | static_cast<SpreadKey>(a) << AspectShift;
49 };
50 if (INVALID_SLOPE == slope || INVALID_ASPECT == aspect || INVALID_FUEL_CODE == fuel)
51 {
52 return do_hash_cell(INVALID_SLOPE, INVALID_ASPECT, INVALID_FUEL_CODE);
53 }
54 // if slope is 0 make aspect north so less unique keys
55 return do_hash_cell(slope, 0 == slope ? 0 : aspect, fuel);
56 }
57 constexpr Cell() noexcept
58 : Cell(
59 numeric_limits<SlopeSize>::min(),
60 numeric_limits<AspectSize>::min(),
61 numeric_limits<FuelCodeSize>::min()
62 )
63 { }
71 constexpr Cell(const SlopeSize slope, const AspectSize aspect, const FuelCodeSize& fuel) noexcept
72 : Cell(hashCell(slope, aspect, fuel))
73 { }
79 [[nodiscard]] static constexpr SpreadKey key(const SpreadKey value) noexcept
80 {
81 // can just shift since these are the only bits left after
82 return static_cast<SpreadKey>(value >> FuelShift);
83 }
89 [[nodiscard]] static constexpr AspectSize aspect(const SpreadKey value) noexcept
90 {
91 return static_cast<AspectSize>((value & AspectMask) >> AspectShift);
92 }
98 [[nodiscard]] static constexpr FuelCodeSize fuelCode(const SpreadKey value) noexcept
99 {
100 return static_cast<FuelCodeSize>((value & FuelMask) >> FuelShift);
101 }
107 [[nodiscard]] static constexpr SlopeSize slope(const SpreadKey value) noexcept
108 {
109 return static_cast<SlopeSize>((value & SlopeMask) >> SlopeShift);
110 }
115 [[nodiscard]] constexpr SpreadKey key() const noexcept { return Cell::key(data_); }
120 [[nodiscard]] constexpr AspectSize aspect() const noexcept { return Cell::aspect(data_); }
125 [[nodiscard]] constexpr FuelCodeSize fuelCode() const noexcept { return Cell::fuelCode(data_); }
130 [[nodiscard]] constexpr SlopeSize slope() const noexcept { return Cell::slope(data_); }
131
132protected:
133 /*
134 * Field Natural Range Used Range Bits Bit Range
135 * Y 0 - 4095 0 - 4095 12 0 - 4095
136 * X 0 - 4095 0 - 4095 12 0 - 4095
137 * PADDING 10
138 * Fuel 0 - 140 0 - 140 8 0 - 255
139 * Aspect 0 - 359 0 - 359 9 0 - 511
140 * Slope 0 - infinity 0 - 511 9 0 - 511
141 * Extra 8
142 *
143 * X and Y are restricted to 4096 since that's what gets clipped out of
144 * the GIS outputs.
145 *
146 * Fuel is tied to how many variations of percent conifer/dead fir we want to use, and
147 * if we want to allow M1/M2/M3/M4 on their own, or just use the ones that are
148 * automatically tied to the green-up.
149 *
150 * Aspect is calculated to be in degrees, so 0 - 359.
151 *
152 * Slope is truncated to 0 - 70 for slope effect calculations speed for slopes,
153 * but keep higher range because there's an issue with this when it tries to calculate the
154 * horizontal rate of spread if the slope has been truncated and the distance
155 * calculated will be wrong.
156 */
160 static constexpr uint32_t FuelShift = 0;
161 // // Need to make sure that fuel, slope & aspect aren't in first 32 bits
162 // static_assert(32 <= FuelShift);
166 static constexpr uint32_t FuelBits = std::bit_width<uint32_t>(NUMBER_OF_FUELS);
170 static constexpr SpreadKey FuelBitMask = bit_mask<FuelBits, SpreadKey>();
171 static_assert(FuelBitMask == 0xFF);
172 static_assert(FuelBitMask >= NUMBER_OF_FUELS);
176 static constexpr SpreadKey FuelMask = FuelBitMask << FuelShift;
180 static constexpr uint32_t AspectShift = FuelBits + FuelShift;
184 static constexpr uint32_t AspectBits = std::bit_width<uint32_t>(MAX_ASPECT);
188 static constexpr SpreadKey AspectBitMask = bit_mask<AspectBits, SpreadKey>();
189 static_assert(AspectBitMask == 0x1FF);
190 static_assert(AspectBitMask >= INVALID_ASPECT);
194 static constexpr SpreadKey AspectMask = AspectBitMask << AspectShift;
198 static constexpr uint32_t SlopeShift = AspectBits + AspectShift;
202 static constexpr uint32_t SlopeBits = std::bit_width<uint32_t>(MAX_SLOPE_FOR_DISTANCE);
203 static_assert(SlopeBits == 9);
207 static constexpr SpreadKey SlopeBitMask = bit_mask<SlopeBits, SpreadKey>();
208 static_assert(SlopeBitMask == 0x1FF);
209 static_assert(SlopeBitMask >= INVALID_SLOPE);
213 static constexpr SpreadKey SlopeMask = SlopeBitMask << SlopeShift;
214 static_assert(
215 static_cast<size_t>(std::bit_width(std::numeric_limits<SpreadKey>::max()))
217 );
218};
219}
220#endif
A Position with a Slope, Aspect, and Fuel.
Definition Cell.h:13
static constexpr uint32_t FuelBits
Number of bits in fuel bitmask.
Definition Cell.h:166
static constexpr AspectSize aspect(const SpreadKey value) noexcept
Aspect (degrees)
Definition Cell.h:89
static constexpr SpreadKey hashCell(const SlopeSize slope, const AspectSize aspect, const FuelCodeSize &fuel) noexcept
Hash attributes into a SpreadKey value.
Definition Cell.h:38
constexpr Cell(const SlopeSize slope, const AspectSize aspect, const FuelCodeSize &fuel) noexcept
Construct based on given attributes.
Definition Cell.h:71
static constexpr SpreadKey key(const SpreadKey value) noexcept
A key defining Slope, Aspect, and Fuel, used for determining Cells that spread the same.
Definition Cell.h:79
static constexpr uint32_t SlopeShift
Shift for slope bitmask.
Definition Cell.h:198
constexpr SpreadKey key() const noexcept
A key defining Slope, Aspect, and Fuel, used for determining Cells that spread the same.
Definition Cell.h:115
static constexpr SpreadKey FuelBitMask
Bitmask for fuel information in SpreadKey before shift.
Definition Cell.h:170
static constexpr uint32_t AspectBits
Number of bits in aspect bitmask.
Definition Cell.h:184
static constexpr SpreadKey SlopeBitMask
Bitmask for slope in SpreadKey before shift.
Definition Cell.h:207
static constexpr SlopeSize slope(const SpreadKey value) noexcept
Slope (degrees)
Definition Cell.h:107
static constexpr SpreadKey AspectBitMask
Bitmask for aspect in SpreadKey before shift.
Definition Cell.h:188
constexpr Cell(const SpreadKey hash) noexcept
Construct from hash value.
Definition Cell.h:30
constexpr SlopeSize slope() const noexcept
Slope (degrees)
Definition Cell.h:130
static constexpr uint32_t SlopeBits
Number of bits in slope bitmask.
Definition Cell.h:202
static constexpr SpreadKey SlopeMask
Bitmask for slope in SpreadKey.
Definition Cell.h:213
SpreadKey data_
Stored hash that contains x and y data.
Definition Cell.h:18
static constexpr uint32_t FuelShift
Shift for fuel bitmask.
Definition Cell.h:160
static constexpr SpreadKey AspectMask
Bitmask for aspect in SpreadKey.
Definition Cell.h:194
static constexpr SpreadKey FuelMask
Bitmask for fuel information in SpreadKey.
Definition Cell.h:176
constexpr SpreadKey fullHash() const
Full stored hash that may contain data from subclasses.
Definition Cell.h:25
static constexpr FuelCodeSize fuelCode(const SpreadKey value) noexcept
Fuel.
Definition Cell.h:98
static constexpr uint32_t AspectShift
Shift for aspect bitmask.
Definition Cell.h:180
constexpr FuelCodeSize fuelCode() const noexcept
Fuel.
Definition Cell.h:125
constexpr AspectSize aspect() const noexcept
Aspect (degrees)
Definition Cell.h:120