All files / Sources/Interaction/Animations/TimeStepBasedAnimationHandler index.js

3.12% Statements 2/64
0% Branches 0/41
0% Functions 0/15
3.17% Lines 2/63

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 152                                                                                                                                                                                                                            1x                                                                 1x                
import macro from 'vtk.js/Sources/macros';
 
// ----------------------------------------------------------------------------
// vtkTimeStepBasedAnimationHandler methods
// ----------------------------------------------------------------------------
 
function vtkTimeStepBasedAnimationHandler(publicAPI, model) {
  publicAPI.setCurrentTimeStep = (time) => {
    if (!model.timeSteps || model.timeSteps.length === 0) {
      return;
    }
    if (model.timeSteps.includes(time)) {
      model.currentTimeStep = time;
    } else if (time <= model.timeSteps[0]) {
      model.currentTimeStep = model.timeSteps[0];
    } else {
      model.currentTimeStep = model.timeSteps.find((prevTime, index, array) => {
        if (index === array.length - 1) {
          return true;
        }
        return time >= prevTime && time < array[index + 1];
      });
    }
    publicAPI.update();
  };
 
  publicAPI.setData = (data) => {
    model.data = data;
 
    // Refresh timesteps
    if (data.timeSteps) {
      model.timeSteps = data.timeSteps.map((timeStep) => timeStep.time);
      model.timeRange = [
        model.timeSteps[0],
        model.timeSteps[model.timeSteps.length - 1],
      ];
      model.currentTimeStep = model.timeSteps[0];
    }
  };
 
  publicAPI.setScene = (scene, originalMetadata, applySettings, renderer) => {
    model.scene = scene;
    model.originalMetadata = originalMetadata;
    model.applySettings = applySettings;
    model.renderer = renderer;
  };
 
  publicAPI.setCameraParameters = (params) => {
    if (model.renderers) {
      model.renderers.forEach((renderer) => {
        const camera = renderer.getActiveCamera();
        if (camera) {
          camera.set(params);
        }
      });
    }
  };
 
  publicAPI.setBackground = (color) => {
    if (model.renderers) {
      model.renderers.forEach((renderer) => {
        renderer.setBackground(color);
      });
    }
  };
 
  publicAPI.update = () => {
    if (!model.data || !model.scene) {
      return;
    }
    const currentScene = model.data.timeSteps.find(
      (scene) => scene.time === model.currentTimeStep
    );
    if (currentScene.camera) {
      const camera = { ...model.originalMetadata.camera };
      Object.assign(camera, currentScene.camera);
      publicAPI.setCameraParameters(camera);
    }
    if (currentScene.background) {
      publicAPI.setBackground(currentScene.background);
    }
    if (model.scene) {
      model.scene.forEach((sceneItem) => {
        if (
          sceneItem.source.setUpdateTimeStep &&
          sceneItem.source.getTimeSteps().includes(model.currentTimeStep)
        ) {
          sceneItem.source.setUpdateTimeStep(model.currentTimeStep);
        }
        const id = sceneItem.id;
        if (currentScene[id]) {
          const settings = { ...sceneItem.defaultSettings };
          Object.assign(settings, currentScene[id]);
          model.applySettings(sceneItem, settings);
        }
      });
    }
  };
 
  publicAPI.addRenderer = (renderer) => {
    if (renderer && !model.renderers.includes(renderer)) {
      model.renderers.push(renderer);
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  timeSteps: [0.0],
  timeRange: [0.0, 0.0],
  currentTimeStep: 0.0,
  scene: null,
  data: null,
  renderers: [],
  applySettings: null,
  originalMetadata: null,
};
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
 
  macro.get(publicAPI, model, [
    'applySettings',
    'currentTimeStep',
    'data',
    'originalMetada',
    'scene',
    'timeRange',
    'timeSteps',
  ]);
 
  macro.setGet(publicAPI, model, ['renderers']);
 
  // Object methods
  vtkTimeStepBasedAnimationHandler(publicAPI, model);
}
 
export const newInstance = macro.newInstance(
  extend,
  'vtkTimeStepBasedAnimationHandler'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };