-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelpers.ts
190 lines (165 loc) · 6.07 KB
/
helpers.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
185
186
187
188
189
190
import type {
InteractiveWaypoint,
ProjectPathTrainResult,
Waypoint,
} from '@osrd-project/ui-manchette/dist/types';
import type { OperationalPoint } from '@osrd-project/ui-spacetimechart/dist/lib/types';
import { clamp } from 'lodash';
import {
BASE_WAYPOINT_HEIGHT,
MAX_TIME_WINDOW,
MAX_ZOOM_MS_PER_PX,
MAX_ZOOM_X,
MIN_ZOOM_MS_PER_PX,
MIN_ZOOM_X,
} from './consts';
import { calcTotalDistance, getHeightWithoutLastWaypoint, msToS } from './utils';
type WaypointsOptions = { isProportional: boolean; yZoom: number; height: number };
export const calcWaypointsToDisplay = (
waypoints: Waypoint[],
{ height, isProportional, yZoom }: WaypointsOptions
): InteractiveWaypoint[] => {
if (!isProportional || waypoints.length === 0) {
// For non-proportional display, we always display all the waypoints:
return waypoints.map((waypoint) => ({ ...waypoint, display: true }));
}
// For proportional display, we only display waypoints that do not overlap with
// the last displayed point:
const result: InteractiveWaypoint[] = [{ ...waypoints[0], display: true }];
const totalDistance = calcTotalDistance(waypoints);
const heightWithoutFinalWaypoint = getHeightWithoutLastWaypoint(height);
let lastDisplayedWaypoint = result[0];
// We iterate through all points, and only add them if they don't collide
// with the last visible point:
for (let i = 1; i < waypoints.length; i++) {
const waypoint = waypoints[i];
const diff = waypoint.position - lastDisplayedWaypoint.position;
const display =
(diff / totalDistance) * heightWithoutFinalWaypoint * yZoom >= BASE_WAYPOINT_HEIGHT;
if (display) {
result.push({ ...waypoint, display });
lastDisplayedWaypoint = waypoint;
}
}
// In the end, to make sure the last point is visible, if it's not, we swap
// it with the last visible item:
const lastItem = result[result.length - 1];
if (!lastItem.display) {
const lastVisibleItem = result.findLast((waypoint) => waypoint.display);
if (lastVisibleItem) {
lastVisibleItem.display = false;
lastItem.display = true;
}
}
return result;
};
export const calcWaypointsHeight = (
waypoints: InteractiveWaypoint[],
{ height, isProportional, yZoom }: WaypointsOptions
) => {
if (waypoints.length < 2) return [];
const totalDistance = calcTotalDistance(waypoints);
const heightWithoutFinalWaypoint = getHeightWithoutLastWaypoint(height);
return waypoints.map((waypoint, index) => {
const nextWaypoint = waypoints.at(index + 1);
if (!nextWaypoint) {
return { ...waypoint, styles: { height: `${BASE_WAYPOINT_HEIGHT}px` } };
}
if (isProportional) {
return {
...waypoint,
styles: {
height: `${
((nextWaypoint.position - waypoint.position) / totalDistance) *
heightWithoutFinalWaypoint *
yZoom
}px`,
},
};
} else {
return { ...waypoint, styles: { height: `${BASE_WAYPOINT_HEIGHT * yZoom}px` } };
}
});
};
export const computeTimeWindow = (trains: ProjectPathTrainResult[]) => {
const { minTime, maxTime } = trains.reduce(
(times, train) => {
if (train.spaceTimeCurves.length === 0) return times;
const lastCurve = train.spaceTimeCurves.at(-1);
if (!lastCurve || lastCurve.times.length < 2) return times;
const firstPoint = Number(train.departureTime);
const lastPoint = Number(train.departureTime) + lastCurve.times.at(-1)!;
return {
minTime: times.minTime === -1 || times.minTime > firstPoint ? firstPoint : times.minTime,
maxTime: times.maxTime === -1 || times.maxTime < lastPoint ? lastPoint : times.maxTime,
};
},
{ minTime: -1, maxTime: -1 }
);
const timeWindow = msToS(maxTime - minTime);
return timeWindow > MAX_TIME_WINDOW ? MAX_TIME_WINDOW : timeWindow;
};
export const getWaypointsWithPosition = (waypoints: InteractiveWaypoint[]): OperationalPoint[] =>
waypoints.map((point) => ({
id: point.id,
label: point.id,
position: point.position,
importanceLevel: 1,
}));
export const getScales = (
waypoints: Waypoint[],
{ height, isProportional, yZoom }: WaypointsOptions
) => {
if (waypoints.length < 2) return [];
if (!isProportional) {
return waypoints.slice(0, -1).map((from, index) => {
const to = waypoints[index + 1];
return {
from: from.position,
to: to.position,
size: BASE_WAYPOINT_HEIGHT * yZoom,
};
});
}
const from = waypoints.at(0)!.position;
const to = waypoints.at(-1)!.position;
const totalDistance = calcTotalDistance(waypoints);
const heightWithoutFinalWaypoint = getHeightWithoutLastWaypoint(height);
const scaleCoeff = isProportional
? { coefficient: totalDistance / heightWithoutFinalWaypoint / yZoom }
: { size: BASE_WAYPOINT_HEIGHT * (waypoints.length - 1) * yZoom };
return [
{
from,
to,
...scaleCoeff,
},
];
};
export const zoomValueToTimeScale = (slider: number) =>
MIN_ZOOM_MS_PER_PX * Math.pow(MAX_ZOOM_MS_PER_PX / MIN_ZOOM_MS_PER_PX, slider / 100);
export const timeScaleToZoomValue = (timeScale: number) =>
(100 * Math.log(timeScale / MIN_ZOOM_MS_PER_PX)) /
Math.log(MAX_ZOOM_MS_PER_PX / MIN_ZOOM_MS_PER_PX);
/** Zoom on X axis and center on the mouse position */
export const zoomX = (
currentXZoom: number,
currentXOffset: number,
newXZoom: number,
position: number
) => {
const boundedXZoom = clamp(newXZoom, MIN_ZOOM_X, MAX_ZOOM_X);
const oldTimeScale = zoomValueToTimeScale(currentXZoom);
const newTimeScale = zoomValueToTimeScale(boundedXZoom);
// by default zooming expands the graph around timeOrigin
// changing the offset to timeOriginShift alone keeps the left border in place.
// subtracting centerShift keeps the center in place
// (xOffset = how many px the timeOrigin is shifted to the right)
const timeOriginShift = (currentXOffset * oldTimeScale) / newTimeScale;
const centerShift = (position * oldTimeScale - position * newTimeScale) / newTimeScale;
const newOffset = timeOriginShift - centerShift;
return {
xZoom: boundedXZoom,
xOffset: newOffset,
};
};