src/layers/craters/rays.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/rays.h" | ||
| 4 | |||
| 5 | #include "terraingen/geometry/geometry.h" | ||
| 6 | #include "terraingen/layers/craters/crater_fields.h" | ||
| 7 | #include "terraingen/random/stream.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | 67 | void ValidateRayLayout(const RayLayout& layout) { | |
| 16 | 67 | CheckRanges(layout); | |
| 17 | 67 | ValidateValueDistribution(layout.count); | |
| 18 | 67 | ValidateValueDistribution(layout.widthDegrees); | |
| 19 | 67 | if (layout.count.Lowest() < 0.0 || layout.count.Highest() > 64.0) { | |
| 20 | 2 | throw std::invalid_argument("Craters must have 0 to 64 rays"); | |
| 21 | } | ||
| 22 | 65 | if (!(layout.widthDegrees.Lowest() > 0.0 && layout.widthDegrees.Highest() < 180.0)) { | |
| 23 | 3 | throw std::invalid_argument("Ray widths must be in (0, 180) degrees"); | |
| 24 | } | ||
| 25 | 62 | } | |
| 26 | |||
| 27 | 1767 | std::vector<RayDirection> RayDirections(const Crater& crater, const RayLayout& layout) { | |
| 28 | 1767 | const IdWords id = SplitId(crater.id); | |
| 29 | 1767 | RandomStream random(layout.seed, kCraterRayStream, id.low, id.high); | |
| 30 | 1767 | const auto count = static_cast<int>(std::lround(layout.count.Draw(random))); | |
| 31 | 1767 | std::vector<RayDirection> rays; | |
| 32 | 1767 | rays.reserve(static_cast<std::size_t>(std::max(count, 0))); | |
| 33 | 18987 | for (int i = 0; i < count; ++i) { | |
| 34 | 17220 | RayDirection ray; | |
| 35 | 17220 | ray.azimuth = 2.0 * kPi * random.NextDouble(); | |
| 36 | 17220 | ray.sinHalfWidth = std::sin(0.5 * layout.widthDegrees.Draw(random) * kDegree); | |
| 37 | 17220 | ray.key = random.NextU32(); | |
| 38 | 17220 | rays.push_back(ray); | |
| 39 | } | ||
| 40 | 1767 | return rays; | |
| 41 | } | ||
| 42 | |||
| 43 | } // namespace terraingen | ||
| 44 |