v0.1.6
Quickvoxel Core is a pure Javascript toolkit for volumetric visualization of neuro files in the web browser. Everything that happens in Quickvoxel is strictly client-side, without the need of an active server (i.e. can run on a Github page)
Features:
Requirement:
Quickvoxel Core is backed by Pixpipe for decoding volume files and process the data, and by BabylonJS for the WebGL2 rendering.
Since this project is a core only, it is not bound to any frontend framework and needs to be sugar coated with some UI elements to provide a proper user interaction. You can find a minimal 10-lines example here (source).
A lot of additional methods to do more interesting things with Quickvoxel are implemented in the core and need to be tied to UI element to be fully usable. We'll see that in the following part.
(Most of the demos are less than 30 lines)
In addition QuickGui (source) is a more advanced project, developed for the #BrainHack2018 in Montreal. It uses some features of Quickvoxel Core with a simple and accessible UI.
Since Quickvoxel Core will most likely be used as a dependency, it can be used in multiple ways:
From a simple HTML page:
<!-- ES6 version -->
<script src="quickvoxelcore/dist/quickvoxelcore.es6.js"></script>
<!-- or ES5 version -->
<script src="quickvoxelcore/dist/quickvoxelcore.js"></script>
<!-- or ES5 minified version -->
<script src="quickvoxelcore/dist/quickvoxelcore.min.js"></script>
From another ES module:
npm install quickvoxelcore --save
Then, from your module:
// import the ES5 version
import quickvoxelcore from 'quickvoxelcore'
// or import the ES6 version
import quickvoxelcore from 'quickvoxelcore/dist/quickvoxelcore.es6.js'
To start, QuickvoxelCore needs an HTML5 canvas element:
<html>
<head>
<title>QuickvoxelCore Test</title>
<style>
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script src="../dist/quickvoxelcore.es6.js"></script>
<canvas id="renderCanvas"></canvas>
<script>
let canvas = document.getElementById("renderCanvas")
// ...
</script>
</body>
</html>
No matter the way you pick (simple HTML page or ES module to be bundled), the features are accessible from the quickvoxelcore
namespace:
let canvas = document.getElementById("renderCanvas")
let qvc = new quickvoxelcore.QuickvoxelCore( canvas )
The constructor quickvoxelcore.QuickvoxelCore(...)
initializes several internal objects, three important ones can then be fetched:
VolumeCollection
RenderEngine
CameraCrew
// ...
let qvc = new quickvoxelcore.QuickvoxelCore( canvas )
let volumeCollection = qvc.getVolumeCollection()
let renderEngine = qvc.getRenderEngine()
let camcrew = qvc.getCameraCrew()
Though, before launching your main app, if can be nice to check if QuickvoxelCore is running in a WebGL2 compatible environment. We have a function for that:
// test compatibility with WebGL2
if (!quickvoxelcore.webGL2()){
alert( 'Quickvoxel Core cannot run here because this web browser is not compatible with WebGL2.' )
} else {
// launch your app here
}
The VolumeCollection
instance allows you to add new volume from file URL or from a file dialog. Once added, a volume file will automatically:
The methods you will use from your VolumeCollection
instance are:
.addVolumeFromUrl( String )
to add a volume from a URL.addVolumeFromFile( File)
to add a volume from a file in the local filesystemIn addition, VolumeCollection
provides some events so that actions can be triggered during the lifecycle of a Volume
:
volumeAdded
is called when the volume is parsed and added to the collection. But its webGL texture is not ready yet! The callbacks attached to this event will have the volume object as argument.volumeReady
called after volumeAdded
, at the moment the added volume has its WegGL 3D texture ready. At this stage, a volume is ready to be displayed.The callbacks attached to this event will have the volume object as argument.volumeRemoved
is called when a volume is removed from the collection with the method .removeVolume(id)
. The callbacks attached to this event will have the volume id (string) as argument.errorAddingVolume
is called when a volume failed to be added with .addVolumeFromUrl()
and .addVolumeFromFile()
. The callbacks attached to this event will have the url or the HTML5 File object as argument.To each event can be attached multiple callbacks, they will simply be called successively in the order the were declared. To associate a callback function to an event, just do:
myVolumeCollection.on("volumeReady", function(volume){
// Do something with this volume
})
In general, events are most likely to be defined from the main scope or from where you also have access to the RenderEngine
instance.
VolumeCollection has plenty of methods, get the full description here. You may also want to check the documentation of the Volume class here.
The RenderEngine
instance is in charge of displaying the volume from the collection, once they are loaded. It also comes with all the features to rotates/translates the three orthogonal planes (referred as _planeSystem
in the source), apply a colormaps, change brightness/contrast and deal with blending.
A RenderEngine
can display only 2 volumes at the same time. The terminology used in the doc and source is
Two slots are available to mount volumes on the render engine. Those slots are called primary and secondary.
Then, some volume can be unmounted from a given slot and another volume from the volume collection can be mounted.
Rendering features such as colormap, contrast and brightness are associated to slots and not to volumes. This means, if you use the primary slot to mount a structural MRI and the secondary slot to mount a functional MRI, and then adjust the brightness/contrast/colormap of the secondary slot, mounting another fMRI instead of the one in place will not change those settings. Note: there are plans to add a additional volume for masking
RenderEngine has plenty of other methods, get the full description here
The CameraCrew
instance is automatically created at the instanciation of the QuickvoxelCore
object. The purpose of the cameracrew is to provide an interface for camera and point of view manipulation.
The default camera in QuickvoxelCore is the perspective camera, but three additional orthographic cameras are provided:
When the orthogonal planes are rotated, the orthographic cameras associated are also rotated to be always facing their respective plane. Each orthogonal camera can independently be zoomed in/out, translated and rotated.
To change the camera, the method .defineCamera()
from the CameraCrew
instance must be called. Though, multiple camera naming are possible:
'aOrtho'
, 'bOrtho'
and 'cOrtho'
. Those names where made generic because they don't imply a specific direction, even though before any rotation happen, aOrtho looks toward x, bOrtho looks towards y and cOrtho looks toward z.'a'
, 'b'
and 'c'
, same logic as above'x'
, 'y'
and 'z'
. This method is convenient because the names are dynamically associated with camera a, b and c depending on the dominant direction they are looking at and this association is reevaluated at every ortho plane rotation. For example, at first and before any plane rotation is performed, the x camera is the a camera. After some rotations, the a camera is probably no longer looking towards the x direction, then if we need the camera that looks toward x, we can no longer select the a camera for that. This is why this axis naming is important.'sagittal'
(always toward x), 'coronal'
(always toward y) and 'axial'
(always towards z). Note that this relation between the anatomical names and the axis names is established by the MNI space conventions.In addition to those names, two other are possible:
'main'
is the perspective camera'current'
is the current camera being usedOf course the current keyword is not useful in the context of .defineCamera()
but it is very handy when it comes to modifying the setting of a camera, i.e. no need to remember if we are changing the zoom setting of this or that camera, we are changing it on current.
Here is an example of how to change the camera:
// ...
let qvc = new quickvoxelcore.QuickvoxelCore( canvas )
let camcrew = qvc.getCameraCrew()
camcrew.defineCamera('coronal')
// ...
CameraCrew has plenty of other methods, get the full description here.
Here is how to load a volume from a URL (that has to comply with CORS, i.e. be in the same server as Quickvoxel)
// ...
volumeCollection.addVolumeFromUrl( "./data/structural.nii.gz" );
// mount the volume when it's ready!
volumeCollection.on("volumeReady", function(volume){
// to mount the loaded volume on a specific engine slot.
// (if a volume is already on this slot, it's unmounted and replaced by the new one)
renderEngine.mountVolumeN( 0, volume )
// OR, you can just mount it on the first slot available
let couldMount = renderEngine.mountVolumeOnFirstEmptySlot( volume )
if( !couldMount ){
console.log("All volume slots are taken on the render engine, make some space before rendering this volume.");
}
})
Alternatively, a volume can be loaded from you filesystem using a file dialog. Look at the example here. Then, the logic for mounting on a slot is the same.
The RenderEngine
object has a lot of methods that can be used to tweak your visualization. Do no hesitate to consult the API doc conserning the RenderEngine to make sure you use them properly.
Here is a list of what you can do:
up
vector needs to be set too)In what is probably the order of future developments:
Build an instance of QuickvoxelCore to initialize Quickvoxel Core.
Once constructed, the methods .getVolumeCollection()
and .getRenderEngine()
can be called to provide more features.
(any)
Get the CameraCrew instance in order to perform some camera manipulations
CameraCrew
:
Get the rendering engine to perform some 3D tasks, such as interacting with the view
RenderingEngine
:
Get the volume collection, to access to some features such as adding/removing a volume
VolumeCollection
:
Unmount the volume from the slot n in the rendering engine. Note: this method is jsut a call to the rendering engine, since the volume itself is not needed to be unmounted.
([type])
[
description
]
[type]
:
[
description
]
[unmountVolumeWIthId description]
([type])
[
description
]
[type]
:
[
description
]
The VolumeCollection is automatically initialized by the constructor of QuickVoxelCore. When the QuickVoxelCore object is created, the VolumeCollection can be fetched to perform actions directly on it.
A instance of VolumeCollection manages and identifies Volume instances.
A Volume
can be added to the collection using .addVolumeFromUrl()
and .addVolumeFromFile()
.
Once one of these two method is called, a Volume
instance is created (itself generating a 3D texture)
and added to the collection with a given index.
VolumeCollection provides some events, so that actions can be triggered during the lifecycle of a Volume
:
startAddingVolume
is called when a new volume is about to be added. This event is convenient mostly for UI purpose so that we can for example show a loading spinner, that then will be hidden when the event volumeAdded
or errorAddingVolume
are calledvolumeAdded
is called when the volume is parsed and added to the collection. But its webGL texture is not ready yet! The callbacks attached to this event will have the volume object as argument.volumeReady
called after volumeAdded
, at the moment the added volume has its WegGL 3D texture ready. At this stage, a volume is ready to be displayed.The callbacks attached to this event will have the volume object as argument.volumeRemoved
is called when a volume is removed from the collection with the method .removeVolume(id)
. The callbacks attached to this event will have the volume id (string) as argument.errorAddingVolume
is called when a volume failled to be added with .addVolumeFromUrl()
and .addVolumeFromFile()
. The callbacks attached to this event will have the url or the filename (if opened from file dialog) as argument.To each event can be attached multiple callbacks, they will simply be called successivelly in the order the were declared. To assiciate a callback function to an event, just do:
myVolumeCollection.on("volumeReady", function(volume){
// Do something with this volume
})
Extends EventManager
Add a volume to the collection from a file (most likely using a file dialog)
(File)
a compatible volumetric file
Get the list of all volume ids available in this collection
[type]
:
[
description
]
The RenderEngine is automatically initialized by the constructor of QuickVoxelCore. The engine in in charge of the visualization part by initializing the WebGL environment, sending data to shaders, and updating them. Once the QuickVoxelCore object is created, the RenderEngine can be fetched to call methods from it directly.
Extends EventManager
(DomElement)
a DOM object of a canvas
Get the Euler angle of the plane system
BABYLON.Vector3
:
The Euler angle
Get the position of the center of the plane
BABYLON.Vector3
:
position
Get the babylonjs scene object, because it's necessary to build textures in Volume
BABYLON.Scene
:
the scene
Get the the one of the 3 normal vectors of the _planeSystem that goes dominantly towards the X direction (Here "dominantly" is deducted by performing a dot product with [!, 0, 0])
BABYLON.Vector3
:
the normal vector (as a clone)
Get the the one of the 3 normal vectors of the _planeSystem that goes dominantly towards the Y direction
BABYLON.Vector3
:
the normal vector (as a clone)
Get the the one of the 3 normal vectors of the _planeSystem that goes dominantly towards the Z direction. (Here "dominantly" is deducted by performing a dot product with [0, 0, 1])
BABYLON.Vector3
:
the normal vector (as a clone)
Reset the rotation of the _planeSystem
Change the blending method. Note that this matters only when 2 textures are displayed. Available are:
quickvoxelcore.CONSTANTS.BLENDING_METHODS.ratio
quickvoxelcore.CONSTANTS.BLENDING_METHODS.added-weighted
quickvoxelcore.CONSTANTS.BLENDING_METHODS.multiply
(default)(any)
(String)
method of blending
Unmount the volume that is suposedly mounted on the slot N. this means the texture attached to the volume on slot N will no longer be visible.
([type])
[
description
]
A Volume instance is a volumetric representation of some data that can be queried, displayed and identified.
pixpipe.Image3DAlt
pixpipe.Image3DAlt
VolumeCollection
(any)
(any)
Build the texture corresponding to this volume. This requires a scene instance
([type])
[
description
]
[type]
:
[
description
]
Get the Pixpipe Image3DAlt object
pixpipe.Image3DAlt
:
The volume data as loaded by Pixpipe
get the babylonjs texture3d object
BABYLON.RawTexture3D
:
the texture corresponding to this volume
Get the voxel value at the given world position.
Note: the world coordinates are floating point and this method perform a lookup
in voxel coordinates in the pixpipe.Image3DAlt
data. Voxel coordinates being integer,
no interpolation from worl to voxel is performed by this method.
This just gives the value of the closest voxel.
(Object
= {x:0,y:0,z:0}
)
position in world coordinates
(Number
= 0
)
time index (makes sense only for time series)
Number
:
the voxel intensity
An instance of ColormapManager is used by RenderEngine so generate colormaps to apply on the volume.
Several types of colormaps are available, they are generated as pixpipe.Image2D
, from which are made
BABYLON.RawTexture
(2D) and HTML5 Canvas
. The texture is to be sent to the shader while the canvas
can be used for UI purpose. Note that the original pixpipe.Image2D
colormap is just used temporary
and is not kept in memory.
(BABYLON.Scene)
the babylonjs scene (necessary to generate textures)
(Number
= 512
)
number of samples generated per colormap
Get a canvas element representing the given colormap.
This canvas elem can directly be append
to some div.
(String
= 'default'
)
the name of the colormap (default: 'default')
Canvas
:
the Canvas object, of height 1px and width 512px (this depends on the default)
The CameraCrew provides four cameras (1 perspective and 3 orthographic) and a simplified API to control them. This include:
As you can read, the fine tunings are not available with the perspective camera, this is because a mouse control is attached to it. These functions are accesible by giving the name of the camera as argument. Camera have names they were initialized with: 'aOrtho', 'bOrtho' and 'cOrtho', in addition to the 'main', which is the perspective cam. Those camera can also be called by their shorter names 'a', 'b' and 'c' and by there direction pointing names. The direction pointing names are names given dinamically to each camera depending on the direction they are pointing. Of course, those names are updated at every single rotation to make sure the direction pointing names remain relevant. Those names are 'x', 'y' and 'z' and they refer to the MNI space (or Talairach coordinates).
When using the CameraCrew in 'sinel view' mode (default, the view from only one camera is shown) then, the name 'current' can also be used to address the camera currenlty being used.
Extends EventManager
(RenderEngine)
the renderEngine instance created by QuickVoxelCore
(Canvas)
the canvas DOM element used by QuickVoxelCore
Define the absolute angle of the camera, considering the original position represents the origin. This rotation will modify the upVector but keep the direction the camera is pointing
Get the camera pointing normalized vector. Note: this vector complied to MNI space and Talairach coordinates regarding the 'x' axis, which means +x is on the right and -x is on the left (which is the opposite of OpenGL/WebGL conventions)
(String)
one of the name of the camera
Object
:
normalized vector {x: Number, y: Number, z: Number}
Get the ortho cam that points the most towards X direction. Note: due to the sucessive rotation potentially performed on this camera, it is possible that the name of this camera is not 'aOrtho'
(Boolean
= false
)
force recomputing is true, get last value from the LUT if false (default: false)
String
:
the name of the camera pointing towards X direction
Get the ortho cam that points the most towards Y direction. Note: due to the sucessive rotation potentially performed on this camera, it is possible that the name of this camera is not 'bOrtho'
(Boolean
= false
)
force recomputing is true, get last value from the LUT if false (default: false)
String
:
the name of the camera pointing towards Y direction
Get the ortho cam that points the most towards Z direction. Note: due to the sucessive rotation potentially performed on this camera, it is possible that the name of this camera is not 'cOrtho'
(Boolean
= false
)
force recomputing is true, get last value from the LUT if false (default: false)
String
:
the name of the camera pointing towards Z direction
Modify the absolute position of the camera on its axis. The default position is (0, 0) when the camera is centered on the ortho planes origin.
Rotates the given camera relatively to its current state. The camera will keep its direction, only the upVector will be changed (giving the impresion of image spinning)
Update the span of a given orthographic camera. Bear in mind two things:
Moves the given camera relatively to its curent position.
If the right
and up
come from screen coordinates, it is most likely that
the alternative method .translateOrthoCamScreenAlign()
is the one to use.
Moves the given camera relatively to its curent position when using screen coordinates.
Contrary to .translateOrthoCam()
, this method takes in account the rotation of the camera (if any).
This method is most likely the one to use when the right
and up
(aka. dx and dy)
come from screen coordinates such as mouse/pointer.
Multiply the enlargement of the ortho cam by a factor. A factor lower than 1 will make the image smaller, a factor higher than 1 will make the image bigger. Note: Under the hood, the camera span is multiplied by (1/factor).
The EventManager deals with events, create them, call them. This class is mostly for being inherited from.
Gives the property name in the object that has the given value
(Object)
an object that may contain a property associated with a specific value
(Object)
a value to look for
String
:
Name of the first property that has the given value. Or null if not found
Know if the current environment is webGL2 compatible. Usage:
if (!quickvoxelcore.webGL2()){
alert( 'Quickvoxel Core cannot run here because this web browser is not compatible with WebGL2.' )
} else {
// call the main app
}
Boolean
:
true if compatible with WebGL2, false if not