Skip to content

Commit cc099f4

Browse files
committed
refactor: use some functions from @skybrush/show-format instead of declaring them here
1 parent 154c52d commit cc099f4

7 files changed

Lines changed: 21 additions & 62 deletions

File tree

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@skybrush/math": "^1.0.1",
5454
"@skybrush/mui-components": "^6.9.0",
5555
"@skybrush/redux-toolkit": "^5.1.0",
56-
"@skybrush/show-format": "^5.5.1",
56+
"@skybrush/show-format": "^5.6.1",
5757
"@tippyjs/react": "^4.2.6",
5858
"@turf/boolean-contains": "^7.4.0",
5959
"@turf/buffer": "^7.4.0",

src/features/show/selectors/core.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import type {
1212
Trajectory,
1313
ValidationSettings,
1414
} from '@skybrush/show-format';
15-
import { EnvironmentType } from '@skybrush/show-format';
15+
import {
16+
EnvironmentType,
17+
getTrajectoriesFromSpecification,
18+
getTrajectoryDuration,
19+
} from '@skybrush/show-format';
1620

1721
import {
1822
getMinimumIndoorTakeoffSpacing,
@@ -32,7 +36,6 @@ import {
3236
type TakeoffHeadingSpecification,
3337
} from '../constants';
3438
import { SettingsSynchronizationStatus } from '../enums';
35-
import { getDurationOfTrajectory, isValidTrajectory } from '../trajectory';
3639
import type {
3740
CoordinateSystem,
3841
EnvironmentState,
@@ -222,18 +225,16 @@ export const getNumberOfDronesInShow: AppSelector<number> = createSelector(
222225
* undefined for all the drones that have no fixed trajectories in the mission.
223226
*/
224227
export const getTrajectories: AppSelector<Array<Trajectory | undefined>> =
225-
createSelector(getDroneSwarmSpecification, (swarm) =>
226-
swarm.map((drone) => {
227-
const trajectory = drone.settings.trajectory;
228-
return isValidTrajectory(trajectory) ? trajectory : undefined;
229-
})
228+
createSelector(
229+
(state: RootState) => state.show.data,
230+
(spec) => getTrajectoriesFromSpecification(spec)
230231
);
231232

232233
/**
233234
* Returns the total duration of the show, in seconds.
234235
*/
235236
export const getShowDuration = createSelector(getTrajectories, (trajectories) =>
236-
max(trajectories.map((x) => (x ? (getDurationOfTrajectory(x) ?? 0) : 0)))
237+
max(trajectories.map((x) => (x ? getTrajectoryDuration(x) : 0)))
237238
);
238239

239240
/**

src/features/show/selectors/js-core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import max from 'lodash-es/max';
1010
import { createCachedSelector } from 're-reselect';
1111

1212
import { convexHull2D, getCentroid } from '@skybrush/math';
13+
import { isValidTrajectory } from '@skybrush/show-format';
1314

1415
import { CommonClockId } from '~/features/clocks/types';
1516
import {
@@ -32,7 +33,6 @@ import {
3233
getMaximumHeightOfTrajectory,
3334
getMaximumHorizontalDistanceFromTakeoffPositionInTrajectory,
3435
getPointsOfTrajectory,
35-
isValidTrajectory,
3636
} from '../trajectory';
3737
import { makeSegmentSelectors, transformPoints } from './trajectory';
3838

src/features/show/selectors/trajectory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
SwarmSpecification,
88
TimeWindow,
99
} from '@skybrush/show-format';
10+
import { isValidTrajectory } from '@skybrush/show-format';
1011

1112
import { type GPSPosition } from '~/model/geography';
1213
import { type AppSelector } from '~/store/reducers';
@@ -17,7 +18,6 @@ import { EMPTY_ARRAY } from '~/utils/redux';
1718
import {
1819
getConvexHullOfTrajectory,
1920
getTrajectoryInTimeWindow,
20-
isValidTrajectory,
2121
} from '../trajectory';
2222
import {
2323
isOutdoorCoordinateSystemWithOrigin,

src/features/show/trajectory.ts

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import isObject from 'lodash-es/isObject';
21
import max from 'lodash-es/max';
32

43
import { convexHull2D, euclideanDistance2D } from '@skybrush/math';
54
import {
5+
getTrajectorySegmentsInTimeWindow,
6+
isValidTrajectory,
67
type TimeWindow,
78
type Trajectory,
89
type TrajectorySegment,
9-
trajectorySegmentsInTimeWindow,
1010
} from '@skybrush/show-format';
1111

1212
import type { Coordinate2D, Coordinate3D } from '~/utils/math';
@@ -119,27 +119,6 @@ export const getPointsOfTrajectory = (
119119
return points.map((point) => point[1]);
120120
};
121121

122-
/**
123-
* Returns the duration of a single drone trajectory, in seconds.
124-
*/
125-
export const getDurationOfTrajectory = (
126-
trajectory: Trajectory
127-
): number | undefined => {
128-
if (!isValidTrajectory(trajectory)) {
129-
return;
130-
}
131-
132-
const { points, takeoffTime } = trajectory;
133-
134-
// TODO: `isValidTrajectory` already ensures `points.length > 0`...
135-
if (points.length > 0) {
136-
const lastPoint = points.at(-1);
137-
if (Array.isArray(lastPoint) && lastPoint.length > 1) {
138-
return lastPoint[0] + (takeoffTime ?? 0);
139-
}
140-
}
141-
};
142-
143122
/**
144123
* Returns the subtrajectory of the given trajectory that is within the given time window.
145124
*
@@ -155,7 +134,7 @@ export function getTrajectoryInTimeWindow(
155134
): Trajectory {
156135
return {
157136
...trajectory,
158-
points: trajectorySegmentsInTimeWindow(
137+
points: getTrajectorySegmentsInTimeWindow(
159138
trajectory.points,
160139
timeWindow
161140
// TODO: Get rid of this type assertion! It only holds if the given
@@ -164,22 +143,3 @@ export function getTrajectoryInTimeWindow(
164143
) as Trajectory['points'],
165144
};
166145
}
167-
168-
/**
169-
* Returns whether a trajectory object "looks like" a valid trajectory.
170-
*
171-
* TODO: Add validation for the optional `takeoffTime` and `landingTime` fields
172-
* Also, maybe this function should be in `@skybrush/show-format` instead
173-
*/
174-
export const isValidTrajectory = (
175-
trajectory: unknown
176-
): trajectory is Trajectory =>
177-
// prettier-ignore
178-
isObject(trajectory)
179-
// `version` is valid
180-
&& 'version' in trajectory
181-
&& trajectory.version === 1
182-
// `points` is a valid, non-empty array
183-
&& 'points' in trajectory
184-
&& Array.isArray(trajectory.points)
185-
&& trajectory.points.length > 0;

src/features/uavs/selectors.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
euclideanDistance2D,
1212
getMeanAngle,
1313
} from '@skybrush/math';
14+
import { isValidTrajectory } from '@skybrush/show-format';
1415

1516
import { Status } from '~/components/semantics';
1617
import {
@@ -34,10 +35,7 @@ import {
3435
getTrajectories,
3536
isShowIndoor,
3637
} from '~/features/show/selectors';
37-
import {
38-
getPointsOfTrajectory,
39-
isValidTrajectory,
40-
} from '~/features/show/trajectory';
38+
import { getPointsOfTrajectory } from '~/features/show/trajectory';
4139
import {
4240
errorSeverityToSemantics,
4341
getSeverityOfErrorCode,

0 commit comments

Comments
 (0)