Skip to content

Commit 2f8adf9

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 Updated ts definition
1 parent 47d8ca9 commit 2f8adf9

File tree

5 files changed

+1322
-362
lines changed

5 files changed

+1322
-362
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: 208 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,172 @@
1-
import { vtkObject } from "../../../interfaces";
2-
import { Bounds, Vector3 } from "../../../types";
1+
import { vtkObject } from '../../../interfaces';
2+
import { Bounds, 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 POINT_IN_POLYGON {
14-
FAILURE,
15-
OUTSIDE,
16-
INSIDE,
17-
INTERSECTION,
18-
ON_LINE,
13+
export enum PolygonWithPointIntersectionState {
14+
FAILURE,
15+
OUTSIDE,
16+
INSIDE,
17+
}
18+
19+
/**
20+
* Different states which intersectWith2DConvexCell could return.
21+
*/
22+
export enum PolygonWithPolygonIntersectionState {
23+
NO_INTERSECTION,
24+
LINE_INTERSECTION,
25+
POINT_INTERSECTION,
26+
}
27+
28+
interface IIntersectWithLine {
29+
intersection: boolean;
30+
betweenPoints: boolean;
31+
t: number;
32+
x: Vector3;
1933
}
2034

2135
export interface vtkPolygon extends vtkObject {
36+
/**
37+
* Get the array of triangles that triangulate the polygon.
38+
*/
39+
getPointArray(): Vector3[];
40+
41+
/**
42+
* Set the polygon's points
43+
* Points must be ordered in counterclockwise order
44+
* @param {Vector3[]} points The polygon's points.
45+
*/
46+
setPoints(points: Vector3[]): void;
47+
48+
/**
49+
* Computes the polygon normal
50+
* @return {number} squared norm before normalization
51+
*/
52+
computeNormal(): number;
53+
54+
/**
55+
* Triangulate this polygon.
56+
* The output data must be accessed through `getPointArray`.
57+
* The output data contains points by group of three: each three-group
58+
* defines one triangle.
59+
*/
60+
triangulate(): void;
61+
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;
2280

23-
/**
24-
* Get the array of triangles that triangulate the polygon.
25-
*/
26-
getPointArray(): Vector3[];
27-
28-
/**
29-
* Set the polygon's points.
30-
* @param {Vector3[]} points The polygon's points.
31-
*/
32-
setPoints(points: Vector3[]): void;
33-
34-
/**
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.
39-
*/
40-
triangulate(): void;
41-
42-
/**
43-
* Determine whether a point is inside a polygon. The function uses a winding
44-
* number calculation generalized to the 3D plane one which the polygon
45-
* resides. Returns 0 if point is not in the polygon; 1 if it is inside. Can
46-
* also return -1 to indicate a degenerate polygon. This implementation is
47-
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
48-
* Algorithms.
49-
* @param {Vector3} point Point to check
50-
* @param {Vector3[]} vertices Vertices of the polygon
51-
* @param {Bounds} bounds Bounds of the vertices
52-
* @param {Vector3} normal normal vector of the polygon
53-
*/
54-
pointInPolygon(
55-
point: Vector3,
56-
vertices: Vector3[],
57-
bounds: Bounds,
58-
normal: Vector3
59-
): number;
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(point: Vector3, useMVCInterpolation: boolean): Number[];
107+
108+
/**
109+
* Computes intersection of polygon with a line defined by two points
110+
* @param x1 first point of line
111+
* @param x2 second point of line
112+
* @return intersection point coordinates
113+
*/
114+
intersectWithLine(x1: Vector3, x2: Vector3): IIntersectWithLine;
115+
116+
/**
117+
* Computes intersection of polygon with another polygon.
118+
* It can be a line, a point or no intersection.
119+
* Note: Expects both polygons to be convex
120+
* @param polygon vtkPolygon with which to compute intersection
121+
* @return {PolygonWithPolygonIntersectionState} type of intersection
122+
*/
123+
intersectConvex2DCells(
124+
polygon: vtkPolygon
125+
): PolygonWithPolygonIntersectionState;
60126
}
61127

128+
// ---------------------------------------------------
129+
/**
130+
* Compute the normal of a polygon and return its squared norm.
131+
* @param {Array<number>|TypedArray<number>} poly
132+
* @param {vtkPoints} points
133+
* @param {Vector3} normal
134+
* @returns {number}
135+
*/
136+
export function getNormal(
137+
poly: Vector3[],
138+
points: vtkPoints,
139+
normal: Vector3
140+
): number;
141+
142+
/**
143+
* Determines whether a polygon is convex
144+
* @param points vtkPoints defining the polygon
145+
* @return {boolean} whether the polygon is convex or not
146+
*/
147+
export function isConvex(points: vtkPoints): boolean;
148+
149+
/**
150+
* Given a set of points, computes the centroid of the corresponding polygon
151+
* @param points vtkPoints defining the polygon
152+
* @param normal normal to the polygon of which the centroid is computed
153+
* @return {Vector3} centroid. Returns null for degenerate polygon
154+
*/
155+
export function computeCentroid(points: vtkPoints, normal: Vector3): Vector3;
156+
157+
/**
158+
* Given a set of points, computes the area of the corresponding polygon
159+
* @param points vtkPoints defining the polygon
160+
* @param normal normal to the polygon of which the centroid is computed
161+
* @return {number} area of polygon
162+
*/
163+
export function computeArea(points: vtkPoints, normal: Vector3): number;
164+
62165
/**
63166
* Determine whether a point is inside a polygon. The function uses a winding
64167
* number calculation generalized to the 3D plane one which the polygon
65-
* resides. Returns 0 if point is not in the polygon; 1 if it is inside. Can
66-
* also return -1 to indicate a degenerate polygon. This implementation is
168+
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
169+
* also return FAILURE to indicate a degenerate polygon. This implementation is
67170
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
68171
* Algorithms.
69172
*
@@ -75,19 +178,69 @@ export interface vtkPolygon extends vtkObject {
75178
*/
76179
export function pointInPolygon(
77180
point: Vector3,
78-
vertices: Array|TypedArray,
181+
vertices: Array | TypedArray,
79182
bounds: Bounds,
80183
normal: Vector3
81184
): PolygonIntersectionState;
82185

186+
/**
187+
* Given a set of points that define a polygon, determines whether a line defined
188+
* by two points intersect with the polygon. There can be no intersection, a point
189+
* intersection or a line intersection.
190+
* @param p1 first point of the line
191+
* @param p2 second point of the line
192+
* @param points points defining the polygon
193+
* @param normal normal to the polygon
194+
* @return {IIntersectWithLine} type of intersection
195+
*/
196+
export function intersectWithLine(
197+
p1: Vector3,
198+
p2: Vector3,
199+
points: vtkPoints,
200+
normal: Vector3
201+
): IIntersectWithLine;
202+
203+
/**
204+
* Given a set of points that define a polygon and another polygon, computes their
205+
* intersection. It can be a line, a point or no intersection.
206+
* Note: Expects both polygons to be convex
207+
* @param polygon vtkPolygon with which to compute intersection
208+
* @param points points defining the polygon
209+
* @param normal normal to the polygon
210+
* @return {PolygonWithPolygonIntersectionState} type of intersection
211+
*/
212+
export function intersectConvex2DCells(
213+
polygon: vtkPolygon,
214+
points: vtkPoints,
215+
normal: Vector3
216+
): PolygonWithPolygonIntersectionState;
217+
218+
/**
219+
* Given a set of points, computes the weights corresponding to the interpolation of the
220+
* given point with regard to the points of the polygon. The returned array corresponds to
221+
* the weights and therefore its size is the number of points in the polygon
222+
* @param point point we want the interpolation of
223+
* @param points points defining the polygon
224+
* @param useMVCInterpolation whether to use MVC interpolation
225+
*/
226+
export function interpolateFunctions(
227+
point: Vector3,
228+
points: vtkPoints,
229+
useMVCInterpolation: boolean
230+
): Array<number>;
231+
83232
/**
84233
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
85234
*
86235
* @param publicAPI object on which methods will be bounds (public)
87236
* @param model object on which data structure will be bounds (protected)
88237
* @param {IPolygonInitialValues} [initialValues] (default: {})
89238
*/
90-
export function extend(publicAPI: object, model: object, initialValues?: IPolygonInitialValues): void;
239+
export function extend(
240+
publicAPI: object,
241+
model: object,
242+
initialValues?: IPolygonInitialValues
243+
): void;
91244

92245
/**
93246
* Method used to create a new instance of vtkPolygon.
@@ -97,15 +250,13 @@ export function newInstance(initialValues?: IPolygonInitialValues): vtkPolygon;
97250

98251
/**
99252
* vtkPolygon represents a 2D n-sided polygon.
100-
*
253+
*
101254
* The polygons cannot have any internal holes, and cannot self-intersect.
102255
* Define the polygon with n-points ordered in the counter-clockwise direction.
103256
* Do not repeat the last point.
104257
*/
105258
export declare const vtkPolygon: {
106-
newInstance: typeof newInstance,
107-
extend: typeof extend;
108-
// static
109-
259+
newInstance: typeof newInstance;
260+
extend: typeof extend;
110261
};
111262
export default vtkPolygon;

0 commit comments

Comments
 (0)