Skip to content

Commit

Permalink
Merge pull request #576 from thewtex/volume-rendering-performance
Browse files Browse the repository at this point in the history
Volume rendering performance
  • Loading branch information
jourdain authored Feb 12, 2018
2 parents fa779a0 + 650be91 commit 5c77a5a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Sources/Common/Core/Math/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Same as Math.min().

Same as Math.max().

### isPowerOfTwo(number) : Boolean
### arrayMin(array)

NOT IMPLEMENTED
Minimum of the array.

### nearestPowerOfTwo(number) : int
### arrayMax(a, b)

NOT IMPLEMENTED
Maximum of the array.

### factorial(n) : number

Expand Down
30 changes: 30 additions & 0 deletions Sources/Common/Core/Math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { vtkErrorMacro, vtkWarningMacro } = macro;
let randomSeedValue = 0;
const VTK_MAX_ROTATIONS = 20;
const VTK_SMALL_NUMBER = 1.0e-12;
const MAX_FUNCTION_ARGUMENTS = 32768;

function notImplemented(method) {
return () => vtkErrorMacro(`vtkMath::${method} - NOT IMPLEMENTED`);
Expand Down Expand Up @@ -41,6 +42,33 @@ const Pi = () => Math.PI;
const radiansFromDegrees = (deg) => deg / 180 * Math.PI;
const degreesFromRadians = (rad) => rad * 180 / Math.PI;
const { round, floor, ceil, min, max } = Math;

function arrayMin(arr) {
let minValue = Infinity;
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
const submin = Math.min.apply(
null,
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
);
minValue = Math.min(submin, minValue);
}

return minValue;
}

function arrayMax(arr) {
let maxValue = -Infinity;
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
const submax = Math.max.apply(
null,
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
);
maxValue = Math.max(submax, maxValue);
}

return maxValue;
}

const ceilLog2 = notImplemented('ceilLog2');
const factorial = notImplemented('factorial');

Expand Down Expand Up @@ -1993,6 +2021,8 @@ export default {
ceilLog2,
min,
max,
arrayMin,
arrayMax,
isPowerOfTwo,
nearestPowerOfTwo,
factorial,
Expand Down
16 changes: 6 additions & 10 deletions Sources/Interaction/Widgets/PiecewiseGaussianWidget/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import macro from 'vtk.js/Sources/macro';
import vtkMath from 'vtk.js/Sources/Common/Core/Math';

/* eslint-disable no-continue */

Expand Down Expand Up @@ -438,26 +439,21 @@ function vtkPiecewiseGaussianWidget(publicAPI, model) {
numberOfBinsToSkip = 1
) => {
model.histogramArray = array;
const size = array.length;
let max = array[0];
let min = array[0];
for (let i = 1; i < size; i++) {
max = Math.max(max, array[i]);
min = Math.min(min, array[i]);
}
const max = vtkMath.arrayMax(array);
const min = vtkMath.arrayMin(array);

const delta = max - min;
model.dataRange = [min, max];
model.histogram = [];
while (model.histogram.length < model.numberOfBins) {
model.histogram.push(0);
}
array.forEach((value) => {
for (let i = 0, len = array.length; i < len; i++) {
const idx = Math.floor(
(model.numberOfBins - 1) * (Number(value) - min) / delta
(model.numberOfBins - 1) * (Number(array[i]) - min) / delta
);
model.histogram[idx] += 1;
});
}

// Smart Rescale Histogram
const sampleSize = Math.min(
Expand Down
17 changes: 8 additions & 9 deletions Sources/Rendering/OpenGL/Texture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,12 +930,8 @@ function vtkOpenGLTexture(publicAPI, model) {
const numPixelsIn = width * height * depth;

// compute min and max values
let min = data[0];
let max = data[0];
for (let i = 0; i < numPixelsIn; ++i) {
min = Math.min(min, data[i]);
max = Math.max(max, data[i]);
}
const min = vtkMath.arrayMin(data);
let max = vtkMath.arrayMax(data);
if (min === max) {
max = min + 1.0;
}
Expand Down Expand Up @@ -1115,6 +1111,7 @@ function vtkOpenGLTexture(publicAPI, model) {
// have to compute the gradient to get the normal
// and magnitude
const tmpArray = new Float32Array(width * height * depth * 4);
const tmpMagArray = new Float32Array(width * height * depth);

let inPtr = 0;
let outPtr = 0;
Expand Down Expand Up @@ -1151,18 +1148,20 @@ function vtkOpenGLTexture(publicAPI, model) {
);

const mag = vec3.length(grad);
minMag = Math.min(mag, minMag);
maxMag = Math.max(mag, maxMag);

vec3.normalize(grad, grad);
tmpArray[outPtr++] = grad[0];
tmpArray[outPtr++] = grad[1];
tmpArray[outPtr++] = grad[2];
tmpArray[outPtr++] = mag;
tmpMagArray[inPtr] = mag;
inPtr++;
}
}
}
const arrayMinMag = vtkMath.arrayMin(tmpMagArray);
const arrayMaxMag = vtkMath.arrayMax(tmpMagArray);
minMag = Math.min(arrayMinMag, minMag);
maxMag = Math.max(arrayMaxMag, maxMag);

// store the information, we will need it later
model.volumeInfo = { min: minMag, max: maxMag };
Expand Down

0 comments on commit 5c77a5a

Please sign in to comment.