Skip to content

Commit

Permalink
Updated JS bindings (elalish#268)
Browse files Browse the repository at this point in the history
* Updated JS binding type definitions

* Ignore sublime text projects

* Ignore clangd cache

* Expose isManifold method

* Added wrapper for Manifold constructor, updated Mesh interface (...)
- Optional Mesh fields no longer error out with a 'missing property' error when they are missing
- Manifold constructor now uses the JS style Mesh instead of the serialized Mesh interface (MeshVec)
  - The old constructor was kept as a new static function (ManifoldFromMeshVec)
  - Error checking added to the constructor. If the status is not 0, an error with the correct message and code is thrown
- Updated type definitions

* Fix typo
whoops...

* Fix broken MeshRelation deserialization

* Revert "Expose isManifold method"

This reverts commit cedde70.

* Dummy commit to trigger CI
  • Loading branch information
rafern authored Nov 4, 2022
1 parent ca81951 commit 50fc5d8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,7 @@ buildWASM
docs/html
node_modules/
__pycache__
.vscode/c_cpp_properties.json
.vscode/c_cpp_properties.json
*.sublime-project
*.sublime-workspace
.cache
68 changes: 63 additions & 5 deletions bindings/wasm/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Module.setup = function () {
_ManifoldInitialized = true;

function toVec(vec, list, f = x => x) {
if (list != null) {
if (list) {
for (let x of list) {
vec.push_back(f(x));
}
Expand Down Expand Up @@ -166,17 +166,18 @@ Module.setup = function () {
const result = this._getMeshRelation();
const oldBarycentric = result.barycentric;
const oldTriBary = result.triBary;
const conversion1 = v => ['x', 'y', 'z'].map(f => v[f]);
const conversion2 = v => {
const conversion1 = v => [v.x, v.y, v.z];
const conversion2 = v => [v[0], v[1], v[2]];
const conversion3 = v => {
return {
meshID: v.meshID,
originalID: v.originalID,
tri: v.tri,
vertBary: conversion1(v.vertBary)
vertBary: conversion2(v.vertBary)
};
};
result.barycentric = fromVec(oldBarycentric, conversion1);
result.triBary = fromVec(oldTriBary, conversion2);
result.triBary = fromVec(oldTriBary, conversion3);
oldBarycentric.delete();
oldTriBary.delete();
return result;
Expand All @@ -190,6 +191,63 @@ Module.setup = function () {
};
};

Module.ManifoldError = function ManifoldError(code, ...args) {
let message = 'Unknown error';
switch (code) {
case Module.status.NON_FINITE_VERTEX.value:
message = 'Non-finite vertex';
break;
case Module.status.NOT_MANIFOLD.value:
message = 'Not manifold';
break;
case Module.status.VERTEX_INDEX_OUT_OF_BOUNDS.value:
message = 'Vertex index out of bounds';
break;
case Module.status.PROPERTIES_WRONG_LENGTH.value:
message = 'Properties have wrong length';
break;
case Module.status.TRI_PROPERTIES_WRONG_LENGTH.value:
message = 'Tri properties have wrong length';
break;
case Module.status.TRI_PROPERTIES_OUT_OF_BOUNDS.value:
message = 'Tri properties out of bounds';
}

const base = Error.apply(this, [message, ...args]);
base.name = this.name = 'ManifoldError';
this.message = base.message;
this.stack = base.stack;
this.code = code;
}

Module.ManifoldError.prototype = Object.create(Error.prototype, {
constructor: {
value: Module.ManifoldError,
writable: true,
configurable: true
}
});

const ManifoldCtor = Module.Manifold;
Module.ManifoldFromMeshVec = function(meshVec) {
const manifold = new ManifoldCtor(meshVec);

const status = manifold.status();
if (status.value !== 0) {
throw new Module.ManifoldError(status.value);
}

return manifold;
}

Module.Manifold = function Manifold(mesh) {
const meshVec = mesh2vec(mesh);
const manifold = Module.ManifoldFromMeshVec(meshVec);
disposeMesh(meshVec);
return manifold;
}
Module.Manifold.prototype = Object.create(ManifoldCtor.prototype);

Module.cube = function (...args) {
let size = undefined;
if (args.length == 0)
Expand Down
45 changes: 44 additions & 1 deletion bindings/wasm/manifold.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ type Mesh = {
vertNormal?: Vec3[],
halfedgeTangent?: Vec4[]
};
type SerializedVec3 = {
x: number,
y: number,
z: number,
};
type SerializedVec4 = {
x: number,
y: number,
z: number,
w: number,
};
interface Vector<T> {
get(idx: number): T;
push_back(value: T): void;
resize(count: number, value: T): void;
set(idx: number, value: T): void;
size(): number;
}
class Vector_vec3 implements Vector<SerializedVec3>{};
class Vector_vec4 implements Vector<SerializedVec4>{};
type MeshVec = {
vertPos: Vector_vec3,
triVerts: Vector_vec3,
vertNormal: Vector_vec3,
halfedgeTangent: Vector_vec4
};
type Box = {
min: Vec3,
max: Vec3
Expand Down Expand Up @@ -56,6 +82,10 @@ type MeshRelation = {
};

declare class Manifold {
/**
* Create a Manifold from a Mesh object.
*/
constructor(mesh: Mesh);
/**
* Transform this Manifold in space. The first three columns form a 3x3 matrix
* transform and the last is a translation vector. This operation can be
Expand Down Expand Up @@ -408,6 +438,14 @@ declare function setCircularSegments(segments: number): void;
declare function getCircularSegments(radius: number): number;
///@}

/**
* Create a Manifold from a serialized Mesh object (MeshVec). Unlike the
* constructor, this method does not dispose the Mesh after using it.
*
* @param meshVec The serialized Mesh object to convert into a Manifold.
*/
declare function ManifoldFromMeshVec(meshVec: MeshVec): Manifold;

declare interface ManifoldStatic {
cube: typeof cube;
cylinder: typeof cylinder;
Expand All @@ -425,6 +463,11 @@ declare interface ManifoldStatic {
setMinCircularEdgeLength: typeof setMinCircularEdgeLength;
setCircularSegments: typeof setCircularSegments;
getCircularSegments: typeof getCircularSegments;
ManifoldFromMeshVec: typeof ManifoldFromMeshVec;
Manifold: typeof Manifold;
Vector_vec3: typeof Vector_vec3;
Vector_vec4: typeof Vector_vec4;
setup: function(): void;
}

declare const Module: ManifoldStatic;
declare function Module(): Promise<ManifoldStatic>;

0 comments on commit 50fc5d8

Please sign in to comment.