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

51.54% Statements 50/97
30.76% Branches 12/39
50% Functions 15/30
50.54% Lines 46/91

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    1x 1x             2x               187x                 209x     209x 224x     224x 224x                 224x       209x 2x 1x     224x     209x 187x     209x 208x     208x 208x 208x       209x         209x     209x 290x   312x 41x         209x 276x 276x 152x   124x       209x                                                                                 209x                 209x                 209x                             1x                         209x     209x   209x           209x 209x 209x 209x     209x         1x                      
import macro from 'vtk.js/Sources/macros';
 
const DEFAULT_VIEW_API = 'WebGL';
const VIEW_CONSTRUCTORS = Object.create(null);
 
// ----------------------------------------------------------------------------
// static methods
// ----------------------------------------------------------------------------
 
export function registerViewConstructor(name, constructor) {
  VIEW_CONSTRUCTORS[name] = constructor;
}
 
export function listViewAPIs() {
  return Object.keys(VIEW_CONSTRUCTORS);
}
 
export function newAPISpecificView(name, initialValues = {}) {
  return VIEW_CONSTRUCTORS[name] && VIEW_CONSTRUCTORS[name](initialValues);
}
 
// ----------------------------------------------------------------------------
// vtkRenderWindow methods
// ----------------------------------------------------------------------------
 
function vtkRenderWindow(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkRenderWindow');
 
  // Add renderer
  publicAPI.addRenderer = (renderer) => {
    Iif (publicAPI.hasRenderer(renderer)) {
      return;
    }
    renderer.setRenderWindow(publicAPI);
    model.renderers.push(renderer);
 
    // for (this->Renderers->InitTraversal(rsit);
    //      (aren = this->Renderers->GetNextRenderer(rsit)); )
    //   {
    //   aren->SetAllocatedRenderTime
    //     (1.0/(this->DesiredUpdateRate*this->Renderers->GetNumberOfItems()));
    //   }
 
    publicAPI.modified();
  };
 
  // Remove renderer
  publicAPI.removeRenderer = (renderer) => {
    model.renderers = model.renderers.filter((r) => r !== renderer);
    publicAPI.modified();
  };
 
  publicAPI.hasRenderer = (ren) => model.renderers.indexOf(ren) !== -1;
 
  // get an API specific view of this data
  publicAPI.newAPISpecificView = (name, initialValues = {}) =>
    newAPISpecificView(name || model.defaultViewAPI, initialValues);
 
  // Add renderer
  publicAPI.addView = (view) => {
    Iif (publicAPI.hasView(view)) {
      return;
    }
    view.setRenderable(publicAPI);
    model._views.push(view);
    publicAPI.modified();
  };
 
  // Remove renderer
  publicAPI.removeView = (view) => {
    model._views = model._views.filter((r) => r !== view);
    publicAPI.modified();
  };
 
  publicAPI.hasView = (view) => model._views.indexOf(view) !== -1;
 
  // handle any pre render initializations
  publicAPI.preRender = () => {
    model.renderers.forEach((ren) => {
      // make sure we have a camera
      if (!ren.isActiveCameraCreated()) {
        ren.resetCamera();
      }
    });
  };
 
  publicAPI.render = () => {
    publicAPI.preRender();
    if (model.interactor) {
      model.interactor.render();
    } else {
      model._views.forEach((view) => view.traverseAllPasses());
    }
  };
 
  publicAPI.getStatistics = () => {
    const results = { propCount: 0, invisiblePropCount: 0, gpuMemoryMB: 0 };
    model._views.forEach((v) => {
      if (v.getGraphicsMemoryInfo)
        results.gpuMemoryMB += v.getGraphicsMemoryInfo() / 1e6;
    });
    model.renderers.forEach((ren) => {
      const props = ren.getViewProps();
      const gren = model._views[0].getViewNodeFor(ren);
      props.forEach((prop) => {
        if (prop.getVisibility()) {
          results.propCount += 1;
          const mpr = prop.getMapper && prop.getMapper();
          if (mpr && mpr.getPrimitiveCount) {
            const gmpr = gren.getViewNodeFor(mpr);
            if (gmpr) {
              if (gmpr.getAllocatedGPUMemoryInBytes) {
                results.gpuMemoryMB +=
                  gmpr.getAllocatedGPUMemoryInBytes() / 1e6;
              }
              const pcount = mpr.getPrimitiveCount();
              Object.keys(pcount).forEach((keyName) => {
                if (!results[keyName]) {
                  results[keyName] = 0;
                }
                results[keyName] += pcount[keyName];
              });
            }
          }
        } else {
          results.invisiblePropCount += 1;
        }
      });
    });
    results.str = Object.keys(results)
      .map((keyName) => `${keyName}: ${results[keyName]}`)
      .join('\n');
 
    return results;
  };
 
  publicAPI.captureImages = (format = 'image/png', opts = {}) => {
    macro.setImmediate(publicAPI.render);
    return model._views
      .map((view) =>
        view.captureNextImage ? view.captureNextImage(format, opts) : undefined
      )
      .filter((i) => !!i);
  };
 
  publicAPI.addRenderWindow = (child) => {
    if (model.childRenderWindows.includes(child)) {
      return false;
    }
    model.childRenderWindows.push(child);
    publicAPI.modified();
    return true;
  };
 
  publicAPI.removeRenderWindow = (child) => {
    const childIndex = model.childRenderWindows.findIndex((x) => x === child);
    if (childIndex < 0) {
      return false;
    }
    model.childRenderWindows.splice(childIndex, 1);
    publicAPI.modified();
    return true;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  defaultViewAPI: DEFAULT_VIEW_API,
  renderers: [],
  views: [],
  interactor: null,
  neverRendered: true,
  numberOfLayers: 1,
  childRenderWindows: [],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
 
  macro.setGet(publicAPI, model, [
    'interactor',
    'numberOfLayers',
    '_views',
    'defaultViewAPI',
  ]);
  macro.get(publicAPI, model, ['neverRendered']);
  macro.getArray(publicAPI, model, ['renderers', 'childRenderWindows']);
  macro.moveToProtected(publicAPI, model, ['views']);
  macro.event(publicAPI, model, 'completion');
 
  // Object methods
  vtkRenderWindow(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkRenderWindow');
 
// ----------------------------------------------------------------------------
 
export default {
  newInstance,
  extend,
  registerViewConstructor,
  listViewAPIs,
  newAPISpecificView,
};