-
-
Notifications
You must be signed in to change notification settings - Fork 394
feat(TransformControlsWidget): add new widget #3280
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
floryst
wants to merge
2
commits into
master
Choose a base branch
from
transform-widget
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import macro from 'vtk.js/Sources/macros'; | ||
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; | ||
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// vtkTorusSource methods | ||
// Adapted from three.js TorusGeometry | ||
// ---------------------------------------------------------------------------- | ||
|
||
const TAU = Math.PI * 2; | ||
|
||
function vtkTorusSource(publicAPI, model) { | ||
// Set our className | ||
model.classHierarchy.push('vtkTorusSource'); | ||
|
||
function requestData(inData, outData) { | ||
let dataset = outData[0]; | ||
|
||
// Points | ||
const points = macro.newTypedArray( | ||
model.pointType, | ||
3 * (model.resolution + 1) * (model.tubeResolution + 1) | ||
); | ||
let pointIdx = 0; | ||
|
||
for (let ti = 0; ti <= model.tubeResolution; ti++) { | ||
const v = (ti / model.tubeResolution) * TAU; | ||
const cosV = Math.cos(v); | ||
const sinV = Math.sin(v); | ||
for (let ri = 0; ri <= model.resolution; ri++) { | ||
const u = (ri / model.resolution) * model.arcLength; | ||
points[pointIdx++] = | ||
(model.radius + model.tubeRadius * cosV) * Math.cos(u); | ||
points[pointIdx++] = | ||
(model.radius + model.tubeRadius * cosV) * Math.sin(u); | ||
points[pointIdx++] = model.tubeRadius * sinV; | ||
} | ||
} | ||
|
||
// Cells | ||
const cellArraySize = 4 * 2 * (model.resolution * model.tubeResolution); | ||
let cellLocation = 0; | ||
const polys = new Uint32Array(cellArraySize); | ||
|
||
for (let ti = 1; ti <= model.tubeResolution; ti++) { | ||
for (let ri = 1; ri <= model.resolution; ri++) { | ||
const a = (model.resolution + 1) * ti + ri - 1; | ||
const b = (model.resolution + 1) * (ti - 1) + ri - 1; | ||
const c = (model.resolution + 1) * (ti - 1) + ri; | ||
const d = (model.resolution + 1) * ti + ri; | ||
|
||
polys[cellLocation++] = 3; | ||
polys[cellLocation++] = a; | ||
polys[cellLocation++] = b; | ||
polys[cellLocation++] = d; | ||
|
||
polys[cellLocation++] = 3; | ||
polys[cellLocation++] = b; | ||
polys[cellLocation++] = c; | ||
polys[cellLocation++] = d; | ||
} | ||
} | ||
|
||
// Apply transformation to the points coordinates | ||
vtkMatrixBuilder | ||
.buildFromRadian() | ||
.translate(...model.center) | ||
.rotateFromDirections([1, 0, 0], model.direction) | ||
.apply(points); | ||
|
||
dataset = vtkPolyData.newInstance(); | ||
dataset.getPoints().setData(points, 3); | ||
dataset.getPolys().setData(polys, 1); | ||
|
||
// Update output | ||
outData[0] = dataset; | ||
} | ||
|
||
// Expose methods | ||
publicAPI.requestData = requestData; | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Object factory | ||
// ---------------------------------------------------------------------------- | ||
|
||
const DEFAULT_VALUES = { | ||
radius: 0.5, | ||
tubeRadius: 0.01, | ||
resolution: 64, | ||
tubeResolution: 64, | ||
arcLength: TAU, | ||
center: [0, 0, 0], | ||
direction: [1.0, 0.0, 0.0], | ||
pointType: 'Float64Array', | ||
}; | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
Object.assign(model, DEFAULT_VALUES, initialValues); | ||
|
||
// Build VTK API | ||
macro.obj(publicAPI, model); | ||
macro.setGet(publicAPI, model, [ | ||
'radius', | ||
'tubeRadius', | ||
'resolution', | ||
'tubeResolution', | ||
'arcLength', | ||
]); | ||
macro.setGetArray(publicAPI, model, ['center', 'direction'], 3); | ||
macro.algo(publicAPI, model, 0, 1); | ||
vtkTorusSource(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance(extend, 'vtkTorusSource'); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
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
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
44 changes: 44 additions & 0 deletions
44
Sources/Widgets/Representations/RotateTransformHandleRepresentation/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,44 @@ | ||
import macro from 'vtk.js/Sources/macros'; | ||
import vtkGlyphRepresentation from 'vtk.js/Sources/Widgets/Representations/GlyphRepresentation'; | ||
import vtkTorusSource from 'vtk.js/Sources/Filters/Sources/TorusSource'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// vtkRotateTransformHandleRepresentation methods | ||
// ---------------------------------------------------------------------------- | ||
|
||
function vtkRotateTransformHandleRepresentation(publicAPI, model) { | ||
// Set our className | ||
model.classHierarchy.push('vtkRotateTransformHandleRepresentation'); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Object factory | ||
// ---------------------------------------------------------------------------- | ||
|
||
// ---------------------------------------------------------------------------- | ||
function defaultValues(initialValues) { | ||
return { | ||
_pipeline: { | ||
glyph: vtkTorusSource.newInstance({}), | ||
}, | ||
...initialValues, | ||
}; | ||
} | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
vtkGlyphRepresentation.extend(publicAPI, model, defaultValues(initialValues)); | ||
|
||
// Object specific methods | ||
vtkRotateTransformHandleRepresentation(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance( | ||
extend, | ||
'vtkRotateTransformHandleRepresentation' | ||
); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export default { newInstance, extend }; |
59 changes: 59 additions & 0 deletions
59
Sources/Widgets/Representations/ScaleTransformHandleRepresentation/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,59 @@ | ||
import macro from 'vtk.js/Sources/macros'; | ||
import vtkGlyphRepresentation from 'vtk.js/Sources/Widgets/Representations/GlyphRepresentation'; | ||
import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource'; | ||
|
||
import vtkTransformHandleSource from 'vtk.js/Sources/Widgets/Representations/TranslateTransformHandleRepresentation/TransformHandleSource'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// vtkScaleTransformHandleRepresentation methods | ||
// ---------------------------------------------------------------------------- | ||
|
||
function vtkScaleTransformHandleRepresentation(publicAPI, model) { | ||
// Set our className | ||
model.classHierarchy.push('vtkScaleTransformHandleRepresentation'); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Object factory | ||
// ---------------------------------------------------------------------------- | ||
|
||
// ---------------------------------------------------------------------------- | ||
function defaultValues(initialValues) { | ||
const source = vtkTransformHandleSource.newInstance({ | ||
height: initialValues.height ?? 1, | ||
radius: initialValues.radius ?? 1, | ||
resolution: initialValues.glyphResolution ?? 12, | ||
direction: [0, 0, 1], | ||
}); | ||
|
||
const cube1 = vtkCubeSource.newInstance(initialValues.cubeSource); | ||
const cube2 = vtkCubeSource.newInstance(initialValues.cubeSource); | ||
|
||
source.addInputConnection(cube1.getOutputPort()); | ||
source.addInputConnection(cube2.getOutputPort()); | ||
|
||
return { | ||
_pipeline: { | ||
glyph: source, | ||
}, | ||
...initialValues, | ||
}; | ||
} | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
vtkGlyphRepresentation.extend(publicAPI, model, defaultValues(initialValues)); | ||
|
||
// Object specific methods | ||
vtkScaleTransformHandleRepresentation(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance( | ||
extend, | ||
'vtkScaleTransformHandleRepresentation' | ||
); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export default { newInstance, extend }; |
105 changes: 105 additions & 0 deletions
105
...s/Widgets/Representations/TranslateTransformHandleRepresentation/TransformHandleSource.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,105 @@ | ||
import macro from 'vtk.js/Sources/macros'; | ||
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder'; | ||
import vtkAppendPolyData from 'vtk.js/Sources/Filters/General/AppendPolyData'; | ||
import vtkCylinderSource from 'vtk.js/Sources/Filters/Sources/CylinderSource'; | ||
|
||
function rotatePolyData(pd, direction) { | ||
const points = pd.getPoints().getData(); | ||
|
||
vtkMatrixBuilder | ||
.buildFromRadian() | ||
.rotateFromDirections([0, 1, 0], direction) | ||
.apply(points); | ||
|
||
pd.getPoints().modified(); | ||
pd.modified(); | ||
floryst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
function translatePolyData(pd, translation) { | ||
floryst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const points = pd.getPoints().getData(); | ||
|
||
vtkMatrixBuilder | ||
.buildFromRadian() | ||
.translate(...translation) | ||
.apply(points); | ||
|
||
pd.modified(); | ||
} | ||
|
||
function vtkTransformHandleSource(publicAPI, model) { | ||
// Set our className | ||
model.classHierarchy.push('vtkTransformHandleSource'); | ||
|
||
function requestData(inData, outData) { | ||
const cylinderSource = vtkCylinderSource.newInstance({ | ||
height: model.height, | ||
initAngle: model.initAngle, | ||
radius: model.radius, | ||
resolution: model.resolution, | ||
capping: model.capping, | ||
pointType: model.pointType, | ||
center: [0, 0, 0], | ||
direction: [0, 1, 0], | ||
}); | ||
|
||
const appendFilter = vtkAppendPolyData.newInstance(); | ||
appendFilter.setInputConnection(cylinderSource.getOutputPort(), 0); | ||
|
||
if (inData[0]) { | ||
translatePolyData(inData[0], [0, model.height / 2, 0]); | ||
appendFilter.addInputData(inData[0]); | ||
} | ||
if (inData[1]) { | ||
rotatePolyData(inData[1], [0, -1, 0]); | ||
translatePolyData(inData[1], [0, -model.height / 2, 0]); | ||
appendFilter.addInputData(inData[1]); | ||
} | ||
|
||
const poly = appendFilter.getOutputData(); | ||
const points = poly.getPoints().getData(); | ||
|
||
// Apply transformation to the points coordinates | ||
vtkMatrixBuilder | ||
.buildFromRadian() | ||
.translate(...model.center) | ||
.rotateFromDirections([0, 1, 0], model.direction) | ||
.translate(...model.center.map((c) => c * -1)) | ||
.apply(points); | ||
|
||
// Update output | ||
outData[0] = poly; | ||
} | ||
|
||
// Expose methods | ||
publicAPI.requestData = requestData; | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Object factory | ||
// ---------------------------------------------------------------------------- | ||
|
||
const DEFAULT_VALUES = { | ||
capPolyData: null, | ||
}; | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export function extend(publicAPI, model, initialValues = {}) { | ||
Object.assign(model, DEFAULT_VALUES, initialValues); | ||
|
||
vtkCylinderSource.extend(publicAPI, model, initialValues); | ||
macro.algo(publicAPI, model, 2, 1); | ||
|
||
vtkTransformHandleSource(publicAPI, model); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export const newInstance = macro.newInstance( | ||
extend, | ||
'vtkTransformHandleSource' | ||
); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
export default { newInstance, extend }; |
Oops, something went wrong.
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.