-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from Kitware/add-color-opacity-functions
feat: add color + opacity funcs to slice + volume
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction'; | ||
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction'; | ||
import vtkProp3D from '@kitware/vtk.js/Rendering/Core/Prop3D'; | ||
import { useEffect } from 'react'; | ||
|
||
interface PropertyWithColorAndOpacity { | ||
setRGBTransferFunction( | ||
index: number, | ||
fn: vtkColorTransferFunction | null | ||
): boolean; | ||
getRGBTransferFunction(index?: number): vtkColorTransferFunction | null; | ||
setScalarOpacity(index: number, fn: vtkPiecewiseFunction | null): boolean; | ||
getScalarOpacity(index?: number): vtkPiecewiseFunction | null; | ||
} | ||
|
||
interface ActorWithColorAndOpacity<E> extends vtkProp3D { | ||
getProperty(): E; | ||
} | ||
|
||
export default function useColorAndOpacity< | ||
E extends PropertyWithColorAndOpacity, | ||
T extends ActorWithColorAndOpacity<E> | ||
>( | ||
getActor: () => T, | ||
colorTransferFunctions: | ||
| Array<vtkColorTransferFunction | null | undefined> | ||
| null | ||
| undefined, | ||
scalarOpacityFunctions: | ||
| Array<vtkPiecewiseFunction | null | undefined> | ||
| null | ||
| undefined, | ||
trackModified: (b: boolean) => void | ||
) { | ||
useEffect(() => { | ||
if (!colorTransferFunctions?.length) return; | ||
const property = getActor().getProperty(); | ||
|
||
colorTransferFunctions.forEach((fn, component) => { | ||
property.setRGBTransferFunction(component, fn); | ||
}); | ||
|
||
return () => { | ||
for (let i = 0; i < 4; i++) { | ||
property.setRGBTransferFunction(i, null); | ||
} | ||
}; | ||
}, [colorTransferFunctions, getActor]); | ||
|
||
useEffect(() => { | ||
if (!scalarOpacityFunctions?.length) return; | ||
const property = getActor().getProperty(); | ||
|
||
scalarOpacityFunctions.forEach((fn, component) => { | ||
property.setScalarOpacity(component, fn); | ||
}); | ||
|
||
return () => { | ||
for (let i = 0; i < 4; i++) { | ||
property.setScalarOpacity(i, null); | ||
} | ||
}; | ||
}, [scalarOpacityFunctions, getActor]); | ||
|
||
// watch for vtk.js object changes | ||
|
||
useEffect(() => { | ||
if (!colorTransferFunctions?.length) return; | ||
const subs = colorTransferFunctions | ||
.filter((fn): fn is vtkColorTransferFunction => !!fn) | ||
.map((fn) => { | ||
return fn.onModified(() => { | ||
trackModified(true); | ||
}); | ||
}); | ||
return () => subs.forEach((sub) => sub.unsubscribe()); | ||
}); | ||
|
||
useEffect(() => { | ||
if (!scalarOpacityFunctions?.length) return; | ||
const subs = scalarOpacityFunctions | ||
.filter((fn): fn is vtkPiecewiseFunction => !!fn) | ||
.map((fn) => { | ||
return fn.onModified(() => { | ||
trackModified(true); | ||
}); | ||
}); | ||
return () => subs.forEach((sub) => sub.unsubscribe()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters