// FE-ATLAS2-PAINT-401 to FE-ATLAS2-PAINT-412: the edges of the globe projection the // main globeMath tests leave out (clamps, the far side, degenerate arcs). import { describe, expect, it } from 'vitest'; import { arcSegments, frameParams, interpolateGreatCircle, latLimits, moonPhase, MORPH_END, morphOf, projectPoint, RAD, smoothstep, wrapPi, xSqueeze, type GlobeViewport, } from './globeMath'; const VP: GlobeViewport = { width: 1000, height: 800, baseRadius: 300, globeY: 300, flatY: 500 }; describe('globeMath edges', () => { it('FE-ATLAS2-PAINT-401: smoothstep clamps below and above its range and eases in between', () => { expect(smoothstep(0, 1, -2)).toBe(0); expect(smoothstep(0, 1, 3)).toBe(1); expect(smoothstep(0, 1, 0.5)).toBe(0.5); expect(smoothstep(0, 1, 0.25)).toBeCloseTo(0.15625); expect(morphOf(MORPH_END * 4)).toBe(1); }); it('FE-ATLAS2-PAINT-402: wraps exactly pi onto minus pi and leaves small angles alone', () => { expect(wrapPi(Math.PI)).toBeCloseTo(-Math.PI); expect(wrapPi(-Math.PI)).toBeCloseTo(-Math.PI); expect(wrapPi(1)).toBe(1); expect(wrapPi(Math.PI * 4 + 0.5)).toBeCloseTo(0.5); }); it('FE-ATLAS2-PAINT-403: moves the origin from the globe centre to the map centre as it unrolls', () => { const globe = frameParams({ lon: 20, lat: 10, zoom: 1 }, VP); expect(globe.t).toBe(0); expect(globe.k).toBe(1); expect(globe.originX).toBe(500); expect(globe.originY).toBe(300); expect(globe.scale).toBe(300); expect(globe.centerLon).toBeCloseTo(20 * RAD); expect(globe.centerLat).toBeCloseTo(10 * RAD); const flat = frameParams({ lon: 20, lat: 10, zoom: MORPH_END }, VP); expect(flat.t).toBe(1); expect(flat.k).toBeCloseTo(0.0005); expect(flat.originY).toBe(500); expect(flat.scale).toBeCloseTo(300 * MORPH_END); }); it('FE-ATLAS2-PAINT-404: hides a point past the antimeridian of its reference on the far side', () => { const f = frameParams({ lon: 0, lat: 0, zoom: 1 }, VP); const p = projectPoint(-179, 0, f, 179, 0); expect(p.visible).toBe(-1); }); it('FE-ATLAS2-PAINT-405: shades full in the middle, dark at the limb, and evenly once flat', () => { const globe = frameParams({ lon: 0, lat: 0, zoom: 1 }, VP); expect(projectPoint(0, 0, globe).shade).toBeCloseTo(1); expect(projectPoint(89.9, 0, globe).shade).toBeLessThan(0.01); const flat = frameParams({ lon: 0, lat: 0, zoom: MORPH_END }, VP); expect(projectPoint(80, 30, flat).shade).toBeCloseTo(1); }); it('FE-ATLAS2-PAINT-406: never squeezes longitude more than 0.3 near the poles', () => { const f = frameParams({ lon: 0, lat: 85, zoom: MORPH_END }, VP); expect(xSqueeze(f)).toBeCloseTo(0.3); const globe = frameParams({ lon: 0, lat: 85, zoom: 1 }, VP); expect(xSqueeze(globe)).toBe(1); }); it('FE-ATLAS2-PAINT-407: a great circle between one point and itself stays on that point', () => { expect(interpolateGreatCircle(0, 0, 0, 0, 0.5)).toEqual([0, 0]); const [lon, lat] = interpolateGreatCircle(13.4, 52.5, 13.4, 52.5, 0.5); expect(lon).toBeCloseTo(13.4); expect(lat).toBeCloseTo(52.5); }); it('FE-ATLAS2-PAINT-408: a great circle starts and ends on its airports', () => { const [lon0, lat0] = interpolateGreatCircle(8.57, 50.03, -73.78, 40.64, 0); const [lon1, lat1] = interpolateGreatCircle(8.57, 50.03, -73.78, 40.64, 1); expect(lon0).toBeCloseTo(8.57); expect(lat0).toBeCloseTo(50.03); expect(lon1).toBeCloseTo(-73.78); expect(lat1).toBeCloseTo(40.64); }); it('FE-ATLAS2-PAINT-409: no routes give no segments, and the default cuts each arc in 48', () => { const none = arcSegments([]); expect(none.count).toBe(0); expect(none.data).toHaveLength(0); const two = arcSegments([ { a: [0, 0], b: [10, 0] }, { a: [20, 0], b: [30, 0] }, ]); expect(two.count).toBe(96); // The second arc starts at its own first airport. expect(two.data[48 * 8]).toBeCloseTo(20 * RAD); }); it('FE-ATLAS2-PAINT-410: a route to the same airport still lifts by the minimum peak', () => { const { data, count } = arcSegments([{ a: [5, 5], b: [5, 5] }], 2); expect(count).toBe(2); // Lift at the middle of the arc is the minimum peak. expect(data[5]).toBeCloseTo(0.015); expect(data[0]).toBeCloseTo(5 * RAD); expect(data[2]).toBeCloseTo(5 * RAD); }); it('FE-ATLAS2-PAINT-411: knows the moon phase before its reference new moon too', () => { const old = moonPhase(new Date(Date.UTC(1999, 0, 2))); expect(old.phase).toBeGreaterThanOrEqual(0); expect(old.phase).toBeLessThan(1); expect(old.lit).toBeGreaterThan(0.95); const ref = moonPhase(new Date(Date.UTC(2000, 0, 6, 18, 14))); expect(ref.phase).toBe(0); expect(ref.lit).toBe(0); }); it('FE-ATLAS2-PAINT-412: an off-centre flat map gets different southern and northern limits', () => { const [south, north] = latLimits(MORPH_END, VP); const scale = VP.baseRadius * MORPH_END; expect(north).toBeCloseTo(90 - VP.flatY / scale / RAD); expect(south).toBeCloseTo(-90 + (VP.height - VP.flatY) / scale / RAD); expect(north - south).toBeGreaterThan(0); }); });