Skip to content

Commit

Permalink
implement a basic destroy for the volume and slice.
Browse files Browse the repository at this point in the history
still some leaks to be investigated.
  • Loading branch information
NicolasRannou committed Jun 4, 2014
1 parent 2a77f9e commit 0e273b6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 36 deletions.
28 changes: 28 additions & 0 deletions objects/object.js
Expand Up @@ -254,6 +254,33 @@ X.object.prototype.remove = function(propagateEvent) {

};

/**
* Destroy an object and its children to free the allocated memory.
*/
X.object.prototype.destroy = function() {

// extra security
goog.events.removeAll(this);

// check if this object has children
if (this._children.length > 0) {

// loop through the children and recursively remove the objects
var children = this._children;
var numberOfChildren = children.length;
var c = 0;

for (c = 0; c < numberOfChildren; c++) {
if (typeof(children[c]) != "undefined"){
this._children[c].destroy();
}
}
}

this._children.length = 0;
this._colortable = null;
this._scalars = null;
};

/**
* Compare two X.objects by their opacity values and their distance to the
Expand Down Expand Up @@ -319,3 +346,4 @@ X.object.OPACITY_COMPARATOR = function(object1, object2) {
goog.exportSymbol('X.object', X.object);
goog.exportSymbol('X.object.prototype.modified', X.object.prototype.modified);
goog.exportSymbol('X.object.prototype.remove', X.object.prototype.remove);
goog.exportSymbol('X.object.prototype.destroy', X.object.prototype.destroy);
48 changes: 33 additions & 15 deletions objects/slice.js
Expand Up @@ -81,7 +81,7 @@ X.slice = function(slice) {
* @protected
*/
this._up = [0, 1, 0];

this._right= [1, 0, 0];

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ X.slice.prototype.copy_ = function(slice) {

/**
* Set the height of this slice.
*
*
* @param {number} height The height.
*/
X.slice.prototype.__defineSetter__('height', function(height) {
Expand All @@ -192,7 +192,7 @@ X.slice.prototype.__defineSetter__('height', function(height) {

/**
* Set the width of this slice.
*
*
* @param {number} width The width.
*/
X.slice.prototype.__defineSetter__('width', function(width) {
Expand All @@ -203,7 +203,7 @@ X.slice.prototype.__defineSetter__('width', function(width) {

/**
* Set the width of this slice.
*
*
* @param {number} width The width.
*/
X.slice.prototype.__defineGetter__('up', function() {
Expand All @@ -214,7 +214,7 @@ X.slice.prototype.__defineGetter__('up', function() {

/**
* Set the width of this slice.
*
*
* @param {number} width The width.
*/
X.slice.prototype.__defineGetter__('right', function() {
Expand Down Expand Up @@ -294,7 +294,7 @@ X.slice.prototype.setup = function(center, front, up, right, width, height, bord
this._front = front;

this._up = up;

this._right = right;

this._width = width;
Expand All @@ -314,11 +314,28 @@ X.slice.prototype.setup = function(center, front, up, right, width, height, bord
* Create a default X.slice.
*/
X.slice.prototype.create = function() {

this.create_();

};

/**
* @inheritDoc
*/
X.slice.prototype.destroy = function(){

// call destroy of parent
goog.base(this, 'destroy');

this._center.length = 0;
this._front.length = 0;
this._up.length = 0;
this._right.length = 0;
this._textureCoordinateMap.length = 0;
this._volume = null;
this._labelmap = null;
this._borderColor.length = 0;
}

/**
* Create the slice.
Expand All @@ -332,29 +349,29 @@ X.slice.prototype.create_ = function() {
this._front[2]).normalize();
var upVector = new goog.math.Vec3(this._up[0], this._up[1], this._up[2]);
var rightVector = new goog.math.Vec3(this._right[0], this._right[1], this._right[2]);

var centerVector = new goog.math.Vec3(this._center[0], this._center[1],
this._center[2]);

// -right -up
var dirVector = goog.math.Vec3.sum(rightVector.clone().invert().scale(this._width/2), upVector
.clone().invert().scale(this._height/2));
// move center in this direction
var point0 = new goog.math.Vec3(dirVector.x + centerVector.x, dirVector.y + centerVector.y,
dirVector.z + centerVector.z);

// -right +up
dirVector = goog.math.Vec3.sum(rightVector.clone().invert().scale(this._width/2), upVector.clone().scale(this._height/2));
var point1 = new goog.math.Vec3(dirVector.x + centerVector.x, dirVector.y + centerVector.y,
dirVector.z + centerVector.z);

// +right -up
dirVector = goog.math.Vec3.sum(rightVector.clone().scale(this._width/2), upVector.clone().invert().scale(this._height/2));
var point2 = new goog.math.Vec3(dirVector.x + centerVector.x, dirVector.y + centerVector.y,
dirVector.z + centerVector.z);

var point3 = point2;

// +right +up
dirVector = goog.math.Vec3.sum(rightVector.clone().scale(this._width/2), upVector.clone().scale(this._height/2));
var point4 = new goog.math.Vec3(dirVector.x + centerVector.x, dirVector.y + centerVector.y,
Expand All @@ -375,7 +392,7 @@ X.slice.prototype.create_ = function() {
this._points.add(point3.x, point3.y, point3.z); // 3
this._points.add(point4.x, point4.y, point4.z); // 4
this._points.add(point5.x, point5.y, point5.z); // 5

// add the normals based on the orientation (we don't really need them since
// we assume each Slice has a texture)
this._normals.add(frontVector.x, frontVector.y, frontVector.z);
Expand Down Expand Up @@ -424,3 +441,4 @@ X.slice.prototype.create_ = function() {
// constructors with duck typing)
goog.exportSymbol('X.slice', X.slice);
goog.exportSymbol('X.slice.prototype.create', X.slice.prototype.create);
goog.exportSymbol('X.slice.prototype.destroy', X.slice.prototype.destroy);
73 changes: 52 additions & 21 deletions objects/volume.js
Expand Up @@ -358,6 +358,36 @@ X.volume.prototype.create_ = function(_info) {
this._dirty = true;
};

/**
* Re-show the slices or re-activate the volume rendering for this volume.
*
* @inheritDoc
*/
X.volume.prototype.destroy = function() {

// call destroy of parent
// call the update_ method of the superclass
goog.base(this, 'destroy');

this._image.length = 0;
this._children.length = 0;
this._slicesX._children.length = 0;
this._slicesX = 0;
this._slicesY._children.length = 0;
this._slicesY.length = 0;
this._slicesZ._children.length = 0;
this._slicesZ.length = 0;
this._BBox.length = 0;
this._childrenInfo.length = 0;
this._RASCenter.length = 0;
this._RASDimensions.length = 0;
this._RASSpacing.length = 0;
this._IJKVolume.length = 0;
this._IJKVolumeN.length = 0;
this._filedata.length = 0;

};

/**
* Re-show the slices or re-activate the volume rendering for this volume.
*
Expand Down Expand Up @@ -500,7 +530,7 @@ X.volume.prototype.slicing_ = function() {
if(xyz != this._volumeRenderingDirection){

_currentSlice['visible'] = false;
_currentSlice._opacity = 0.0;
_currentSlice._opacity = 0.0;

}

Expand Down Expand Up @@ -1329,7 +1359,7 @@ X.volume.prototype.sliceInfoChanged = function(index){
// for each child
for(var i=0; i<this._children[index]._children.length; i++){
if(typeof this._children[index]._children[i] != 'undefined'){

if(this.hasLabelMap) {
// add it to create the texture
this._labelmap._children[index]._children[i].remove();
Expand All @@ -1338,7 +1368,7 @@ X.volume.prototype.sliceInfoChanged = function(index){

this._children[index]._children[i].remove();
this._children[index]._children[i] = null;

}

}
Expand Down Expand Up @@ -1370,7 +1400,7 @@ X.volume.prototype.sliceInfoChanged = function(index){

_slice._labelmap = _slice._texture;
_slice._labelmap = this._labelmap._children[index]._children[Math.floor(this._childrenInfo[index]._nb/2)]._texture;

}

this._children[index]._children[Math.floor(this._childrenInfo[index]._nb/2)] = _slice;
Expand All @@ -1379,10 +1409,10 @@ X.volume.prototype.sliceInfoChanged = function(index){

this._indexX = Math.floor(this._childrenInfo[index]._nb/2);
this._indexXold = Math.floor(this._childrenInfo[index]._nb/2);

}
else if(index == 1) {

this._indexY = Math.floor(this._childrenInfo[index]._nb/2);
this._indexYold = Math.floor(this._childrenInfo[index]._nb/2);

Expand All @@ -1391,7 +1421,7 @@ X.volume.prototype.sliceInfoChanged = function(index){

this._indexZ = Math.floor(this._childrenInfo[index]._nb/2);
this._indexZold = Math.floor(this._childrenInfo[index]._nb/2);

}

// add it to renderer!
Expand Down Expand Up @@ -1463,9 +1493,9 @@ X.volume.prototype.volumeRendering_ = function(direction) {
// store the direction
this._volumeRenderingDirection = direction;

this._dirty = false;
this._dirty = false;

// and that's it
// and that's it

return;

Expand All @@ -1475,12 +1505,12 @@ X.volume.prototype.volumeRendering_ = function(direction) {
// we are using timeouts here, just for interaction with the user interface
//


setTimeout(function() {

// hide old volume rendering slices
var _child = null;

if( this._volumeRenderingDirection >= 0 ){

_child = this._children[this._volumeRenderingDirection];
Expand Down Expand Up @@ -1539,7 +1569,7 @@ X.volume.prototype.volumeRendering_ = function(direction) {
_child._children[i]._visible = true;

}

}

this.onComputingProgress_(0.25);
Expand Down Expand Up @@ -1583,7 +1613,7 @@ X.volume.prototype.volumeRendering_ = function(direction) {

_child._children[i]._visible = true;

}
}
}

this.onComputingProgress_(0.50);
Expand Down Expand Up @@ -1627,8 +1657,8 @@ X.volume.prototype.volumeRendering_ = function(direction) {

_child._children[i]._visible = true;

}
}

}

this.onComputingProgress_(0.75);
Expand Down Expand Up @@ -1672,7 +1702,7 @@ X.volume.prototype.volumeRendering_ = function(direction) {

_child._children[i]._visible = true;

}
}

}

Expand All @@ -1683,14 +1713,14 @@ X.volume.prototype.volumeRendering_ = function(direction) {
if (this._computing) {

// add it to renderer!
this._children[direction].modified(true);
this._children[direction].modified(true);

}

// store the direction
this._volumeRenderingDirection = direction;

this._dirty = false;
this._dirty = false;

if (this._computing) {
//call computing end callback
Expand Down Expand Up @@ -1772,7 +1802,7 @@ X.volume.prototype.onComputingEnd_ = function(direction) {
/**
* This callback gets fired when computation has to happen in order
* to display volume rendering.
*
*
* @param {!number} direction The direction to compute.
* @public
*
Expand All @@ -1786,7 +1816,7 @@ X.volume.prototype.onComputing = function(direction) {

/**
* This callback gets fired when computation is happening.
*
*
* @param {!number} progress The current computation progress in percent.
* @public
*
Expand All @@ -1800,7 +1830,7 @@ X.volume.prototype.onComputingProgress = function(progress) {

/**
* This callback gets fired when computation is complete.
*
*
* @param {!number} direction The direction which was computed.
* @public
*
Expand All @@ -1815,6 +1845,7 @@ X.volume.prototype.onComputingEnd = function(direction) {
// export symbols (required for advanced compilation)
goog.exportSymbol('X.volume', X.volume);
goog.exportSymbol('X.volume.prototype.modified', X.volume.prototype.modified);
goog.exportSymbol('X.volume.prototype.destroy', X.volume.prototype.destroy);
goog.exportSymbol('X.volume.prototype.sliceInfoChanged', X.volume.prototype.sliceInfoChanged);
goog.exportSymbol('X.volume.prototype.onComputing', X.volume.prototype.onComputing);
goog.exportSymbol('X.volume.prototype.onComputingProgress', X.volume.prototype.onComputingProgress);
Expand Down

0 comments on commit 0e273b6

Please sign in to comment.