| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/// @file |
| 2 |
|
|
/// @brief Craters, boulders and other values as NumPy columns. |
| 3 |
|
|
#pragma once |
| 4 |
|
|
|
| 5 |
|
|
#include "common.h" |
| 6 |
|
|
|
| 7 |
|
|
#include "terraingen/layers/boulders/boulder.h" |
| 8 |
|
|
#include "terraingen/layers/craters/generation/population.h" |
| 9 |
|
|
#include "terraingen/layers/craters/shaping/attributes.h" |
| 10 |
|
|
|
| 11 |
|
|
#include <nanobind/ndarray.h> |
| 12 |
|
|
|
| 13 |
|
|
#include <cstdint> |
| 14 |
|
|
#include <limits> |
| 15 |
|
|
#include <utility> |
| 16 |
|
|
#include <vector> |
| 17 |
|
|
|
| 18 |
|
|
namespace terraingen::python { |
| 19 |
|
|
|
| 20 |
|
|
/// A NumPy array owning a copy of `values`. |
| 21 |
|
918 |
template <typename T> inline nb::object Column(std::vector<T> values) { |
| 22 |
5/5
nanobind::object terraingen::python::Column<double>(std::vector<double, std::allocator<double> >):
✓ Branch 1 taken 637 times.
nanobind::object terraingen::python::Column<float>(std::vector<float, std::allocator<float> >):
✓ Branch 1 taken 1 time.
nanobind::object terraingen::python::Column<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> >):
✓ Branch 1 taken 133 times.
nanobind::object terraingen::python::Column<unsigned int>(std::vector<unsigned int, std::allocator<unsigned int> >):
✓ Branch 1 taken 51 times.
nanobind::object terraingen::python::Column<unsigned long>(std::vector<unsigned long, std::allocator<unsigned long> >):
✓ Branch 1 taken 96 times.
|
918 |
auto* owned = new std::vector<T>(std::move(values)); |
| 23 |
5/5
nanobind::object terraingen::python::Column<double>(std::vector<double, std::allocator<double> >):
✓ Branch 2 taken 637 times.
nanobind::object terraingen::python::Column<float>(std::vector<float, std::allocator<float> >):
✓ Branch 2 taken 1 time.
nanobind::object terraingen::python::Column<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> >):
✓ Branch 2 taken 133 times.
nanobind::object terraingen::python::Column<unsigned int>(std::vector<unsigned int, std::allocator<unsigned int> >):
✓ Branch 2 taken 51 times.
nanobind::object terraingen::python::Column<unsigned long>(std::vector<unsigned long, std::allocator<unsigned long> >):
✓ Branch 2 taken 96 times.
|
918 |
nb::capsule owner(owned, [](void* p) noexcept { delete static_cast<std::vector<T>*>(p); }); |
| 24 |
10/10
nanobind::object terraingen::python::Column<double>(std::vector<double, std::allocator<double> >):
✓ Branch 5 taken 637 times.
✓ Branch 8 taken 637 times.
nanobind::object terraingen::python::Column<float>(std::vector<float, std::allocator<float> >):
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
nanobind::object terraingen::python::Column<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> >):
✓ Branch 5 taken 133 times.
✓ Branch 8 taken 133 times.
nanobind::object terraingen::python::Column<unsigned int>(std::vector<unsigned int, std::allocator<unsigned int> >):
✓ Branch 5 taken 51 times.
✓ Branch 8 taken 51 times.
nanobind::object terraingen::python::Column<unsigned long>(std::vector<unsigned long, std::allocator<unsigned long> >):
✓ Branch 5 taken 96 times.
✓ Branch 8 taken 96 times.
|
2754 |
return nb::cast(nb::ndarray<nb::numpy, T, nb::ndim<1>>(owned->data(), {owned->size()}, owner)); |
| 25 |
|
918 |
} |
| 26 |
|
|
|
| 27 |
|
|
/// The craters as NumPy columns, with their attributes when there is one per crater. |
| 28 |
|
44 |
inline nb::dict CraterTable(const CraterList& craters, |
| 29 |
|
|
const std::vector<CraterAttributes>& attributes = {}) { |
| 30 |
|
44 |
nb::dict table; |
| 31 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["x"] = Column(craters.X()); |
| 32 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["y"] = Column(craters.Y()); |
| 33 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["diameter"] = Column(craters.Diameters()); |
| 34 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["age_gyr"] = Column(craters.AgesGyr()); |
| 35 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["id"] = Column(craters.Ids()); |
| 36 |
4/4
✓ Branch 2 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
|
132 |
table["manual"] = Column(std::vector<std::uint8_t>(craters.Manual())); |
| 37 |
5/6
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 37 times.
✓ Branch 8 taken 7 times.
|
44 |
if (!attributes.empty() && attributes.size() == craters.Size()) { |
| 38 |
|
37 |
std::vector<double> floorDepth, rimHeight, depthRatio, freshness; |
| 39 |
|
37 |
std::vector<std::uint8_t> complex; |
| 40 |
2/2
✓ Branch 4 taken 17492 times.
✓ Branch 5 taken 37 times.
|
17529 |
for (const CraterAttributes& a : attributes) { |
| 41 |
1/1
✓ Branch 1 taken 17492 times.
|
17492 |
floorDepth.push_back(a.floorDepth); |
| 42 |
1/1
✓ Branch 1 taken 17492 times.
|
17492 |
rimHeight.push_back(a.rimHeight); |
| 43 |
1/1
✓ Branch 1 taken 17492 times.
|
17492 |
depthRatio.push_back(a.depthRatio); |
| 44 |
1/1
✓ Branch 1 taken 17492 times.
|
17492 |
freshness.push_back(a.freshness); |
| 45 |
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 17492 times.
✓ Branch 3 taken 17492 times.
|
17492 |
complex.push_back(a.complex ? 1 : 0); |
| 46 |
|
|
} |
| 47 |
3/3
✓ Branch 3 taken 37 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 37 times.
|
111 |
table["floor_depth"] = Column(std::move(floorDepth)); |
| 48 |
3/3
✓ Branch 3 taken 37 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 37 times.
|
111 |
table["rim_height"] = Column(std::move(rimHeight)); |
| 49 |
3/3
✓ Branch 3 taken 37 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 37 times.
|
111 |
table["depth_ratio"] = Column(std::move(depthRatio)); |
| 50 |
3/3
✓ Branch 3 taken 37 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 37 times.
|
111 |
table["freshness"] = Column(std::move(freshness)); |
| 51 |
3/3
✓ Branch 3 taken 37 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 37 times.
|
111 |
table["complex"] = Column(std::move(complex)); |
| 52 |
|
37 |
} |
| 53 |
|
44 |
return table; |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
/// The boulders as NumPy columns. |
| 57 |
|
26 |
inline nb::dict BoulderTable(const std::vector<Boulder>& boulders) { |
| 58 |
|
26 |
std::vector<double> x, y, ground, diameter, height, burial, yaw, lean, wear, tilt, azimuth; |
| 59 |
|
26 |
std::vector<std::uint64_t> id, parent; |
| 60 |
|
26 |
std::vector<std::uint32_t> piece; |
| 61 |
|
26 |
std::vector<std::uint8_t> model, rest; |
| 62 |
2/2
✓ Branch 5 taken 15455 times.
✓ Branch 6 taken 26 times.
|
15481 |
for (const Boulder& boulder : boulders) { |
| 63 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
x.push_back(boulder.x); |
| 64 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
y.push_back(boulder.y); |
| 65 |
1/1
✓ Branch 3 taken 15455 times.
|
15455 |
ground.push_back(boulder.ground.value_or(std::numeric_limits<double>::quiet_NaN())); |
| 66 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
diameter.push_back(boulder.diameter); |
| 67 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
height.push_back(boulder.height); |
| 68 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
burial.push_back(boulder.burial); |
| 69 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
yaw.push_back(boulder.yaw); |
| 70 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
lean.push_back(boulder.lean); |
| 71 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
id.push_back(boulder.id); |
| 72 |
3/3
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 15309 times.
✓ Branch 3 taken 15455 times.
|
15455 |
model.push_back(boulder.model ? 1 : 0); |
| 73 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
wear.push_back(boulder.wear); |
| 74 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
parent.push_back(boulder.parent); |
| 75 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
piece.push_back(boulder.piece); |
| 76 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
rest.push_back(static_cast<std::uint8_t>(boulder.rest)); |
| 77 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
tilt.push_back(boulder.tilt); |
| 78 |
1/1
✓ Branch 1 taken 15455 times.
|
15455 |
azimuth.push_back(boulder.tiltAzimuth); |
| 79 |
|
|
} |
| 80 |
1/1
✓ Branch 1 taken 26 times.
|
26 |
nb::dict table; |
| 81 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["x"] = Column(std::move(x)); |
| 82 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["y"] = Column(std::move(y)); |
| 83 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["ground"] = Column(std::move(ground)); |
| 84 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["diameter"] = Column(std::move(diameter)); |
| 85 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["height"] = Column(std::move(height)); |
| 86 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["burial"] = Column(std::move(burial)); |
| 87 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["yaw"] = Column(std::move(yaw)); |
| 88 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["lean"] = Column(std::move(lean)); |
| 89 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["id"] = Column(std::move(id)); |
| 90 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["model"] = Column(std::move(model)); |
| 91 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["wear"] = Column(std::move(wear)); |
| 92 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["parent"] = Column(std::move(parent)); |
| 93 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["piece"] = Column(std::move(piece)); |
| 94 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["rest"] = Column(std::move(rest)); |
| 95 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["tilt"] = Column(std::move(tilt)); |
| 96 |
3/3
✓ Branch 3 taken 26 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 26 times.
|
78 |
table["tilt_azimuth"] = Column(std::move(azimuth)); |
| 97 |
|
52 |
return table; |
| 98 |
|
26 |
} |
| 99 |
|
|
|
| 100 |
|
|
} // namespace terraingen::python |
| 101 |
|
|
|