-
Notifications
You must be signed in to change notification settings - Fork 408
/
FilledPolygons.js
executable file
·86 lines (73 loc) · 2.52 KB
/
FilledPolygons.js
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
// @flow
// Copyright (c) 2018-present, GM Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found in the LICENSE file in the root directory of this source tree.
// You may not use this file except in compliance with the License.
import earcut from "earcut";
import React from "react";
import type { Vec3, Point, PolygonType } from "../types";
import { shouldConvert, pointToVec3 } from "../utils/commandUtils";
import { getChildrenForHitmapWithOriginalMarker } from "../utils/getChildrenForHitmapDefaults";
import Triangles, { makeTrianglesCommand } from "./Triangles";
const NO_POSE = {
position: { x: 0, y: 0, z: 0 },
orientation: { x: 0, y: 0, z: 0, w: 0 },
};
const DEFAULT_SCALE = { x: 1, y: 1, z: 1 };
function flatten3D(points: Vec3[]): Float32Array {
const array = new Float32Array(points.length * 3);
for (let i = 0; i < points.length; i++) {
const [x, y, z] = points[i];
array[i * 3] = x;
array[i * 3 + 1] = y;
array[i * 3 + 2] = z;
}
return array;
}
function getEarcutPoints(points: Vec3[]): (Vec3 | Point)[] {
const flattenedPoints = flatten3D(points);
const indices = earcut(flattenedPoints, null, 3);
const newPoints = [];
for (let i = 0; i < indices.length; i++) {
const originalIndex = indices[i];
newPoints.push(points[originalIndex]);
}
return newPoints;
}
type Props = {
children: PolygonType[],
};
const generateTriangles = (polygons: PolygonType[]) => {
return polygons.map((poly) => {
// $FlowFixMe flow doesn't know how shouldConvert works
const points: Vec3[] = shouldConvert(poly.points) ? poly.points.map(pointToVec3) : poly.points;
const pose = poly.pose ? poly.pose : NO_POSE;
const earcutPoints = getEarcutPoints(points);
return {
...poly,
points: earcutPoints,
pose,
scale: DEFAULT_SCALE,
originalMarker: poly,
};
});
};
export const makeFilledPolygonsCommand = () => (regl: any) => {
const trianglesCommand = makeTrianglesCommand()(regl);
return (props: any) => {
trianglesCommand(generateTriangles(props), false);
};
};
// command to draw a filled polygon
function FilledPolygons({ children: polygons = [], ...rest }: Props) {
const triangles = generateTriangles(polygons);
// Overwrite the triangle's default getChildrenForHitmap because we want to event as if each triangle is a single
// polygon.
return (
<Triangles getChildrenForHitmap={getChildrenForHitmapWithOriginalMarker} {...rest}>
{triangles}
</Triangles>
);
}
export default FilledPolygons;