-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Triangulate ourselves w/ earcut (fix with holes) (#51)
* Custom earcut * type ignore
- Loading branch information
1 parent
a4467a1
commit 9a3c7d6
Showing
11 changed files
with
113 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { earcut } from "@math.gl/polygon"; | ||
import { PolygonData } from "./types"; | ||
import { getLineStringChild, getPointChild, getPolygonChild } from "./utils"; | ||
|
||
export function earcutPolygonArray(data: PolygonData): Uint32Array { | ||
const trianglesResults: number[][] = []; | ||
let outputSize = 0; | ||
for (let geomIndex = 0; geomIndex < data.length; geomIndex++) { | ||
const triangles = earcutSinglePolygon(data, geomIndex); | ||
trianglesResults.push(triangles); | ||
outputSize += triangles.length; | ||
} | ||
|
||
const outputArray = new Uint32Array(outputSize); | ||
let idx = 0; | ||
for (const triangles of trianglesResults) { | ||
for (const value of triangles) { | ||
outputArray[idx] = value; | ||
idx += 1; | ||
} | ||
} | ||
|
||
return outputArray; | ||
} | ||
|
||
function earcutSinglePolygon(data: PolygonData, geomIndex: number): number[] { | ||
const geomOffsets = data.valueOffsets; | ||
const rings = getPolygonChild(data); | ||
const ringOffsets = rings.valueOffsets; | ||
|
||
const coords = getLineStringChild(rings); | ||
const dim = coords.type.listSize; | ||
const flatCoords = getPointChild(coords); | ||
|
||
const ringBegin = geomOffsets[geomIndex]; | ||
const ringEnd = geomOffsets[geomIndex + 1]; | ||
|
||
const coordsBegin = ringOffsets[ringBegin]; | ||
const coordsEnd = ringOffsets[ringEnd]; | ||
|
||
const slicedFlatCoords = flatCoords.values.subarray( | ||
coordsBegin * dim, | ||
coordsEnd * dim | ||
); | ||
|
||
const initialCoordIndex = ringOffsets[ringBegin]; | ||
const holeIndices = []; | ||
for (let holeRingIdx = ringBegin + 1; holeRingIdx < ringEnd; holeRingIdx++) { | ||
holeIndices.push(ringOffsets[holeRingIdx] - initialCoordIndex); | ||
} | ||
// @ts-expect-error earcut typing is off. Should allow TypedArray but here | ||
// only says number[] | ||
const triangles = earcut(slicedFlatCoords, holeIndices, dim); | ||
|
||
for (let i = 0; i < triangles.length; i++) { | ||
triangles[i] += initialCoordIndex; | ||
} | ||
|
||
return triangles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters