Skip to content

Commit

Permalink
feat(polygon, testPolygon): add functions to polygon
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joannacirillo committed Aug 23, 2022
1 parent 2171b20 commit 28c5dea
Show file tree
Hide file tree
Showing 10 changed files with 1,601 additions and 197 deletions.
14 changes: 9 additions & 5 deletions Sources/Common/Core/PriorityQueue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ function vtkPriorityQueue(publicAPI, model) {
model.elements.splice(i, 0, { priority, element });
};

publicAPI.deleteById = (id) => {
model.elements = model.elements.filter(({ element }) => element !== id);
};

publicAPI.pop = () => {
if (model.elements.length > 0) {
return model.elements.shift().element;
const element = model.elements.reduce((prev, current) =>
prev.priority > current.priority ? prev : current
);
publicAPI.deleteById(element.element);
return element.element;
}

return null;
};

publicAPI.deleteById = (id) => {
model.elements = model.elements.filter(({ element }) => element.id !== id);
};

publicAPI.length = () => model.elements.length;
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/Common/DataModel/Cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ function vtkCell(publicAPI, model) {
cell.initialize(model.points, model.pointsIds);
};

publicAPI.getCellDimension = () => {}; // virtual
publicAPI.intersectWithLine = (p1, p2, tol, t, x, pcoords, subId) => {}; // virtual
publicAPI.getCellDimension = () => {
macro.vtkErrorMacro('vtkCell.getCellDimension is not implemented.');
}; // virtual
publicAPI.intersectWithLine = (p1, p2, tol, t, x, pcoords, subId) => {
macro.vtkErrorMacro('vtkCell.intersectWithLine is not implemented.');
}; // virtual
publicAPI.evaluatePosition = (
x,
closestPoint,
Expand Down
12 changes: 10 additions & 2 deletions Sources/Common/DataModel/Polygon/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export const PolygonWithPointIntersectionState = {
FAILURE: -1,
OUTSIDE: 0,
INSIDE: 1,
INTERSECTION: 2,
ON_LINE: 3,
};
export const VTK_DBL_EPSILON = 2.2204460492503131e-16;

export const PolygonWithPolygonIntersectionState = {
NO_INTERSECTION: 0,
POINT_INTERSECTION: 1,
LINE_INTERSECTION: 2,
COINCIDENT: 3,
};

export default { PolygonWithPointIntersectionState };
235 changes: 206 additions & 29 deletions Sources/Common/DataModel/Polygon/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,240 @@
import { vtkObject } from "../../../interfaces";
import { Bounds, TypedArray, Vector3 } from "../../../types";
import vtkPoints from "../../Core/Points";
import vtkCell from "../../Core/Cell";

export interface IPolygonInitialValues {
firstPoint?: Vector3,
pointCount?: number,
tris?: Vector3[],
pointCount?: number;
tris?: Vector3[];
}

/**
* Different states which pointInPolygon could return.
*/
export enum PolygonIntersectionState {
export enum PolygonWithPointIntersectionState {
FAILURE,
OUTSIDE,
INSIDE,
INTERSECTION,
ON_LINE,
}

/**
* Different states that intersectWith2DConvexCell could return.
*/
export enum PolygonWithPolygonIntersectionState {
NO_INTERSECTION,
LINE_INTERSECTION,
POINT_INTERSECTION,
COINCIDENT
}

interface IIntersectWithLine {
intersection: boolean;
betweenPoints: boolean;
t: number;
x: Vector3;
}

export interface vtkPolygon extends vtkObject {
/**
* Set the polygon's points
* Points must be ordered in counterclockwise order
* @param {Vector3[]|Array<number>} points The polygon's points.
*/
setPoints(points: Vector3[]|Array<number>): void;

/**
* Get the bounds for this mapper as [xmin, xmax, ymin, ymax, zmin, zmax].
* @return {Bounds} bounds
*/
getBounds(): Bounds

/**
* Computes the polygon normal
* @return {Vector3} squared norm before normalization
*/
computeNormal(): Vector3;

/**
* Get the array of triangles that triangulate the polygon.
* Determine whether a point is inside a polygon. The function uses a winding
* number calculation generalized to the 3D plane one which the polygon
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
* also return FAILURE to indicate a degenerate polygon. This implementation is
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
* Algorithms.
* @param {Vector3} point Point to check
* @return {PolygonWithPointIntersectionState} type of intersection
*/
getPointArray(): Vector3[];
pointInPolygon(point: Vector3): PolygonWithPointIntersectionState;

/**
* Set the polygon's points.
* @param {Vector3[]} points The polygon's points.
* Compute ear triangulation of current polygon
* The polygon must be convex and have at least 3 points
* @return {boolean} whether triangulation failed or not
*/
setPoints(points: Vector3[]): void;
triangulate(): boolean;

/**
* Triangulate this polygon.
* The output data must be accessed through `getPointArray`.
* The output data contains points by group of three: each three-group
* defines one triangle.
* Returns the centroid of this polygon
* @return {Vector3} centroid
*/
triangulate(): void;
computeCentroid(): Vector3;

/**
* Returns the area of the polygon
* @return {number} area
*/
computeArea(): number;

/**
* Returns whether the polygon is convex or not
* Returns false for degenerate polygon
* @return {boolean} is convex or not
*/
isConvex(): boolean;

/**
* Interpolates functions with polygon points
* @param {Vector3} point point to compute the interpolation on
* @param {boolean} useMVCInterpolation
* @return weights corresponding to each point of polygon parametrizing the given point
*/
interpolateFunctions(
point: Vector3,
useMVCInterpolation: boolean
): number[];

/**
* Computes intersection of polygon with a line defined by two points
* @param {Vector3} x1 first point of line
* @param {Vector3} x2 second point of line
* @return intersection point coordinates
*/
intersectWithLine(x1: Vector3, x2: Vector3): IIntersectWithLine;

/**
* Computes intersection of polygon with another cell.
* It can be a line, a point, no intersection or coincident
* Note: Expects both polygons/cell to be convex
* @param {vtkCell} cell polygon or any object extending from vtkCell with which to compute intersection
* Note : the function intersectWithLine need to be implemented on the class of the cell given
* @return {PolygonWithPolygonIntersectionState} type of intersection
*/
intersectConvex2DCells(
cell: cell
): PolygonWithPolygonIntersectionState;
}

// ---------------------------------------------------
/**
* Compute the normal of a polygon and return its squared norm.
* @param {Array<number>|TypedArray<number>} poly
* @param {vtkPoints} points
* @param {Vector3} normal
* @return {number}
*/
export function getNormal(
poly: Vector3[],
points: vtkPoints,
normal: Vector3
): number;

/**
* Get the bounds for these points as [xmin, xmax, ymin, ymax,zmin, zmax].
* @param {vtkPoints} points
* @return {Bounds}
*/
export function getBounds(points: vtkPoints): Bounds;

/**
* Determines whether a polygon is convex
* @param {vtkPoints} points vtkPoints defining the polygon
* @return {boolean} whether the polygon is convex or not
*/
export function isConvex(points: vtkPoints): boolean;

/**
* Given a set of points, computes the centroid of the corresponding polygon
* @param {vtkPoints} points vtkPoints defining the polygon
* @param {Vector3} normal normal to the polygon of which the centroid is computed
* @return {Vector3} centroid. Returns null for degenerate polygon
*/
export function computeCentroid(points: vtkPoints, normal: Vector3): Vector3;

/**
* Given a set of points, computes the area of the corresponding polygon
* @param {vtkPoints} points vtkPoints defining the polygon
* @param {Vector3} normal normal to the polygon of which the centroid is computed
* @return {number} area of polygon
*/
export function computeArea(points: vtkPoints, normal: Vector3): number;

/**
* Determine whether a point is inside a polygon. The function uses a winding
* number calculation generalized to the 3D plane one which the polygon
* resides. Returns 0 if point is not in the polygon; 1 if it is inside. Can
* also return -1 to indicate a degenerate polygon. This implementation is
* resides. Returns OUTSIDE if point is not in the polygon; INSIDE if it is inside. Can
* also return FAILURE to indicate a degenerate polygon. This implementation is
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
* Algorithms.
*
* @param {Vector3} point Point to check
* @param {Array<Number>|TypedArray} vertices Vertices of the polygon
* @param {Array<number>|TypedArray} vertices Vertices of the polygon
* @param {Bounds} bounds Bounds of the vertices
* @param {Vector3} normal Normal vector of the polygon
* @returns {PolygonIntersectionState} Integer indicating the type of intersection
* @return {PolygonWithPointIntersectionState} Integer indicating the type of intersection
*/
export function pointInPolygon(
point: Vector3,
vertices: Array<number>|TypedArray,
bounds: Bounds,
normal: Vector3
): PolygonIntersectionState;
point: Vector3,
vertices: Array<number> | TypedArray,
bounds: Bounds,
normal: Vector3
): PolygonWithPointIntersectionState;

/**
* Given a set of points that define a polygon, determines whether a line defined
* by two points intersect with the polygon. There can be no intersection, a point
* intersection or a line intersection.
* @param {Vector3} p1 first point of the line
* @param {Vector3} p2 second point of the line
* @param {vtkPoints} points points defining the polygon
* @param {Vector3} normal normal to the polygon
* @return {IIntersectWithLine} type of intersection
*/
export function intersectWithLine(
p1: Vector3,
p2: Vector3,
points: vtkPoints,
normal: Vector3
): IIntersectWithLine;

/**
* Given a set of points that define a polygon and another polygon, computes their
* intersection. It can be a line, a point, no intersection or coincident
* Note: Expects both polygons need to be convex
* @param {vtkCell} cell polygon or any object extending from vtkCell with which to compute intersection
* Note : the function intersectWithLine need to be implemented on the class of the cell given
* @param {vtkPoints} points points defining the polygon
* @param {Vector3} normal normal to the polygon
* @return {PolygonWithPolygonIntersectionState} type of intersection
*/
export function intersectConvex2DCells(
cell: vtkCell,
points: vtkPoints,
normal: Vector3
): PolygonWithPolygonIntersectionState;

/**
* Given a set of points, computes the weights corresponding to the interpolation of the
* given point with regard to the points of the polygon. The returned array corresponds to
* the weights and therefore its size is the number of points in the polygon
* @param {Vector3} point point we want the interpolation of
* @param {vtkPoints} points points defining the polygon
* @param {boolean} useMVCInterpolation whether to use MVC interpolation
*/
export function interpolateFunctions(
point: Vector3,
points: vtkPoints,
useMVCInterpolation: boolean
): Array<number>;

/**
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
Expand All @@ -69,7 +243,11 @@ export function pointInPolygon(
* @param model object on which data structure will be bounds (protected)
* @param {IPolygonInitialValues} [initialValues] (default: {})
*/
export function extend(publicAPI: object, model: object, initialValues?: IPolygonInitialValues): void;
export function extend(
publicAPI: object,
model: object,
initialValues?: IPolygonInitialValues
): void;

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

/**
* vtkPolygon represents a 2D n-sided polygon.
*
*
* The polygons cannot have any internal holes, and cannot self-intersect.
* Define the polygon with n-points ordered in the counter-clockwise direction.
* Do not repeat the last point.
*/
export declare const vtkPolygon: {
newInstance: typeof newInstance,
newInstance: typeof newInstance;
extend: typeof extend;
// static

};
export default vtkPolygon;
Loading

0 comments on commit 28c5dea

Please sign in to comment.