Something went wrong. Try again.
COSMO is a tool for exploring astronomical data from the Gaia catalog. tedius.tngl.sh/cosmo
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586// The 3D scales span the plotted values, so a filter that moves an axis's min or max rescales it.import { describe, expect, test } from "vite-plus/test";import { Color } from "three";import { getColor } from "../color";import { createScale, normalizePoints, vertexColors } from "../three-plot";import { extent } from "../utils";import { FEH_RGB, INDEX_RGB } from "@tedius-npm/vandelay";
describe("extent", () => { test("skips NaN and the infinities", () => { expect(extent(Float64Array.from([NaN, 3, -Infinity, -2, Infinity, 7]))).toEqual([-2, 7]); });
test("is null with no finite value", () => { expect(extent(new Float64Array(0))).toBeNull(); expect(extent(Float64Array.from([NaN, Infinity]))).toBeNull(); });
test("does not spread, so it takes more values than the argument limit", () => { const values = Float64Array.from({ length: 300_000 }, (_, i) => i); expect(extent(values)).toEqual([0, 299_999]); });});
describe("createScale", () => { test("maps the plotted min and max to the ends of the scene", () => { const scale = createScale(Float64Array.from([5, 10, 15])); expect([scale.min, scale.max, scale.range]).toEqual([5, 15, 10]); expect(scale.toNormalized(5)).toBe(-1); expect(scale.toNormalized(15)).toBe(1); expect(scale.toReal(0)).toBe(10); });
test("narrower values give a narrower scale", () => { const all = createScale(Float64Array.from([0, 50, 100])); const filtered = createScale(Float64Array.from([40, 50, 60])); expect(all.toNormalized(60)).toBeCloseTo(0.2); expect(filtered.toNormalized(60)).toBe(1); });
test("no values, or a single one, put every point at the center", () => { expect(createScale(new Float64Array(0)).toNormalized(3)).toBe(0); expect(createScale(Float64Array.from([4, 4])).toNormalized(4)).toBe(0); });});
describe("normalizePoints", () => { test("gives what mapping each value through its scale gives", () => { const xs = Float64Array.from([5, 10, NaN, 15]); const ys = Float64Array.from([-1, 0, 1, 2]); const zs = Float64Array.from([7, 7, 7, 7]); const [sx, sy, sz] = [createScale(xs), createScale(ys), createScale(zs)]; const points = normalizePoints(xs, ys, zs, sx, sy, sz); expect(points.xs).toEqual(Float32Array.from(xs, sx.toNormalized)); expect(points.ys).toEqual(Float32Array.from(ys, sy.toNormalized)); expect(points.zs).toEqual(Float32Array.from(zs, sz.toNormalized)); });});
describe("vertexColors", () => { // How the scene colored each point before: the hex string of getColor through Color.set function throughHex(values: Float64Array, stops: typeof INDEX_RGB): Float32Array { const out = new Float32Array(values.length * 3); const color = new Color(); values.forEach((v, i) => { color.set(Number.isFinite(v) ? getColor(v, stops) : 0x000000); out.set([color.r, color.g, color.b], i * 3); }); return out; }
test("matches Color.set on the hex string of getColor, past both ends of the stops too", () => { for (const stops of [INDEX_RGB, FEH_RGB]) { const values = Float64Array.from({ length: 4001 }, (_, i) => -6 + i * 0.003); expect(vertexColors(values, stops)).toEqual(throughHex(values, stops)); } });
test("is black where the value is not finite", () => { const colors = vertexColors(Float64Array.from([NaN, 1, Infinity]), INDEX_RGB); expect(Array.from(colors.subarray(0, 3))).toEqual([0, 0, 0]); expect(Array.from(colors.subarray(6, 9))).toEqual([0, 0, 0]); expect(colors.subarray(3, 6).some((c) => c > 0)).toBe(true); });});