import { IClone } from "./IClone" ; import { BoundingBox } from "./BoundingBox" ; import { Vector3 } from "./Vector3" ; /**
 * A bounding sphere.
 * */ export class BoundingSphere implements IClone { private static _tempVec30 : Vector3 = new Vector3 ( ) ; param points param out static fromPoints ( points , out ) { if ( ! points || points . length === 0 ) { throw new Error ( "points must be array and length must > 0" ) ; } const len = points . length ; const center = BoundingSphere . _tempVec30 ; center . x = center . y = center . z = 0 ; for ( let i = 0 ; i < len ; ++ i ) { Vector3 . add ( points [ i ] , center , center ) ; } Vector3 . scale ( center , 1 / len , out . center ) ; let radius = 0.0 ; for ( let i = 0 ; i < len ; ++ i ) { const distance = Vector3 . distanceSquared ( center , points [ i ] ) ; distance > radius && ( radius = distance ) ; } out . radius = Math . sqrt ( radius ) ; } param box param out static fromBox ( box , out ) { const { center } = out ; const { min , max } = box ; center . x = ( min . x + max . x ) * 0.5 ; center . y = ( min . y + max . y ) * 0.5 ; center . z = ( min . z + max . z ) * 0.5 ; out . radius = Vector3 . distance ( center , max ) ; } /** The center point of the sphere. */ public readonly center : Vector3 = new Vector3 ( ) ; /** The radius of the sphere. */ public radius : number = 0 ; param center param radius constructor ( center = null , radius = 0 ) { center && center . cloneTo ( this . center ) ; this . radius = radius ; } returns clone ( ) { return new BoundingSphere ( this . center , this . radius ) ; } param out returns cloneTo ( out ) { this . center . cloneTo ( out . center ) ; out . radius = this . radius ; return out ; } }