-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtessellation.ts
184 lines (165 loc) · 5.86 KB
/
tessellation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { ObjectId, WithId } from "mongodb";
import { MongoScene } from "./scenes.js";
import { distance, D2R, R2D } from "@wwtelescope/astro";
import { GeoVoronoi, geoVoronoi, PointSpherical } from "d3-geo-voronoi";
import { Response } from "express";
import { Request as JwtRequest } from "express-jwt";
import { State } from "./globals.js";
export interface MongoTessellation {
name: string;
neighbors: number[][];
polygons: PointSpherical[][];
scene_ids: ObjectId[];
points: number[][];
last_updated: Date;
}
export function createVoronoi(scenes: WithId<MongoScene>[]): GeoVoronoi {
const places = scenes.map(scene => scene.place);
const points: PointSpherical[] = places.map(place => {
let raDeg = place.ra_rad * R2D;
if (raDeg > 180) {
raDeg -= 360;
}
return [raDeg, place.dec_rad * R2D];
});
return geoVoronoi(points);
}
export function createTessellation(scenes: WithId<MongoScene>[], name: string): MongoTessellation {
const voronoi = createVoronoi(scenes);
const polygons: PointSpherical[][] = voronoi.polygons().features.map(polygon => {
return polygon.geometry.coordinates[0].map(p => [p[0] * D2R, p[1] * D2R]);
});
return {
name,
neighbors: voronoi.delaunay.neighbors,
points: voronoi.points.map(p => [p[0] * D2R, p[1] * D2R]),
polygons,
scene_ids: scenes.map(scene => new ObjectId(scene._id.toString())),
last_updated: new Date(),
};
}
export function findCell(tessellation: MongoTessellation, raRad: number, decRad: number, next?: number | null): number {
if (next == null) {
next = 0;
}
let found = next;
let cell = 0;
let dist = 0;
do {
cell = next || 0;
next = null;
const pt = tessellation.points[cell];
dist = distance(raRad, decRad, pt[0], pt[1]);
tessellation.neighbors[cell].forEach((i) => {
const p = tessellation.points[i];
const ndist = distance(raRad, decRad, p[0], p[1]);
if (ndist < dist) {
dist = ndist;
next = i;
found = i;
return;
}
});
} while (next !== null);
return found;
}
/**
* In principle, the global tessellation should include all of the scenes.
* However, this will generally be problematic, as having multiple scenes
* at the same location will lead to degenerate (i.e. single-point) polygons.
* (In our d3-geo-voronoi implementation, these don't get added, and lead to
* a mismatch between cell and polygons indices, which is very bad!)
* To get around this, we only use a subset of scenes. Going from most to
* least popular, we only 'accept' scenes that are a certain minimum distance
* away from any other scene that we've used already (essentially giving each
* scene a minimum 'size'). Note that if the home timeline ordering changes,
* re-running this will give a different result; also this makes our prep time
* scale quadratically with the number of scenes.
*/
export async function createGlobalTessellation(state: State, minDistanceRad = 0.01): Promise<MongoTessellation> {
const scenes = state.scenes.find({
home_timeline_sort_key: { $gte: 0 },
published: true,
}).sort({ home_timeline_sort_key: 1 });
const tessellationScenes: WithId<MongoScene>[] = [];
for await (const scene of scenes) {
const place = scene.place;
const accept = tessellationScenes.every(s => {
return distance(place.ra_rad, place.dec_rad, s.place.ra_rad, s.place.dec_rad) > minDistanceRad;
});
if (accept) {
tessellationScenes.push(scene);
}
}
return createTessellation(tessellationScenes, "global");
}
export function nearbySceneIDs(sceneID: ObjectId, baseTessellation: MongoTessellation, size: number): ObjectId[] {
const sceneIDs: ObjectId[] = [sceneID];
const index = baseTessellation.scene_ids.findIndex((id) => id.equals(sceneID));
if (index < 0) {
return sceneIDs;
}
const queue: number[] = [index];
const visited = new Set<number>();
while (sceneIDs.length < size && queue.length > 0) {
const sceneIndex = queue.shift();
if (sceneIndex === undefined || visited.has(sceneIndex)) {
continue;
}
visited.add(sceneIndex);
sceneIDs.push(baseTessellation.scene_ids[sceneIndex]);
const neighbors = baseTessellation.neighbors[sceneIndex];
queue.push(...neighbors);
}
return sceneIDs;
}
export function initializeTessellationEndpoints(state: State) {
/**
* This route has the ability to specify an ID or a tessellation 'name'
* which is something that I'm imagining being unique.
* The idea being that if we want something out of, say, the global tessellation
* the frontend can just ask for 'global', rather than needing to know the
* the tessellation ID
*/
state.app.get(
"/tessellations/:key/cell",
async (req: JwtRequest, res: Response) => {
const tessellation = await state.tessellations.findOne({ name: req.params.key });
if (tessellation === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
const ra = parseFloat(req.query.ra as string);
const dec = parseFloat(req.query.dec as string);
if (isNaN(ra) || isNaN(dec)) {
res.statusCode = 400;
res.json({
error: true,
message: "Right ascension and declination must be numbers"
});
return;
}
const cell = findCell(tessellation, ra, dec);
if (cell === null) {
res.statusCode = 500;
res.json({
error: true,
message: "Unable to find cell for given location"
});
return;
}
const location = tessellation.points[cell];
const scene_id = tessellation.scene_ids[cell];
const neighbors = tessellation.neighbors[cell].map(index => tessellation.scene_ids[index]);
res.json({
neighbors,
location: {
ra: location[0],
dec: location[1]
},
scene_id
});
}
);
}