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 a force nearest option for each component of a volume #3035

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
</fieldset>
<select id='volume' style="margin: 5px;"></select>
<select id='preset' style="margin: 5px; display: none;"></select>
<div id="forceNearest"></div>
</form>
63 changes: 49 additions & 14 deletions Sources/Rendering/Core/VolumeMapper/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true });
const volumeOptions = {};
const volumeSelectElem = document.getElementById('volume');
const presetSelectElem = document.getElementById('preset');
const forceNearestElem = document.getElementById('forceNearest');

const actor = vtkVolume.newInstance();
const mapper = vtkVolumeMapper.newInstance();
Expand Down Expand Up @@ -144,6 +145,14 @@ if (light.getPositional()) {
renderer.addActor(lca);
}

{
const optionElem = document.createElement('option');
optionElem.label = 'Default';
optionElem.value = '';
presetSelectElem.appendChild(optionElem);
presetSelectElem.value = optionElem.value;
}

Object.keys(ColorMixPreset).forEach((key) => {
if (key === 'CUSTOM') {
// Don't enable custom mode
Expand All @@ -157,27 +166,41 @@ Object.keys(ColorMixPreset).forEach((key) => {
presetSelectElem.appendChild(optionElem);
});

{
const optionElem = document.createElement('option');
optionElem.label = 'Default';
optionElem.value = '';
presetSelectElem.appendChild(optionElem);
}

const setColorMixPreset = (presetKey) => {
const preset = presetKey ? ColorMixPreset[presetKey] : null;
actor.getProperty().setColorMixPreset(preset);
presetSelectElem.value = presetKey;
};

function updateForceNearestElem(comp) {
forceNearestElem.replaceChildren();
for (let c = 0; c < comp; ++c) {
const checkboxElem = document.createElement('input');
checkboxElem.type = 'checkbox';
checkboxElem.checked = actor.getProperty().getForceNearestInterpolation(c);
checkboxElem.addEventListener('change', () => {
actor.getProperty().setForceNearestInterpolation(c, checkboxElem.checked);
renderWindow.render();
});
forceNearestElem.appendChild(checkboxElem);
const labelElem = document.createElement('label');
labelElem.innerText = `Force linear interpolation for component ${c}`;
forceNearestElem.appendChild(labelElem);
forceNearestElem.appendChild(document.createElement('br'));
}
}

updateForceNearestElem(1);

volumeSelectElem.addEventListener('change', () => {
const { comp, data } = volumeOptions[volumeSelectElem.value];
setColorMixPreset('');
if (comp === 1) {
setColorMixPreset('');
presetSelectElem.style.display = 'none';
} else {
presetSelectElem.style.display = 'block';
}
updateForceNearestElem(comp);
const array = mapper.getInputData().getPointData().getArray(0);
array.setData(data);
array.setNumberOfComponents(comp);
Expand All @@ -200,21 +223,29 @@ reader.setUrl(`${__BASE_PATH__}/data/volume/LIDC2.vti`).then(() => {
const baseComp = 1;
const baseData = array.getData();
const newComp = 2;
const newData = new Float32Array(newComp * baseData.length);
const cubeData = new Float32Array(newComp * baseData.length);
const sphereData = new Float32Array(newComp * baseData.length);
const dims = imageData.getDimensions();
for (let z = 0; z < dims[2]; ++z) {
for (let y = 0; y < dims[1]; ++y) {
for (let x = 0; x < dims[0]; ++x) {
const iTuple = x + dims[0] * (y + dims[1] * z);
newData[iTuple * newComp + 0] = baseData[iTuple];
const isInMask =
const isInCube =
x >= 0.3 * dims[0] &&
x <= 0.7 * dims[0] &&
y >= 0.3 * dims[1] &&
y <= 0.7 * dims[1] &&
z >= 0.3 * dims[2] &&
z <= 0.7 * dims[2];
newData[iTuple * newComp + 1] = isInMask ? 1 : 0;
cubeData[iTuple * newComp + 0] = baseData[iTuple];
cubeData[iTuple * newComp + 1] = isInCube ? 1 : 0;
const isInSphere =
(x / dims[0] - 0.5) ** 2 +
(y / dims[1] - 0.5) ** 2 +
(z / dims[2] - 0.5) ** 2 <
0.2 ** 2;
sphereData[iTuple * newComp + 0] = baseData[iTuple];
sphereData[iTuple * newComp + 1] = isInSphere ? 1 : 0;
}
}
}
Expand All @@ -223,9 +254,13 @@ reader.setUrl(`${__BASE_PATH__}/data/volume/LIDC2.vti`).then(() => {
comp: baseComp,
data: baseData,
};
volumeOptions['Labelmap volume'] = {
volumeOptions['Sphere labelmap volume'] = {
comp: newComp,
data: sphereData,
};
volumeOptions['Cube labelmap volume'] = {
comp: newComp,
data: newData,
data: cubeData,
};

Object.keys(volumeOptions).forEach((key) => {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Rendering/Core/VolumeProperty/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ export interface vtkVolumeProperty extends vtkObject {
*/
getUseGradientOpacity(index: number): boolean;

/**
* @see setForceNearestInterpolation
* @param {Number} index
*/
getForceNearestInterpolation(index: number): boolean;

/**
*
*/
Expand Down Expand Up @@ -315,6 +321,15 @@ export interface vtkVolumeProperty extends vtkObject {
*/
setComponentWeight(index: number, value: number): boolean;

/**
* Force the nearest neighbor interpolation of one or more of the components
* The interpolation for the rest of the volume is set using `setInterpolationType`
* @see setInterpolationType
* @param {Number} index
* @param {Boolean} value
*/
setForceNearestInterpolation(index: number, value: boolean): boolean;
bruyeret marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the scalar component weights.
* @param {Number} index
Expand Down
3 changes: 3 additions & 0 deletions Sources/Rendering/Core/VolumeProperty/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ function vtkVolumeProperty(publicAPI, model) {
'gradientOpacityMaximumValue',
'gradientOpacityMaximumOpacity',
'opacityMode',
'forceNearestInterpolation',
];
sets.forEach((val) => {
const cap = macro.capitalize(val);
Expand All @@ -232,6 +233,7 @@ function vtkVolumeProperty(publicAPI, model) {
'gradientOpacityMaximumValue',
'gradientOpacityMaximumOpacity',
'opacityMode',
'forceNearestInterpolation',
];
gets.forEach((val) => {
const cap = macro.capitalize(val);
Expand Down Expand Up @@ -283,6 +285,7 @@ export function extend(publicAPI, model, initialValues = {}) {
useGradientOpacity: false,

componentWeight: 1.0,
forceNearestInterpolation: false,
});
}
}
Expand Down
Loading
Loading