All files / Sources/Rendering/SceneGraph/ViewNode index.js

79.56% Statements 109/137
63.26% Branches 31/49
78.94% Functions 15/19
79.84% Lines 103/129

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255    1x   1x               2903x     2903x     2903x   2903x     35916x 35916x 35916x 4133x 4133x       31783x   31783x 30186x     31783x     2903x 71522x 71522x 44128x       2903x 9076x 4391x     4685x 4685x 4685x 4685x 4398x     287x     2903x 12668x     12668x 12332x   336x     2903x 20207x 8207x   12000x 12000x 3998x   8002x 8002x             2903x 9027x         9027x 9027x 6607x 6607x       2420x 2420x 2256x 2256x 2256x 2256x 2256x     164x         2903x 5184x 4394x     790x 4596x 4596x               2903x                                       2903x                           2903x 4865x 6611x       2903x 24337x     2903x 4865x 4865x 8867x 8867x 8867x 8863x 8863x   4x 4x 4x   4x       4865x     2903x 2420x       2420x 2420x 2256x   2420x     2903x 2903x 2440x 2234x   2440x               1x                     2903x     2903x 2903x   2903x   2903x 2903x 2903x 2903x     2903x         1x          
import macro from 'vtk.js/Sources/macros';
 
const { vtkErrorMacro } = macro;
 
const PASS_TYPES = ['Build', 'Render'];
 
// ----------------------------------------------------------------------------
// vtkViewNode methods
// ----------------------------------------------------------------------------
 
function vtkViewNode(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkViewNode');
 
  // Builds myself.
  publicAPI.build = (prepass) => {};
 
  // Renders myself
  publicAPI.render = (prepass) => {};
 
  publicAPI.traverse = (renderPass) => {
    // we can choose to do special
    // traversal here based on pass
    const passTraversal = renderPass.getTraverseOperation();
    const fn = publicAPI[passTraversal];
    if (fn) {
      fn(renderPass);
      return;
    }
 
    // default traversal
    publicAPI.apply(renderPass, true);
 
    for (let index = 0; index < model.children.length; index++) {
      model.children[index].traverse(renderPass);
    }
 
    publicAPI.apply(renderPass, false);
  };
 
  publicAPI.apply = (renderPass, prepass) => {
    const customRenderPass = publicAPI[renderPass.getOperation()];
    if (customRenderPass) {
      customRenderPass(prepass, renderPass);
    }
  };
 
  publicAPI.getViewNodeFor = (dataObject) => {
    if (model.renderable === dataObject) {
      return publicAPI;
    }
 
    for (let index = 0; index < model.children.length; ++index) {
      const child = model.children[index];
      const vn = child.getViewNodeFor(dataObject);
      if (vn) {
        return vn;
      }
    }
    return undefined;
  };
 
  publicAPI.getFirstAncestorOfType = (type) => {
    Iif (!model._parent) {
      return null;
    }
    if (model._parent.isA(type)) {
      return model._parent;
    }
    return model._parent.getFirstAncestorOfType(type);
  };
 
  publicAPI.getLastAncestorOfType = (type) => {
    if (!model._parent) {
      return null;
    }
    const lastAncestor = model._parent.getLastAncestorOfType(type);
    if (lastAncestor) {
      return lastAncestor;
    }
    if (model._parent.isA(type)) {
      return model._parent;
    }
    return null;
  };
 
  // add a missing node/child for the passed in renderables. This should
  // be called only in between prepareNodes and removeUnusedNodes
  publicAPI.addMissingNode = (dobj) => {
    Iif (!dobj) {
      return undefined;
    }
 
    // if found just mark as visited
    const result = model._renderableChildMap.get(dobj);
    if (result !== undefined) {
      result.setVisited(true);
      return result;
    }
 
    // otherwise create a node
    const newNode = publicAPI.createViewNode(dobj);
    if (newNode) {
      newNode.setParent(publicAPI);
      newNode.setVisited(true);
      model._renderableChildMap.set(dobj, newNode);
      model.children.push(newNode);
      return newNode;
    }
 
    return undefined;
  };
 
  // add missing nodes/children for the passed in renderables. This should
  // be called only in between prepareNodes and removeUnusedNodes
  publicAPI.addMissingNodes = (dataObjs) => {
    if (!dataObjs || !dataObjs.length) {
      return;
    }
 
    for (let index = 0; index < dataObjs.length; ++index) {
      const dobj = dataObjs[index];
      publicAPI.addMissingNode(dobj);
    }
  };
 
  // ability to add children that have no renderable use in the same manner
  // as addMissingNodes This case is when a normal viewnode wants to
  // delegate passes to a helper or child that doeasn't map to a clear
  // renderable or any renderable
  publicAPI.addMissingChildren = (children) => {
    if (!children || !children.length) {
      return;
    }
 
    for (let index = 0; index < children.length; ++index) {
      const child = children[index];
      const cindex = model.children.indexOf(child);
      if (cindex === -1) {
        child.setParent(publicAPI);
        model.children.push(child);
        const childRenderable = child.getRenderable();
        if (childRenderable) {
          model._renderableChildMap.set(childRenderable, child);
        }
      }
      child.setVisited(true);
    }
  };
 
  publicAPI.removeNode = (child) => {
    const childIdx = model.children.findIndex((x) => x === child);
    if (childIdx < 0) {
      return false;
    }
    const renderable = child.getRenderable();
    if (renderable) {
      model._renderableChildMap.delete(renderable);
    }
    child.delete();
    model.children.splice(childIdx, 1);
    return true;
  };
 
  publicAPI.prepareNodes = () => {
    for (let index = 0; index < model.children.length; ++index) {
      model.children[index].setVisited(false);
    }
  };
 
  publicAPI.setVisited = (val) => {
    model.visited = val;
  };
 
  publicAPI.removeUnusedNodes = () => {
    let visitedCount = 0;
    for (let index = 0; index < model.children.length; ++index) {
      const child = model.children[index];
      const visited = child.getVisited();
      if (visited) {
        model.children[visitedCount++] = child;
        child.setVisited(false);
      } else {
        const renderable = child.getRenderable();
        if (renderable) {
          model._renderableChildMap.delete(renderable);
        }
        child.delete();
      }
    }
 
    model.children.length = visitedCount;
  };
 
  publicAPI.createViewNode = (dataObj) => {
    Iif (!model.myFactory) {
      vtkErrorMacro('Cannot create view nodes without my own factory');
      return null;
    }
    const ret = model.myFactory.createNode(dataObj);
    if (ret) {
      ret.setRenderable(dataObj);
    }
    return ret;
  };
 
  const parentDelete = publicAPI.delete;
  publicAPI.delete = () => {
    for (let i = 0; i < model.children.length; i++) {
      model.children[i].delete();
    }
    parentDelete();
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // _parent: null,
  renderable: null,
  myFactory: null,
  children: [],
  visited: false,
};
 
// ----------------------------------------------------------------------------
 
function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.event(publicAPI, model, 'event');
 
  model._renderableChildMap = new Map();
 
  macro.get(publicAPI, model, ['visited']);
  macro.setGet(publicAPI, model, ['_parent', 'renderable', 'myFactory']);
  macro.getArray(publicAPI, model, ['children']);
  macro.moveToProtected(publicAPI, model, ['parent']);
 
  // Object methods
  vtkViewNode(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
const newInstance = macro.newInstance(extend, 'vtkViewNode');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, PASS_TYPES };