Skip to content

Commit

Permalink
Array.toReversed replaced by Array.reduceRight because of better comp… (
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev authored May 20, 2024
1 parent 436fb19 commit cfcac93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog.d/20240520_130757_boris_replaced_function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- Slice function may not work in Google Chrome < 110
(<https://github.com/cvat-ai/cvat/pull/7916>)
11 changes: 11 additions & 0 deletions cvat-canvas/src/typescript/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2019-2022 Intel Corporation
// Copyright (C) 2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -535,5 +536,15 @@ export function segmentsFromPoints(points: number[], circuit = false): Segment[]
}, []);
}

export function toReversed<T>(array: Array<T>): Array<T> {
// actually toReversed already exists in ESMA specification
// but not all CVAT customers uses a browser fresh enough to use it
// instead of using a library with polyfills I will prefer just to rewrite it with reduceRight
return array.reduceRight<Array<T>>((acc, val: T) => {
acc.push(val);
return acc;
}, []);
}

export type Segment = [[number, number], [number, number]];
export type PropType<T, Prop extends keyof T> = T[Prop];
12 changes: 5 additions & 7 deletions cvat-canvas/src/typescript/sliceHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (C) 2023 CVAT.ai Corporation
// Copyright (C) 2023-2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import * as SVG from 'svg.js';
import {
stringifyPoints, translateToCanvas, translateFromCanvas, translateToSVG,
findIntersection, zipChannels, Segment, findClosestPointOnSegment, segmentsFromPoints,
toReversed,
} from './shared';
import {
Geometry, SliceData, Configuration, CanvasHint,
Expand Down Expand Up @@ -294,8 +295,7 @@ export class SliceHandlerImpl implements SliceHandler {
const d2 = Math.sqrt((p2[0] - p[0]) ** 2 + (p2[1] - p[1]) ** 2);

if (d2 > d1) {
// @ts-ignore error TS2551 (need to update typescript up to 5.2)
contour2.push(...otherPoints.toReversed().flat());
contour2.push(...toReversed<[number, number]>(otherPoints).flat());
} else {
contour2.push(...otherPoints.flat());
}
Expand All @@ -312,8 +312,7 @@ export class SliceHandlerImpl implements SliceHandler {
...firstSegmentPoint, // first intersection
// intermediate points (reversed if intersections order was swopped)
...(firstSegmentIdx === firstIntersectedSegmentIdx ?
// @ts-ignore error TS2551 (need to update typescript up to 5.2)
intermediatePoints : intermediatePoints.toReversed()
intermediatePoints : toReversed<[number, number]>(intermediatePoints)
).flat(),
// second intersection
...secondSegmentPoint,
Expand All @@ -326,8 +325,7 @@ export class SliceHandlerImpl implements SliceHandler {
...firstSegmentPoint, // first intersection
// intermediate points (reversed if intersections order was swopped)
...(firstSegmentIdx === firstIntersectedSegmentIdx ?
// @ts-ignore error TS2551 (need to update typescript up to 5.2)
intermediatePoints : intermediatePoints.toReversed()
intermediatePoints : toReversed<[number, number]>(intermediatePoints)
).flat(),
...secondSegmentPoint,
// all the previous contours points N, N-1, .. until (including) the first intersected segment
Expand Down

0 comments on commit cfcac93

Please sign in to comment.