All files / Sources/Rendering/Core/Light index.js

69.38% Statements 34/49
42.85% Branches 3/7
61.53% Functions 8/13
69.56% Lines 32/46

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            1x               216x 216x   216x 5x     5x   5x     216x                           216x 12x 10x 10x 10x   12x       216x           216x                           216x 207x     216x       216x 1x 1x     3988x   216x   216x             1x                                         216x     216x 216x                       216x               216x         1x          
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import { vec3 } from 'gl-matrix';
 
// ----------------------------------------------------------------------------
 
export const LIGHT_TYPES = ['HeadLight', 'CameraLight', 'SceneLight'];
 
// ----------------------------------------------------------------------------
// vtkLight methods
// ----------------------------------------------------------------------------
 
function vtkLight(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkLight');
  const tmpVec = new Float64Array(3);
 
  publicAPI.getTransformedPosition = () => {
    Iif (model.transformMatrix) {
      vec3.transformMat4(tmpVec, model.position, model.transformMatrix);
    } else {
      vec3.set(tmpVec, model.position[0], model.position[1], model.position[2]);
    }
    return tmpVec;
  };
 
  publicAPI.getTransformedFocalPoint = () => {
    if (model.transformMatrix) {
      vec3.transformMat4(tmpVec, model.focalPoint, model.transformMatrix);
    } else {
      vec3.set(
        tmpVec,
        model.focalPoint[0],
        model.focalPoint[1],
        model.focalPoint[2]
      );
    }
    return tmpVec;
  };
 
  publicAPI.getDirection = () => {
    if (model.directionMTime < model.mtime) {
      vec3.sub(model.direction, model.focalPoint, model.position);
      vtkMath.normalize(model.direction);
      model.directionMTime = model.mtime;
    }
    return model.direction;
  };
 
  // Sets the direction from a vec3 instead of a focal point
  publicAPI.setDirection = (directionVector) => {
    const newFocalPoint = new Float64Array(3);
    vec3.sub(newFocalPoint, model.position, directionVector);
    model.focalPoint = newFocalPoint;
  };
 
  publicAPI.setDirectionAngle = (elevation, azimuth) => {
    const elevationRadians = vtkMath.radiansFromDegrees(elevation);
    const azimuthRadians = vtkMath.radiansFromDegrees(azimuth);
 
    publicAPI.setPosition(
      Math.cos(elevationRadians) * Math.sin(azimuthRadians),
      Math.sin(elevationRadians),
      Math.cos(elevationRadians) * Math.cos(azimuthRadians)
    );
 
    publicAPI.setFocalPoint(0, 0, 0);
    publicAPI.setPositional(0);
  };
 
  publicAPI.setLightTypeToHeadLight = () => {
    publicAPI.setLightType('HeadLight');
  };
 
  publicAPI.setLightTypeToCameraLight = () => {
    publicAPI.setLightType('CameraLight');
  };
 
  publicAPI.setLightTypeToSceneLight = () => {
    publicAPI.setTransformMatrix(null);
    publicAPI.setLightType('SceneLight');
  };
 
  publicAPI.lightTypeIsHeadLight = () => model.lightType === 'HeadLight';
 
  publicAPI.lightTypeIsSceneLight = () => model.lightType === 'SceneLight';
 
  publicAPI.lightTypeIsCameraLight = () => model.lightType === 'CameraLight';
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  switch: true,
  intensity: 1,
  color: [1, 1, 1],
  position: [0, 0, 1],
  focalPoint: [0, 0, 0],
  positional: false,
  exponent: 1,
  coneAngle: 30,
  coneFalloff: 5,
  attenuationValues: [1, 0, 0],
  transformMatrix: null,
  lightType: 'SceneLight',
  shadowAttenuation: 1,
  direction: [0, 0, 0],
  directionMTime: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, [
    'intensity',
    'switch',
    'positional',
    'exponent',
    'coneAngle',
    'coneFalloff',
    'transformMatrix',
    'lightType',
    'shadowAttenuation',
    'attenuationValues',
  ]);
  macro.setGetArray(
    publicAPI,
    model,
    ['color', 'position', 'focalPoint', 'attenuationValues'],
    3
  );
 
  // Object methods
  vtkLight(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkLight');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, LIGHT_TYPES };