Skip to content

Commit f9fc138

Browse files
committed
Refactor the code
1 parent 2792f3f commit f9fc138

File tree

3 files changed

+89
-68
lines changed

3 files changed

+89
-68
lines changed

examples/surface-viewer-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ <h4>Views:</h4>
193193
</p>
194194
<p>
195195
<div>
196-
<input type="checkbox" id="model_centric" checked="true"><label for="model_centric">Origin correspond to model center</label>
196+
<input type="checkbox" id="model_centric"><label for="model_centric">Origin correspond to model center</label>
197197
</div>
198198
</p>
199199
<p>

examples/surface-viewer-demo.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ $(function() {
496496

497497
// Origin position
498498
$("#model_centric").change(function() {
499-
viewer.model_centric($(this).is(":checked"));
499+
viewer.modelCentric($(this).is(":checked"));
500500
});
501501

502502
// Color map URLs are read from the config file and added to the
@@ -516,6 +516,7 @@ $(function() {
516516
viewer.clearScreen();
517517
current_request = 0;
518518
current_request_name = "";
519+
document.getElementById("model_centric").checked = false;
519520
loading_div.hide();
520521
});
521522

@@ -524,8 +525,8 @@ $(function() {
524525
if (viewer.model.children.length === 0) return;
525526

526527
var annotation_display = $("#annotation-display");
527-
var media = $("#annotation-media");
528-
var pick_info = viewer.pick();
528+
var media = $("#annotation-media");
529+
var pick_info = viewer.pick();
529530
var model_data, intensity_data;
530531
var annotation_info;
531532
var value, label, text;
@@ -920,10 +921,9 @@ $(function() {
920921
viewer.loadModelFromFile(document.getElementById("objfile"), {
921922
format: format,
922923
complete: function() {
923-
if ($("#model_centric").is(":checked")) {
924-
viewer.model_centric(true);
925-
}
926-
hideLoading();
924+
document.getElementById("model_centric").checked = true;
925+
viewer.modelCentric(true);
926+
hideLoading();
927927
}
928928
});
929929

src/brainbrowser/surface-viewer/modules/rendering.js

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -630,38 +630,87 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
630630
return vertex_data;
631631
};
632632

633-
viewer.model_centric = function(model_centric) {
633+
634+
/**
635+
* @doc function
636+
* @name viewer.rendering:model_centric
637+
* @param {boolean} if true, recenter all userData shape on origin. Otherwise
638+
* return to the original userData.
639+
*
640+
* @description
641+
* Use to recenter data when userData input is shifted in space.
642+
*
643+
*
644+
* ```js
645+
* viewer.modelCentric(true);
646+
* ```
647+
*/
648+
viewer.modelCentric = function(model_centric=false) {
634649
var model = viewer.model;
635650

636-
// Calculate bounding box only if needed
637-
if (model.userData.model_center_offset === undefined) {
638-
// Calculate bounding box for all children given by the user
639-
// ignore other children
640-
var min_x, max_x, min_y, max_y, min_z, max_z;
641-
min_x = min_y = min_z = Number.POSITIVE_INFINITY;
642-
max_x = max_y = max_z = Number.NEGATIVE_INFINITY;
643-
644-
model.children.forEach(function(children){
645-
var model_name = children.userData.model_name;
646-
var model_data = viewer.model_data.get(model_name);
647-
648-
var current_shape = undefined;
649-
var children_name = children.name;
650-
model_data.shapes.forEach(function(shape){
651-
if (shape.name !== children_name) { return };
652-
if (children.material.opacity === 0) { return };
653-
current_shape = shape;
654-
var bounding_box = shape.bounding_box;
655-
656-
// min
657-
min_x = Math.min(min_x, bounding_box.min_x);
658-
min_y = Math.min(min_y, bounding_box.min_y);
659-
min_z = Math.min(min_z, bounding_box.min_z);
660-
// max
661-
max_x = Math.max(max_x, bounding_box.max_x);
662-
max_y = Math.max(max_y, bounding_box.max_y);
663-
max_z = Math.max(max_z, bounding_box.max_z);
664-
});
651+
viewer.findUserDataCentroid(model);
652+
653+
if (model_centric === model.userData.model_centric) {return};
654+
655+
// Caculate the offset
656+
var offset_centroid = new THREE.Vector3();
657+
offset_centroid.copy(model.userData.model_center_offset);
658+
if (model_centric === false) { offset_centroid.negate()};
659+
660+
model.children.forEach(function(children) {
661+
// Return if children is not given by the user
662+
if (Object.keys(children.userData).length === 0 && children.userData.constructor === Object) {return};
663+
children.translateX(offset_centroid.x);
664+
children.translateY(offset_centroid.y);
665+
children.translateZ(offset_centroid.z);
666+
model.userData.model_centric = model_centric;
667+
});
668+
viewer.updated = true;
669+
};
670+
671+
/**
672+
* @doc function
673+
* @name viewer.rendering:findUserDataCentroid
674+
* @param {object} a model.
675+
*
676+
* @description
677+
* Find centroid of the model (only take in account userData).
678+
*
679+
* @returns {object} The initial information with additionnal model_center_offset argument.
680+
*
681+
* ```js
682+
* viewer.findUserDataCentroid(true);
683+
* ```
684+
*/
685+
viewer.findUserDataCentroid = function(model) {
686+
// Calculate only if needed
687+
if (model.userData.model_center_offset !== undefined) {return};
688+
689+
// Calculate bounding box for all children given by the user
690+
// ignore other children
691+
var min_x, max_x, min_y, max_y, min_z, max_z;
692+
min_x = min_y = min_z = Number.POSITIVE_INFINITY;
693+
max_x = max_y = max_z = Number.NEGATIVE_INFINITY;
694+
695+
model.children.forEach(function(children){
696+
var model_name = children.userData.model_name;
697+
var model_data = viewer.model_data.get(model_name);
698+
699+
var current_shape = undefined;
700+
var children_name = children.name;
701+
model_data.shapes.forEach(function(shape){
702+
if (shape.name !== children_name) { return };
703+
current_shape = shape;
704+
var bounding_box = shape.bounding_box;
705+
706+
// min
707+
min_x = Math.min(min_x, bounding_box.min_x);
708+
min_y = Math.min(min_y, bounding_box.min_y);
709+
min_z = Math.min(min_z, bounding_box.min_z);
710+
// max
711+
max_x = Math.max(max_x, bounding_box.max_x);
712+
max_y = Math.max(max_y, bounding_box.max_y);
713+
max_z = Math.max(max_z, bounding_box.max_z);
665714
});
666715

667716
// centroid of all the model
@@ -670,36 +719,8 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
670719
centroid.y = min_y + (max_y - min_y) / 2;
671720
centroid.z = min_z + (max_z - min_z) / 2;
672721

673-
model.userData.model_centric = true;
674-
model.userData.model_center_offset = new THREE.Vector3(-centroid.x, -centroid.y, -centroid.z)
675-
}
676-
677-
678-
if (model_centric === true) {
679-
// Calculate bounding only if needed
680-
// Translate each children
681-
centroid = model.userData.model_center_offset;
682-
model.children.forEach(function(children) {
683-
// Return if children is not given by the user
684-
if (Object.keys(children.userData).length === 0 && children.userData.constructor === Object) {return};
685-
console.log(children.name)
686-
children.translateX(centroid.x);
687-
children.translateY(centroid.y);
688-
children.translateZ(centroid.z);
689-
});
690-
} else {
691-
// Revert the translation for each children
692-
if (model.userData.model_centric !== true) {return};
693-
var centroid = model.userData.model_center_offset;
694-
model.children.forEach(function(children) {
695-
// Return if children is not given by the user
696-
if (Object.keys(children.userData).length === 0 && children.userData.constructor === Object) {return};
697-
children.translateX(-centroid.x);
698-
children.translateY(-centroid.y);
699-
children.translateZ(-centroid.z);
700-
});
701-
}
702-
viewer.updated = true;
722+
model.userData.model_center_offset = new THREE.Vector3(-centroid.x, -centroid.y, -centroid.z)
723+
});
703724
};
704725

705726
////////////////////////////////////

0 commit comments

Comments
 (0)