All files / Sources/Common/DataModel/ImplicitBoolean index.js

27.27% Statements 21/77
0% Branches 0/23
12.5% Functions 2/16
31.34% Lines 21/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151        1x                               1x     1x   1x                       1x     1x 1x 1x   1x 1x   1x           1x             1x       1x                                                                           1x                                             1x               1x     1x   1x   1x         1x          
import macro from 'vtk.js/Sources/macros';
import Constants from 'vtk.js/Sources/Common/DataModel/ImplicitBoolean/Constants';
import vtkImplicitFunction from 'vtk.js/Sources/Common/DataModel/ImplicitFunction';
 
const { Operation } = Constants;
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
// vtkImplicitBoolean methods
// ----------------------------------------------------------------------------
 
function vtkImplicitBoolean(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImplicitBoolean');
 
  // Capture "parentClass" api for internal use
  const superClass = { ...publicAPI };
 
  publicAPI.getMTime = () => {
    let mTime = superClass.getMTime();
    if (!model.functions || model.functions.length <= 0) {
      return mTime;
    }
 
    for (let i = 0; i < model.functions.length; ++i) {
      mTime = Math.max(mTime, model.functions[i].getMTime());
    }
    return mTime;
  };
 
  publicAPI.getOperationAsString = () =>
    macro.enumToString(Operation, model.operation);
 
  publicAPI.setOperationToUnion = () => publicAPI.setOperation(0);
  publicAPI.setOperationToIntersection = () => publicAPI.setOperation(1);
  publicAPI.setOperationToDifference = () => publicAPI.setOperation(2);
 
  publicAPI.getFunctions = () => model.functions;
  publicAPI.hasFunction = (f) =>
    !!model.functions.filter((item) => item === f).length;
  publicAPI.addFunction = (f) => {
    if (f && !publicAPI.hasFunction(f)) {
      model.functions = model.functions.concat(f);
    }
  };
 
  publicAPI.removeFunction = (f) => {
    const newFunctionList = model.functions.filter((item) => item !== f);
    if (model.functions.length !== newFunctionList.length) {
      model.functions = newFunctionList;
    }
  };
 
  publicAPI.removeAllFunctions = () => {
    model.functions = [];
  };
 
  publicAPI.evaluateFunction = (xyz) => {
    let value = 0.0;
    if (model.functions.length <= 0) {
      return value;
    }
 
    if (model.operation === Operation.UNION) {
      value = Number.MAX_VALUE;
      for (let i = 0; i < model.functions.length; ++i) {
        const f = model.functions[i];
        const v = f.functionValue(xyz);
        if (v < value) {
          value = v;
        }
      }
    } else if (model.operation === Operation.INTERSECTION) {
      value = -Number.MAX_VALUE;
      for (let i = 0; i < model.functions.length; ++i) {
        const f = model.functions[i];
        const v = f.functionValue(xyz);
        if (v > value) {
          value = v;
        }
      }
    } else {
      const firstF = model.functions[0];
      value = firstF.functionValue(xyz);
      for (let i = 1; i < model.functions.length; ++i) {
        const f = model.functions[i];
        const v = -1.0 * f.evaluateFunction(xyz);
        if (v > value) {
          value = v;
        }
      }
    }
    return value;
  };
 
  publicAPI.evaluateGradient = (xyz) => {
    const t =
      model.axis[0] * (xyz[0] - model.center[0]) +
      model.axis[1] * (xyz[1] - model.center[1]) +
      model.axis[2] * (xyz[2] - model.center[2]);
 
    const cp = new Float32Array(3);
    cp[0] = model.center[0] + t * model.axis[0];
    cp[1] = model.center[1] + t * model.axis[1];
    cp[2] = model.center[2] + t * model.axis[2];
 
    const retVal = [
      2.0 * (xyz[0] - cp[0]),
      2.0 * (xyz[1] - cp[1]),
      2.0 * (xyz[2] - cp[2]),
    ];
    return retVal;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
  operation: 0,
  functions: [],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkImplicitFunction.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, ['operation']);
 
  vtkImplicitBoolean(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImplicitBoolean');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };