All files / Sources/Rendering/Misc/SynchronizableRenderWindow/BehaviorManager index.js

3.92% Statements 2/51
0% Branches 0/28
0% Functions 0/5
4.16% Lines 2/48

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    1x                                                                                       1x                                                                                                                                      
import vtkCameraSynchronizer from './CameraSynchronizer';
 
const BEHAVIORS = {};
 
class CameraSync {
  constructor(ctx, config) {
    this.ctx = ctx;
    this.behavior = vtkCameraSynchronizer.newInstance(
      this.getProperties(config)
    );
    this.behavior.update();
  }
 
  getProperties({ actorBounds, srcRenderer, dstRenderer }) {
    const distance =
      3.4 *
      Math.max(
        actorBounds[1] - actorBounds[0],
        actorBounds[3] - actorBounds[2],
        actorBounds[5] - actorBounds[4]
      );
    const focalPoint = [
      0.5 * (actorBounds[0] + actorBounds[1]),
      0.5 * (actorBounds[2] + actorBounds[3]),
      0.5 * (actorBounds[4] + actorBounds[5]),
    ];
    const mode = vtkCameraSynchronizer.SynchronizationMode.MODE_ORIENTATION;
    return {
      distance,
      focalPoint,
      mode,
      srcRenderer: this.ctx.getInstance(srcRenderer),
      dstRenderer: this.ctx.getInstance(dstRenderer),
    };
  }
 
  update(config) {
    this.behavior.set(this.getProperties(config));
    this.behavior.update();
  }
 
  delete() {
    this.behavior.delete();
  }
}
 
const BEHAVIORS_TYPES = {
  CameraSync,
};
 
export function applyBehaviors(renderWindow, state, context) {
  const rwId = renderWindow.get('synchronizedViewId').synchronizedViewId;
  if (!state.behaviors || !rwId) {
    return;
  }
  // Apply auto behavior
  if (!BEHAVIORS[rwId]) {
    BEHAVIORS[rwId] = {};
  }
  const localBehaviors = BEHAVIORS[rwId];
 
  if (state.behaviors.autoOrientation) {
    const renderers = renderWindow.getRenderers();
    if (!localBehaviors.autoOrientationAxes && renderers.length === 2) {
      let srcRenderer = null;
      let dstRenderer = null;
      for (let i = 0; i < renderers.length; i++) {
        const renderer = renderers[i];
        if (renderer.getInteractive()) {
          srcRenderer = renderer;
        } else {
          dstRenderer = renderer;
        }
      }
      if (srcRenderer && dstRenderer) {
        localBehaviors.autoOrientationAxes = vtkCameraSynchronizer.newInstance({
          srcRenderer,
          dstRenderer,
        });
      }
    }
    if (localBehaviors.autoOrientationAxes && renderers.length !== 2) {
      localBehaviors.autoOrientationAxes.delete();
      delete localBehaviors.autoOrientationAxes;
    }
  }
 
  const currentSets = Object.keys(state.behaviors);
  const existingSets = Object.keys(localBehaviors);
  for (let i = 0; i < currentSets.length; i++) {
    const key = currentSets[i];
    if (!localBehaviors[key]) {
      const config = state.behaviors[key];
      if (BEHAVIORS_TYPES[config.type]) {
        localBehaviors[key] = new BEHAVIORS_TYPES[config.type](context, config);
      } else {
        console.log('No mapping for', config);
      }
    } else {
      localBehaviors[key].update(state.behaviors[key]);
    }
  }
  for (let i = 0; i < existingSets.length; i++) {
    const key = currentSets[i];
    if (!state.behaviors[key]) {
      // Need to delete previously created behavior
      localBehaviors[key].delete();
      delete localBehaviors[key];
    }
  }
}
 
export default { applyBehaviors };