-
-
Notifications
You must be signed in to change notification settings - Fork 394
LineGlyphRepresentation to connect widget handles #2899
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
Open
PaulHax
wants to merge
11
commits into
Kitware:master
Choose a base branch
from
PaulHax:segment-rep
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd796c2
feat(SegmentedLineRepresentation): copy from PolyLineRepresentation
PaulHax f385853
feat(SegmentedLineRepresentation): orient glyphs to next point
PaulHax 662e980
feat(SegmentedLineRepresentation): scale cylinders to connect points
PaulHax a99041a
fix(WidgetRepresentation): edit and apply prettier
PaulHax 0be7980
refactor(SegmentedLineRepresentation): rename to LineGlyphRepresentation
PaulHax 1003dfd
refactor(LineGlyphRepresentation): offset cylinder center
PaulHax 499d15f
fix(LineGlyphRepresentation): extend GlyphRepresentation
PaulHax 0529fd5
feat(LineGlyphRepresentation): support scale1 state mixin
PaulHax dcd8cc9
feat(LineGlyphRepresentation): use scale1 value and add forceLineThic…
PaulHax 689e664
docs(examples): fix SplineWidget image link
PaulHax df0a9cc
refactor(LineGlyphRepresentation): use @kitware/vtk.js import alias
PaulHax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
Sources/Widgets/Representations/LineGlyphRepresentation/example/index.js
This file contains hidden or 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,67 @@ | ||
import '@kitware/vtk.js/favicon'; | ||
|
||
// Load the rendering pieces we want to use (for both WebGL and WebGPU) | ||
import '@kitware/vtk.js/Rendering/Profiles/All'; | ||
|
||
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; | ||
import vtkStateBuilder from '@kitware/vtk.js/Widgets/Core/StateBuilder'; | ||
|
||
import vtkSphereHandleRepresentation from '@kitware/vtk.js/Widgets/Representations/SphereHandleRepresentation'; | ||
import vtkLineGlyphRepresentation from '@kitware/vtk.js/Widgets/Representations/LineGlyphRepresentation'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Standard rendering code setup | ||
// ---------------------------------------------------------------------------- | ||
|
||
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ | ||
background: [0, 0, 0], | ||
}); | ||
const renderer = fullScreenRenderer.getRenderer(); | ||
const renderWindow = fullScreenRenderer.getRenderWindow(); | ||
|
||
// ----------------------------------------------------------- | ||
// State | ||
// ----------------------------------------------------------- | ||
|
||
const compositeState = vtkStateBuilder | ||
.createBuilder() | ||
.addDynamicMixinState({ | ||
labels: ['handles'], | ||
mixins: ['origin', 'scale1'], | ||
name: 'handle', | ||
}) | ||
.build(); | ||
|
||
const z = -50; | ||
const points = [ | ||
[0, 0, z], | ||
[5, -5, z], | ||
[15, 5, z], | ||
[5, 10, z], | ||
]; | ||
points.forEach((point) => { | ||
const handle = compositeState.addHandle(); | ||
handle.setOrigin(point); | ||
handle.setScale1(1.5); | ||
}); | ||
|
||
// ----------------------------------------------------------- | ||
// Representation | ||
// ----------------------------------------------------------- | ||
|
||
const widgetRep = vtkLineGlyphRepresentation.newInstance({ | ||
scaleInPixels: false, | ||
}); | ||
widgetRep.setInputData(compositeState); | ||
widgetRep.setLabels(['handles']); | ||
widgetRep.getActors().forEach(renderer.addActor); | ||
floryst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const handleRep = vtkSphereHandleRepresentation.newInstance({ | ||
scaleInPixels: false, | ||
}); | ||
handleRep.setInputData(compositeState); | ||
handleRep.setLabels(['handles']); | ||
handleRep.getActors().forEach(renderer.addActor); | ||
|
||
renderer.resetCamera(); | ||
renderWindow.render(); |
149 changes: 149 additions & 0 deletions
149
Sources/Widgets/Representations/LineGlyphRepresentation/index.js
This file contains hidden or 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,149 @@ | ||
import macro from '@kitware/vtk.js/macros'; | ||
import * as vtkMath from '@kitware/vtk.js/Common/Core/Math'; | ||
import vtkGlyph3DMapper from '@kitware/vtk.js/Rendering/Core/Glyph3DMapper'; | ||
import vtkGlyphRepresentation from '@kitware/vtk.js/Widgets/Representations/GlyphRepresentation'; | ||
import { Behavior } from '@kitware/vtk.js/Widgets/Representations/WidgetRepresentation/Constants'; | ||
import { allocateArray } from '@kitware/vtk.js/Widgets/Representations/WidgetRepresentation'; | ||
import vtkCylinderSource from '@kitware/vtk.js/Filters/Sources/CylinderSource'; | ||
import { OrientationModes } from '@kitware/vtk.js/Rendering/Core/Glyph3DMapper/Constants'; | ||
import { getPixelWorldHeightAtCoord } from '@kitware/vtk.js/Widgets/Core/WidgetManager'; | ||
|
||
function vtkLineGlyphRepresentation(publicAPI, model) { | ||
model.classHierarchy.push('vtkLineGlyphRepresentation'); | ||
|
||
publicAPI.setGlyphResolution = macro.chain( | ||
publicAPI.setGlyphResolution, | ||
model._pipeline.glyph.setResolution | ||
); | ||
} | ||
|
||
function cylinderScale(publicAPI, model) { | ||
return (polyData, states) => { | ||
model._pipeline.mapper.setScaleArray('scale'); | ||
model._pipeline.mapper.setScaleFactor(1); | ||
model._pipeline.mapper.setScaling(true); | ||
model._pipeline.mapper.setScaleMode( | ||
vtkGlyph3DMapper.ScaleModes.SCALE_BY_COMPONENTS | ||
); | ||
const scales = allocateArray( | ||
polyData, | ||
'scale', | ||
states.length, | ||
'Float32Array', | ||
3 | ||
).getData(); | ||
let j = 0; | ||
for (let i = 0; i < states.length; ++i) { | ||
const state = states[i]; | ||
const origin = state.getOrigin(); | ||
const nextOrigin = | ||
states[i === states.length - 1 ? 0 : i + 1].getOrigin(); | ||
|
||
const direction = vtkMath.subtract(nextOrigin, origin, []); | ||
const length = vtkMath.normalize(direction); | ||
|
||
let scaleFactor = state.getActive() ? model.activeScaleFactor : 1; | ||
if (publicAPI.getScaleInPixels()) { | ||
scaleFactor *= getPixelWorldHeightAtCoord( | ||
state.getOrigin(), | ||
model.displayScaleParams | ||
); | ||
} | ||
if (!model.forceLineThickness) { | ||
scaleFactor *= state.getScale1?.() ?? 1; | ||
} | ||
const scale = [1, model.lineThickness, model.lineThickness]; | ||
scales[j++] = length * scale[0]; | ||
scales[j++] = scaleFactor * scale[1]; | ||
scales[j++] = scaleFactor * scale[2]; | ||
} | ||
}; | ||
} | ||
|
||
function cylinderDirection(publicAPI, model) { | ||
return (polyData, states) => { | ||
model._pipeline.mapper.setOrientationArray('orientation'); | ||
model._pipeline.mapper.setOrientationMode(OrientationModes.MATRIX); | ||
const orientation = allocateArray( | ||
polyData, | ||
'orientation', | ||
states.length, | ||
'Float32Array', | ||
9 | ||
).getData(); | ||
for (let i = 0; i < states.length; ++i) { | ||
const state = states[i]; | ||
const origin = state.getOrigin(); | ||
const nextOrigin = | ||
states[i === states.length - 1 ? 0 : i + 1].getOrigin(); | ||
|
||
const direction = vtkMath.subtract(nextOrigin, origin, []); | ||
vtkMath.normalize(direction); | ||
const right = [1, 0, 0]; | ||
const up = [0, 1, 0]; | ||
vtkMath.perpendiculars(direction, up, right, 0); | ||
|
||
orientation.set(direction, 9 * i); | ||
orientation.set(up, 9 * i + 3); | ||
orientation.set(right, 9 * i + 6); | ||
} | ||
}; | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Object factory | ||
// ---------------------------------------------------------------------------- | ||
|
||
function defaultValues(publicAPI, model, initialValues) { | ||
return { | ||
behavior: Behavior.CONTEXT, | ||
glyphResolution: 32, | ||
lineThickness: 0.5, // radius of the cylinder | ||
forceLineThickness: false, | ||
...initialValues, | ||
_pipeline: { | ||
glyph: | ||
initialValues?.pipeline?.glyph ?? | ||
vtkCylinderSource.newInstance({ | ||
direction: [1, 0, 0], | ||
center: [0.5, 0, 0], // origin of cylinder at end, not center | ||
capping: false, | ||
}), | ||
...initialValues?.pipeline, | ||
}, | ||
applyMixin: { | ||
noScale: cylinderScale(publicAPI, model), | ||
scale1: cylinderScale(publicAPI, model), | ||
finetjul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
noOrientation: cylinderDirection(publicAPI, model), | ||
}, | ||
}; | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
vtkGlyphRepresentation.extend( | ||
publicAPI, | ||
model, | ||
defaultValues(publicAPI, model, initialValues) | ||
); | ||
macro.setGet(publicAPI, model, [ | ||
'glyphResolution', | ||
'lineThickness', | ||
'forceLineThickness', | ||
]); | ||
macro.get(publicAPI, model._pipeline, ['glyph', 'mapper', 'actor']); | ||
|
||
vtkLineGlyphRepresentation(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance( | ||
extend, | ||
'vtkLineGlyphRepresentation' | ||
); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export default { newInstance, extend }; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.