./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
40 / 42
95.2%
Branches:
64 / 98
65.3%
Functions:
5 / 6, 1 excl
83.3%

include/terraingen/layers/masked.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `MaskedLayer`, another layer applied only inside a rectangle, circle or polygon.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/layers/boulders/boulder_placer.h"
9 #include "terraingen/layers/craters/crater_placer.h"
10 #include "terraingen/layers/fade.h"
11 #include "terraingen/layers/layer.h"
12 #include "terraingen/raster/cell_loops.h"
13 #include "terraingen/raster/grid.h"
14
15 #include <memory>
16 #include <optional>
17 #include <utility>
18 #include <vector>
19
20 namespace terraingen {
21
22 /// An area of the surface: a rectangle, a circle or a polygon.
23
17/20
✓ Branch 3 taken 2 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 15 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 23 taken 1 time.
✗ Branch 25 not taken.
✓ Branch 26 taken 1 time.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✓ Branch 30 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 56 taken 1 time.
✓ Branch 61 taken 1 time.
✓ Branch 70 taken 1 time.
✓ Branch 78 taken 3 times.
✓ Branch 85 taken 1 time.
✓ Branch 90 taken 1 time.
✗ Branch 145 not taken.
✓ Branch 146 taken 1 time.
123 struct RegionShape {
24 /// Which shape the region is.
25 enum class Kind { Rectangle, Circle, Polygon };
26
27 Kind kind = Kind::Rectangle; ///< Which of the fields below apply.
28 Bounds2d rectangle{}; ///< For `Rectangle`; a longitude-latitude box on a sphere.
29 Vec2d center{}; ///< For `Circle`; longitude and latitude on a sphere.
30 double radius = 0.0; ///< For `Circle`, in meters.
31 std::vector<Vec2d>
32 points{}; ///< For `Polygon`, its outline; longitude and latitude on a sphere.
33 std::vector<std::vector<Vec2d>> holes{}; ///< For `Polygon`, outlines cut out of it.
34
35 /// A rectangular region of `bounds` (surface coordinates).
36 19 static RegionShape Rectangle(const Bounds2d& bounds) {
37
9/15
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
19 RegionShape shape;
38 19 shape.rectangle = bounds;
39
9/15
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
19 return shape;
40 }
41 /// A circular region around `center` (surface coordinates).
42 /// @param radius In meters.
43 37 static RegionShape Circle(Vec2d center, double radius) {
44
7/12
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
37 RegionShape shape;
45 37 shape.kind = Kind::Circle;
46 37 shape.center = center;
47 37 shape.radius = radius;
48
7/12
✓ Branch 0 taken 1 time.
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
37 return shape;
49 }
50 /// A polygonal region outlined by `points` (surface coordinates, x east and y north), less
51 /// the polygons `holes`.
52 ///
53 /// The outline is closed from the last point back to the first and may be concave. A point
54 /// is inside when a ray from it crosses the outline and holes an odd number of times.
55 28 static RegionShape Polygon(std::vector<Vec2d> points,
56 std::vector<std::vector<Vec2d>> holes = {}) {
57 28 RegionShape shape;
58 28 shape.kind = Kind::Polygon;
59 28 shape.points = std::move(points);
60 28 shape.holes = std::move(holes);
61 28 return shape;
62 }
63
64 /// The signed distance in meters from surface (x, y) to the shape's edge, positive outside;
65 /// a polygon's holes are outside it.
66 /// On a sphere it is measured in the point's local frame, which holds for small feathers.
67 double DistanceOutside(double x, double y, const Surface& surface = {}) const;
68
69 /// The smallest box holding the shape, in `surface`'s coordinates.
70 Bounds2d Bounds(const Surface& surface = {}) const;
71
72 3 bool operator==(const RegionShape& other) const {
73
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return kind == other.kind && rectangle.minX == other.rectangle.minX &&
74
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 rectangle.minY == other.rectangle.minY && rectangle.maxX == other.rectangle.maxX &&
75
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 rectangle.maxY == other.rectangle.maxY && center == other.center &&
76
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
6 radius == other.radius && points == other.points && holes == other.holes;
77 }
78 1 bool operator!=(const RegionShape& other) const {
79 1 return !(*this == other);
80 }
81 };
82
83 /// Checks a region shape.
84 /// @throws std::invalid_argument for a shape that is not finite, a rectangle with a min above its
85 /// max, a circle with a negative radius, or a polygon or hole with fewer than 3 points, a
86 /// zero-length edge or no area.
87 void ValidateRegionShape(const RegionShape& shape);
88
89 /// Another layer applied only inside a region, blended back to the unchanged height over
90 /// `feather` meters outside it.
91 ///
92 /// The inner layer sees the same context and, on grids, is applied only to the cells near the
93 /// region. Of its features, those centered within the region or its feather (`Holds`) belong to it.
94 class MaskedLayer final : public Layer, public CraterPlacer, public BoulderPlacer {
95 public:
96 /// @param feather Width of the blend outside the region, in meters; non-negative.
97 /// @throws std::invalid_argument for a null `inner`, an invalid `shape`, or a negative or
98 /// non-finite `feather`.
99 MaskedLayer(std::shared_ptr<const Layer> inner, RegionShape shape, double feather = 0.0,
100 BlendShape blend = BlendShape::Smoothstep);
101
102 /// The inner layer's weight at (x, y) on `surface`: 1 inside the shape, 0 from `feather`
103 /// outside it, the blend between.
104 double Weight(double x, double y, const Surface& surface = {}) const;
105
106 /// The confined layer.
107 15 const Layer& Inner() const {
108
3/4
terraingen::RegionShape::Polygon(std::vector<terraingen::Vec2d, std::allocator<terraingen::Vec2d> >, std::vector<std::vector<terraingen::Vec2d, std::allocator<terraingen::Vec2d> >, std::allocator<std::vector<terraingen::Vec2d, std::allocator<terraingen::Vec2d> > > >):
✓ Branch 1 taken 12 times.
terraingen::RegionShape::operator==(terraingen::RegionShape const&) const:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
15 return *m_inner;
109 }
110
111 /// The confined layer's base, if it was built on one.
112 27 std::optional<TerrainBase> LayerBase() const override {
113
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
27 return m_inner->LayerBase();
114 }
115
116 /// The layers the confined layer reads.
117 1 double GroundReach(double spacing) const override {
118
1/1
✓ Branch 1 taken 1 time.
1 return m_inner->GroundReach(spacing);
119 }
120
121 19 std::vector<const Layer*> Dependencies() const override {
122
1/1
✓ Branch 1 taken 1 time.
19 return m_inner->Dependencies();
123 }
124
125 /// The layer confining the confined layer reseeded with `seed`; null when it has no seeds.
126 LayerPtr Reseeded(std::uint64_t seed) const override;
127
128 /// The layer confining the confined layer rebound to `replaced`; null when it reads none of
129 /// them.
130 LayerPtr Rebound(const LayerReplacements& replaced) const override;
131
132 /// The layer confining the confined layer rebuilt on `base`; null when it was built on none.
133 LayerPtr Rebased(const TerrainBase& base) const override;
134
135 /// Whether a feature centered at (x, y) on `surface` belongs to the region: within it or its
136 /// feather.
137 bool Holds(double x, double y, const Surface& surface = {}) const;
138
139 /// The inner layer's craters over its part of `grid` that the region `Holds`; none when the
140 /// inner layer places no craters.
141 CraterFeatures CratersOver(const RasterGrid& grid, double spacing) const override;
142
143 /// The inner layer's boulders over its part of `grid` that the region `Holds`; none when the
144 /// inner layer places no boulders.
145 BoulderFeatures BouldersOver(const RasterGrid& grid, double spacing,
146 const Ground* ground) const override;
147
148 protected:
149 /// Applies the inner layer to the samples near the region and blends it in by `Weight`.
150 void ApplySamples(SampleBlock& samples, const LayerContext& context) const override;
151
152 private:
153 void ApplyToGrid(Raster<float>& heights, const LayerContext& context) const;
154 void ApplyToPoints(SampleBlock& samples, const LayerContext& context) const;
155 PixelWindow InnerWindow(const RasterGrid& grid, const Surface& surface) const;
156 /// The part of `grid` the inner layer is applied to; nothing when no cell is near the region.
157 std::optional<RasterGrid> InnerGrid(const RasterGrid& grid, const Surface& surface) const;
158 /// This layer confining `inner` in place of its own.
159 LayerPtr Confining(LayerPtr inner) const;
160
161 std::shared_ptr<const Layer> m_inner;
162 RegionShape m_shape;
163 double m_feather;
164 BlendShape m_blend;
165 };
166
167 } // namespace terraingen
168