src/layers/boulders/boulder.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder.h" | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <limits> | ||
| 7 | #include <stdexcept> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | 1180 | template <typename T> void AppendColumn(std::vector<T>& column, const std::vector<T>& other) { | |
| 15 | 1180 | column.insert(column.end(), other.begin(), other.end()); | |
| 16 | 1180 | } | |
| 17 | |||
| 18 | /// Whether two columns of doubles hold the same values, NaN equal to NaN. | ||
| 19 | 23 | bool SameDoubles(const std::vector<double>& a, const std::vector<double>& b) { | |
| 20 | 23 | if (a.size() != b.size()) { | |
| 21 | return false; | ||
| 22 | } | ||
| 23 | 99 | for (std::size_t i = 0; i < a.size(); ++i) { | |
| 24 | 77 | if (!(a[i] == b[i] || (std::isnan(a[i]) && std::isnan(b[i])))) { | |
| 25 | return false; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | return true; | ||
| 29 | } | ||
| 30 | |||
| 31 | } // namespace | ||
| 32 | |||
| 33 | 7 | BoulderList::BoulderList(std::initializer_list<Boulder> boulders) { | |
| 34 | 7 | Reserve(boulders.size()); | |
| 35 | 20 | for (const Boulder& boulder : boulders) { | |
| 36 | 13 | Append(boulder); | |
| 37 | } | ||
| 38 | 7 | } | |
| 39 | |||
| 40 | 62428 | void BoulderList::Reserve(std::size_t count) { | |
| 41 | 62428 | m_x.reserve(count); | |
| 42 | 62428 | m_y.reserve(count); | |
| 43 | 62428 | m_diameter.reserve(count); | |
| 44 | 62428 | m_height.reserve(count); | |
| 45 | 62428 | m_burial.reserve(count); | |
| 46 | 62428 | m_yaw.reserve(count); | |
| 47 | 62428 | m_lean.reserve(count); | |
| 48 | 62428 | m_id.reserve(count); | |
| 49 | 62428 | m_rest.reserve(count); | |
| 50 | 62428 | m_tilt.reserve(count); | |
| 51 | 62428 | m_tiltAzimuth.reserve(count); | |
| 52 | 62428 | m_ground.reserve(count); | |
| 53 | 62428 | m_model.reserve(count); | |
| 54 | 62428 | m_wear.reserve(count); | |
| 55 | 62428 | m_parent.reserve(count); | |
| 56 | 62428 | m_piece.reserve(count); | |
| 57 | 62428 | m_facets.reserve(count); | |
| 58 | 62428 | m_facetCount.reserve(count); | |
| 59 | 62428 | m_shapeSeed.reserve(count); | |
| 60 | 62428 | m_hasShapeSeed.reserve(count); | |
| 61 | 62428 | } | |
| 62 | |||
| 63 | 396215 | void BoulderList::Append(const Boulder& boulder) { | |
| 64 | 396215 | m_x.push_back(boulder.x); | |
| 65 | 396215 | m_y.push_back(boulder.y); | |
| 66 | 396215 | m_diameter.push_back(boulder.diameter); | |
| 67 | 396215 | m_height.push_back(boulder.height); | |
| 68 | 396215 | m_burial.push_back(boulder.burial); | |
| 69 | 396215 | m_yaw.push_back(boulder.yaw); | |
| 70 | 396215 | m_lean.push_back(boulder.lean); | |
| 71 | 396215 | m_id.push_back(boulder.id); | |
| 72 | 396215 | m_rest.push_back(static_cast<std::uint8_t>(boulder.rest)); | |
| 73 | 396215 | m_tilt.push_back(boulder.tilt); | |
| 74 | 396215 | m_tiltAzimuth.push_back(boulder.tiltAzimuth); | |
| 75 | 619983 | m_ground.push_back(boulder.ground.value_or(std::numeric_limits<double>::quiet_NaN())); | |
| 76 | 786503 | m_model.push_back(boulder.model ? 1 : 0); | |
| 77 | 396215 | m_wear.push_back(boulder.wear); | |
| 78 | 396215 | m_parent.push_back(boulder.parent); | |
| 79 | 396215 | m_piece.push_back(boulder.piece); | |
| 80 | 396215 | m_facets.push_back(boulder.facets); | |
| 81 | 396215 | m_facetCount.push_back(boulder.facetCount); | |
| 82 | 396231 | m_shapeSeed.push_back(boulder.shapeSeed.value_or(0)); | |
| 83 | 792414 | m_hasShapeSeed.push_back(boulder.shapeSeed ? 1 : 0); | |
| 84 | 396215 | } | |
| 85 | |||
| 86 | 59 | void BoulderList::Append(const BoulderList& other) { | |
| 87 | 59 | AppendColumn(m_x, other.m_x); | |
| 88 | 59 | AppendColumn(m_y, other.m_y); | |
| 89 | 59 | AppendColumn(m_diameter, other.m_diameter); | |
| 90 | 59 | AppendColumn(m_height, other.m_height); | |
| 91 | 59 | AppendColumn(m_burial, other.m_burial); | |
| 92 | 59 | AppendColumn(m_yaw, other.m_yaw); | |
| 93 | 59 | AppendColumn(m_lean, other.m_lean); | |
| 94 | 59 | AppendColumn(m_id, other.m_id); | |
| 95 | 59 | AppendColumn(m_rest, other.m_rest); | |
| 96 | 59 | AppendColumn(m_tilt, other.m_tilt); | |
| 97 | 59 | AppendColumn(m_tiltAzimuth, other.m_tiltAzimuth); | |
| 98 | 59 | AppendColumn(m_ground, other.m_ground); | |
| 99 | 59 | AppendColumn(m_model, other.m_model); | |
| 100 | 59 | AppendColumn(m_wear, other.m_wear); | |
| 101 | 59 | AppendColumn(m_parent, other.m_parent); | |
| 102 | 59 | AppendColumn(m_piece, other.m_piece); | |
| 103 | 59 | AppendColumn(m_facets, other.m_facets); | |
| 104 | 59 | AppendColumn(m_facetCount, other.m_facetCount); | |
| 105 | 59 | AppendColumn(m_shapeSeed, other.m_shapeSeed); | |
| 106 | 59 | AppendColumn(m_hasShapeSeed, other.m_hasShapeSeed); | |
| 107 | 59 | } | |
| 108 | |||
| 109 | 353441 | Boulder BoulderList::At(std::size_t index) const { | |
| 110 | 353441 | if (index >= Size()) { | |
| 111 | 3 | throw std::out_of_range("Boulder index " + std::to_string(index) + " is out of range for " + | |
| 112 | 4 | std::to_string(Size()) + " boulders"); | |
| 113 | } | ||
| 114 | 353440 | Boulder boulder; | |
| 115 | 353440 | boulder.x = m_x[index]; | |
| 116 | 353440 | boulder.y = m_y[index]; | |
| 117 | 353440 | boulder.diameter = m_diameter[index]; | |
| 118 | 353440 | boulder.height = m_height[index]; | |
| 119 | 353440 | boulder.burial = m_burial[index]; | |
| 120 | 353440 | boulder.yaw = m_yaw[index]; | |
| 121 | 353440 | boulder.lean = m_lean[index]; | |
| 122 | 353440 | boulder.id = m_id[index]; | |
| 123 | 353440 | boulder.rest = static_cast<BoulderRest>(m_rest[index]); | |
| 124 | 353440 | boulder.tilt = m_tilt[index]; | |
| 125 | 353440 | boulder.tiltAzimuth = m_tiltAzimuth[index]; | |
| 126 | 353440 | if (!std::isnan(m_ground[index])) { | |
| 127 | 201078 | boulder.ground = m_ground[index]; | |
| 128 | } | ||
| 129 | 353440 | boulder.model = m_model[index] != 0; | |
| 130 | 353440 | boulder.wear = m_wear[index]; | |
| 131 | 353440 | boulder.parent = m_parent[index]; | |
| 132 | 353440 | boulder.piece = m_piece[index]; | |
| 133 | 353440 | boulder.facets = m_facets[index]; | |
| 134 | 353440 | boulder.facetCount = m_facetCount[index]; | |
| 135 | 353440 | if (m_hasShapeSeed[index] != 0) { | |
| 136 | 9 | boulder.shapeSeed = m_shapeSeed[index]; | |
| 137 | } | ||
| 138 | 353440 | return boulder; | |
| 139 | } | ||
| 140 | |||
| 141 | 3 | bool BoulderList::operator==(const BoulderList& other) const { | |
| 142 | 2 | return SameDoubles(m_x, other.m_x) && SameDoubles(m_y, other.m_y) && | |
| 143 | 2 | SameDoubles(m_diameter, other.m_diameter) && SameDoubles(m_height, other.m_height) && | |
| 144 | 2 | SameDoubles(m_burial, other.m_burial) && SameDoubles(m_yaw, other.m_yaw) && | |
| 145 | 2 | SameDoubles(m_lean, other.m_lean) && m_id == other.m_id && m_rest == other.m_rest && | |
| 146 | 2 | SameDoubles(m_tilt, other.m_tilt) && SameDoubles(m_tiltAzimuth, other.m_tiltAzimuth) && | |
| 147 | 2 | SameDoubles(m_ground, other.m_ground) && m_model == other.m_model && | |
| 148 | 2 | SameDoubles(m_wear, other.m_wear) && m_parent == other.m_parent && | |
| 149 | 2 | m_piece == other.m_piece && m_facets == other.m_facets && | |
| 150 | 5 | m_facetCount == other.m_facetCount && m_shapeSeed == other.m_shapeSeed && | |
| 151 | 2 | m_hasShapeSeed == other.m_hasShapeSeed; | |
| 152 | } | ||
| 153 | |||
| 154 | } // namespace terraingen | ||
| 155 |