Skip to content

Commit 2b3f815

Browse files
authored
🤖 Merge PR DefinitelyTyped#74167 Add types for libtess by @jpt
1 parent 7cc6185 commit 2b3f815

File tree

5 files changed

+349
-0
lines changed

5 files changed

+349
-0
lines changed

types/libtess/.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
6+

types/libtess/index.d.ts

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* Polygon tessellation library, ported from SGI's GLU implementation
3+
* written by Eric Veach in 1994
4+
*/
5+
6+
/**
7+
* GLU tessellation property and callback type enums
8+
*/
9+
export enum gluEnum {
10+
/** Winding rule property */
11+
GLU_TESS_WINDING_RULE = 100140,
12+
/** Boundary only property - if true, only outputs boundary contours instead of triangulated polygons */
13+
GLU_TESS_BOUNDARY_ONLY = 100141,
14+
/** Tolerance property for vertex merging */
15+
GLU_TESS_TOLERANCE = 100142,
16+
17+
/** Begin callback - called when beginning a new primitive */
18+
GLU_TESS_BEGIN = 100100,
19+
/** Vertex callback - called for each vertex */
20+
GLU_TESS_VERTEX = 100101,
21+
/** End callback - called when ending a primitive */
22+
GLU_TESS_END = 100102,
23+
/** Error callback - called when an error occurs */
24+
GLU_TESS_ERROR = 100103,
25+
/** Edge flag callback - called to indicate whether edges are on the polygon boundary */
26+
GLU_TESS_EDGE_FLAG = 100104,
27+
/** Combine callback - called when vertices need to be merged or interpolated */
28+
GLU_TESS_COMBINE = 100105,
29+
30+
/** Begin callback with client data */
31+
GLU_TESS_BEGIN_DATA = 100106,
32+
/** Vertex callback with client data */
33+
GLU_TESS_VERTEX_DATA = 100107,
34+
/** End callback with client data */
35+
GLU_TESS_END_DATA = 100108,
36+
/** Error callback with client data */
37+
GLU_TESS_ERROR_DATA = 100109,
38+
/** Edge flag callback with client data */
39+
GLU_TESS_EDGE_FLAG_DATA = 100110,
40+
/** Combine callback with client data */
41+
GLU_TESS_COMBINE_DATA = 100111,
42+
}
43+
44+
/**
45+
* Winding rules determine which regions are considered "inside" the polygon
46+
*/
47+
export enum windingRule {
48+
/** Odd-even winding rule - alternating regions are inside/outside */
49+
GLU_TESS_WINDING_ODD = 100130,
50+
/** Non-zero winding rule - regions with non-zero winding number are inside */
51+
GLU_TESS_WINDING_NONZERO = 100131,
52+
/** Positive winding rule - regions with positive winding number are inside */
53+
GLU_TESS_WINDING_POSITIVE = 100132,
54+
/** Negative winding rule - regions with negative winding number are inside */
55+
GLU_TESS_WINDING_NEGATIVE = 100133,
56+
/** Absolute value >= 2 winding rule - regions where |winding| >= 2 are inside */
57+
GLU_TESS_WINDING_ABS_GEQ_TWO = 100134,
58+
}
59+
60+
/**
61+
* Primitive types that can be output by the tessellator
62+
*/
63+
export enum primitiveType {
64+
/** Line loop primitive */
65+
GL_LINE_LOOP = 2,
66+
/** Triangle list primitive */
67+
GL_TRIANGLES = 4,
68+
/** Triangle strip primitive */
69+
GL_TRIANGLE_STRIP = 5,
70+
/** Triangle fan primitive */
71+
GL_TRIANGLE_FAN = 6,
72+
}
73+
74+
/**
75+
* Error codes that can be returned by the tessellator
76+
*/
77+
export enum errorType {
78+
GLU_TESS_MISSING_BEGIN_POLYGON = 100151,
79+
GLU_TESS_MISSING_END_POLYGON = 100153,
80+
GLU_TESS_MISSING_BEGIN_CONTOUR = 100152,
81+
GLU_TESS_MISSING_END_CONTOUR = 100154,
82+
GLU_TESS_COORD_TOO_LARGE = 100155,
83+
GLU_TESS_NEED_COMBINE_CALLBACK = 100156,
84+
}
85+
86+
/**
87+
* The main GLU tessellator class. Used to tessellate complex polygons with holes into simpler primitives
88+
*/
89+
export class GluTesselator {
90+
/**
91+
* Create a new tessellator object
92+
*/
93+
constructor();
94+
95+
/**
96+
* Delete the tessellator object. Note: In JavaScript this is largely a no-op as garbage collection handles cleanup
97+
*/
98+
gluDeleteTess(): void;
99+
100+
/**
101+
* Set a tessellation property
102+
* @param which - The property to set (from gluEnum)
103+
* @param value - The value to set
104+
*/
105+
gluTessProperty(which: gluEnum, value: number | boolean): void;
106+
107+
/**
108+
* Get a tessellation property
109+
* @param which - The property to get (from gluEnum)
110+
* @returns The current value of the property
111+
*/
112+
gluGetTessProperty(which: gluEnum): number | boolean;
113+
114+
/**
115+
* Specify the normal vector for the polygon. This helps the tessellator determine the polygon orientation. For 2D tessellation, typically use (0, 0, 1)
116+
* @param x - X component of normal
117+
* @param y - Y component of normal
118+
* @param z - Z component of normal
119+
*/
120+
gluTessNormal(x: number, y: number, z: number): void;
121+
122+
/**
123+
* Register a callback function for tessellation events. Different callback types have different signatures
124+
*/
125+
gluTessCallback(
126+
which: gluEnum.GLU_TESS_BEGIN | gluEnum.GLU_TESS_BEGIN_DATA,
127+
callback: ((type: primitiveType, polygonData?: any) => void) | null,
128+
): void;
129+
gluTessCallback(
130+
which: gluEnum.GLU_TESS_EDGE_FLAG | gluEnum.GLU_TESS_EDGE_FLAG_DATA,
131+
callback: ((flag: boolean, polygonData?: any) => void) | null,
132+
): void;
133+
gluTessCallback(
134+
which: gluEnum.GLU_TESS_VERTEX | gluEnum.GLU_TESS_VERTEX_DATA,
135+
callback: ((vertexData: any, polygonData?: any) => void) | null,
136+
): void;
137+
gluTessCallback(
138+
which: gluEnum.GLU_TESS_END | gluEnum.GLU_TESS_END_DATA,
139+
callback: ((polygonData?: any) => void) | null,
140+
): void;
141+
gluTessCallback(
142+
which: gluEnum.GLU_TESS_ERROR | gluEnum.GLU_TESS_ERROR_DATA,
143+
callback: ((errno: errorType | gluEnum, polygonData?: any) => void) | null,
144+
): void;
145+
gluTessCallback(
146+
which: gluEnum.GLU_TESS_COMBINE | gluEnum.GLU_TESS_COMBINE_DATA,
147+
callback:
148+
| ((
149+
coords: number[],
150+
vertexData: any[],
151+
weight: number[],
152+
polygonData?: any,
153+
) => any)
154+
| null,
155+
): void;
156+
157+
/**
158+
* Add a vertex to the current contour. Must be called between gluTessBeginContour and gluTessEndContour
159+
* @param coords - Vertex coordinates [x, y, z]
160+
* @param data - Client data associated with this vertex (will be passed to callbacks)
161+
*/
162+
gluTessVertex(coords: number[], data: any): void;
163+
164+
/**
165+
* Begin a new polygon. Must be called before adding any contours
166+
* @param data - Client data for this polygon (will be passed to callbacks)
167+
*/
168+
gluTessBeginPolygon(data: any): void;
169+
170+
/**
171+
* Begin a new contour within the current polygon. Must be called after gluTessBeginPolygon and before adding vertices
172+
*/
173+
gluTessBeginContour(): void;
174+
175+
/**
176+
* End the current contour. Must be called after adding vertices to a contour
177+
*/
178+
gluTessEndContour(): void;
179+
180+
/**
181+
* End the current polygon and perform tessellation. This triggers the tessellation and fires the registered callbacks
182+
*/
183+
gluTessEndPolygon(): void;
184+
}

types/libtess/libtess-tests.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { errorType, gluEnum, GluTesselator, primitiveType, windingRule } from "libtess";
2+
3+
// Create a tessellator
4+
const tess = new GluTesselator();
5+
6+
// Set properties
7+
tess.gluTessProperty(gluEnum.GLU_TESS_WINDING_RULE, windingRule.GLU_TESS_WINDING_ODD);
8+
tess.gluTessProperty(gluEnum.GLU_TESS_BOUNDARY_ONLY, false);
9+
tess.gluTessProperty(gluEnum.GLU_TESS_TOLERANCE, 0.0);
10+
11+
// Get properties
12+
const windingRuleValue: number | boolean = tess.gluGetTessProperty(gluEnum.GLU_TESS_WINDING_RULE);
13+
const boundaryOnly: number | boolean = tess.gluGetTessProperty(gluEnum.GLU_TESS_BOUNDARY_ONLY);
14+
15+
// Set normal (for 2D tessellation)
16+
tess.gluTessNormal(0, 0, 1);
17+
18+
// Register callbacks
19+
tess.gluTessCallback(gluEnum.GLU_TESS_BEGIN, (type: primitiveType) => {
20+
if (type === primitiveType.GL_TRIANGLES) {
21+
// handle triangles
22+
}
23+
});
24+
25+
tess.gluTessCallback(gluEnum.GLU_TESS_VERTEX, (vertexData: any) => {
26+
// handle vertex
27+
});
28+
29+
tess.gluTessCallback(gluEnum.GLU_TESS_END, () => {
30+
// handle end
31+
});
32+
33+
tess.gluTessCallback(gluEnum.GLU_TESS_ERROR, (errno: errorType | gluEnum) => {
34+
if (errno === errorType.GLU_TESS_NEED_COMBINE_CALLBACK) {
35+
// handle error
36+
}
37+
});
38+
39+
tess.gluTessCallback(gluEnum.GLU_TESS_EDGE_FLAG, (flag: boolean) => {
40+
// handle edge flag
41+
});
42+
43+
tess.gluTessCallback(
44+
gluEnum.GLU_TESS_COMBINE,
45+
(coords: number[], vertexData: any[], weight: number[]) => {
46+
return { coords };
47+
},
48+
);
49+
50+
// Callbacks with polygon data
51+
const polygonData = { id: 1 };
52+
53+
tess.gluTessCallback(gluEnum.GLU_TESS_BEGIN_DATA, (type: primitiveType, data?: any) => {
54+
if (data) {
55+
// handle with data
56+
}
57+
});
58+
59+
tess.gluTessCallback(gluEnum.GLU_TESS_VERTEX_DATA, (vertexData: any, data?: any) => {
60+
if (data) {
61+
// handle with data
62+
}
63+
});
64+
65+
tess.gluTessCallback(gluEnum.GLU_TESS_END_DATA, (data?: any) => {
66+
if (data) {
67+
// handle with data
68+
}
69+
});
70+
71+
tess.gluTessCallback(gluEnum.GLU_TESS_ERROR_DATA, (errno: errorType | gluEnum, data?: any) => {
72+
if (data) {
73+
// handle with data
74+
}
75+
});
76+
77+
tess.gluTessCallback(gluEnum.GLU_TESS_EDGE_FLAG_DATA, (flag: boolean, data?: any) => {
78+
if (data) {
79+
// handle with data
80+
}
81+
});
82+
83+
tess.gluTessCallback(
84+
gluEnum.GLU_TESS_COMBINE_DATA,
85+
(coords: number[], vertexData: any[], weight: number[], data?: any) => {
86+
return { coords, data };
87+
},
88+
);
89+
90+
// Clear a callback
91+
tess.gluTessCallback(gluEnum.GLU_TESS_BEGIN, null);
92+
93+
// Tessellate a simple square
94+
tess.gluTessBeginPolygon(polygonData);
95+
tess.gluTessBeginContour();
96+
tess.gluTessVertex([0, 0, 0], { id: 0 });
97+
tess.gluTessVertex([1, 0, 0], { id: 1 });
98+
tess.gluTessVertex([1, 1, 0], { id: 2 });
99+
tess.gluTessVertex([0, 1, 0], { id: 3 });
100+
tess.gluTessEndContour();
101+
tess.gluTessEndPolygon();
102+
103+
// Tessellate a polygon with a hole
104+
tess.gluTessBeginPolygon(null);
105+
106+
// Outer contour
107+
tess.gluTessBeginContour();
108+
tess.gluTessVertex([0, 0, 0], 0);
109+
tess.gluTessVertex([2, 0, 0], 1);
110+
tess.gluTessVertex([2, 2, 0], 2);
111+
tess.gluTessVertex([0, 2, 0], 3);
112+
tess.gluTessEndContour();
113+
114+
// Inner contour (hole)
115+
tess.gluTessBeginContour();
116+
tess.gluTessVertex([0.5, 0.5, 0], 4);
117+
tess.gluTessVertex([1.5, 0.5, 0], 5);
118+
tess.gluTessVertex([1.5, 1.5, 0], 6);
119+
tess.gluTessVertex([0.5, 1.5, 0], 7);
120+
tess.gluTessEndContour();
121+
122+
tess.gluTessEndPolygon();
123+
124+
// Clean up
125+
tess.gluDeleteTess();

types/libtess/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/libtess",
4+
"version": "1.2.9999",
5+
"projects": [
6+
"https://github.com/brendankenny/libtess.js"
7+
],
8+
"devDependencies": {
9+
"@types/libtess": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Jeremy Tribby",
14+
"githubUsername": "jpt"
15+
}
16+
]
17+
}

types/libtess/tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": ["es6"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"types": [],
10+
"noEmit": true,
11+
"forceConsistentCasingInFileNames": true
12+
},
13+
"files": [
14+
"index.d.ts",
15+
"libtess-tests.ts"
16+
]
17+
}

0 commit comments

Comments
 (0)