src/config/materials.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/materials.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/material.h" | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | namespace terraingen::config { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | /// The material classes a terrain's surface can have: soil, bedrock where exposed, dust patches, | ||
| 14 | /// and what its layers lay. | ||
| 15 | 9 | std::vector<MaterialClass> PresentMaterials(const MaterialOptions& options, | |
| 16 | const Terrain& terrain) { | ||
| 17 | 9 | std::vector<MaterialClass> present{MaterialClass::Soil}; | |
| 18 | 9 | if (options.exposure) { | |
| 19 | 4 | present.push_back(MaterialClass::Bedrock); | |
| 20 | } | ||
| 21 | 9 | if (options.dustCover > 0.0) { | |
| 22 | 1 | present.push_back(MaterialClass::Dust); | |
| 23 | } | ||
| 24 | 9 | ForEachLayer<MaterialSource>(terrain, [&](const MaterialSource& source, std::size_t) { | |
| 25 | 5 | present.push_back(source.Material()); | |
| 26 | 5 | }); | |
| 27 | 9 | return present; | |
| 28 | } | ||
| 29 | |||
| 30 | } // namespace | ||
| 31 | |||
| 32 | 10 | MaterialPhotometry BuildPhotometry(const PhotometrySpec& spec) { | |
| 33 | 10 | MaterialPhotometry photometry{}; | |
| 34 | 10 | if (spec.reference == PhotometryReference::MarsPancam) { | |
| 35 | 4 | photometry = MarsPancamPhotometry(); | |
| 36 | } | ||
| 37 | 60 | for (std::size_t k = 0; k < kMaterialClasses; ++k) { | |
| 38 | 50 | if (spec.classes[k]) { | |
| 39 | 8 | photometry[k] = spec.classes[k]; | |
| 40 | } | ||
| 41 | } | ||
| 42 | 10 | return photometry; | |
| 43 | } | ||
| 44 | |||
| 45 | 9 | void CheckMaterials(const MaterialsSpec& materials, bool photometryOutput, const Terrain& terrain) { | |
| 46 | 27 | At("materials", [&] { ValidateMaterialOptions(materials.options); }); | |
| 47 | 9 | const MaterialPhotometry photometry = BuildPhotometry(materials.photometry); | |
| 48 | 27 | At("materials.photometry", [&] { ValidateMaterialPhotometry(photometry); }); | |
| 49 | 9 | if (!photometryOutput) { | |
| 50 | ✗ | return; | |
| 51 | } | ||
| 52 | 22 | for (const MaterialClass material : PresentMaterials(materials.options, terrain)) { | |
| 53 | 19 | const auto index = static_cast<std::size_t>(material); | |
| 54 | 19 | if (!photometry[index]) { | |
| 55 | 12 | throw ConfigError(FieldPath("materials.photometry", kMaterialClassKeys[index]), | |
| 56 | 18 | "required by the photometry output: the surface has some"); | |
| 57 | } | ||
| 58 | 9 | } | |
| 59 | } | ||
| 60 | |||
| 61 | } // namespace terraingen::config | ||
| 62 |