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

81.74% Statements 103/126
60% Branches 27/45
88.23% Functions 15/17
81.51% Lines 97/119

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    1x   1x               2826x     2826x     2826x   2826x     35583x 35583x 35583x 4086x 4086x       31497x   31497x 29936x     31497x     2826x 70900x 70900x 43752x       2826x 9002x 4354x     4648x 4648x 4648x 4648x 4361x     287x     2826x 20503x     20503x 16201x   4302x     2826x 200x 200x                           2826x 8957x         8957x 8957x 6563x 6563x       2394x 2394x 2230x 2230x 2230x 2230x 2230x     164x         2826x 5133x 4369x     764x 4561x 4561x               2826x                                       2826x 4817x 6567x       2826x 24153x     2826x 4817x 4817x 8797x 8797x 8797x 8793x 8793x   4x 4x 4x   4x       4817x     2826x 2394x       2394x 2394x 2230x   2394x     2826x 2826x 2409x 2208x   2409x               1x                     2826x     2826x 2826x   2826x   2826x 2826x 2826x 2826x     2826x         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.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 };