Skip to content

Commit 4584c83

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 and triangulate to return the triangles Updated ts definition
1 parent dc94f8e commit 4584c83

File tree

10 files changed

+1723
-217
lines changed

10 files changed

+1723
-217
lines changed

Sources/Common/Core/PriorityQueue/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ function vtkPriorityQueue(publicAPI, model) {
1515
model.elements.splice(i, 0, { priority, element });
1616
};
1717

18+
publicAPI.deleteById = (id) => {
19+
model.elements = model.elements.filter(({ element }) => element !== id);
20+
};
21+
1822
publicAPI.pop = () => {
1923
if (model.elements.length > 0) {
20-
return model.elements.shift().element;
24+
const element = model.elements.reduce((prev, current) =>
25+
prev.priority > current.priority ? prev : current
26+
);
27+
publicAPI.deleteById(element.element);
28+
return element.element;
2129
}
2230

2331
return null;
2432
};
2533

26-
publicAPI.deleteById = (id) => {
27-
model.elements = model.elements.filter(({ element }) => element.id !== id);
28-
};
29-
3034
publicAPI.length = () => model.elements.length;
3135
}
3236

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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ 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 PolygonWithCellIntersectionState = {
13+
NO_INTERSECTION: 0,
14+
POINT_INTERSECTION: 1,
15+
LINE_INTERSECTION: 2,
16+
OVERLAP: 3,
17+
INCLUDED: 4,
18+
};
19+
20+
export default { PolygonWithPointIntersectionState };
Lines changed: 228 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,262 @@
11
import { vtkObject } from "../../../interfaces";
22
import { Bounds, TypedArray, Vector3 } from "../../../types";
3+
import vtkPoints from "../../Core/Points";
4+
import vtkCell from "../Cell";
35

46
export interface IPolygonInitialValues {
5-
firstPoint?: Vector3,
6-
pointCount?: number,
7-
tris?: Vector3[],
7+
pointCount?: number;
8+
tris?: Vector3[];
89
}
910

1011
/**
1112
* Different states which pointInPolygon could return.
1213
*/
13-
export enum PolygonIntersectionState {
14+
export enum PolygonWithPointIntersectionState {
1415
FAILURE,
1516
OUTSIDE,
1617
INSIDE,
17-
INTERSECTION,
18-
ON_LINE,
18+
}
19+
20+
/**
21+
* Different states that intersectWith2DConvexCell could return.
22+
*/
23+
export enum PolygonWithCellIntersectionState {
24+
NO_INTERSECTION,
25+
LINE_INTERSECTION,
26+
POINT_INTERSECTION,
27+
OVERLAP,
28+
INCLUDED
29+
}
30+
31+
interface IIntersectWithLine {
32+
intersection: boolean;
33+
betweenPoints: boolean;
34+
t: number;
35+
x: Vector3;
36+
}
37+
38+
interface IDistanceToPolygon {
39+
t: number,
40+
distance: number
1941
}
2042

2143
export interface vtkPolygon extends vtkObject {
44+
/**
45+
* Set the polygon's points
46+
* Points must be ordered in counterclockwise order
47+
* @param {Vector3[]|Array<number>} points The polygon's points.
48+
* @param {Array<number>} pointIds pointIds
49+
*/
50+
setPoints(points: Vector3[]|Array<number>, pointIds?: Array<number>): void;
51+
52+
/**
53+
* Get the bounds for this polygon as [xmin, xmax, ymin, ymax, zmin, zmax].
54+
* @return {Bounds} bounds
55+
*/
56+
getBounds(): Bounds
57+
58+
/**
59+
* Computes the polygon normal
60+
* @return {number} norm of normal (before normalization)
61+
*/
62+
computeNormal(): number;
2263

2364
/**
24-
* Get the array of triangles that triangulate the polygon.
65+
* Determine whether a point is inside a polygon. The function uses a winding
66+
* number calculation generalized to the 3D plane one which the polygon
67+
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
68+
* also return FAILURE to indicate a degenerate polygon (points non coplanar or on a line).
69+
* This implementation is inspired by Dan Sunday's algorithm found in the book Practical
70+
* Geometry Algorithms.
71+
* @param {Vector3} point Point to check
72+
* @return {PolygonWithPointIntersectionState} type of intersection
2573
*/
26-
getPointArray(): Vector3[];
74+
pointInPolygon(point: Vector3): PolygonWithPointIntersectionState;
2775

2876
/**
29-
* Set the polygon's points.
30-
* @param {Vector3[]} points The polygon's points.
77+
* Compute ear triangulation of current polygon
78+
* The polygon must be convex and have at least 3 points
79+
* @return {boolean} whether triangulation failed or not
3180
*/
32-
setPoints(points: Vector3[]): void;
81+
triangulate(): boolean;
3382

3483
/**
35-
* Triangulate this polygon.
36-
* The output data must be accessed through `getPointArray`.
37-
* The output data contains points by group of three: each three-group
38-
* defines one triangle.
84+
* Returns the centroid of this polygon
85+
* @return {Vector3} centroid
3986
*/
40-
triangulate(): void;
87+
computeCentroid(): Vector3;
4188

89+
/**
90+
* Returns the area of the polygon
91+
* @return {number} area
92+
*/
93+
computeArea(): number;
94+
95+
/**
96+
* determine the distance from a point to a polygon.
97+
* @param {Vector3} x
98+
* @param {Vector3} closest filled with the closest point in the polygon
99+
* @return {IDistanceToPolygon} object containing the distance (distance) and the tolerance with wich the distance is given (t)
100+
*/
101+
distanceToPolygon(x: Vector3, closest: Vector3): IDistanceToPolygon;
102+
103+
/**
104+
* Returns whether the polygon is convex or not
105+
* Returns false for degenerate polygon
106+
* @return {boolean} is convex or not
107+
*/
108+
isConvex(): boolean;
109+
110+
/**
111+
* Interpolates functions with polygon points
112+
* @param {Vector3} point point to compute the interpolation on
113+
* @param {boolean} useMVCInterpolation
114+
* @return weights corresponding to each point of polygon parametrizing the given point
115+
*/
116+
interpolateFunctions(
117+
point: Vector3,
118+
useMVCInterpolation: boolean
119+
): number[];
120+
121+
/**
122+
* Computes intersection of polygon with a line defined by two points
123+
* @param {Vector3} x1 first point of line
124+
* @param {Vector3} x2 second point of line
125+
* @return intersection point coordinates
126+
*/
127+
intersectWithLine(x1: Vector3, x2: Vector3): IIntersectWithLine;
128+
129+
/**
130+
* Computes intersection of polygon with another cell.
131+
* It can be a line, a point, no intersection or coincident
132+
* Note: Expects both polygons/cell to be convex
133+
* @param {vtkCell} cell polygon or any object extending from vtkCell with which to compute intersection
134+
* Note : the function intersectWithLine need to be implemented on the class of the cell given
135+
* @return {PolygonWithCellIntersectionState} type of intersection
136+
*/
137+
intersectConvex2DCells(
138+
cell: vtkCell
139+
): PolygonWithCellIntersectionState;
42140
}
43141

142+
// ---------------------------------------------------
143+
/**
144+
* Compute the normal of a polygon and return its squared norm.
145+
* @param {vtkPoints} points
146+
* @param {Vector3} normal
147+
* @return {number}
148+
*/
149+
export function getNormal(
150+
points: vtkPoints,
151+
normal: Vector3
152+
): number;
153+
154+
/**
155+
* Get the bounds for these points as [xmin, xmax, ymin, ymax,zmin, zmax].
156+
* @param {vtkPoints} points
157+
* @return {Bounds}
158+
*/
159+
export function getBounds(points: vtkPoints): Bounds;
160+
161+
/**
162+
* Determines whether a polygon is convex
163+
* @param {vtkPoints} points vtkPoints defining the polygon
164+
* @return {boolean} whether the polygon is convex or not
165+
*/
166+
export function isConvex(points: vtkPoints): boolean;
167+
168+
/**
169+
* Given a set of points, computes the centroid of the corresponding polygon
170+
* @param {vtkPoints} points vtkPoints defining the polygon
171+
* @param {Vector3} normal normal to the polygon of which the centroid is computed
172+
* @return {Vector3} centroid. Returns null for degenerate polygon
173+
*/
174+
export function computeCentroid(points: vtkPoints, normal: Vector3): Vector3;
175+
176+
/**
177+
* Given a set of points, computes the area of the corresponding polygon
178+
* @param {vtkPoints} points vtkPoints defining the polygon
179+
* @param {Vector3} normal normal to the polygon of which the centroid is computed
180+
* @return {number} area of polygon
181+
*/
182+
export function computeArea(points: vtkPoints, normal: Vector3): number;
183+
184+
/**
185+
* Given a set of points, determine the distance from a point to a polygon.
186+
* @param {Vector3} x
187+
* @param {vtkPoints} points vtkPoints defining the polygon
188+
* @param {Vector3} closest filled with the closest point in the polygon
189+
* @return {IDistanceToPolygon} object containing the distance (distance) and the tolerance with wich the distance is given (t)
190+
*/
191+
export function distanceToPolygon(x: Vector3, points: vtkPoints, closest: Vector3): IDistanceToPolygon;
192+
44193
/**
45194
* Determine whether a point is inside a polygon. The function uses a winding
46195
* 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
196+
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
197+
* also return FAILURE to indicate a degenerate polygon. This implementation is
49198
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
50199
* Algorithms.
51200
*
52201
* @param {Vector3} point Point to check
53-
* @param {Array<Number>|TypedArray} vertices Vertices of the polygon
202+
* @param {Array<number>|TypedArray} vertices Vertices of the polygon
54203
* @param {Bounds} bounds Bounds of the vertices
55204
* @param {Vector3} normal Normal vector of the polygon
56-
* @returns {PolygonIntersectionState} Integer indicating the type of intersection
205+
* @return {PolygonWithPointIntersectionState} Integer indicating the type of intersection
57206
*/
58207
export function pointInPolygon(
59-
point: Vector3,
60-
vertices: Array<number>|TypedArray,
61-
bounds: Bounds,
62-
normal: Vector3
63-
): PolygonIntersectionState;
208+
point: Vector3,
209+
vertices: Array<number> | TypedArray,
210+
bounds: Bounds,
211+
normal: Vector3
212+
): PolygonWithPointIntersectionState;
213+
214+
/**
215+
* Given a set of points that define a polygon, determines whether a line defined
216+
* by two points intersect with the polygon. There can be no intersection, a point
217+
* intersection or a line intersection.
218+
* @param {Vector3} p1 first point of the line
219+
* @param {Vector3} p2 second point of the line
220+
* @param {vtkPoints} points points defining the polygon
221+
* @param {Vector3} normal normal to the polygon
222+
* @return {IIntersectWithLine} type of intersection
223+
*/
224+
export function intersectWithLine(
225+
p1: Vector3,
226+
p2: Vector3,
227+
points: vtkPoints,
228+
normal: Vector3
229+
): IIntersectWithLine;
230+
231+
/**
232+
* Given a set of points that define a polygon and another polygon, computes their
233+
* intersection. It can be a line, a point, no intersection or coincident
234+
* Note: Expects both polygons need to be convex
235+
* @param {vtkCell} cell polygon or any object extending from vtkCell with which to compute intersection
236+
* Note : the function intersectWithLine need to be implemented on the class of the cell given
237+
* @param {vtkPoints} points points defining the polygon
238+
* @param {Vector3} normal normal to the polygon
239+
* @return {PolygonWithCellIntersectionState} type of intersection
240+
*/
241+
export function intersectConvex2DCells(
242+
cell: vtkCell,
243+
points: vtkPoints,
244+
normal: Vector3
245+
): PolygonWithCellIntersectionState;
246+
247+
/**
248+
* Given a set of points, computes the weights corresponding to the interpolation of the
249+
* given point with regard to the points of the polygon. The returned array corresponds to
250+
* the weights and therefore its size is the number of points in the polygon
251+
* @param {Vector3} point point we want the interpolation of
252+
* @param {vtkPoints} points points defining the polygon
253+
* @param {boolean} useMVCInterpolation whether to use MVC interpolation
254+
*/
255+
export function interpolateFunctions(
256+
point: Vector3,
257+
points: vtkPoints,
258+
useMVCInterpolation: boolean
259+
): Array<number>;
64260

65261
/**
66262
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
@@ -69,7 +265,11 @@ export function pointInPolygon(
69265
* @param model object on which data structure will be bounds (protected)
70266
* @param {IPolygonInitialValues} [initialValues] (default: {})
71267
*/
72-
export function extend(publicAPI: object, model: object, initialValues?: IPolygonInitialValues): void;
268+
export function extend(
269+
publicAPI: object,
270+
model: object,
271+
initialValues?: IPolygonInitialValues
272+
): void;
73273

74274
/**
75275
* Method used to create a new instance of vtkPolygon.
@@ -79,15 +279,14 @@ export function newInstance(initialValues?: IPolygonInitialValues): vtkPolygon;
79279

80280
/**
81281
* vtkPolygon represents a 2D n-sided polygon.
82-
*
282+
*
83283
* The polygons cannot have any internal holes, and cannot self-intersect.
84284
* Define the polygon with n-points ordered in the counter-clockwise direction.
85285
* Do not repeat the last point.
86286
*/
87287
export declare const vtkPolygon: {
88-
newInstance: typeof newInstance,
288+
newInstance: typeof newInstance;
89289
extend: typeof extend;
90290
// static
91-
92291
};
93292
export default vtkPolygon;

0 commit comments

Comments
 (0)