RenderWindow

Introduction

vtkRenderWindow is an abstract object to specify the behavior of a rendering window.

A rendering window is a window in a graphical user interface where renderers draw their images.
Methods are provided to synchronize the rendering process, set window size, and control double buffering.
The window also allows rendering in stereo. The interlaced render stereo type is for output to a VRex stereo projector.
All of the odd horizontal lines are from the left eye, and the even lines are from the right eye.
The user has to make the render window aligned with the VRex projector, or the eye will be swapped.

Methods

addRenderer

Add renderer

Argument Type Required Description
renderer Yes

addView

Add renderer

Argument Type Required Description
view Yes

captureImages

Argument Type Required Description
format Yes

extend

Method use to decorate a given object (publicAPI+model) with vtkRenderWindow characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IRenderWindowInitialValues No (default: {})

getDefaultViewAPI

getInteractor

getNeverRendered

getNumberOfLayers

getRenderers

getRenderersByReference

getStatistics

getViews

hasRenderer

Argument Type Required Description
ren vtkRenderer Yes

hasView

Argument Type Required Description
view Yes

invisiblePropCount

listViewAPIs

newAPISpecificView

newAPISpecificView

Argument Type Required Description
name Yes
initialValues Yes

newInstance

Method use to create a new instance of vtkRenderWindow

onCompletion

Argument Type Required Description
callback Yes

propCount

registerViewConstructor

removeRenderer

Remove renderer

Argument Type Required Description
renderer Yes

removeView

Remove renderer

Argument Type Required Description
view Yes

render

setDefaultViewAPI

Argument Type Required Description
defaultViewAPI Yes

setInteractor

Argument Type Required Description
interactor Yes

setNumberOfLayers

Argument Type Required Description
numberOfLayers Yes

setViews

Argument Type Required Description
views Yes

str

Source

index.d.ts
import { vtkObject, vtkSubscription } from "vtk.js/Sources/interfaces";
import vtkRenderer from "vtk.js/Sources/Rendering/Core/Renderer";
import vtkRenderWindowInteractor from "vtk.js/Sources/Rendering/Core/RenderWindowInteractor";
// import vtkOpenGLRenderWindow from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow';

interface IRenderWindowInitialValues {
renderers?: vtkRenderer[],
views?: vtkRenderWindow[],
interactor?: any,
neverRendered?: boolean,
numberOfLayers?: number
}

interface IStatistics {

/**
*
*/
propCount: number;

/**
*
*/
invisiblePropCount: number;

/**
*
*/
str: string;
}

export const enum DEFAULT_VIEW_API {
'WebGL',
'WebGPU'
}

export interface vtkRenderWindow extends vtkObject {

/**
* Add renderer
* @param renderer
*/
addRenderer(renderer: vtkRenderer): void;

/**
* Add renderer
* @param view
*/
addView(view: any): void;

/**
*
* @param format
*/
captureImages(format: string): void;

/**
*
*/
getDefaultViewAPI(): string;

/**
*
*/
getInteractor(): vtkRenderWindowInteractor;

/**
*
*/
getNumberOfLayers(): number;

/**
*
*/
getNeverRendered(): boolean;

/**
*
*/
getRenderers(): vtkRenderer[];

/**
*
*/
getRenderersByReference(): vtkRenderer[];

/**
*
*/
getStatistics(): IStatistics;

/**
*
*/
getViews(): any[];

// getViews(): vtkOpenGLRenderWindow[];

/**
*
* @param {vtkRenderer} ren
* @return {Boolean} true if the windows has a renderer
*/
hasRenderer(ren: vtkRenderer): boolean;

/**
*
* @param view
*/
hasView(view: any): boolean;

//hasView(view: vtkOpenGLRenderWindow): boolean;

/**
*
* @param callback
*/
onCompletion(callback: (instance: vtkObject) => any): vtkSubscription;

/**
*
* @param name
* @param initialValues
*/
newAPISpecificView(name: string, initialValues: object): any;

/**
* Remove renderer
* @param renderer
*/
removeRenderer(renderer: vtkRenderer): void;

/**
* Remove renderer
* @param view
*/
removeView(view: any): void;

/**
*
*/
render(): void;

/**
*
* @param defaultViewAPI
*/
setDefaultViewAPI(defaultViewAPI: DEFAULT_VIEW_API): boolean;

/**
*
* @param interactor
*/
setInteractor(interactor: vtkRenderWindowInteractor): boolean;

/**
*
* @param numberOfLayers
*/
setNumberOfLayers(numberOfLayers: number): boolean;

/**
*
* @param views
*/
setViews(views: any[]): boolean;

// setViews(views: vtkOpenGLRenderWindow[]): boolean;
}


/**
* Method use to decorate a given object (publicAPI+model) with vtkRenderWindow characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IRenderWindowInitialValues} [initialValues] (default: {})
*/
export function extend(publicAPI: object, model: object, initialValues?: IRenderWindowInitialValues): void;

/**
* Method use to create a new instance of vtkRenderWindow
*/
export function newInstance(initialValues?: IRenderWindowInitialValues): vtkRenderWindow;

/**
*
*/
export function registerViewConstructor(name: string, constructor: any): void;

/**
*
*/
export function listViewAPIs(): string[];

/**
*
*/
export function newAPISpecificView(name: string, initialValues: object): any;


/**
* vtkRenderWindow is an abstract object to specify the behavior of a rendering window.
*
* A rendering window is a window in a graphical user interface where renderers draw their images.
* Methods are provided to synchronize the rendering process, set window size, and control double buffering.
* The window also allows rendering in stereo. The interlaced render stereo type is for output to a VRex stereo projector.
* All of the odd horizontal lines are from the left eye, and the even lines are from the right eye.
* The user has to make the render window aligned with the VRex projector, or the eye will be swapped.
*/
export declare const vtkRenderWindow: {
newInstance: typeof newInstance,
extend: typeof extend,
registerViewConstructor: typeof registerViewConstructor,
listViewAPIs: typeof listViewAPIs,
newAPISpecificView: typeof newAPISpecificView,
};
export default vtkRenderWindow;
index.js
import macro from 'vtk.js/Sources/macro';

const DEFAULT_VIEW_API = navigator.gpu ? 'WebGPU' : '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) => {
if (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) => {
if (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;

publicAPI.render = () => {
if (model.interactor) {
model.interactor.render();
} else {
model.views.forEach((view) => view.traverseAllPasses());
}
};

publicAPI.getStatistics = () => {
const results = { propCount: 0, invisiblePropCount: 0 };
model.renderers.forEach((ren) => {
const props = ren.getViewProps();
props.forEach((prop) => {
if (prop.getVisibility()) {
results.propCount += 1;
const mpr = prop.getMapper && prop.getMapper();
if (mpr && mpr.getPrimitiveCount) {
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') => {
macro.setImmediate(publicAPI.render);
return model.views
.map((view) =>
view.captureNextImage ? view.captureNextImage(format) : undefined
)
.filter((i) => !!i);
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
defaultViewAPI: DEFAULT_VIEW_API,
renderers: [],
views: [],
interactor: null,
neverRendered: true,
numberOfLayers: 1,
};

// ----------------------------------------------------------------------------

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']);
macro.event(publicAPI, model, 'completion');

// Object methods
vtkRenderWindow(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend, 'vtkRenderWindow');

// ----------------------------------------------------------------------------

export default {
newInstance,
extend,
registerViewConstructor,
listViewAPIs,
newAPISpecificView,
};