Skip to content

Commit

Permalink
fix(DataArray): Add magnitude range computation
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jan 12, 2018
1 parent 2deb74a commit 3564c02
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions Sources/Common/Core/DataArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,61 @@ import macro from 'vtk.js/Sources/macro';
import Constants from 'vtk.js/Sources/Common/Core/DataArray/Constants';

const { DefaultDataType } = Constants;
const { vtkErrorMacro } = macro;
const TUPLE_HOLDER = [];

// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------

function computeRange(values, component = 0, tuple = 1) {
const range = { min: Number.MAX_VALUE, max: -Number.MAX_VALUE };
function createRangeHelper() {
let min = Number.MAX_VALUE;
let max = -Number.MAX_VALUE;
let count = 0;
let sum = 0;

return {
add(value) {
if (min > value) {
min = value;
}
if (max < value) {
max = value;
}
count++;
sum += value;
},
get() {
return { min, max, count, sum, mean: sum / count };
},
getRange() {
return { min, max };
},
};
}

function computeRange(values, component = 0, numberOfComponents = 1) {
const helper = createRangeHelper();
const size = values.length;
let value = 0;

if (component < 0) {
// Compute magnitude
vtkErrorMacro('vtkDataArray: Compute magnitude - NOT IMPLEMENTED');
return range;
for (let i = 0; i < size; i += numberOfComponents) {
value = 0;
for (let j = 0; j < numberOfComponents; j++) {
value += values[i + j] * values[i + j];
}
value **= 0.5;
helper.add(value);
}
return helper.getRange();
}

const size = values.length;
for (let i = component; i < size; i += tuple) {
const value = values[i];
if (range.min > value) {
range.min = value;
}
if (range.max < value) {
range.max = value;
}
for (let i = component; i < size; i += numberOfComponents) {
helper.add(values[i]);
}

return range;
return helper.getRange();
}

function ensureRangeSize(rangeArray, size = 0) {
Expand All @@ -55,6 +82,7 @@ function getDataType(typedArray) {
export const STATIC = {
computeRange,
getDataType,
createRangeHelper,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -93,7 +121,7 @@ function vtkDataArray(publicAPI, model) {

publicAPI.getData = () => model.values;

publicAPI.getRange = (componentIndex = 0) => {
publicAPI.getRange = (componentIndex = -1) => {
const rangeIdx =
componentIndex < 0 ? model.numberOfComponents : componentIndex;
let range = null;
Expand Down

0 comments on commit 3564c02

Please sign in to comment.