-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Webworker #595
Merged
Merged
Webworker #595
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c34ebe
perf(VolumeMapper): Compute gradients in web workers
thewtex 6537b63
perf(PiecewiseGaussianWidgets): Compute histogram asynchronously in p…
thewtex 69b71ea
fix(Dependencies): Do not explicitly specify worker-loader
thewtex 0913a11
fix(VolumeMapper): Specify a callback to run after lighting activated
thewtex 2f6132d
fix(Workers): Do not use async keyword
thewtex 0aee200
fix(VolumeMapper): Improve LightingActivated event invocation
thewtex 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
17 changes: 17 additions & 0 deletions
17
Sources/Interaction/Widgets/PiecewiseGaussianWidget/ComputeHistogram.worker.js
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,17 @@ | ||
import registerWebworker from 'webworker-promise/lib/register'; | ||
|
||
registerWebworker(async ({ array, min, max, numberOfBins }, emit) => { | ||
const delta = max - min; | ||
const histogram = new Float32Array(numberOfBins); | ||
histogram.fill(0); | ||
for (let i = 0, len = array.length; i < len; i++) { | ||
const idx = Math.floor( | ||
(numberOfBins - 1) * (Number(array[i]) - min) / delta | ||
); | ||
histogram[idx] += 1; | ||
} | ||
|
||
return new registerWebworker.TransferableResponse(histogram, [ | ||
histogram.buffer, | ||
]); | ||
}); |
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
77 changes: 77 additions & 0 deletions
77
Sources/Rendering/OpenGL/Texture/ComputeGradients.worker.js
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,77 @@ | ||
import { vec3 } from 'gl-matrix'; | ||
import registerWebworker from 'webworker-promise/lib/register'; | ||
|
||
import vtkMath from 'vtk.js/Sources/Common/Core/Math'; | ||
|
||
registerWebworker( | ||
async ( | ||
{ width, height, depth, spacing, data, haveWebgl2, depthStart, depthEnd }, | ||
emit | ||
) => { | ||
// have to compute the gradient to get the normal | ||
// and magnitude | ||
const depthLength = depthEnd - depthStart + 1; | ||
const gradients = new Float32Array(width * height * depthLength * 4); | ||
const gradientMagnitudes = new Float32Array(width * height * depthLength); | ||
|
||
const sliceSize = width * height; | ||
let inPtr = 0; | ||
let outPtr = 0; | ||
const grad = vec3.create(); | ||
vec3.set( | ||
grad, | ||
(data[inPtr + 1] - data[inPtr]) / spacing[0], | ||
(data[inPtr + width] - data[inPtr]) / spacing[1], | ||
(data[inPtr + sliceSize] - data[inPtr]) / spacing[2] | ||
); | ||
let minMag = vec3.length(grad); | ||
let maxMag = -1.0; | ||
for (let z = depthStart; z < depthEnd + 1; ++z) { | ||
let zedge = 0; | ||
if (z === depth - 1) { | ||
zedge = -sliceSize; | ||
} | ||
for (let y = 0; y < height; ++y) { | ||
let yedge = 0; | ||
if (y === height - 1) { | ||
yedge = -width; | ||
} | ||
for (let x = 0; x < width; ++x) { | ||
let edge = inPtr + zedge + yedge; | ||
if (x === width - 1) { | ||
edge--; | ||
} | ||
vec3.set( | ||
grad, | ||
(data[edge + 1] - data[edge]) / spacing[0], | ||
(data[edge + width] - data[edge]) / spacing[1], | ||
(data[edge + sliceSize] - data[edge]) / spacing[2] | ||
); | ||
|
||
const mag = vec3.length(grad); | ||
vec3.normalize(grad, grad); | ||
gradients[outPtr++] = grad[0]; | ||
gradients[outPtr++] = grad[1]; | ||
gradients[outPtr++] = grad[2]; | ||
gradients[outPtr++] = mag; | ||
gradientMagnitudes[inPtr] = mag; | ||
inPtr++; | ||
} | ||
} | ||
} | ||
const arrayMinMag = vtkMath.arrayMin(gradientMagnitudes); | ||
const arrayMaxMag = vtkMath.arrayMax(gradientMagnitudes); | ||
minMag = Math.min(arrayMinMag, minMag); | ||
maxMag = Math.max(arrayMaxMag, maxMag); | ||
|
||
const result = { | ||
subGradients: gradients, | ||
subMinMag: minMag, | ||
subMaxMag: maxMag, | ||
subDepthStart: depthStart, | ||
}; | ||
return new registerWebworker.TransferableResponse(result, [ | ||
gradients.buffer, | ||
]); | ||
} | ||
); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for float32 instead of uint32?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later, we normalize.