./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
8 / 8
100.0%
Branches:
-
-%
Functions:
1 / 1, 2 excl
100.0%

include/terraingen/sources/shape_model_source.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `ShapeModelSource`, a shape-model body's own labels refined by an upsampler.
5 #pragma once
6
7 #include "terraingen/geometry/surface.h"
8 #include "terraingen/sources/source.h"
9 #include "terraingen/sources/upsampling.h"
10
11 #include <array>
12 #include <cstddef>
13 #include <cstdint>
14 #include <memory>
15 #include <optional>
16
17 namespace terraingen {
18
19 /// The heights a shape model's labels give when an upsampler refines them, over the body made
20 /// from it (`Surface::ShapeModel`), whose radius interpolates the same labels bilinearly.
21 ///
22 /// Each of the six ICQ faces is a lattice of (Q + 1)^2 posts, the label radii, in degrees: Q
23 /// intervals span the face's 90 degrees. Posts beyond a face's edge are read from the shape where
24 /// the face's lattice extends onto its neighbors, so kernel upsamplers agree along the edges. Each
25 /// face's posts are numbered apart from the others', so diamond-square displaces each face
26 /// differently, and within half a post of an edge the faces' heights are blended. A height is the
27 /// refined radius less the body's, 0 at every label: the shape gives the labels and the source
28 /// what an upsampler makes between them.
29 class ShapeModelSource final : public Source {
30 public:
31 /// @param body A shape-model body.
32 /// @param upsampler How the labels are refined; null for cubic interpolation.
33 /// @throws std::invalid_argument for a body without a shape, or as `Upsampler::FitTo`.
34 explicit ShapeModelSource(Surface body, std::shared_ptr<const Upsampler> upsampler = nullptr);
35
36 ShapeModelSource(const ShapeModelSource&) = delete;
37 ShapeModelSource& operator=(const ShapeModelSource&) = delete;
38
39 /// A grid without cells over the whole body, its cells the labels' spacing at a face's center.
40 RasterGrid Grid() const override;
41
42 /// The labels' spacing at a face's center, 90 / Q degrees.
43 double ResolutionAt(double x, double y) const override;
44
45 /// The labels' spacing at a face's center, 90 / Q degrees.
46 double PostSpacingAt(double x, double y) const override;
47
48 /// The body.
49 2 const Surface& Body() const {
50 2 return m_body;
51 }
52
53 /// The upsampler of each face, fitted to its posts.
54 const std::array<std::shared_ptr<const Upsampler>, 6>& UpsamplersInUse() const {
55 1 return m_upsamplers;
56 }
57
58 protected:
59 /// The refined radius at each sample's longitude and latitude in degrees at a sample spacing
60 /// of `spacing` degrees, less the body's radius there; each face upsamples the points it
61 /// weighs at once, a grid tile by tile over threads.
62 void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const override;
63
64 private:
65 /// `FillSamples` of `points` into `heights`.
66 void FillPoints(Span<const Vec2d> points, Span<float> heights, double spacing) const;
67
68 /// Columns between the first posts of consecutive faces, more than any face reads.
69 static constexpr std::int64_t kFaceColumns = std::int64_t{1} << 20;
70
71 /// One ICQ face's labels as posts, less the reference radius, numbered from `face` times
72 /// `kFaceColumns` columns.
73 168 class FacePosts final : public PostLattice {
74 public:
75 FacePosts(const ShapeModelSource& source, std::size_t face);
76 GeoTransform Transform() const override;
77 std::optional<PixelWindow> Extent() const override;
78 void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height,
79 Span<float> heights) const override;
80
81 private:
82 const ShapeModelSource& m_source;
83 std::size_t m_face;
84 };
85
86 Surface m_body;
87 double m_reference = 0.0;
88 double m_labelDegrees = 0.0;
89 std::array<std::unique_ptr<FacePosts>, 6> m_posts;
90 std::array<std::shared_ptr<const Upsampler>, 6> m_upsamplers;
91 };
92
93 } // namespace terraingen
94