forked from Climate-REF/ref-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSpatialIndex.ts
More file actions
202 lines (178 loc) · 5.29 KB
/
useSpatialIndex.ts
File metadata and controls
202 lines (178 loc) · 5.29 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import type * as d3 from "d3";
import { useMemo } from "react";
import type { SeriesMetadata } from "../types";
import type { XScaleType } from "./useChartScales";
import type { ChartDataPoint } from "./utils";
export interface IndexedPoint {
seriesIndex: number;
dataIndex: number;
x: number;
y: number;
pixelX: number;
pixelY: number;
}
interface SpatialGrid {
cells: Map<string, IndexedPoint[]>;
cellSize: number;
}
function cellKey(cx: number, cy: number): string {
return `${cx},${cy}`;
}
function buildGrid(
chartData: ChartDataPoint[],
seriesMetadata: SeriesMetadata[],
indexName: string,
xScale: XScaleType,
yScale: d3.ScaleLinear<number, number>,
cellSize: number,
): SpatialGrid {
const cells = new Map<string, IndexedPoint[]>();
for (const meta of seriesMetadata) {
const key = `series_${meta.seriesIndex}`;
for (let i = 0; i < chartData.length; i++) {
const xVal = chartData[i][indexName];
const yVal = chartData[i][key];
if (
typeof xVal !== "number" ||
typeof yVal !== "number" ||
!Number.isFinite(yVal)
) {
continue;
}
const pixelX = xScale(xVal);
const pixelY = yScale(yVal);
const cx = Math.floor(pixelX / cellSize);
const cy = Math.floor(pixelY / cellSize);
const k = cellKey(cx, cy);
const point: IndexedPoint = {
seriesIndex: meta.seriesIndex,
dataIndex: i,
x: xVal,
y: yVal,
pixelX,
pixelY,
};
const bucket = cells.get(k);
if (bucket) {
bucket.push(point);
} else {
cells.set(k, [point]);
}
}
}
return { cells, cellSize };
}
export interface NearestResult {
point: IndexedPoint;
metadata: SeriesMetadata;
distance: number;
}
export interface SpatialIndex {
findNearest: (
pixelX: number,
pixelY: number,
hiddenLabels: Set<string>,
maxDistance?: number,
) => NearestResult | null;
findNearestAtX: (
pixelX: number,
pixelY: number,
hiddenLabels: Set<string>,
) => NearestResult[];
}
export function useSpatialIndex(
chartData: ChartDataPoint[],
seriesMetadata: SeriesMetadata[],
indexName: string,
xScale: XScaleType,
yScale: d3.ScaleLinear<number, number>,
): SpatialIndex {
return useMemo(() => {
const cellSize = 30;
const grid = buildGrid(
chartData,
seriesMetadata,
indexName,
xScale,
yScale,
cellSize,
);
function findNearest(
pixelX: number,
pixelY: number,
hiddenLabels: Set<string>,
maxDistance = 50,
): NearestResult | null {
const cx = Math.floor(pixelX / cellSize);
const cy = Math.floor(pixelY / cellSize);
const searchRadius = Math.ceil(maxDistance / cellSize);
let best: NearestResult | null = null;
let bestDist = maxDistance * maxDistance;
for (let dx = -searchRadius; dx <= searchRadius; dx++) {
for (let dy = -searchRadius; dy <= searchRadius; dy++) {
const bucket = grid.cells.get(cellKey(cx + dx, cy + dy));
if (!bucket) continue;
for (const pt of bucket) {
const meta = seriesMetadata[pt.seriesIndex];
if (!meta || hiddenLabels.has(meta.label)) continue;
const distSq =
(pt.pixelX - pixelX) ** 2 + (pt.pixelY - pixelY) ** 2;
if (distSq < bestDist) {
bestDist = distSq;
best = { point: pt, metadata: meta, distance: Math.sqrt(distSq) };
}
}
}
}
return best;
}
function findNearestAtX(
pixelX: number,
pixelY: number,
hiddenLabels: Set<string>,
): NearestResult[] {
// Find the closest data index by X pixel position
const xVal = xScale.invert(pixelX) as number;
let closestDataIdx = 0;
let closestXDist = Number.POSITIVE_INFINITY;
for (let i = 0; i < chartData.length; i++) {
const v = chartData[i][indexName];
if (typeof v !== "number") continue;
const dist = Math.abs(v - xVal);
if (dist < closestXDist) {
closestXDist = dist;
closestDataIdx = i;
}
}
// Collect all visible series values at this data index
const results: NearestResult[] = [];
for (const meta of seriesMetadata) {
if (hiddenLabels.has(meta.label)) continue;
const key = `series_${meta.seriesIndex}`;
const yVal = chartData[closestDataIdx]?.[key];
if (typeof yVal !== "number" || !Number.isFinite(yVal)) continue;
const ptPixelX = xScale(chartData[closestDataIdx][indexName] as number);
const ptPixelY = yScale(yVal);
const dist = Math.sqrt(
(ptPixelX - pixelX) ** 2 + (ptPixelY - pixelY) ** 2,
);
results.push({
point: {
seriesIndex: meta.seriesIndex,
dataIndex: closestDataIdx,
x: chartData[closestDataIdx][indexName] as number,
y: yVal,
pixelX: ptPixelX,
pixelY: ptPixelY,
},
metadata: meta,
distance: dist,
});
}
// Sort by distance to cursor Y
results.sort((a, b) => a.distance - b.distance);
return results;
}
return { findNearest, findNearestAtX };
}, [chartData, seriesMetadata, indexName, xScale, yScale]);
}