From 9b1b1d1131c1aeaaefe54c491d1a2523a79164e4 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Wed, 5 Oct 2016 10:06:20 -0700 Subject: [PATCH] added center/boundingBox support to Shape --- src/component/shape-component.js | 31 +++++++++++++++++++++++++++++++ src/geometry/shape.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/component/shape-component.js b/src/component/shape-component.js index dee945b83..61e449da4 100644 --- a/src/component/shape-component.js +++ b/src/component/shape-component.js @@ -60,6 +60,37 @@ ShapeComponent.prototype = Object.assign( Object.create( }, + centerView: function( zoom ){ + + zoom = defaults( zoom, true ); + + var center = this.getCenter(); + + if( zoom ){ + + var bb = this.shape.boundingBox; + var bbSize = bb.size(); + var maxSize = Math.max( bbSize.x, bbSize.y, bbSize.z ); + var minSize = Math.min( bbSize.x, bbSize.y, bbSize.z ); + // var avgSize = ( bbSize.x + bbSize.y + bbSize.z ) / 3; + zoom = Math.max( 1, maxSize + ( minSize / 2 ) ); // object size + + // zoom = bb.size().length(); + + } + + this.viewer.centerView( zoom, center ); + + return this; + + }, + + getCenter: function(){ + + return this.shape.center; + + }, + dispose: function(){ this.shape.dispose(); diff --git a/src/geometry/shape.js b/src/geometry/shape.js index 7ec67546e..34da6c35b 100644 --- a/src/geometry/shape.js +++ b/src/geometry/shape.js @@ -5,6 +5,8 @@ */ +import { Vector3, Box3 } from "../../lib/three.es6.js"; + import { defaults } from "../utils.js"; import MeshBuffer from "../buffer/mesh-buffer.js"; import SphereBuffer from "../buffer/sphere-buffer.js"; @@ -47,6 +49,12 @@ function Shape( name, params ){ var disableImpostor = defaults( p.disableImpostor, false ); var openEnded = defaults( p.openEnded, false ); + var center = new Vector3(); + var boundingBox = new Box3(); + + var tmpVec = new Vector3(); + var tmpBox = new Box3(); + var bufferList = []; var spherePosition = []; @@ -133,6 +141,9 @@ function Shape( name, params ){ var meshBuffer = new MeshBuffer( position, color, index, normal ); bufferList.push( meshBuffer ); + tmpBox.setFromArray( position ); + boundingBox.union( tmpBox ); + } /** @@ -152,6 +163,8 @@ function Shape( name, params ){ addElement( color, sphereColor ); sphereRadius.push( radius ); + boundingBox.expandByPoint( tmpVec.fromArray( position ) ); + } /** @@ -175,6 +188,8 @@ function Shape( name, params ){ addElement( majorAxis, ellipsoidMajorAxis ); addElement( minorAxis, ellipsoidMinorAxis ); + boundingBox.expandByPoint( tmpVec.fromArray( position ) ); + } /** @@ -196,6 +211,9 @@ function Shape( name, params ){ addElement( color, cylinderColor ); cylinderRadius.push( radius ); + boundingBox.expandByPoint( tmpVec.fromArray( from ) ); + boundingBox.expandByPoint( tmpVec.fromArray( to ) ); + } /** @@ -217,6 +235,9 @@ function Shape( name, params ){ addElement( color, coneColor ); coneRadius.push( radius ); + boundingBox.expandByPoint( tmpVec.fromArray( from ) ); + boundingBox.expandByPoint( tmpVec.fromArray( to ) ); + } /** @@ -238,6 +259,9 @@ function Shape( name, params ){ addElement( color, arrowColor ); arrowRadius.push( radius ); + boundingBox.expandByPoint( tmpVec.fromArray( from ) ); + boundingBox.expandByPoint( tmpVec.fromArray( to ) ); + } function getBufferList(){ @@ -365,6 +389,13 @@ function Shape( name, params ){ // API + Object.defineProperties( this, { + center: { + get: function(){ return boundingBox.center( center ); } + }, + } ); + this.boundingBox = boundingBox; + this.addBuffer = addBuffer; this.addMesh = addMesh; this.addSphere = addSphere;