import { IClone } from "./IClone" ; import { BoundingSphere } from "./BoundingSphere" ; import { Matrix } from "./Matrix" ; import { Vector3 } from "./Vector3" ; /**
 * Axis Aligned Bound Box (AABB).
 */ export class BoundingBox implements IClone { private static _tempVec30 : Vector3 = new Vector3 ( ) ; private static _tempVec31 : Vector3 = new Vector3 ( ) ; param center param extent param out static fromCenterAndExtent ( center , extent , out ) { Vector3 . subtract ( center , extent , out . min ) ; Vector3 . add ( center , extent , out . max ) ; } 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 { min , max } = out ; min . x = min . y = min . z = Number . MAX_VALUE ; max . x = max . y = max . z = - Number . MAX_VALUE ; for ( let i = 0 , l = points . length ; i < l ; ++ i ) { const point = points [ i ] ; Vector3 . min ( min , point , min ) ; Vector3 . max ( max , point , max ) ; } } param sphere param out static fromSphere ( sphere , out ) { const { center , radius } = sphere ; const { min , max } = out ; min . x = center . x - radius ; min . y = center . y - radius ; min . z = center . z - radius ; max . x = center . x + radius ; max . y = center . y + radius ; max . z = center . z + radius ; } param source param matrix param out static transform ( source , matrix , out ) { const center = BoundingBox . _tempVec30 ; const extent = BoundingBox . _tempVec31 ; source . getCenter ( center ) ; source . getExtent ( extent ) ; Vector3 . transformCoordinate ( center , matrix , center ) ; const { x , y , z } = extent ; const e = matrix . elements ; extent . x = Math . abs ( x * e [ 0 ] ) + Math . abs ( y * e [ 4 ] ) + Math . abs ( z * e [ 8 ] ) ; extent . y = Math . abs ( x * e [ 1 ] ) + Math . abs ( y * e [ 5 ] ) + Math . abs ( z * e [ 9 ] ) ; extent . z = Math . abs ( x * e [ 2 ] ) + Math . abs ( y * e [ 6 ] ) + Math . abs ( z * e [ 10 ] ) ; Vector3 . subtract ( center , extent , out . min ) ; Vector3 . add ( center , extent , out . max ) ; } param box1 param box2 param out returns static merge ( box1 , box2 , out ) { Vector3 . min ( box1 . min , box2 . min , out . min ) ; Vector3 . max ( box1 . max , box2 . max , out . max ) ; return out ; } /** The minimum point of the box. */ public readonly min : Vector3 = new Vector3 ( ) ; /** The maximum point of the box. */ public readonly max : Vector3 = new Vector3 ( ) ; param min param max constructor ( min = null , max = null ) { min && min . cloneTo ( this . min ) ; max && max . cloneTo ( this . max ) ; } returns clone ( ) { return new BoundingBox ( this . min , this . max ) ; } param out returns cloneTo ( out ) { this . min . cloneTo ( out . min ) ; this . max . cloneTo ( out . max ) ; return out ; } param out returns getCenter ( out ) { Vector3 . add ( this . min , this . max , out ) ; Vector3 . scale ( out , 0.5 , out ) ; return out ; } param out returns getExtent ( out ) { Vector3 . subtract ( this . max , this . min , out ) ; Vector3 . scale ( out , 0.5 , out ) ; return out ; } param out returns getCorners ( out = [ ] ) { const { min , max } = this ; const minX = min . x ; const minY = min . y ; const minZ = min . z ; const maxX = max . x ; const maxY = max . y ; const maxZ = max . z ; const len = out . length ; if ( len < 8 ) { for ( let i = 0 , l = 8 - len ; i < l ; ++ i ) { out [ len + i ] = new Vector3 ( ) ; } } out [ 0 ] . setValue ( minX , maxY , maxZ ) ; out [ 1 ] . setValue ( maxX , maxY , maxZ ) ; out [ 2 ] . setValue ( maxX , minY , maxZ ) ; out [ 3 ] . setValue ( minX , minY , maxZ ) ; out [ 4 ] . setValue ( minX , maxY , minZ ) ; out [ 5 ] . setValue ( maxX , maxY , minZ ) ; out [ 6 ] . setValue ( maxX , minY , minZ ) ; out [ 7 ] . setValue ( minX , minY , minZ ) ; return out ; } param matrix returns public transform ( matrix ) { BoundingBox . transform ( this , matrix , this ) ; return this ; } }