./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
226 / 228
99.1%
Branches:
94 / 102
92.2%
Functions:
26 / 26
100.0%

src/geometry/surface.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/geometry/surface.h"
4
5 #include "util/checks.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <memory>
10 #include <stdexcept>
11 #include <utility>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// The east and north unit vectors at longitude `lon` and latitude `lat` (radians).
18 4139403 void LocalAxes(double lon, double lat, Vec3d& east, Vec3d& north) {
19 4139403 const double sinLon = std::sin(lon);
20 4139403 const double cosLon = std::cos(lon);
21 4139403 const double sinLat = std::sin(lat);
22 4139403 east = {-sinLon, cosLon, 0.0};
23 4139403 north = {-sinLat * cosLon, -sinLat * sinLon, std::cos(lat)};
24 4139403 }
25
26 /// The volumetric mean radius in meters of a shape model, from the mean of r^3 over directions.
27 99 double MeanRadius(const RadialShape& shape) {
28 99 constexpr int kSteps = 180;
29 99 double cubed = 0.0;
30 99 double weight = 0.0;
31
2/2
✓ Branch 0 taken 17820 times.
✓ Branch 1 taken 99 times.
17919 for (int j = 0; j < kSteps; ++j) {
32 17820 const double lat = -90.0 + (j + 0.5) * 180.0 / kSteps;
33 17820 const double cosLat = std::cos(lat * kDegree);
34
2/2
✓ Branch 0 taken 6415200 times.
✓ Branch 1 taken 17820 times.
6433020 for (int i = 0; i < 2 * kSteps; ++i) {
35 6415200 const double r = shape.At(UnitVector(-180.0 + (i + 0.5) * 180.0 / kSteps, lat));
36 6415200 cubed += r * r * r * cosLat;
37 6415200 weight += cosLat;
38 }
39 }
40 99 return std::cbrt(cubed / weight);
41 }
42
43 } // namespace
44
45 35456256 Vec3d UnitVector(double lonDeg, double latDeg) {
46 35456256 const double lon = lonDeg * kDegree;
47 35456256 const double lat = latDeg * kDegree;
48 35456256 const double cosLat = std::cos(lat);
49 35456256 return {cosLat * std::cos(lon), cosLat * std::sin(lon), std::sin(lat)};
50 }
51
52 6896 Surface Surface::Sphere(double radius) {
53 6896 RequirePositive(radius, "A sphere's radius");
54 6893 Surface surface;
55 6893 surface.m_form = Form::Sphere;
56 6893 surface.m_radius = radius;
57 6893 surface.m_unitLength = radius * kDegree;
58 6893 surface.m_radii = {radius, radius, radius};
59 6893 return surface;
60 }
61
62 88 Surface Surface::Ellipsoid(double a, double b, double c) {
63 88 RequirePositive(a, "An ellipsoid's radius a");
64 87 RequirePositive(b, "An ellipsoid's radius b");
65 85 RequirePositive(c, "An ellipsoid's radius c");
66
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 81 times.
84 if (a == b && b == c) {
67 3 return Sphere(a);
68 }
69 81 Surface surface;
70 81 surface.m_form = Form::Ellipsoid;
71 81 surface.m_radius = std::cbrt(a * b * c);
72 81 surface.m_unitLength = surface.m_radius * kDegree;
73 81 surface.m_radii = {a, b, c};
74 81 return surface;
75 81 }
76
77 100 Surface Surface::ShapeModel(std::shared_ptr<const RadialShape> shape) {
78
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 99 times.
100 if (!shape) {
79
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A shape model surface needs a radial shape");
80 }
81 99 Surface surface;
82 99 surface.m_form = Form::Shape;
83
1/1
✓ Branch 1 taken 99 times.
99 surface.m_radius = MeanRadius(*shape);
84 99 surface.m_unitLength = surface.m_radius * kDegree;
85 99 surface.m_shape = std::move(shape);
86 99 return surface;
87 }
88
89 606951 double Surface::MinRadius() const {
90
2/2
✓ Branch 0 taken 599233 times.
✓ Branch 1 taken 7718 times.
606951 if (m_form == Form::Shape) {
91 599233 return m_shape->MinRadius();
92 }
93 7718 return std::min({m_radii.x, m_radii.y, m_radii.z});
94 }
95
96 9 double Surface::MaxRadius() const {
97
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 if (m_form == Form::Shape) {
98 8 return m_shape->MaxRadius();
99 }
100 1 return std::max({m_radii.x, m_radii.y, m_radii.z});
101 }
102
103 9383408 double Surface::RadiusAt(const Vec2d& point) const {
104
4/4
✓ Branch 0 taken 2785 times.
✓ Branch 1 taken 6958736 times.
✓ Branch 2 taken 2421886 times.
✓ Branch 3 taken 1 time.
9383408 switch (m_form) {
105 case Form::Plane:
106 return 0.0;
107 2785 case Form::Sphere:
108 2785 return m_radius;
109 6958736 case Form::Shape:
110 6958736 return m_shape->At(UnitVector(point.x, point.y));
111 case Form::Ellipsoid:
112 break;
113 }
114 2421886 const Vec3d u = UnitVector(point.x, point.y);
115 2421886 const double ex = u.x / m_radii.x;
116 2421886 const double ey = u.y / m_radii.y;
117 2421886 const double ez = u.z / m_radii.z;
118 2421886 return 1.0 / std::sqrt(ex * ex + ey * ey + ez * ez);
119 }
120
121 3904972 double Surface::AreaPerSteradian(const Vec2d& point) const {
122
3/3
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3904970 times.
✓ Branch 2 taken 1 time.
3904972 switch (m_form) {
123 case Form::Plane:
124 return 1.0;
125 1 case Form::Sphere:
126 1 return m_radius * m_radius;
127 case Form::Ellipsoid:
128 case Form::Shape:
129 break;
130 }
131 3904970 const Vec3d p = Position(point);
132 3907035 const double r = Length(p);
133 3907072 const double cosine = Dot(p, Normal(point, p)) / r;
134 3902589 return r * r / cosine;
135 }
136
137 4561884 Vec3d Surface::Normal(const Vec2d& point, const Vec3d& p) const {
138 4561884 Vec3d n;
139
2/2
✓ Branch 0 taken 3026537 times.
✓ Branch 1 taken 1535347 times.
4561884 if (m_form == Form::Shape) {
140 3026537 constexpr double kStep = 1e-6;
141 3026537 const Vec3d u = UnitVector(point.x, point.y);
142 3026537 Vec3d east;
143 3026537 Vec3d north;
144 3026537 LocalAxes(point.x * kDegree, point.y * kDegree, east, north);
145 15046345 const auto at = [&](const Vec3d& axis, double sign) {
146 12012967 const Vec3d d = u + axis * (sign * kStep);
147 12012967 const Vec3d unit = d / Length(d);
148 12010327 return unit * m_shape->At(unit);
149 3033378 };
150 3033378 n = Cross(at(east, 1.0) - at(east, -1.0), at(north, 1.0) - at(north, -1.0));
151 } else {
152 1535347 n = {p.x / (m_radii.x * m_radii.x), p.y / (m_radii.y * m_radii.y),
153 1535347 p.z / (m_radii.z * m_radii.z)};
154 }
155 4550967 return n / Length(n);
156 }
157
158 502919 Vec2d Surface::TangentOffset(const Vec2d& center, const Vec2d& point) const {
159 502919 const LocalFrame frame = Frame(center);
160 503813 const Vec3d d = Position(point) - Position(center);
161 502761 return {Dot(d, frame.east), Dot(d, frame.north)};
162 }
163
164 77491 Vec2d Surface::TangentLocate(const Vec2d& center, const Vec2d& offset) const {
165
4/4
✓ Branch 0 taken 15713 times.
✓ Branch 1 taken 61778 times.
✓ Branch 2 taken 1981 times.
✓ Branch 3 taken 13732 times.
77491 if (offset.x == 0.0 && offset.y == 0.0) {
166 1981 return center;
167 }
168 75510 const LocalFrame frame = Frame(center);
169 75510 const Vec3d q = Position(center) + frame.east * offset.x + frame.north * offset.y;
170 75510 const double depth =
171
2/2
✓ Branch 0 taken 38245 times.
✓ Branch 1 taken 37265 times.
75510 m_form == Form::Shape ? ShapeDepth(q, frame.up) : EllipsoidDepth(q, frame.up);
172 75510 return Coordinates(q - frame.up * depth);
173 }
174
175 38245 double Surface::ShapeDepth(const Vec3d& q, const Vec3d& up) const {
176 3939246 const auto excess = [&](double s) {
177 3901001 const Vec3d x = q - up * s;
178 3901001 const double r = Length(x);
179 3901001 return r - m_shape->At(x / r);
180 38245 };
181 38245 const double step = m_shape->MaxRadius() / 64.0;
182 38245 double inside = 0.0;
183 38245 double outside = 0.0;
184
2/2
✓ Branch 1 taken 38219 times.
✓ Branch 2 taken 26 times.
38245 if (excess(0.0) >= 0.0) {
185 inside = step;
186
3/4
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 38219 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
38230 while (excess(inside) >= 0.0 && inside < 4.0 * m_shape->MaxRadius()) {
187 11 outside = inside;
188 11 inside += step;
189 }
190 } else {
191 26 outside = -step;
192
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
26 while (excess(outside) < 0.0 && outside > -4.0 * m_shape->MaxRadius()) {
193 ✗ inside = outside;
194 ✗ outside -= step;
195 }
196 }
197
2/2
✓ Branch 0 taken 3824500 times.
✓ Branch 1 taken 38245 times.
3862745 for (int iteration = 0; iteration < 100; ++iteration) {
198 3824500 const double middle = 0.5 * (inside + outside);
199
2/2
✓ Branch 1 taken 2163666 times.
✓ Branch 2 taken 1660834 times.
3824500 (excess(middle) >= 0.0 ? outside : inside) = middle;
200 }
201 38245 return 0.5 * (inside + outside);
202 }
203
204 37265 double Surface::EllipsoidDepth(const Vec3d& q, const Vec3d& up) const {
205 37265 const Vec3d inverse{1.0 / (m_radii.x * m_radii.x), 1.0 / (m_radii.y * m_radii.y),
206 37265 1.0 / (m_radii.z * m_radii.z)};
207 37265 const double a = up.x * up.x * inverse.x + up.y * up.y * inverse.y + up.z * up.z * inverse.z;
208 37265 const double b = q.x * up.x * inverse.x + q.y * up.y * inverse.y + q.z * up.z * inverse.z;
209 37265 const double cq = q.x * q.x * inverse.x + q.y * q.y * inverse.y + q.z * q.z * inverse.z;
210 37265 const double discriminant = b * b - a * (cq - 1.0);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37265 times.
37265 return (b - std::sqrt(std::max(discriminant, 0.0))) / a;
212 }
213
214 360674 Vec2d Surface::SphereOffset(const Vec2d& center, const Vec2d& point) const {
215 360674 const Vec3d c = UnitVector(center.x, center.y);
216 360674 const Vec3d p = UnitVector(point.x, point.y);
217 360674 Vec3d east;
218 360674 Vec3d north;
219 360674 LocalAxes(center.x * kDegree, center.y * kDegree, east, north);
220 360705 const double angle = std::atan2(Length(Cross(c, p)), Dot(c, p));
221
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 360681 times.
360697 const double e = Dot(p, east);
222 360697 const double n = Dot(p, north);
223 360697 const double across = std::hypot(e, n);
224
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 360681 times.
360697 if (across == 0.0) {
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 return {0.0, angle == 0.0 ? 0.0 : m_radius * angle};
226 }
227 360681 const double scale = m_radius * angle / across;
228 360681 return {e * scale, n * scale};
229 }
230
231 87864 Vec2d Surface::SphereLocate(const Vec2d& center, const Vec2d& offset) const {
232 87864 const double distance = std::hypot(offset.x, offset.y);
233
2/2
✓ Branch 0 taken 982 times.
✓ Branch 1 taken 86882 times.
87864 if (distance == 0.0) {
234 982 return center;
235 }
236 86882 const Vec3d c = UnitVector(center.x, center.y);
237 86882 Vec3d east;
238 86882 Vec3d north;
239 86882 LocalAxes(center.x * kDegree, center.y * kDegree, east, north);
240 86882 const double angle = distance / m_radius;
241 86882 const double along = std::sin(angle) / distance;
242 86882 const double cosAngle = std::cos(angle);
243 86882 return Coordinates(c * cosAngle + (east * offset.x + north * offset.y) * along);
244 }
245
246 1177077 double Surface::Distance(const Vec2d& a, const Vec2d& b) const {
247
3/3
✓ Branch 0 taken 57068 times.
✓ Branch 1 taken 215155 times.
✓ Branch 2 taken 904854 times.
1177077 switch (m_form) {
248 57068 case Form::Plane:
249 57068 return std::hypot(b.x - a.x, b.y - a.y);
250 215155 case Form::Ellipsoid:
251 215155 case Form::Shape:
252 215155 return Length(Position(b) - Position(a));
253 case Form::Sphere:
254 break;
255 }
256 904854 const Vec3d u = UnitVector(a.x, a.y);
257 904854 const Vec3d v = UnitVector(b.x, b.y);
258 904854 return m_radius * std::atan2(Length(Cross(u, v)), Dot(u, v));
259 }
260
261 9721036 Vec3d Surface::Position(const Vec2d& point) const {
262
2/2
✓ Branch 0 taken 156594 times.
✓ Branch 1 taken 9564442 times.
9721036 if (m_form == Form::Plane) {
263 156594 return {point.x, point.y, 0.0};
264 }
265 9564442 const Vec3d unit = UnitVector(point.x, point.y);
266
2/2
✓ Branch 0 taken 984604 times.
✓ Branch 1 taken 8579838 times.
9564442 return unit * (m_form == Form::Sphere ? m_radius : RadiusAt(point));
267 }
268
269 660702 LocalFrame Surface::Frame(const Vec2d& point) const {
270
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 660662 times.
660702 if (m_form == Form::Plane) {
271 40 return {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
272 }
273 660662 LocalFrame frame;
274 660662 LocalAxes(point.x * kDegree, point.y * kDegree, frame.east, frame.north);
275 660983 frame.up = UnitVector(point.x, point.y);
276
2/2
✓ Branch 0 taken 657569 times.
✓ Branch 1 taken 3414 times.
660983 if (IsNonSphere()) {
277 657569 const Vec3d n = Normal(point, Position(point));
278 654712 const Vec3d east = frame.east - n * Dot(frame.east, n);
279 654712 frame.up = n;
280 654712 frame.east = east / Length(east);
281 657824 frame.north = Cross(n, frame.east);
282 }
283 661238 return frame;
284 }
285
286 5140276 Vec2d Surface::Coordinates(const Vec3d& direction) const {
287
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5140275 times.
5140276 if (m_form == Form::Plane) {
288 1 return {direction.x, direction.y};
289 }
290 5140275 const double lon = std::atan2(direction.y, direction.x) * kRadian;
291 5140275 const double lat = std::atan2(direction.z, std::hypot(direction.x, direction.y)) * kRadian;
292
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 5139435 times.
5141115 return {lon == -180.0 ? 180.0 : lon, lat};
293 }
294
295 415170 BucketIndex Surface::Bucket(const Vec2d& point, double size) const {
296 415170 const Vec3d p = Position(point);
297 415172 return {static_cast<long long>(std::floor(p.x / size)),
298 415172 static_cast<long long>(std::floor(p.y / size)),
299 415172 static_cast<long long>(std::floor(p.z / size))};
300 }
301
302 4117453 Bounds2d Surface::Expand(const Bounds2d& region, double meters) const {
303
2/2
✓ Branch 0 taken 3472978 times.
✓ Branch 1 taken 644475 times.
4117453 if (m_form == Form::Plane) {
304 3472978 return region.Expanded(meters);
305 }
306 644475 double dLat = meters / m_unitLength;
307 644475 double angle = meters / m_radius;
308
2/2
✓ Branch 0 taken 600856 times.
✓ Branch 1 taken 43619 times.
644475 if (IsNonSphere()) {
309 600856 const double shortest = MinRadius();
310
2/2
✓ Branch 0 taken 600837 times.
✓ Branch 1 taken 19 times.
600856 angle = meters < shortest ? std::asin(meters / shortest) : kPi;
311 600856 dLat = angle * kRadian;
312 }
313 1933425 Bounds2d expanded{region.minX, std::max(-90.0, region.minY - dLat), region.maxX,
314
4/4
✓ Branch 0 taken 637164 times.
✓ Branch 1 taken 7311 times.
✓ Branch 2 taken 636427 times.
✓ Branch 3 taken 8048 times.
1281639 std::min(90.0, region.maxY + dLat)};
315
2/2
✓ Branch 0 taken 130755 times.
✓ Branch 1 taken 513720 times.
644475 const double widest = std::max(std::fabs(region.minY), std::fabs(region.maxY));
316
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 644473 times.
644475 const double reach = std::sin(std::min(angle, kPi / 2.0));
317
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 644459 times.
644475 const double cosLat = std::cos(std::min(widest, 90.0) * kDegree);
318 644475 const double dLon =
319
3/4
✓ Branch 0 taken 644515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 629370 times.
✓ Branch 3 taken 15145 times.
644475 angle < kPi / 2.0 && reach < cosLat ? std::asin(reach / cosLat) * kRadian : 360.0;
320
5/6
✓ Branch 0 taken 637207 times.
✓ Branch 1 taken 7268 times.
✓ Branch 2 taken 629297 times.
✓ Branch 3 taken 7910 times.
✓ Branch 4 taken 629304 times.
✗ Branch 5 not taken.
644475 if (expanded.minY <= -90.0 || expanded.maxY >= 90.0 || dLon >= 180.0 ||
321
2/2
✓ Branch 0 taken 609524 times.
✓ Branch 1 taken 19780 times.
629304 region.maxX - region.minX + 2.0 * dLon >= 360.0) {
322 expanded.minX = -180.0;
323 expanded.maxX = 180.0;
324 } else {
325 609524 expanded.minX = region.minX - dLon;
326 609524 expanded.maxX = region.maxX + dLon;
327 }
328 644475 return expanded;
329 }
330
331 15424260 const Surface& SurfaceOrPlane(const Surface* surface) {
332
4/4
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 15424038 times.
✓ Branch 3 taken 221 times.
✓ Branch 4 taken 1 time.
15424260 static const Surface plane{};
333
2/2
✓ Branch 0 taken 8192299 times.
✓ Branch 1 taken 7231961 times.
15424260 return surface != nullptr ? *surface : plane;
334 }
335
336 } // namespace terraingen
337