Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polygon functions #2547

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
finetjul marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 11 additions & 2 deletions Sources/Common/DataModel/Polygon/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export const PolygonWithPointIntersectionState = {
FAILURE: -1,
OUTSIDE: 0,
INSIDE: 1,
INTERSECTION: 2,
ON_LINE: 3,
finetjul marked this conversation as resolved.
Show resolved Hide resolved
};
export const VTK_DBL_EPSILON = 2.2204460492503131e-16;

export const PolygonWithCellIntersectionState = {
NO_INTERSECTION: 0,
POINT_INTERSECTION: 1,
LINE_INTERSECTION: 2,
OVERLAP: 3,
INCLUDED: 4,
};
finetjul marked this conversation as resolved.
Show resolved Hide resolved

export default { PolygonWithPointIntersectionState };
257 changes: 228 additions & 29 deletions Sources/Common/DataModel/Polygon/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,262 @@
import { vtkObject } from "../../../interfaces";
import { Bounds, TypedArray, Vector3 } from "../../../types";
import vtkPoints from "../../Core/Points";
import vtkCell from "../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 PolygonWithCellIntersectionState {
NO_INTERSECTION,
LINE_INTERSECTION,
POINT_INTERSECTION,
OVERLAP,
INCLUDED
}

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

interface IDistanceToPolygon {
finetjul marked this conversation as resolved.
Show resolved Hide resolved
t: number,
distance: number
}

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.
finetjul marked this conversation as resolved.
Show resolved Hide resolved
* @param {Array<number>} pointIds pointIds
finetjul marked this conversation as resolved.
Show resolved Hide resolved
*/
setPoints(points: Vector3[]|Array<number>, pointIds?: Array<number>): void;

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

/**
* Computes the polygon normal
finetjul marked this conversation as resolved.
Show resolved Hide resolved
* @return {number} norm of normal (before normalization)
*/
computeNormal(): number;

/**
* 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 (points non coplanar or on a line).
* 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;

/**
* determine the distance from a point to a polygon.
* @param {Vector3} x
* @param {Vector3} closest filled with the closest point in the polygon
* @return {IDistanceToPolygon} object containing the distance (distance) and the tolerance with wich the distance is given (t)
*/
distanceToPolygon(x: Vector3, closest: Vector3): IDistanceToPolygon;

/**
* 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 {PolygonWithCellIntersectionState} type of intersection
*/
intersectConvex2DCells(
cell: vtkCell
): PolygonWithCellIntersectionState;
}

// ---------------------------------------------------
/**
* Compute the normal of a polygon and return its squared norm.
* @param {vtkPoints} points
* @param {Vector3} normal
* @return {number}
*/
export function getNormal(
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;

/**
* Given a set of points, determine the distance from a point to a polygon.
* @param {Vector3} x
* @param {vtkPoints} points vtkPoints defining the polygon
* @param {Vector3} closest filled with the closest point in the polygon
* @return {IDistanceToPolygon} object containing the distance (distance) and the tolerance with wich the distance is given (t)
*/
export function distanceToPolygon(x: Vector3, points: vtkPoints, closest: Vector3): IDistanceToPolygon;

/**
* 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 {PolygonWithCellIntersectionState} type of intersection
*/
export function intersectConvex2DCells(
cell: vtkCell,
points: vtkPoints,
normal: Vector3
): PolygonWithCellIntersectionState;

/**
* 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 +265,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 +279,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