1+ import { GCodeVector3 } from './GCodeVector3' ;
12export class BoundingBox {
2- private minX : number = Infinity ;
3- private maxX : number = - Infinity ;
4- private minY : number = Infinity ;
5- private maxY : number = - Infinity ;
6- private minZ : number = Infinity ;
7- private maxZ : number = - Infinity ;
8-
9- constructor ( ) {
10- // console.log('BoundingBox initialized');
11- }
3+ private min : GCodeVector3 = new GCodeVector3 ( Infinity , Infinity , Infinity ) ;
4+ private max : GCodeVector3 = new GCodeVector3 ( - Infinity , - Infinity , - Infinity ) ;
125
136 /**
147 * Updates the bounding box with the given coordinates.
@@ -17,12 +10,12 @@ export class BoundingBox {
1710 * @param z - The Z coordinate.
1811 */
1912 public update ( x : number , y : number , z : number ) : void {
20- this . minX = Math . min ( this . minX , x ) ;
21- this . maxX = Math . max ( this . maxX , x ) ;
22- this . minY = Math . min ( this . minY , y ) ;
23- this . maxY = Math . max ( this . maxY , y ) ;
24- this . minZ = Math . min ( this . minZ , z ) ;
25- this . maxZ = Math . max ( this . maxZ , z ) ;
13+ this . min . x = Math . min ( this . min . x , x ) ;
14+ this . max . x = Math . max ( this . max . x , x ) ;
15+ this . min . y = Math . min ( this . min . y , y ) ;
16+ this . max . y = Math . max ( this . max . y , y ) ;
17+ this . min . z = Math . min ( this . min . z , z ) ;
18+ this . max . z = Math . max ( this . max . z , z ) ;
2619 // console.log(`Updated bounding box: minX=${this.minX}, maxX=${this.maxX}, minY=${this.minY}, maxY=${this.maxY}, minZ=${this.minZ}, maxZ=${this.maxZ}`);
2720 }
2821
@@ -31,46 +24,38 @@ export class BoundingBox {
3124 * @returns True if at least one point has been added, false otherwise.
3225 */
3326 public get isValid ( ) : boolean {
34- return this . minX !== Infinity ;
27+ return this . min . x !== Infinity ;
3528 }
3629
3730 /**
3831 * Gets the size of the bounding box.
3932 * @returns An object with x, y, and z dimensions, or null if the bounding box is not valid.
4033 */
41- public get size ( ) : { x : number ; y : number ; z : number } | null {
34+ public get size ( ) : GCodeVector3 | null {
4235 if ( ! this . isValid ) {
4336 return null ;
4437 }
45- return {
46- x : this . maxX - this . minX ,
47- y : this . maxY - this . minY ,
48- z : this . maxZ - this . minZ
49- } ;
38+ return this . max . clone ( ) . sub ( this . min ) ;
5039 }
5140
5241 /**
5342 * Gets the center coordinates of the bounding box.
5443 * @returns An object with x, y, and z center coordinates, or null if the bounding box is not valid.
5544 */
56- public get center ( ) : { x : number ; y : number ; z : number } | null {
45+ public get center ( ) : GCodeVector3 | null {
5746 if ( ! this . isValid ) {
5847 return null ;
5948 }
60- return {
61- x : this . minX + ( this . maxX - this . minX ) / 2 ,
62- y : this . minY + ( this . maxY - this . minY ) / 2 ,
63- z : this . minZ + ( this . maxZ - this . minZ ) / 2
64- } ;
49+ return this . min . clone ( ) . add ( this . max ) . multiplyScalar ( 0.5 ) ;
6550 }
6651
67- public get corners ( ) : { min : { x : number ; y : number ; z : number } ; max : { x : number ; y : number ; z : number } } {
52+ public get corners ( ) : { min : GCodeVector3 ; max : GCodeVector3 } | null {
6853 if ( ! this . isValid ) {
6954 return null ;
7055 }
7156 return {
72- min : { x : this . minX , y : this . minY , z : this . minZ } ,
73- max : { x : this . maxX , y : this . maxY , z : this . maxZ }
57+ min : this . min . clone ( ) ,
58+ max : this . max . clone ( )
7459 } ;
7560 }
7661}
0 commit comments