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 | 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 | 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 | 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 | 100 | if (!shape) { | |
| 79 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 38245 | if (excess(0.0) >= 0.0) { | |
| 185 | inside = step; | ||
| 186 | 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 | 26 | while (excess(outside) < 0.0 && outside > -4.0 * m_shape->MaxRadius()) { | |
| 193 | ✗ | inside = outside; | |
| 194 | ✗ | outside -= step; | |
| 195 | } | ||
| 196 | } | ||
| 197 | 3862745 | for (int iteration = 0; iteration < 100; ++iteration) { | |
| 198 | 3824500 | const double middle = 0.5 * (inside + outside); | |
| 199 | 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 | 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 | 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 | 360697 | if (across == 0.0) { | |
| 225 | 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 | 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 | 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 | 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 | 9564442 | return unit * (m_form == Form::Sphere ? m_radius : RadiusAt(point)); | |
| 267 | } | ||
| 268 | |||
| 269 | 660702 | LocalFrame Surface::Frame(const Vec2d& point) const { | |
| 270 | 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 | 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 | 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 | 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 | 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 | 644475 | if (IsNonSphere()) { | |
| 309 | 600856 | const double shortest = MinRadius(); | |
| 310 | 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 | 1281639 | std::min(90.0, region.maxY + dLat)}; | |
| 315 | 644475 | const double widest = std::max(std::fabs(region.minY), std::fabs(region.maxY)); | |
| 316 | 644475 | const double reach = std::sin(std::min(angle, kPi / 2.0)); | |
| 317 | 644475 | const double cosLat = std::cos(std::min(widest, 90.0) * kDegree); | |
| 318 | 644475 | const double dLon = | |
| 319 | 644475 | angle < kPi / 2.0 && reach < cosLat ? std::asin(reach / cosLat) * kRadian : 360.0; | |
| 320 | 644475 | if (expanded.minY <= -90.0 || expanded.maxY >= 90.0 || dLon >= 180.0 || | |
| 321 | 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 | 15424260 | static const Surface plane{}; | |
| 333 | 15424260 | return surface != nullptr ? *surface : plane; | |
| 334 | } | ||
| 335 | |||
| 336 | } // namespace terraingen | ||
| 337 |