Skip to content

Commit

Permalink
added center/boundingBox support to Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
arose committed Oct 5, 2016
1 parent dfe3c64 commit 9b1b1d1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/component/shape-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions src/geometry/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 );

}

/**
Expand All @@ -152,6 +163,8 @@ function Shape( name, params ){
addElement( color, sphereColor );
sphereRadius.push( radius );

boundingBox.expandByPoint( tmpVec.fromArray( position ) );

}

/**
Expand All @@ -175,6 +188,8 @@ function Shape( name, params ){
addElement( majorAxis, ellipsoidMajorAxis );
addElement( minorAxis, ellipsoidMinorAxis );

boundingBox.expandByPoint( tmpVec.fromArray( position ) );

}

/**
Expand All @@ -196,6 +211,9 @@ function Shape( name, params ){
addElement( color, cylinderColor );
cylinderRadius.push( radius );

boundingBox.expandByPoint( tmpVec.fromArray( from ) );
boundingBox.expandByPoint( tmpVec.fromArray( to ) );

}

/**
Expand All @@ -217,6 +235,9 @@ function Shape( name, params ){
addElement( color, coneColor );
coneRadius.push( radius );

boundingBox.expandByPoint( tmpVec.fromArray( from ) );
boundingBox.expandByPoint( tmpVec.fromArray( to ) );

}

/**
Expand All @@ -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(){
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9b1b1d1

Please sign in to comment.