@@ -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