Skip to content

Commit a436863

Browse files
committed
feat(polygon, testPolygon): add functions to polygon
Translation from cpp implementation and add test to those functions Create functions as static functions and in publicAPI Fix pointInPolygon Updated ts definition
1 parent 2171b20 commit a436863

File tree

5 files changed

+1359
-141
lines changed

5 files changed

+1359
-141
lines changed

Sources/Common/DataModel/Cell/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ function vtkCell(publicAPI, model) {
104104
cell.initialize(model.points, model.pointsIds);
105105
};
106106

107-
publicAPI.getCellDimension = () => {}; // virtual
108-
publicAPI.intersectWithLine = (p1, p2, tol, t, x, pcoords, subId) => {}; // virtual
107+
publicAPI.getCellDimension = () => {
108+
macro.vtkErrorMacro('vtkCell.getCellDimension is not implemented.');
109+
}; // virtual
110+
publicAPI.intersectWithLine = (p1, p2, tol, t, x, pcoords, subId) => {
111+
macro.vtkErrorMacro('vtkCell.intersectWithLine is not implemented.');
112+
}; // virtual
109113
publicAPI.evaluatePosition = (
110114
x,
111115
closestPoint,

Sources/Common/DataModel/Polygon/Constants.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export const PolygonWithPointIntersectionState = {
66
FAILURE: -1,
77
OUTSIDE: 0,
88
INSIDE: 1,
9-
INTERSECTION: 2,
10-
ON_LINE: 3,
119
};
10+
export const VTK_DBL_EPSILON = 2.2204460492503131e-16;
11+
12+
export const PolygonWithPolygonIntersectionState = {
13+
NO_INTERSECTION: 0,
14+
POINT_INTERSECTION: 1,
15+
LINE_INTERSECTION: 2,
16+
};
17+
18+
export default { PolygonWithPointIntersectionState };
Lines changed: 194 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,236 @@
11
import { vtkObject } from "../../../interfaces";
22
import { Bounds, TypedArray, Vector3 } from "../../../types";
3+
import vtkPoints from "../../Core/Points";
34

45
export interface IPolygonInitialValues {
5-
firstPoint?: Vector3,
6-
pointCount?: number,
7-
tris?: Vector3[],
6+
pointCount?: number;
7+
tris?: Vector3[];
88
}
99

1010
/**
1111
* Different states which pointInPolygon could return.
1212
*/
13-
export enum PolygonIntersectionState {
13+
export enum PolygonWithPointIntersectionState {
1414
FAILURE,
1515
OUTSIDE,
1616
INSIDE,
17-
INTERSECTION,
18-
ON_LINE,
1917
}
2018

21-
export interface vtkPolygon extends vtkObject {
19+
/**
20+
* Different states which intersectWith2DConvexCell could return.
21+
*/
22+
export enum PolygonWithPolygonIntersectionState {
23+
NO_INTERSECTION,
24+
LINE_INTERSECTION,
25+
POINT_INTERSECTION,
26+
}
2227

28+
interface IIntersectWithLine {
29+
intersection: boolean;
30+
betweenPoints: boolean;
31+
t: number;
32+
x: Vector3;
33+
}
34+
35+
export interface vtkPolygon extends vtkObject {
2336
/**
2437
* Get the array of triangles that triangulate the polygon.
2538
*/
2639
getPointArray(): Vector3[];
2740

2841
/**
29-
* Set the polygon's points.
42+
* Set the polygon's points
43+
* Points must be ordered in counterclockwise order
3044
* @param {Vector3[]} points The polygon's points.
3145
*/
3246
setPoints(points: Vector3[]): void;
3347

3448
/**
35-
* Triangulate this polygon.
49+
* Computes the polygon normal
50+
* @return {number} squared norm before normalization
51+
*/
52+
computeNormal(): number;
53+
54+
/**
55+
* Triangulate this polygon.
3656
* The output data must be accessed through `getPointArray`.
3757
* The output data contains points by group of three: each three-group
3858
* defines one triangle.
3959
*/
4060
triangulate(): void;
4161

62+
/**
63+
* Determine whether a point is inside a polygon. The function uses a winding
64+
* number calculation generalized to the 3D plane one which the polygon
65+
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
66+
* also return FAILURE to indicate a degenerate polygon. This implementation is
67+
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
68+
* Algorithms.
69+
* @param {Vector3} point Point to check
70+
* @return {PolygonWithPointIntersectionState} type of intersection
71+
*/
72+
pointInPolygon(point: Vector3): PolygonWithPointIntersectionState;
73+
74+
/**
75+
* Compute ear triangulation of current polygon
76+
* The polygon must be convex and have at least 3 points
77+
* @return {boolean} whether triangulation failed or not
78+
*/
79+
triangulate(): boolean;
80+
81+
/**
82+
* Returns the centroid of this polygon
83+
* @return {Vector3} centroid
84+
*/
85+
computeCentroid(): Vector3;
86+
87+
/**
88+
* Returns the area of the polygon
89+
* @return {number} area
90+
*/
91+
computeArea(): number;
92+
93+
/**
94+
* Returns whether the polygon is convex or not
95+
* Returns false for degenerate polygon
96+
* @return {boolean} is convex or not
97+
*/
98+
isConvex(): boolean;
99+
100+
/**
101+
* Interpolates functions with polygon points
102+
* @param point point to compute the interpolation on
103+
* @param useMVCInterpolation
104+
* @return weights corresponding to each point of polygon parametrizing the given point
105+
*/
106+
interpolateFunctions(
107+
point: Vector3,
108+
useMVCInterpolation: boolean
109+
): Number[];
110+
111+
/**
112+
* Computes intersection of polygon with a line defined by two points
113+
* @param x1 first point of line
114+
* @param x2 second point of line
115+
* @return intersection point coordinates
116+
*/
117+
intersectWithLine(x1: Vector3, x2: Vector3): IIntersectWithLine;
118+
119+
/**
120+
* Computes intersection of polygon with another polygon.
121+
* It can be a line, a point or no intersection.
122+
* Note: Expects both polygons to be convex
123+
* @param polygon vtkPolygon with which to compute intersection
124+
* @return {PolygonWithPolygonIntersectionState} type of intersection
125+
*/
126+
intersectConvex2DCells(
127+
polygon: vtkPolygon
128+
): PolygonWithPolygonIntersectionState;
42129
}
43130

131+
// ---------------------------------------------------
132+
/**
133+
* Compute the normal of a polygon and return its squared norm.
134+
* @param {Array<number>|TypedArray<number>} poly
135+
* @param {vtkPoints} points
136+
* @param {Vector3} normal
137+
* @returns {number}
138+
*/
139+
export function getNormal(
140+
poly: Vector3[],
141+
points: vtkPoints,
142+
normal: Vector3
143+
): number;
144+
145+
/**
146+
* Determines whether a polygon is convex
147+
* @param points vtkPoints defining the polygon
148+
* @return {boolean} whether the polygon is convex or not
149+
*/
150+
export function isConvex(points: vtkPoints): boolean;
151+
152+
/**
153+
* Given a set of points, computes the centroid of the corresponding polygon
154+
* @param points vtkPoints defining the polygon
155+
* @param normal normal to the polygon of which the centroid is computed
156+
* @return {Vector3} centroid. Returns null for degenerate polygon
157+
*/
158+
export function computeCentroid(points: vtkPoints, normal: Vector3): Vector3;
159+
160+
/**
161+
* Given a set of points, computes the area of the corresponding polygon
162+
* @param points vtkPoints defining the polygon
163+
* @param normal normal to the polygon of which the centroid is computed
164+
* @return {number} area of polygon
165+
*/
166+
export function computeArea(points: vtkPoints, normal: Vector3): number;
167+
44168
/**
45169
* Determine whether a point is inside a polygon. The function uses a winding
46170
* number calculation generalized to the 3D plane one which the polygon
47-
* resides. Returns 0 if point is not in the polygon; 1 if it is inside. Can
48-
* also return -1 to indicate a degenerate polygon. This implementation is
171+
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
172+
* also return FAILURE to indicate a degenerate polygon. This implementation is
49173
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
50174
* Algorithms.
51175
*
52176
* @param {Vector3} point Point to check
53177
* @param {Array<Number>|TypedArray} vertices Vertices of the polygon
54178
* @param {Bounds} bounds Bounds of the vertices
55179
* @param {Vector3} normal Normal vector of the polygon
56-
* @returns {PolygonIntersectionState} Integer indicating the type of intersection
180+
* @returns {PolygonWithPointIntersectionState} Integer indicating the type of intersection
57181
*/
58182
export function pointInPolygon(
59-
point: Vector3,
60-
vertices: Array<number>|TypedArray,
61-
bounds: Bounds,
62-
normal: Vector3
63-
): PolygonIntersectionState;
183+
point: Vector3,
184+
vertices: Array<number> | TypedArray,
185+
bounds: Bounds,
186+
normal: Vector3
187+
): PolygonWithPointIntersectionState;
188+
189+
/**
190+
* Given a set of points that define a polygon, determines whether a line defined
191+
* by two points intersect with the polygon. There can be no intersection, a point
192+
* intersection or a line intersection.
193+
* @param p1 first point of the line
194+
* @param p2 second point of the line
195+
* @param points points defining the polygon
196+
* @param normal normal to the polygon
197+
* @return {IIntersectWithLine} type of intersection
198+
*/
199+
export function intersectWithLine(
200+
p1: Vector3,
201+
p2: Vector3,
202+
points: vtkPoints,
203+
normal: Vector3
204+
): IIntersectWithLine;
205+
206+
/**
207+
* Given a set of points that define a polygon and another polygon, computes their
208+
* intersection. It can be a line, a point or no intersection.
209+
* Note: Expects both polygons to be convex
210+
* @param polygon vtkPolygon with which to compute intersection
211+
* @param points points defining the polygon
212+
* @param normal normal to the polygon
213+
* @return {PolygonWithPolygonIntersectionState} type of intersection
214+
*/
215+
export function intersectConvex2DCells(
216+
polygon: vtkPolygon,
217+
points: vtkPoints,
218+
normal: Vector3
219+
): PolygonWithPolygonIntersectionState;
220+
221+
/**
222+
* Given a set of points, computes the weights corresponding to the interpolation of the
223+
* given point with regard to the points of the polygon. The returned array corresponds to
224+
* the weights and therefore its size is the number of points in the polygon
225+
* @param point point we want the interpolation of
226+
* @param points points defining the polygon
227+
* @param useMVCInterpolation whether to use MVC interpolation
228+
*/
229+
export function interpolateFunctions(
230+
point: Vector3,
231+
points: vtkPoints,
232+
useMVCInterpolation: boolean
233+
): Array<number>;
64234

65235
/**
66236
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
@@ -69,7 +239,11 @@ export function pointInPolygon(
69239
* @param model object on which data structure will be bounds (protected)
70240
* @param {IPolygonInitialValues} [initialValues] (default: {})
71241
*/
72-
export function extend(publicAPI: object, model: object, initialValues?: IPolygonInitialValues): void;
242+
export function extend(
243+
publicAPI: object,
244+
model: object,
245+
initialValues?: IPolygonInitialValues
246+
): void;
73247

74248
/**
75249
* Method used to create a new instance of vtkPolygon.
@@ -79,15 +253,14 @@ export function newInstance(initialValues?: IPolygonInitialValues): vtkPolygon;
79253

80254
/**
81255
* vtkPolygon represents a 2D n-sided polygon.
82-
*
256+
*
83257
* The polygons cannot have any internal holes, and cannot self-intersect.
84258
* Define the polygon with n-points ordered in the counter-clockwise direction.
85259
* Do not repeat the last point.
86260
*/
87261
export declare const vtkPolygon: {
88-
newInstance: typeof newInstance,
262+
newInstance: typeof newInstance;
89263
extend: typeof extend;
90264
// static
91-
92265
};
93266
export default vtkPolygon;

0 commit comments

Comments
 (0)