Skip to content

Commit 2792f3f

Browse files
committed
Fix original center when model is loaded.
1 parent 29140df commit 2792f3f

File tree

4 files changed

+99
-26
lines changed

4 files changed

+99
-26
lines changed

examples/surface-viewer-demo.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ <h4>Views:</h4>
191191
<p>
192192
<input id="axes-controls" type="checkbox">Display axes<br />
193193
</p>
194+
<p>
195+
<div>
196+
<input type="checkbox" id="model_centric" checked="true"><label for="model_centric">Origin correspond to model center</label>
197+
</div>
198+
</p>
194199
<p>
195200
<a class="button" id="resetview">Reset View</a>
196201
<input type="checkbox" class="button" id="meshmode"><label for="meshmode">Mesh Mode</label>

examples/surface-viewer-demo.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ $(function() {
494494

495495
});
496496

497+
// Origin position
498+
$("#model_centric").change(function() {
499+
viewer.model_centric($(this).is(":checked"));
500+
});
501+
497502
// Color map URLs are read from the config file and added to the
498503
// color map select box.
499504
var color_map_select = $('<select id="color-map-select"></select>').change(function() {
@@ -532,8 +537,8 @@ $(function() {
532537
$("#pick-index").html(pick_info.index);
533538
$("#annotation-wrapper").show();
534539

535-
picked_object = pick_info.object;
536-
model_data = viewer.model_data.get(picked_object.userData.model_name);
540+
picked_object = pick_info.object;
541+
model_data = viewer.model_data.get(picked_object.userData.model_name);
537542
intensity_data = model_data.intensity_data[0];
538543

539544
if (intensity_data) {
@@ -634,9 +639,9 @@ $(function() {
634639
});
635640

636641
$("#annotation-save").click(function() {
637-
var vertex_num = parseInt($("#pick-index").html(), 10);
642+
var vertex_num = parseInt($("#pick-index").html(), 10);
638643
var annotation_display = $("#annotation-display");
639-
var media = $("#annotation-media");
644+
var media = $("#annotation-media");
640645

641646
var annotation, annotation_data;
642647
var vertex;
@@ -658,15 +663,15 @@ $(function() {
658663
vertex = viewer.getVertex(vertex_num);
659664

660665
annotation_data.image = $("#annotation-image").val();
661-
annotation_data.url = $("#annotation-url").val();
662-
annotation_data.text = $("#annotation-text").val();
666+
annotation_data.url = $("#annotation-url").val();
667+
annotation_data.text = $("#annotation-text").val();
663668

664669
media.html("");
665670

666671
if (annotation_data.image) {
667-
var image = new Image();
672+
var image = new Image();
668673
image.width = 200;
669-
image.src = annotation_data.image;
674+
image.src = annotation_data.image;
670675
annotation_display.show();
671676
media.append(image);
672677
}
@@ -914,7 +919,12 @@ $(function() {
914919
showLoading();
915920
viewer.loadModelFromFile(document.getElementById("objfile"), {
916921
format: format,
917-
complete: hideLoading
922+
complete: function() {
923+
if ($("#model_centric").is(":checked")) {
924+
viewer.model_centric(true);
925+
}
926+
hideLoading();
927+
}
918928
});
919929

920930
return false;

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -691,20 +691,6 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
691691
}
692692
}
693693

694-
if (options.model_centric === true) {
695-
var bounding_box = new THREE.BoundingBoxHelper(model);
696-
bounding_box.update();
697-
698-
model.userData.model_centric = true;
699-
model.userData.offset = new THREE.Vector3(-bounding_box.position.x, -bounding_box.position.y, -bounding_box.position.z)
700-
701-
model.children.forEach(function(children) {
702-
children.translateX(-bounding_box.position.x);
703-
children.translateY(-bounding_box.position.y);
704-
children.translateZ(-bounding_box.position.z);
705-
});
706-
}
707-
708694
return new_shapes;
709695
}
710696

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

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
199199
camera.position.set(0, 0, default_camera_distance);
200200
light.position.set(0, 0, default_camera_distance);
201201

202-
var offset = model.userData.offset || new THREE.Vector3(0,0,0);
202+
var offset = model.userData.model_center_offset || new THREE.Vector3(0,0,0);
203203
model.children.forEach(function(shape) {
204204
var centroid = shape.userData.centroid;
205205
var recentered = shape.userData.recentered;
@@ -274,7 +274,7 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
274274
sphere.position.set(x, y, z);
275275

276276
if (viewer.model) {
277-
var offset = viewer.model.userData.offset;
277+
var offset = viewer.model.userData.model_center_offset;
278278
if (offset !== undefined) {
279279
sphere.translateX(offset.x);
280280
sphere.translateY(offset.y);
@@ -630,6 +630,78 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
630630
return vertex_data;
631631
};
632632

633+
viewer.model_centric = function(model_centric) {
634+
var model = viewer.model;
635+
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+
});
665+
});
666+
667+
// centroid of all the model
668+
var centroid = new THREE.Vector3()
669+
centroid.x = min_x + (max_x - min_x) / 2;
670+
centroid.y = min_y + (max_y - min_y) / 2;
671+
centroid.z = min_z + (max_z - min_z) / 2;
672+
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;
703+
};
704+
633705
////////////////////////////////////
634706
// PRIVATE FUNCTIONS
635707
////////////////////////////////////
@@ -640,7 +712,7 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
640712
var delta;
641713
var rotation;
642714
var position = camera.position;
643-
var new_z = default_camera_distance / viewer.zoom;
715+
var new_z = default_camera_distance / viewer.zoom;
644716

645717
window.requestAnimationFrame(renderFrame);
646718

0 commit comments

Comments
 (0)