From beef02e9fe4e40ea180431deeafde2e54e2e1158 Mon Sep 17 00:00:00 2001 From: Yoshihiro Fujimoto Date: Fri, 24 Feb 2023 00:18:48 +0900 Subject: [PATCH] chore: add types --- package.json | 4 +- types/index.d.ts | 4 ++ types/polygon.d.ts | 137 ++++++++++++++++++++++++++++++++++++++++++ types/tesselator.d.ts | 83 +++++++++++++++++++++++++ types/vec.d.ts | 64 ++++++++++++++++++++ 5 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 types/index.d.ts create mode 100644 types/polygon.d.ts create mode 100644 types/tesselator.d.ts create mode 100644 types/vec.d.ts diff --git a/package.json b/package.json index 8fb4805..763e9ab 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "boolean" ], "main": "lib/index.js", + "types": "types", "dependencies": { "libtess": "^1.2.2" }, @@ -45,7 +46,8 @@ "test": "mocha -c", "build": "gulp build", "deploy": "gulp deploy", - "docs": "jsdoc --readme README.md -r src -d docs -c jsdoc/jsdoc.conf" + "docs": "jsdoc --readme README.md -r src -d docs -c jsdoc/jsdoc.conf", + "types": "npx -p typescript tsc src/*.js --declaration --allowJs --emitDeclarationOnly --outDir types" }, "license": "MIT" } diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..8696978 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,4 @@ +export const polygon: typeof p; +export const tesselator: typeof tess; +import * as p from "./polygon"; +import * as tess from "./tesselator"; diff --git a/types/polygon.d.ts b/types/polygon.d.ts new file mode 100644 index 0000000..0146f82 --- /dev/null +++ b/types/polygon.d.ts @@ -0,0 +1,137 @@ +export function ccw(a: any, b: any, c: any): number; +/** + * Polygon normal (2d / 3d) + * + * @param {Array} pts Points of the polygon + * @param {Boolean} [forceNewell=false] Whether to force Newell's method + * + * @return {Array} Polygon normal or null if the polygon is degenerate + */ +export function normal(pts: any[], forceNewell?: boolean): any[]; +/** + * Signed area of a polygon. + * For 3d polygons a signed area can only be computed when the optional + * polygon normal ```n``` is passed in. + * @see http://stackoverflow.com/questions/12642256/python-find-area-of-polygon-from-xyz-coordinates + * + * @param {Array} pts Polygon points + * @param {Array} [n=null] Optional polygon normal, needed to compute the signed area for 3d polygons + * + * @return {Number} + */ +export function area(pts: any[], n?: any[]): number; +/** + * Polygon centroid (2d) + * + * @param {Array} pts + * + * @return {Array} + */ +export function centroid(pts: any[]): any[]; +/** + * Tests wether the polygon winding is counter clockwise + * + * @param {Array} pts + * @param {Array} [n=null] Optional polygon normal, needed for 3d polygons + * + * @return {Boolean} + */ +export function is_ccw(pts: any[], n?: any[]): boolean; +/** + * Tests wether the polygon winding is clockwise + * + * @param {Array} pts + * @param {Array} [n=null] Optional polygon normal, needed for 3d polygons + * + * @return {Boolean} + */ +export function is_cw(pts: any[], n?: any[]): boolean; +/** + * Polygon winding (2d only) + * + * @param {Array} pts + * @param {Array} [n=null] Optional polygon normal, needed for 3d polygons + * + * @return {Number} + */ +export function winding(pts: any[], n?: any[]): number; +/** + * Polygon bounds. + * @typedef {Object} PolygonBounds + * @property {Number} xMin + * @property {Number} yMin + * @property {Number} xMax + * @property {Number} yMax + */ +/** + * Polygon bounds + * + * @param {Array} pts + * + * @return {PolygonBounds} + */ +export function bounds(pts: any[]): PolygonBounds; +/** + * Ensures CW winding + * + * @param {Array} pts + * @param {Array} [n=null] Optional polygon normal, needed for 3d polygons + * + * @return {Array} + */ +export function ensure_cw(pts: any[], n?: any[]): any[]; +/** + * Ensures CCW winding + * + * @param {Array} pts + * @param {Array} [n=null] Optional polygon normal, needed for 3d polygons + * + * @return {Array} + */ +export function ensure_ccw(pts: any[], n?: any[]): any[]; +/** + * Triangulates a polygon + * + * @param {Array} polygon + * @param {Array.} holes + * + * @return triangles + */ +export function triangulate(polygon: any[], holes: Array): any; +/** + * Subtract polygons + * + * @param {Array} polygons + * + * @return {Array} + */ +export function subtract(...polygons: any[]): any[]; +/** + * Union of a set of polygons + * + * @param {Array} polygons + * + * @return {Array} + */ +export function union(...polygons: any[]): any[]; +/** + * Intersection of a set of polygons + * + * @param {Array} a First polygon + * @param {Array} b Second polygon + * + * @return {Array} + */ +export function intersection(a: any[], b: any[]): any[]; +export const WINDING_UNKNOWN: 0; +export const WINDING_CCW: 1; +export const WINDING_CW: 2; +/** + * Polygon bounds. + */ +export type PolygonBounds = { + xMin: number; + yMin: number; + xMax: number; + yMax: number; +}; diff --git a/types/tesselator.d.ts b/types/tesselator.d.ts new file mode 100644 index 0000000..adecb23 --- /dev/null +++ b/types/tesselator.d.ts @@ -0,0 +1,83 @@ +/** + * Runs the tesselator + * @see http://www.glprogramming.com/red/chapter11.html + * + * @param {TesselatorOptions} [options=TesselatorOptions] Options + * + * @returns {Array} + */ +export function run(options?: TesselatorOptions): any[]; +export const GL_LINE_LOOP: any; +export const GL_TRIANGLES: any; +export const GL_TRIANGLE_STRIP: any; +export const GL_TRIANGLE_FAN: any; +export const GLU_TESS_WINDING_ODD: any; +export const GLU_TESS_WINDING_NONZERO: any; +export const GLU_TESS_WINDING_POSITIVE: any; +export const GLU_TESS_WINDING_NEGATIVE: any; +export const GLU_TESS_WINDING_ABS_GEQ_TWO: any; +export namespace DEFAULT_OPTIONS { + export const polygons: any[]; + export const holes: any[]; + export { GLU_TESS_WINDING_POSITIVE as windingRule }; + export const boundaryOnly: boolean; + export const normal: any; + export const autoWinding: boolean; +} +export class Tesselator { + constructor(vsize?: number); + _vsize: number; + _current: any[]; + _out: any[]; + _primitiveType: number; + start(polygons: any, holes: any): void; + run(options?: { + polygons: any[]; + holes: any[]; + windingRule: any; + boundaryOnly: boolean; + normal: any; + autoWinding: boolean; + }): any[]; + _begin(type: any): void; + _end_fan(): void; + _end_strip(): void; + _end(): void; + _vertex(v: any): void; + _edge(): void; + _error(errno: any): void; + _combine(v: any, data: any, w: any): any[]; +} +/** + * Tesselator options. + */ +export type TesselatorOptions = { + /** + * Array of polygons + */ + polygons?: any[]; + /** + * Array of holes + */ + holes?: any[]; + /** + * Vertex size to use + */ + vertexSize?: number; + /** + * Winding rule + */ + windingRule?: number; + /** + * Whether to output boundaries only + */ + boundaryOnly?: boolean; + /** + * Normal + */ + normal?: any[]; + /** + * Whether to automatically set the correct winding on polygons + */ + autoWinding?: boolean; +}; diff --git a/types/vec.d.ts b/types/vec.d.ts new file mode 100644 index 0000000..600480f --- /dev/null +++ b/types/vec.d.ts @@ -0,0 +1,64 @@ +/** + * @module vec + */ +/** + * Cross product + * + * @param {Array} a First vector + * @param {Array} b Second vector + * + * @return {Array} + */ +export function cross(a: any[], b: any[]): any[]; +/** + * Length of vector + * + * @param {Array} v Vector + * + * @return {Number} + */ +export function length(v: any[]): number; +/** + * Dot product + * + * @param {Array} a Vector + * @param {Array} b Vector + * + * @return {Number} + */ +export function dot(a: any[], b: any[]): number; +/** + * Normalize a vector + * + * @param {Array} v Vector + * + * @return {Array} + */ +export function normalize(v: any[]): any[]; +/** + * Add + * + * @param {Array} a First vector + * @param {Array} b Second vector + * + * @return {Array} + */ +export function add(a: any[], b: any[]): any[]; +/** + * Subtract + * + * @param {Array} a First vector + * @param {Array} b Second vector + * + * @return {Array} + */ +export function subtract(a: any[], b: any[]): any[]; +/** + * Subtract + * + * @param {Array} a First vector + * @param {Array} b Second vector + * + * @return {Array} + */ +export function sub(a: any[], b: any[]): any[];