include/terraingen/layers/boulders/boulder.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `Boulder`, the record of one placed boulder, `BoulderList` of many, and `ManualBoulder`, | ||
| 5 | /// one placed by hand. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/geometry/geometry.h" | ||
| 9 | |||
| 10 | #include <array> | ||
| 11 | #include <cmath> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <cstdint> | ||
| 14 | #include <initializer_list> | ||
| 15 | #include <limits> | ||
| 16 | #include <optional> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | /// Which of its axes a boulder stands on end along, the one turned to the vertical. | ||
| 22 | enum class BoulderRest : std::uint8_t { | ||
| 23 | Face, ///< On its broadest face: its short axis up. | ||
| 24 | Side, ///< On a side: its intermediate axis up. | ||
| 25 | End, ///< On an end: its long axis up. | ||
| 26 | }; | ||
| 27 | |||
| 28 | /// A plane that sand-laden wind has cut across a boulder's windward side: a ventifact facet. | ||
| 29 | /// Rock above the plane is gone. | ||
| 30 | struct BoulderFacet { | ||
| 31 | /// Direction the facet faces (upwind), radians counter-clockwise from east. | ||
| 32 | double azimuth = 0.0; | ||
| 33 | double angle = 0.0; ///< Slope of the facet from horizontal, radians, in (0, 90) deg. | ||
| 34 | /// Distance (m) toward `azimuth` from the boulder's center at which the facet meets the surface | ||
| 35 | /// the boulder stands on. | ||
| 36 | double reach = 0.0; | ||
| 37 | |||
| 38 | /// The facet's height (m) above that surface at east and north `offset` meters from the | ||
| 39 | /// boulder's center, negative beyond `reach`. | ||
| 40 | 84695 | double HeightAt(const Vec2d& offset) const { | |
| 41 | 84695 | const double along = offset.x * std::cos(azimuth) + offset.y * std::sin(azimuth); | |
| 42 | 84695 | return (reach - along) * std::tan(angle); | |
| 43 | } | ||
| 44 | |||
| 45 | 19784 | bool operator==(const BoulderFacet& other) const { | |
| 46 | 19784 | return azimuth == other.azimuth && angle == other.angle && reach == other.reach; | |
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | /// One boulder: where it stands, how large it is and how it lies, in surface coordinates | ||
| 51 | /// and meters. | ||
| 52 | struct Boulder { | ||
| 53 | double x = 0.0; ///< Surface x of its center. | ||
| 54 | double y = 0.0; ///< Surface y of its center. | ||
| 55 | double diameter = 0.0; ///< Length of its long axis: its largest horizontal extent on its face. | ||
| 56 | /// Length of its short axis, buried part included: its full height resting on its face. | ||
| 57 | double height = 0.0; | ||
| 58 | double burial = 0.0; ///< Share of its vertical extent below the surface, in [0, 1). | ||
| 59 | double yaw = 0.0; ///< Rotation about the vertical, radians counter-clockwise from east. | ||
| 60 | double lean = 0.0; ///< Tilt of the boulder's vertical axis from the vertical, radians. | ||
| 61 | std::uint64_t id = 0; ///< Stable ID, keyed like crater IDs. | ||
| 62 | BoulderRest rest = BoulderRest::Face; ///< Which axis it stands along. | ||
| 63 | double tilt = 0.0; ///< Angle of its up axis from the vertical, after lean, radians. | ||
| 64 | double tiltAzimuth = 0.0; ///< Direction its up axis tips toward, radians from east. | ||
| 65 | /// Height of the ground beneath its center before any boulders, when the layer knows it. | ||
| 66 | std::optional<double> ground{}; | ||
| 67 | /// Placed as a 3D model of its own rather than drawn into the heights | ||
| 68 | /// (`BoulderModelThreshold`). | ||
| 69 | bool model = false; | ||
| 70 | /// How worn it is, from 0 (fresh) to 1: its crater's wear for a crater block, uniform between | ||
| 71 | /// craters. | ||
| 72 | double wear = 0.0; | ||
| 73 | /// For a fragment, the ID of the boulder it broke from; 0 for a whole boulder. | ||
| 74 | std::uint64_t parent = 0; | ||
| 75 | std::uint32_t piece = 0; ///< For a fragment, which piece of its parent it is. | ||
| 76 | /// Its ventifact facets, the first `facetCount` of them (`BoulderVentifacts`). | ||
| 77 | std::array<BoulderFacet, 2> facets{}; | ||
| 78 | std::uint8_t facetCount = 0; ///< How many facets it has, 0 to 2. | ||
| 79 | /// The seed of its shape; unset derives it from its ID, or its parent's for a fragment | ||
| 80 | /// (`BoulderShapeSeed`). | ||
| 81 | std::optional<std::uint64_t> shapeSeed{}; | ||
| 82 | |||
| 83 | /// Height of the top above the surface it stands on, `height` (1 - `burial`), in meters. | ||
| 84 | 593246 | double ExposedHeight() const { | |
| 85 | 593246 | return height * (1.0 - burial); | |
| 86 | } | ||
| 87 | |||
| 88 | /// `top`, a height (m) of the boulder above the surface at east and north `offset` meters from | ||
| 89 | /// its center, lowered to its facets and not below 0. | ||
| 90 | 321548 | double CutByFacets(const Vec2d& offset, double top) const { | |
| 91 | 389622 | for (std::uint8_t k = 0; k < facetCount; ++k) { | |
| 92 | 68074 | top = std::fmin(top, facets[k].HeightAt(offset)); | |
| 93 | } | ||
| 94 | 321548 | return std::fmax(top, 0.0); | |
| 95 | } | ||
| 96 | |||
| 97 | 9885 | bool operator==(const Boulder& other) const { | |
| 98 | 9885 | return x == other.x && y == other.y && diameter == other.diameter && | |
| 99 | 9885 | height == other.height && burial == other.burial && yaw == other.yaw && | |
| 100 | 9885 | lean == other.lean && id == other.id && rest == other.rest && tilt == other.tilt && | |
| 101 | 19770 | tiltAzimuth == other.tiltAzimuth && ground == other.ground && model == other.model && | |
| 102 | 9885 | wear == other.wear && parent == other.parent && piece == other.piece && | |
| 103 | 19770 | facets == other.facets && facetCount == other.facetCount && | |
| 104 | 9885 | shapeSeed == other.shapeSeed; | |
| 105 | } | ||
| 106 | }; | ||
| 107 | |||
| 108 | /// Boulders stored as one array per field, as `CraterList` stores craters, so each column can be | ||
| 109 | /// shared with NumPy or a GPU without copying. | ||
| 110 | class BoulderList { | ||
| 111 | public: | ||
| 112 | /// An empty list. | ||
| 113 | 62488 | BoulderList() = default; | |
| 114 | |||
| 115 | /// A list of `boulders`, in order. | ||
| 116 | BoulderList(std::initializer_list<Boulder> boulders); | ||
| 117 | |||
| 118 | /// The number of boulders. | ||
| 119 | 769394 | std::size_t Size() const { | |
| 120 | 769383 | return m_id.size(); | |
| 121 | } | ||
| 122 | |||
| 123 | /// Whether the list holds no boulder. | ||
| 124 | 6 | bool Empty() const { | |
| 125 | 6 | return m_id.empty(); | |
| 126 | } | ||
| 127 | |||
| 128 | /// Reserves room for `count` boulders in every column. | ||
| 129 | void Reserve(std::size_t count); | ||
| 130 | |||
| 131 | /// Appends a boulder. | ||
| 132 | void Append(const Boulder& boulder); | ||
| 133 | |||
| 134 | /// Appends every boulder of `other`, in its order. | ||
| 135 | void Append(const BoulderList& other); | ||
| 136 | |||
| 137 | /// The boulder at `index`. | ||
| 138 | /// @throws std::out_of_range if `index` >= `Size()`. | ||
| 139 | Boulder At(std::size_t index) const; | ||
| 140 | |||
| 141 | /// Memberwise equality, column by column. | ||
| 142 | bool operator==(const BoulderList& other) const; | ||
| 143 | 1 | bool operator!=(const BoulderList& other) const { | |
| 144 | 1 | return !(*this == other); | |
| 145 | } | ||
| 146 | |||
| 147 | /// Every east coordinate, in surface coordinates. | ||
| 148 | 33 | const std::vector<double>& X() const { | |
| 149 | 33 | return m_x; | |
| 150 | } | ||
| 151 | /// Every north coordinate, in surface coordinates. | ||
| 152 | 32 | const std::vector<double>& Y() const { | |
| 153 | 32 | return m_y; | |
| 154 | } | ||
| 155 | /// Every diameter, in meters. | ||
| 156 | 33 | const std::vector<double>& Diameters() const { | |
| 157 | 40 | return m_diameter; | |
| 158 | } | ||
| 159 | /// Every height, in meters. | ||
| 160 | 32 | const std::vector<double>& Heights() const { | |
| 161 | 32 | return m_height; | |
| 162 | } | ||
| 163 | /// Every buried share of the vertical extent, in [0, 1). | ||
| 164 | 32 | const std::vector<double>& Burials() const { | |
| 165 | 32 | return m_burial; | |
| 166 | } | ||
| 167 | /// Every yaw, in radians. | ||
| 168 | 32 | const std::vector<double>& Yaws() const { | |
| 169 | 32 | return m_yaw; | |
| 170 | } | ||
| 171 | /// Every lean, in radians. | ||
| 172 | 32 | const std::vector<double>& Leans() const { | |
| 173 | 32 | return m_lean; | |
| 174 | } | ||
| 175 | /// Every stable ID. | ||
| 176 | 34 | const std::vector<std::uint64_t>& Ids() const { | |
| 177 | 34 | return m_id; | |
| 178 | } | ||
| 179 | /// Every `BoulderRest` as a byte. | ||
| 180 | 33 | const std::vector<std::uint8_t>& Rests() const { | |
| 181 | 33 | return m_rest; | |
| 182 | } | ||
| 183 | /// Every tilt, in radians. | ||
| 184 | 32 | const std::vector<double>& Tilts() const { | |
| 185 | 32 | return m_tilt; | |
| 186 | } | ||
| 187 | /// Every tilt azimuth, in radians. | ||
| 188 | 32 | const std::vector<double>& TiltAzimuths() const { | |
| 189 | 32 | return m_tiltAzimuth; | |
| 190 | } | ||
| 191 | /// Every ground height in meters, NaN where unknown. | ||
| 192 | 32 | const std::vector<double>& Grounds() const { | |
| 193 | 32 | return m_ground; | |
| 194 | } | ||
| 195 | /// 1 for boulders placed as 3D models, 0 for the rest. | ||
| 196 | 33 | const std::vector<std::uint8_t>& Models() const { | |
| 197 | 33 | return m_model; | |
| 198 | } | ||
| 199 | /// Every wear, from 0 (fresh) to 1. | ||
| 200 | 32 | const std::vector<double>& Wears() const { | |
| 201 | 32 | return m_wear; | |
| 202 | } | ||
| 203 | /// Every parent ID, 0 for a whole boulder. | ||
| 204 | 32 | const std::vector<std::uint64_t>& Parents() const { | |
| 205 | 32 | return m_parent; | |
| 206 | } | ||
| 207 | /// Every piece index among its parent's pieces. | ||
| 208 | 32 | const std::vector<std::uint32_t>& Pieces() const { | |
| 209 | 32 | return m_piece; | |
| 210 | } | ||
| 211 | /// Every boulder's facets; the first `FacetCounts()` of each are used. | ||
| 212 | const std::vector<std::array<BoulderFacet, 2>>& Facets() const { | ||
| 213 | return m_facets; | ||
| 214 | } | ||
| 215 | /// Every boulder's number of facets in use. | ||
| 216 | 1 | const std::vector<std::uint8_t>& FacetCounts() const { | |
| 217 | 1 | return m_facetCount; | |
| 218 | } | ||
| 219 | /// Every shape seed, 0 where unset (see `HasShapeSeeds`). | ||
| 220 | const std::vector<std::uint64_t>& ShapeSeeds() const { | ||
| 221 | return m_shapeSeed; | ||
| 222 | } | ||
| 223 | /// 1 where a boulder has a shape seed, 0 where it derives one from its ID. | ||
| 224 | 1 | const std::vector<std::uint8_t>& HasShapeSeeds() const { | |
| 225 | 1 | return m_hasShapeSeed; | |
| 226 | } | ||
| 227 | |||
| 228 | private: | ||
| 229 | std::vector<double> m_x; | ||
| 230 | std::vector<double> m_y; | ||
| 231 | std::vector<double> m_diameter; | ||
| 232 | std::vector<double> m_height; | ||
| 233 | std::vector<double> m_burial; | ||
| 234 | std::vector<double> m_yaw; | ||
| 235 | std::vector<double> m_lean; | ||
| 236 | std::vector<std::uint64_t> m_id; | ||
| 237 | std::vector<std::uint8_t> m_rest; | ||
| 238 | std::vector<double> m_tilt; | ||
| 239 | std::vector<double> m_tiltAzimuth; | ||
| 240 | std::vector<double> m_ground; | ||
| 241 | std::vector<std::uint8_t> m_model; | ||
| 242 | std::vector<double> m_wear; | ||
| 243 | std::vector<std::uint64_t> m_parent; | ||
| 244 | std::vector<std::uint32_t> m_piece; | ||
| 245 | std::vector<std::array<BoulderFacet, 2>> m_facets; | ||
| 246 | std::vector<std::uint8_t> m_facetCount; | ||
| 247 | std::vector<std::uint64_t> m_shapeSeed; | ||
| 248 | std::vector<std::uint8_t> m_hasShapeSeed; | ||
| 249 | }; | ||
| 250 | |||
| 251 | /// A boulder placed by hand: its center and size, and any of its pose and shape fixed; unset | ||
| 252 | /// fields are drawn as for generated boulders. Set fields by name with the chainable setters: | ||
| 253 | /// | ||
| 254 | /// ManualBoulder(0.5, 0.5, 1.8).Height(1.1).Burial(0.2).YawDegrees(30.0).Model(true) | ||
| 255 | struct ManualBoulder { | ||
| 256 | double x = 0.0; ///< Surface x of its center. | ||
| 257 | double y = 0.0; ///< Surface y of its center. | ||
| 258 | double diameter = 0.0; ///< Length of its long axis (m). | ||
| 259 | /// Stable ID; unset derives one from position and size (`ManualCraterId`). | ||
| 260 | std::optional<std::uint64_t> id{}; | ||
| 261 | /// Vertical extent (m), buried part included; unset draws it from the pose options. | ||
| 262 | std::optional<double> height{}; | ||
| 263 | std::optional<double> burial{}; ///< Buried share of the height, in [0, 1); unset draws it. | ||
| 264 | /// Rotation about the vertical, degrees counter-clockwise from east; unset draws it. | ||
| 265 | std::optional<double> yawDegrees{}; | ||
| 266 | /// Tilt of its vertical axis from the vertical, degrees; unset draws it. | ||
| 267 | std::optional<double> leanDegrees{}; | ||
| 268 | /// Angle of its up axis from the vertical after lean, degrees; unset draws it. | ||
| 269 | std::optional<double> tiltDegrees{}; | ||
| 270 | /// Direction its up axis tips toward, degrees counter-clockwise from east; unset draws it. | ||
| 271 | std::optional<double> tiltAzimuthDegrees{}; | ||
| 272 | std::optional<double> wear{}; ///< How worn it is, from 0 (fresh) to 1; unset draws it. | ||
| 273 | std::optional<BoulderRest> rest{}; ///< Which axis it stands along; unset draws it. | ||
| 274 | /// The seed of its shape (`GenerateBoulderShape`); unset derives it from its ID. | ||
| 275 | std::optional<std::uint64_t> shapeSeed{}; | ||
| 276 | /// Whether it is a 3D model rather than relief in the heights; unset follows the layer's | ||
| 277 | /// `BoulderRendering::models` threshold. | ||
| 278 | std::optional<bool> model{}; | ||
| 279 | |||
| 280 | 34 | ManualBoulder() = default; | |
| 281 | /// A boulder `length` meters long centered at surface (`centerX`, `centerY`), the rest drawn. | ||
| 282 | 30 | ManualBoulder(double centerX, double centerY, double length) | |
| 283 | 30 | : x(centerX), y(centerY), diameter(length) {} | |
| 284 | |||
| 285 | /// Sets `id`; this and the setters below return the boulder for chaining. | ||
| 286 | 1 | ManualBoulder& Id(std::uint64_t value) { | |
| 287 | 1 | id = value; | |
| 288 | 1 | return *this; | |
| 289 | } | ||
| 290 | /// Sets `height`. | ||
| 291 | 8 | ManualBoulder& Height(double meters) { | |
| 292 | 8 | height = meters; | |
| 293 | 8 | return *this; | |
| 294 | } | ||
| 295 | /// Sets `burial`. | ||
| 296 | 11 | ManualBoulder& Burial(double share) { | |
| 297 | 11 | burial = share; | |
| 298 | 11 | return *this; | |
| 299 | } | ||
| 300 | /// Sets `yawDegrees`. | ||
| 301 | 8 | ManualBoulder& YawDegrees(double degrees) { | |
| 302 | 8 | yawDegrees = degrees; | |
| 303 | 8 | return *this; | |
| 304 | } | ||
| 305 | /// Sets `leanDegrees`. | ||
| 306 | 7 | ManualBoulder& LeanDegrees(double degrees) { | |
| 307 | 7 | leanDegrees = degrees; | |
| 308 | 7 | return *this; | |
| 309 | } | ||
| 310 | /// Sets `tiltDegrees`. | ||
| 311 | 7 | ManualBoulder& TiltDegrees(double degrees) { | |
| 312 | 7 | tiltDegrees = degrees; | |
| 313 | 7 | return *this; | |
| 314 | } | ||
| 315 | /// Sets `tiltAzimuthDegrees`. | ||
| 316 | 1 | ManualBoulder& TiltAzimuthDegrees(double degrees) { | |
| 317 | 1 | tiltAzimuthDegrees = degrees; | |
| 318 | 1 | return *this; | |
| 319 | } | ||
| 320 | /// Sets `wear`. | ||
| 321 | 7 | ManualBoulder& Wear(double value) { | |
| 322 | 7 | wear = value; | |
| 323 | 7 | return *this; | |
| 324 | } | ||
| 325 | /// Sets `rest`. | ||
| 326 | 2 | ManualBoulder& Rest(BoulderRest value) { | |
| 327 | 2 | rest = value; | |
| 328 | 2 | return *this; | |
| 329 | } | ||
| 330 | /// Sets `shapeSeed`. | ||
| 331 | 2 | ManualBoulder& ShapeSeed(std::uint64_t value) { | |
| 332 | 2 | shapeSeed = value; | |
| 333 | 2 | return *this; | |
| 334 | } | ||
| 335 | /// Sets `model`. | ||
| 336 | 7 | ManualBoulder& Model(bool value) { | |
| 337 | 7 | model = value; | |
| 338 | 7 | return *this; | |
| 339 | } | ||
| 340 | |||
| 341 | /// Whether any of its lean, tilt, tilt azimuth or rest is fixed. | ||
| 342 | 18199 | bool FixesOrientation() const { | |
| 343 | 18181 | return leanDegrees || tiltDegrees || tiltAzimuthDegrees || rest; | |
| 344 | } | ||
| 345 | |||
| 346 | /// Whether any of its pose, shape or model choice is fixed. | ||
| 347 | 84 | bool FixesAny() const { | |
| 348 | 148 | return FixesOrientation() || height || burial || yawDegrees || wear || shapeSeed || model; | |
| 349 | } | ||
| 350 | |||
| 351 | 10 | bool operator==(const ManualBoulder& other) const { | |
| 352 | 9 | return x == other.x && y == other.y && diameter == other.diameter && id == other.id && | |
| 353 | 25 | height == other.height && burial == other.burial && yawDegrees == other.yawDegrees && | |
| 354 | 16 | leanDegrees == other.leanDegrees && tiltDegrees == other.tiltDegrees && | |
| 355 | 16 | tiltAzimuthDegrees == other.tiltAzimuthDegrees && wear == other.wear && | |
| 356 | 34 | rest == other.rest && shapeSeed == other.shapeSeed && model == other.model; | |
| 357 | } | ||
| 358 | 2 | bool operator!=(const ManualBoulder& other) const { | |
| 359 | 2 | return !(*this == other); | |
| 360 | } | ||
| 361 | }; | ||
| 362 | |||
| 363 | } // namespace terraingen | ||
| 364 |