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

Initial mesh support #3

Open
wants to merge 1 commit into
base: main
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
85 changes: 72 additions & 13 deletions src/NiivueMutator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Niivue, NVImage } from "@niivue/niivue";
import { Niivue, NVImage, NVMesh } from "@niivue/niivue";
import {
NVRVolume,
LoadableVolumeOptions,
ImageOptions,
NVROptions,
NVRMesh,
HasUrlObject,
} from "./model.ts";
import * as setters from "./setters.ts";
import { ColorMap } from "./reexport.ts";
Expand All @@ -19,6 +21,8 @@ class NiivueMutator {

constructor(nv: Niivue) {
this.nv = nv;
// @ts-ignore
window.nv = nv;
this.volumeUpdateFunctionByIndexMap =
setters.volumeUpdateFunctionByIndexMap(nv);
this.volumeUpdateFunctionByIdMap = setters.volumeUpdateFunctionByIdMap(nv);
Expand Down Expand Up @@ -253,20 +257,62 @@ class NiivueMutator {
removeVolumeByUrl(...args: Parameters<Niivue["removeVolumeByUrl"]>) {
this.nv.removeVolumeByUrl(...args);
}

// === Mesh Methods ===

/**
* Load meshes into the Niivue instance.
*/
public async loadMeshes(meshes: NVRMesh[]) {
await this.nv.loadMeshes(meshes.map(canonicalizeNvrMesh));
}

/**
* Remove a mesh by its URL.
*/
removeMeshByUrl(...args: Parameters<Niivue["removeMeshByUrl"]>) {
this.nv.removeMeshByUrl(...args);
}

/**
* Apply changes to a loaded mesh.
*/
public applyMeshChanges(changes: NVRMesh) {
const meshIndex = this.getMeshIndex(changes);

typedEntries(changes).forEach(([propertyName, value]) => {
if (!isMeshOption(propertyName)) {
return;
}
console.log(propertyName, value);
// @todo: fixme
// @ts-ignore
this.nv.setMeshProperty(meshIndex, propertyName, value);
});
this.nv.updateGLVolume();
}

private getMeshIndex(changes: NVRMesh): number {
const loadedMesh = this.nv.getMediaByUrl(changes.url);
if (typeof loadedMesh === "undefined") {
throw new Error(`No mesh found with URL ${changes.url}`);
}
return this.nv.getMeshIndexByID(loadedMesh.id);
}
}

// /**
// * Converts to `LoadFromUrlParams` (which is not exported by niivue).
// */
// function canonicalizeNvrMesh(mesh: NVRMesh): HasUrlObject {
// if (mesh.layers) {
// return {
// ...mesh,
// layers: Object.values(mesh.layers),
// };
// }
// return mesh;
// }
/**
* Converts to `LoadFromUrlParams` (which is not exported by niivue).
*/
function canonicalizeNvrMesh(mesh: NVRMesh): HasUrlObject {
if (mesh.layers) {
return {
...mesh,
layers: Object.values(mesh.layers),
};
}
return mesh;
}

/**
* Wrapper around `Object.entries` with safe type coercion of the keys to `keyof T`.
Expand Down Expand Up @@ -297,6 +343,19 @@ function isVolumeOption(name: keyof NVRVolume): name is keyof ImageOptions {
return SPECIAL_IMAGE_FIELDS.findIndex((key) => name === key) === -1;
}

/**
* Fields of `NVRMesh` which cannot be changed by setting them directly on `nv.meshes[i]`.
*/
const SPECIAL_MESH_FIELDS: (keyof NVRMesh)[] = ["url"];

/**
* Helper function for filtering volume properties which can be set directly on `NVImage`.
*/
function isMeshOption(name: keyof NVRMesh): name is keyof NVRMesh {
// does TypeScript have a "keyof" function which works in an if statement at runtime?
return SPECIAL_MESH_FIELDS.findIndex((key) => name === key) === -1;
}

/**
* Type for parameter supported by `Niivue.loadVolumes`.
* It's a subset of `ImageFromUrlOptions`, which is sadly a private type.
Expand Down
1 change: 1 addition & 0 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function diffPrimitive<T extends { [key: string]: any }>(
};
}

// @ts-ignore
function zipArrays<X, Y>(x: X[], y: Y[]): [X, Y][] {
return x.map((v, i) => [v, y[i]]);
}
Expand Down
8 changes: 6 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ type NVRMeshLayer = {
/**
* A mesh (e.g. white-matter surface) in Niivue.
*/
type NVRMesh = { url: string; layers: NVRMeshLayer[] } & Pick<
type NVRMesh = {
url: string;
layers?: NVRMeshLayer[];
rgba255?: number[] | Uint8Array;
} & Pick<
Partial<NVMeshFromUrlOptions>,
"name" | "opacity" | "visible" | "rgba255" | "colorbarVisible"
"name" | "opacity" | "visible" | "colorbarVisible"
>;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/reexport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type NiiVueOptions = {
// optional 2D png bitmap that can be rapidly loaded to defer slow loading of 3D image
thumbnail?: string;

meshXRay?: number;

// from NVConfigOptions
sliceType?: SLICE_TYPE;
};
Expand Down