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
Updated ts definition
  • Loading branch information
joannacirillo committed Aug 10, 2022
1 parent 47d8ca9 commit 2f8adf9
Show file tree
Hide file tree
Showing 5 changed files with 1,322 additions and 362 deletions.
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
11 changes: 9 additions & 2 deletions Sources/Common/DataModel/Polygon/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ 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,
};

export default { PolygonWithPointIntersectionState };
265 changes: 208 additions & 57 deletions Sources/Common/DataModel/Polygon/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,172 @@
import { vtkObject } from "../../../interfaces";
import { Bounds, Vector3 } from "../../../types";
import { vtkObject } from '../../../interfaces';
import { Bounds, Vector3 } from '../../../types';
import vtkPoints from '../../Core/Points';

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

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

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

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

export interface vtkPolygon extends vtkObject {
/**
* Get the array of triangles that triangulate the polygon.
*/
getPointArray(): Vector3[];

/**
* Set the polygon's points
* Points must be ordered in counterclockwise order
* @param {Vector3[]} points The polygon's points.
*/
setPoints(points: Vector3[]): void;

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

/**
* 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.
*/
triangulate(): void;

/**
* 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
*/
pointInPolygon(point: Vector3): PolygonWithPointIntersectionState;

/**
* Compute ear triangulation of current polygon
* The polygon must be convex and have at least 3 points
* @return {boolean} whether triangulation failed or not
*/
triangulate(): boolean;

/**
* Get the array of triangles that triangulate the polygon.
*/
getPointArray(): Vector3[];

/**
* Set the polygon's points.
* @param {Vector3[]} points The polygon's points.
*/
setPoints(points: Vector3[]): void;

/**
* 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.
*/
triangulate(): void;

/**
* 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
* inspired by Dan Sunday's algorithm found in the book Practical Geometry
* Algorithms.
* @param {Vector3} point Point to check
* @param {Vector3[]} vertices Vertices of the polygon
* @param {Bounds} bounds Bounds of the vertices
* @param {Vector3} normal normal vector of the polygon
*/
pointInPolygon(
point: Vector3,
vertices: Vector3[],
bounds: Bounds,
normal: Vector3
): number;
/**
* Returns the centroid of this polygon
* @return {Vector3} centroid
*/
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 point point to compute the interpolation on
* @param 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 x1 first point of line
* @param x2 second point of line
* @return intersection point coordinates
*/
intersectWithLine(x1: Vector3, x2: Vector3): IIntersectWithLine;

/**
* Computes intersection of polygon with another polygon.
* It can be a line, a point or no intersection.
* Note: Expects both polygons to be convex
* @param polygon vtkPolygon with which to compute intersection
* @return {PolygonWithPolygonIntersectionState} type of intersection
*/
intersectConvex2DCells(
polygon: vtkPolygon
): PolygonWithPolygonIntersectionState;
}

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

/**
* Determines whether a polygon is convex
* @param 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 points vtkPoints defining the polygon
* @param 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 points vtkPoints defining the polygon
* @param 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.
*
Expand All @@ -75,19 +178,69 @@ export interface vtkPolygon extends vtkObject {
*/
export function pointInPolygon(
point: Vector3,
vertices: Array|TypedArray,
vertices: Array | TypedArray,
bounds: Bounds,
normal: Vector3
): PolygonIntersectionState;

/**
* 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 p1 first point of the line
* @param p2 second point of the line
* @param points points defining the polygon
* @param 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 or no intersection.
* Note: Expects both polygons to be convex
* @param polygon vtkPolygon with which to compute intersection
* @param points points defining the polygon
* @param normal normal to the polygon
* @return {PolygonWithPolygonIntersectionState} type of intersection
*/
export function intersectConvex2DCells(
polygon: vtkPolygon,
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 point point we want the interpolation of
* @param points points defining the polygon
* @param 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.
*
* @param publicAPI object on which methods will be bounds (public)
* @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 @@ -97,15 +250,13 @@ 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,
extend: typeof extend;
// static

newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkPolygon;
Loading

0 comments on commit 2f8adf9

Please sign in to comment.