This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change simplifies the support for working with rings and texture coordinates. The Ring now maintains distinct arrays of position and texture coordinates and has APIs to flatten the Ring into an Array of numbers. Signed-off-by: Roberto Raggi <roberto.raggi@here.com>
- Loading branch information
1 parent
702d21a
commit 02d43fe
Showing
4 changed files
with
342 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2017-2020 HERE Europe B.V. | ||
* Licensed under Apache 2.0, see full license in LICENSE | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ShapeUtils, Vector2 } from "three"; | ||
|
||
/** | ||
* A class representing a ring of a polygon geometry. | ||
*/ | ||
export class Ring { | ||
/** | ||
* The area of this `Ring`. | ||
*/ | ||
readonly area: number; | ||
|
||
/** | ||
* The winding of this `Ring`. | ||
*/ | ||
readonly winding: boolean; | ||
|
||
/** | ||
* The vertex stride. | ||
*/ | ||
readonly vertexStride: number; | ||
|
||
private m_contour?: number[]; | ||
|
||
constructor( | ||
readonly points: Vector2[], | ||
readonly textureCoords?: Vector2[], | ||
readonly extents: number = 4 * 1024 | ||
) { | ||
if (textureCoords !== undefined && textureCoords.length !== points.length) { | ||
throw new Error( | ||
`the array of texture coordinates must have the same number of elements of the array of points` | ||
); | ||
} | ||
|
||
this.vertexStride = 2; | ||
|
||
if (textureCoords !== undefined) { | ||
this.vertexStride = this.vertexStride + 2; | ||
} | ||
|
||
this.area = ShapeUtils.area(this.points); | ||
this.winding = this.area < 0; | ||
} | ||
|
||
/** | ||
* Returns a flattened `Array` containing the position and texture coordinates of this `Ring`. | ||
* | ||
* @param array The target `Array`. | ||
* @param offset Optional offset into the array. | ||
*/ | ||
toArray(array: number[] = [], offset: number = 0): number[] { | ||
this.points.forEach((p, i) => p.toArray(array, offset + this.vertexStride * i)); | ||
this.textureCoords?.forEach((p, i) => p.toArray(array, offset + this.vertexStride * i + 2)); | ||
return array; | ||
} | ||
|
||
/** | ||
* Returns a flattened `Array` containing the position and texture coordinates of this `Ring`. | ||
*/ | ||
get contour(): number[] { | ||
if (this.m_contour === undefined) { | ||
this.m_contour = this.toArray(); | ||
} | ||
return this.m_contour; | ||
} | ||
} |
Oops, something went wrong.