Commit ed4ae2da67
Unsigned
Layout: unified · split
App/Sources/SettingsScreen.swift +4 −1
| @@ -23,6 +23,7 @@ enum SettingsKey { | |||
| 23 | static let dailySummaryMinutes = "dailySummaryMinutes" | 23 | static let dailySummaryMinutes = "dailySummaryMinutes" |
| 24 | /// Messier objects on the sky chart. | 24 | /// Messier objects on the sky chart. |
| 25 | static let showDeepSky = "showDeepSky" | 25 | static let showDeepSky = "showDeepSky" |
| 26 | static let showMilkyWay = "showMilkyWay" | ||
| 26 | /// Cloud forecasts from Apple Weather; off unless turned on. | 27 | /// Cloud forecasts from Apple Weather; off unless turned on. |
| 27 | static let cloudForecast = "cloudForecast" | 28 | static let cloudForecast = "cloudForecast" |
| 28 | /// The eclipse eye-safety note has been shown before a countdown or rehearsal. | 29 | /// The eclipse eye-safety note has been shown before a countdown or rehearsal. |
| @@ -77,6 +78,7 @@ struct SettingsScreen: View { | |||
| 77 | @AppStorage(SettingsKey.nightMode) private var nightMode = "auto" | 78 | @AppStorage(SettingsKey.nightMode) private var nightMode = "auto" |
| 78 | @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western" | 79 | @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western" |
| 79 | @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true | 80 | @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true |
| 81 | @AppStorage(SettingsKey.showMilkyWay) private var showMilkyWay = true | ||
| 80 | @AppStorage(SettingsKey.onboarded) private var onboarded = true | 82 | @AppStorage(SettingsKey.onboarded) private var onboarded = true |
| 81 | @Environment(\.dismiss) private var dismiss | 83 | @Environment(\.dismiss) private var dismiss |
| 82 | @State private var alerts = AuroraAlerts.shared | 84 | @State private var alerts = AuroraAlerts.shared |
| @@ -158,6 +160,7 @@ struct SettingsScreen: View { | |||
| 158 | ForEach(Catalog.skyCultures) { Text($0.name).tag($0.id) } | 160 | ForEach(Catalog.skyCultures) { Text($0.name).tag($0.id) } |
| 159 | } | 161 | } |
| 160 | Toggle("Messier objects", isOn: $showDeepSky) | 162 | Toggle("Messier objects", isOn: $showDeepSky) |
| 163 | Toggle("Milky Way", isOn: $showMilkyWay) | ||
| 161 | Picker("Night mode", selection: $nightMode) { | 164 | Picker("Night mode", selection: $nightMode) { |
| 162 | Text("Off").tag("off") | 165 | Text("Off").tag("off") |
| 163 | Text("On").tag("on") | 166 | Text("On").tag("on") |
| @@ -166,7 +169,7 @@ struct SettingsScreen: View { | |||
| 166 | } header: { | 169 | } header: { |
| 167 | Text("Display") | 170 | Text("Display") |
| 168 | } footer: { | 171 | } footer: { |
| 169 | Text("Constellations from other cultures come from Stellarium's sky cultures and are drawn over the same stars; figures with stars fainter than the app's catalogue are shortened. Messier objects are galaxies, clusters and nebulae, drawn with atlas symbols; most need binoculars. Night mode draws the sky in red to keep your eyes adapted to the dark. After dark turns it on once the Sun is 12° below the horizon.") | 172 | Text("Constellations from other cultures come from Stellarium's sky cultures and are drawn over the same stars; figures with stars fainter than the app's catalogue are shortened. Messier objects are galaxies, clusters and nebulae, drawn with atlas symbols; most need binoculars. The Milky Way is from ESA's Gaia (ESA/Gaia/DPAC, CC BY-SA 3.0 IGO). Night mode draws the sky in red to keep your eyes adapted to the dark. After dark turns it on once the Sun is 12° below the horizon.") |
| 170 | } | 173 | } |
| 171 | Section("About") { | 174 | Section("About") { |
| 172 | LabeledContent("Version", value: Bundle.main.versionString) | 175 | LabeledContent("Version", value: Bundle.main.versionString) |
App/Sources/SkyScreen.swift +2 −1
| @@ -13,6 +13,7 @@ struct SkyScreen: View { | |||
| 13 | @AppStorage(SettingsKey.nightMode) private var nightMode = "auto" | 13 | @AppStorage(SettingsKey.nightMode) private var nightMode = "auto" |
| 14 | @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western" | 14 | @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western" |
| 15 | @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true | 15 | @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true |
| 16 | @AppStorage(SettingsKey.showMilkyWay) private var showMilkyWay = true | ||
| 16 | @State private var showSettings = false | 17 | @State private var showSettings = false |
| 17 | @State private var showEvents = false | 18 | @State private var showEvents = false |
| 18 | @State private var showLog = false | 19 | @State private var showLog = false |
| @@ -46,7 +47,7 @@ struct SkyScreen: View { | |||
| 46 | style.background.ignoresSafeArea() | 47 | style.background.ignoresSafeArea() |
| 47 | if let scene = model.scene { | 48 | if let scene = model.scene { |
| 48 | let view = current(width: geo.size.width) | 49 | let view = current(width: geo.size.width) |
| 49 | SkyView(scene: scene, centerAzimuth: view.azimuth, centerAltitude: view.altitude, fieldOfView: view.fieldOfView, style: style, culture: Catalog.skyCultures.first { $0.id == skyCulture }, highlight: (tour.map { $0.steps[tourStep].target } ?? shown).flatMap(highlight), showDeepSky: showDeepSky) { [stats] d in | 50 | SkyView(scene: scene, centerAzimuth: view.azimuth, centerAltitude: view.altitude, fieldOfView: view.fieldOfView, style: style, culture: Catalog.skyCultures.first { $0.id == skyCulture }, highlight: (tour.map { $0.steps[tourStep].target } ?? shown).flatMap(highlight), showDeepSky: showDeepSky, showMilkyWay: showMilkyWay) { [stats] d in |
| 50 | stats.record(d) | 51 | stats.record(d) |
| 51 | } | 52 | } |
| 52 | .ignoresSafeArea() | 53 | .ignoresSafeArea() |
PLAN.org +3 −1
| @@ -530,7 +530,9 @@ Worker; DSN Now freshness; NASA/ESA image licences; 26 LoC plates bundled. | |||
| 530 | 530 | ||
| 531 | ** v5.2: Sky | 531 | ** v5.2: Sky |
| 532 | - Milky Way from ESA's "Gaia's sky in colour" (CC BY-SA), clipped before | 532 | - Milky Way from ESA's "Gaia's sky in colour" (CC BY-SA), clipped before |
| 533 | projection. | 533 | projection. Done: 2° equatorial grid from the Hammer–Aitoff image |
| 534 | (=scripts/make-milkyway.swift=), cells under −3° dropped before | ||
| 535 | projection, drawn as blurred discs; Settings switch. | ||
| 534 | - Comets in the Sky tab and the orrery from JPL SBDB, fetched daily by the | 536 | - Comets in the Sky tab and the orrery from JPL SBDB, fetched daily by the |
| 535 | Worker. The orrery has periodic comets (Kepler.swift); needs parabolic and | 537 | Worker. The orrery has periodic comets (Kepler.swift); needs parabolic and |
| 536 | hyperbolic orbits. Then bright asteroids. | 538 | hyperbolic orbits. Then bright asteroids. |
Packages/MoonriseKit/Package.swift +1 −1
| @@ -23,7 +23,7 @@ let package = Package( | |||
| 23 | .target( | 23 | .target( |
| 24 | name: "SkyRenderer", | 24 | name: "SkyRenderer", |
| 25 | dependencies: ["SkyEngine"], | 25 | dependencies: ["SkyEngine"], |
| 26 | resources: [.copy("Resources/bsc5.json"), .copy("Resources/constellations.json"), .copy("Resources/coastline.json"), .copy("Resources/spacecraft.json"), .copy("Resources/skycultures.json"), .copy("Resources/messier.json")] | 26 | resources: [.copy("Resources/bsc5.json"), .copy("Resources/constellations.json"), .copy("Resources/coastline.json"), .copy("Resources/spacecraft.json"), .copy("Resources/skycultures.json"), .copy("Resources/messier.json"), .copy("Resources/milkyway.bin")] |
| 27 | ), | 27 | ), |
| 28 | .target(name: "Feeds", dependencies: ["SkyEngine"]), | 28 | .target(name: "Feeds", dependencies: ["SkyEngine"]), |
| 29 | .target(name: "ObservingLog", dependencies: ["Library", "SkyRenderer"]), | 29 | .target(name: "ObservingLog", dependencies: ["Library", "SkyRenderer"]), |
Packages/MoonriseKit/Sources/SkyRenderer/Resources/milkyway.bin added
Binary file not shown.
Packages/MoonriseKit/Sources/SkyRenderer/SkyScene.swift +15
| @@ -97,6 +97,13 @@ public enum Catalog { | |||
| 97 | return try! JSONDecoder().decode([Constellation].self, from: Data(contentsOf: url)) | 97 | return try! JSONDecoder().decode([Constellation].self, from: Data(contentsOf: url)) |
| 98 | }() | 98 | }() |
| 99 | 99 | ||
| 100 | /// Milky Way brightness on a 2° J2000 grid (rows Dec −89° to +89°, columns RA 1° to 359°), from ESA's "Gaia's sky | ||
| 101 | /// in colour" (ESA/Gaia/DPAC, CC BY-SA 3.0 IGO), built by scripts/make-milkyway.swift. | ||
| 102 | public static let milkyWay: [UInt8] = { | ||
| 103 | let url = Bundle.module.url(forResource: "milkyway", withExtension: "bin")! | ||
| 104 | return [UInt8](try! Data(contentsOf: url)) | ||
| 105 | }() | ||
| 106 | |||
| 100 | /// Built by scripts/make-messier.py. | 107 | /// Built by scripts/make-messier.py. |
| 101 | public static let deepSky: [DeepSkyObject] = { | 108 | public static let deepSky: [DeepSkyObject] = { |
| 102 | let url = Bundle.module.url(forResource: "messier", withExtension: "json")! | 109 | let url = Bundle.module.url(forResource: "messier", withExtension: "json")! |
| @@ -133,6 +140,10 @@ public struct SkyScene: Sendable { | |||
| 133 | public var deepSky: [PlacedDeepSky] | 140 | public var deepSky: [PlacedDeepSky] |
| 134 | /// Moon's elongation east of the sun, degrees (0 new, 180 full). | 141 | /// Moon's elongation east of the sun, degrees (0 new, 180 full). |
| 135 | public var moonPhase: Double | 142 | public var moonPhase: Double |
| 143 | /// Milky Way cells brighter than `milkyWayFloor`, above −3° (the rest are dropped before projection). | ||
| 144 | public var milkyWay: [(position: Horizontal, value: UInt8)] | ||
| 145 | |||
| 146 | static let milkyWayFloor: UInt8 = 48 | ||
| 136 | 147 | ||
| 137 | public init(date: Date, observer: Observer, magnitudeLimit: Double = 6.5) throws { | 148 | public init(date: Date, observer: Observer, magnitudeLimit: Double = 6.5) throws { |
| 138 | self.date = date | 149 | self.date = date |
| @@ -153,5 +164,9 @@ public struct SkyScene: Sendable { | |||
| 153 | ) | 164 | ) |
| 154 | } | 165 | } |
| 155 | moonPhase = try SkyEngine.moonPhase(at: date) | 166 | moonPhase = try SkyEngine.moonPhase(at: date) |
| 167 | let cells = Catalog.milkyWay.enumerated().filter { $0.element > Self.milkyWayFloor } | ||
| 168 | let centres = cells.map { (ra: 1 + 2 * Double($0.offset % 180), dec: -89 + 2 * Double($0.offset / 180)) } | ||
| 169 | let placed = SkyEngine.horizontal(j2000: centres, at: date, from: observer) | ||
| 170 | milkyWay = zip(placed, cells).filter { $0.0.altitude > -3 }.map { ($0.0, $0.1.element) } | ||
| 156 | } | 171 | } |
| 157 | } | 172 | } |
Packages/MoonriseKit/Sources/SkyRenderer/SkyView.swift +25 −1
| @@ -40,6 +40,7 @@ public struct SkyStyle: Sendable { | |||
| 40 | public var moonLight = Color(red: 0.93, green: 0.91, blue: 0.85) | 40 | public var moonLight = Color(red: 0.93, green: 0.91, blue: 0.85) |
| 41 | public var moonDark = Color(red: 0.16, green: 0.20, blue: 0.27) | 41 | public var moonDark = Color(red: 0.16, green: 0.20, blue: 0.27) |
| 42 | public var planet = Color(red: 0.95, green: 0.84, blue: 0.60) | 42 | public var planet = Color(red: 0.95, green: 0.84, blue: 0.60) |
| 43 | public var milkyWay = Color(red: 0.75, green: 0.80, blue: 0.95) | ||
| 43 | /// Rings and lines for whatever a tour is pointing at. | 44 | /// Rings and lines for whatever a tour is pointing at. |
| 44 | public var highlight = Color(red: 1.0, green: 0.78, blue: 0.35) | 45 | public var highlight = Color(red: 1.0, green: 0.78, blue: 0.35) |
| 45 | /// Draw stars in a single red, for dark-adapted eyes. | 46 | /// Draw stars in a single red, for dark-adapted eyes. |
| @@ -63,6 +64,7 @@ public struct SkyStyle: Sendable { | |||
| 63 | s.highlight = red | 64 | s.highlight = red |
| 64 | s.moonDark = Color(red: 0.12, green: 0, blue: 0) | 65 | s.moonDark = Color(red: 0.12, green: 0, blue: 0) |
| 65 | s.planet = red | 66 | s.planet = red |
| 67 | s.milkyWay = red | ||
| 66 | s.monochromeStars = red | 68 | s.monochromeStars = red |
| 67 | return s | 69 | return s |
| 68 | }() | 70 | }() |
| @@ -112,15 +114,17 @@ public struct SkyView: View { | |||
| 112 | public var culture: SkyCulture? | 114 | public var culture: SkyCulture? |
| 113 | public var highlight: SkyHighlight? | 115 | public var highlight: SkyHighlight? |
| 114 | public var showDeepSky: Bool | 116 | public var showDeepSky: Bool |
| 117 | public var showMilkyWay: Bool | ||
| 115 | /// Called after each draw with the time the draw took. | 118 | /// Called after each draw with the time the draw took. |
| 116 | public var onDraw: (@Sendable (Duration) -> Void)? | 119 | public var onDraw: (@Sendable (Duration) -> Void)? |
| 117 | 120 | ||
| 118 | public init(scene: SkyScene, centerAzimuth: Double = 180, centerAltitude: Double = 55, fieldOfView: Double = 110, style: SkyStyle = SkyStyle(), culture: SkyCulture? = nil, highlight: SkyHighlight? = nil, showDeepSky: Bool = true, onDraw: (@Sendable (Duration) -> Void)? = nil) { | 121 | public init(scene: SkyScene, centerAzimuth: Double = 180, centerAltitude: Double = 55, fieldOfView: Double = 110, style: SkyStyle = SkyStyle(), culture: SkyCulture? = nil, highlight: SkyHighlight? = nil, showDeepSky: Bool = true, showMilkyWay: Bool = true, onDraw: (@Sendable (Duration) -> Void)? = nil) { |
| 119 | self.scene = scene | 122 | self.scene = scene |
| 120 | self.style = style | 123 | self.style = style |
| 121 | self.culture = culture | 124 | self.culture = culture |
| 122 | self.highlight = highlight | 125 | self.highlight = highlight |
| 123 | self.showDeepSky = showDeepSky | 126 | self.showDeepSky = showDeepSky |
| 127 | self.showMilkyWay = showMilkyWay | ||
| 124 | self.centerAzimuth = centerAzimuth | 128 | self.centerAzimuth = centerAzimuth |
| 125 | self.centerAltitude = centerAltitude | 129 | self.centerAltitude = centerAltitude |
| 126 | self.fieldOfView = fieldOfView | 130 | self.fieldOfView = fieldOfView |
| @@ -132,6 +136,7 @@ public struct SkyView: View { | |||
| 132 | let start = ContinuousClock.now | 136 | let start = ContinuousClock.now |
| 133 | let p = Projection(centerAzimuth: centerAzimuth, centerAltitude: centerAltitude, fieldOfView: fieldOfView, size: size) | 137 | let p = Projection(centerAzimuth: centerAzimuth, centerAltitude: centerAltitude, fieldOfView: fieldOfView, size: size) |
| 134 | context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(style.background)) | 138 | context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(style.background)) |
| 139 | if showMilkyWay { drawMilkyWay(&context, p) } | ||
| 135 | drawConstellations(&context, p) | 140 | drawConstellations(&context, p) |
| 136 | drawStars(&context, p) | 141 | drawStars(&context, p) |
| 137 | if showDeepSky { drawDeepSky(&context, p) } | 142 | if showDeepSky { drawDeepSky(&context, p) } |
| @@ -142,6 +147,25 @@ public struct SkyView: View { | |||
| 142 | } | 147 | } |
| 143 | } | 148 | } |
| 144 | 149 | ||
| 150 | /// Discs a little larger than a 2° cell in eight steps of brightness, blurred by about a cell so the band reads | ||
| 151 | /// as a glow rather than as cells. | ||
| 152 | func drawMilkyWay(_ context: inout GraphicsContext, _ p: Projection) { | ||
| 153 | let r = 1.2 * Double(p.size.width) / fieldOfView | ||
| 154 | let floor = Double(SkyScene.milkyWayFloor) | ||
| 155 | var bins = [Path](repeating: Path(), count: 8) | ||
| 156 | for cell in scene.milkyWay { | ||
| 157 | guard let pt = p.point(cell.position), pt.x > -r, pt.y > -r, pt.x < p.size.width + r, pt.y < p.size.height + r else { continue } | ||
| 158 | let step = Int((Double(cell.value) - floor) / (256 - floor) * 8) | ||
| 159 | bins[min(7, max(0, step))].addEllipse(in: CGRect(x: pt.x - r, y: pt.y - r, width: 2 * r, height: 2 * r)) | ||
| 160 | } | ||
| 161 | context.drawLayer { layer in | ||
| 162 | layer.addFilter(.blur(radius: r)) | ||
| 163 | for (i, path) in bins.enumerated() where !path.isEmpty { | ||
| 164 | layer.fill(path, with: .color(style.milkyWay.opacity(0.05 + 0.035 * Double(i)))) | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 145 | func drawConstellations(_ context: inout GraphicsContext, _ p: Projection) { | 169 | func drawConstellations(_ context: inout GraphicsContext, _ p: Projection) { |
| 146 | if let culture { | 170 | if let culture { |
| 147 | drawCulture(culture, &context, p) | 171 | drawCulture(culture, &context, p) |
THIRD_PARTY.org +1
| @@ -12,6 +12,7 @@ | |||
| 12 | | Sun texture for the 3D solar system (=App/Globes/sun.jpg=) | generated noise, made for this app | — | — | | 12 | | Sun texture for the 3D solar system (=App/Globes/sun.jpg=) | generated noise, made for this app | — | — | |
| 13 | | Orbital elements for Ceres, Haumea, Makemake, Eris and seven comets (=Packages/MoonriseKit/Sources/SkyEngine/Kepler.swift=) | JPL Small-Body Database, fetched 2026-10-19 | NASA/JPL, no published restrictions | https://ssd.jpl.nasa.gov/tools/sbdb_lookup.html | | 13 | | Orbital elements for Ceres, Haumea, Makemake, Eris and seven comets (=Packages/MoonriseKit/Sources/SkyEngine/Kepler.swift=) | JPL Small-Body Database, fetched 2026-10-19 | NASA/JPL, no published restrictions | https://ssd.jpl.nasa.gov/tools/sbdb_lookup.html | |
| 14 | | ConstellationLines v1.4, Copyright (c) 2005-2026 Marc van der Sluys | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/constellations.json= | CC BY 4.0 | https://github.com/MarcvdSluys/ConstellationLines, doi:10.5281/zenodo.10397192 | | 14 | | ConstellationLines v1.4, Copyright (c) 2005-2026 Marc van der Sluys | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/constellations.json= | CC BY 4.0 | https://github.com/MarcvdSluys/ConstellationLines, doi:10.5281/zenodo.10397192 | |
| 15 | | Gaia's sky in colour, ESA/Gaia/DPAC: Milky Way brightness on a 2° grid (=scripts/make-milkyway.swift=) | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/milkyway.bin= | CC BY-SA 3.0 IGO; the derived grid is under the same licence | https://www.esa.int/ESA_Multimedia/Images/2018/04/Gaia_s_sky_in_colour2 | | ||
| 15 | | Natural Earth 1:110m coastline v5.1.2 | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/coastline.json= | Public domain | https://www.naturalearthdata.com | | 16 | | Natural Earth 1:110m coastline v5.1.2 | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/coastline.json= | Public domain | https://www.naturalearthdata.com | |
| 16 | | Launch Library 2 (The Space Devs) | fetched at runtime by =server/= | Free API; credit shown in the app | https://thespacedevs.com/llapi | | 17 | | Launch Library 2 (The Space Devs) | fetched at runtime by =server/= | Free API; credit shown in the app | https://thespacedevs.com/llapi | |
| 17 | | JPL Horizons spacecraft vectors (Voyager 1, 2, Pioneer 10, 11, New Horizons) | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/spacecraft.json= | NASA/JPL, no published restrictions | https://ssd.jpl.nasa.gov/horizons/ | | 18 | | JPL Horizons spacecraft vectors (Voyager 1, 2, Pioneer 10, 11, New Horizons) | =Packages/MoonriseKit/Sources/SkyRenderer/Resources/spacecraft.json= | NASA/JPL, no published restrictions | https://ssd.jpl.nasa.gov/horizons/ | |
scripts/make-milkyway.swift added +75
| @@ -0,0 +1,75 @@ | |||
| 1 | // Milky Way brightness on a 2° equatorial grid from ESA's "Gaia's sky in colour" (ESA/Gaia/DPAC, CC BY-SA 3.0 IGO), | ||
| 2 | // a Hammer–Aitoff map in galactic coordinates with longitude increasing to the left. | ||
| 3 | // | ||
| 4 | // swift scripts/make-milkyway.swift Gaia_s_sky_in_colour.jpg | ||
| 5 | // | ||
| 6 | // Writes Packages/MoonriseKit/Sources/SkyRenderer/Resources/milkyway.bin: 180 × 90 bytes, rows from Dec −89° to | ||
| 7 | // +89°, columns from RA 1° to 359°, each the cell's brightness above the high-latitude sky, scaled so the | ||
| 8 | // 99.5th percentile is 255. The file is an adaptation of the image and is under the same licence. | ||
| 9 | import CoreGraphics | ||
| 10 | import Foundation | ||
| 11 | import ImageIO | ||
| 12 | |||
| 13 | let input = URL(fileURLWithPath: CommandLine.arguments[1]) | ||
| 14 | let source = CGImageSourceCreateWithURL(input as CFURL, nil)! | ||
| 15 | let image = CGImageSourceCreateImageAtIndex(source, 0, nil)! | ||
| 16 | let w = image.width, h = image.height | ||
| 17 | var pixels = [UInt8](repeating: 0, count: w * h * 4) | ||
| 18 | let context = CGContext(data: &pixels, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w * 4, | ||
| 19 | space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)! | ||
| 20 | context.draw(image, in: CGRect(x: 0, y: 0, width: w, height: h)) | ||
| 21 | |||
| 22 | func luminance(_ x: Int, _ y: Int) -> Double { | ||
| 23 | let i = (y * w + x) * 4 | ||
| 24 | return (0.2126 * Double(pixels[i]) + 0.7152 * Double(pixels[i + 1]) + 0.0722 * Double(pixels[i + 2])) / 255 | ||
| 25 | } | ||
| 26 | |||
| 27 | // The map's ellipse: the extent of pixels brighter than the black surround along the middle row and column. | ||
| 28 | let midY = h / 2, midX = w / 2 | ||
| 29 | let xs = (0..<w).filter { luminance($0, midY) > 0.05 }, ys = (0..<h).filter { luminance(midX, $0) > 0.05 } | ||
| 30 | let cx = Double(xs.first! + xs.last!) / 2, cy = Double(ys.first! + ys.last!) / 2 | ||
| 31 | let ax = Double(xs.last! - xs.first!) / 2, ay = Double(ys.last! - ys.first!) / 2 | ||
| 32 | print("ellipse centre", cx, cy, "semi-axes", ax, ay) | ||
| 33 | |||
| 34 | let r = Double.pi / 180 | ||
| 35 | /// J2000 equatorial to galactic (IAU 1958 pole at RA 192.85948°, Dec 27.12825°; node 32.93192°). | ||
| 36 | func galactic(ra: Double, dec: Double) -> (l: Double, b: Double) { | ||
| 37 | let a0 = 192.85948 * r, d0 = 27.12825 * r, l0 = 122.93192 * r | ||
| 38 | let a = ra * r, d = dec * r | ||
| 39 | let sinb = sin(d) * sin(d0) + cos(d) * cos(d0) * cos(a - a0) | ||
| 40 | let y = cos(d) * sin(a - a0), x = sin(d) * cos(d0) - cos(d) * sin(d0) * cos(a - a0) | ||
| 41 | return ((l0 - atan2(y, x)) / r, asin(sinb) / r) | ||
| 42 | } | ||
| 43 | |||
| 44 | /// Mean luminance in a 5 × 5 patch at the Hammer–Aitoff position of (l, b). | ||
| 45 | func sample(l: Double, b: Double) -> Double { | ||
| 46 | let lw = remainder(l, 360) * r, bb = b * r | ||
| 47 | let z = (1 + cos(bb) * cos(lw / 2)).squareRoot() | ||
| 48 | let x = 2 * 2.0.squareRoot() * cos(bb) * sin(lw / 2) / z, y = 2.0.squareRoot() * sin(bb) / z | ||
| 49 | let px = Int(cx - x / (2 * 2.0.squareRoot()) * ax), py = Int(cy - y / 2.0.squareRoot() * ay) | ||
| 50 | var sum = 0.0, n = 0.0 | ||
| 51 | for dy in -2...2 { for dx in -2...2 { | ||
| 52 | let qx = min(max(px + dx, 0), w - 1), qy = min(max(py + dy, 0), h - 1) | ||
| 53 | sum += luminance(qx, qy); n += 1 | ||
| 54 | } } | ||
| 55 | return sum / n | ||
| 56 | } | ||
| 57 | |||
| 58 | var raw: [Double] = [], high: [Double] = [] | ||
| 59 | for row in 0..<90 { | ||
| 60 | let dec = -89 + 2 * Double(row) | ||
| 61 | for col in 0..<180 { | ||
| 62 | let ra = 1 + 2 * Double(col) | ||
| 63 | let g = galactic(ra: ra, dec: dec) | ||
| 64 | let v = sample(l: g.l, b: g.b) | ||
| 65 | raw.append(v) | ||
| 66 | if abs(g.b) > 60 { high.append(v) } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | let floor = high.sorted()[high.count / 2] | ||
| 70 | let above = raw.map { max(0, $0 - floor) } | ||
| 71 | let top = above.sorted()[Int(Double(above.count) * 0.995)] | ||
| 72 | let bytes = above.map { UInt8(min(255, ($0 / top * 255).rounded())) } | ||
| 73 | let out = URL(fileURLWithPath: "Packages/MoonriseKit/Sources/SkyRenderer/Resources/milkyway.bin") | ||
| 74 | try! Data(bytes).write(to: out) | ||
| 75 | print("floor", floor, "top", top, "cells over 40:", bytes.filter { $0 > 40 }.count) | ||