[refactor] updated BoundingBox class to be more useful

- FlightConfiguration now exposes the BoundingBox method for its rocket
This commit is contained in:
Daniel_M_Williams 2018-06-24 16:57:08 -04:00
parent 95b1e8718b
commit 9d76ece128
3 changed files with 100 additions and 39 deletions

View File

@ -587,11 +587,11 @@ public abstract class FinSet extends ExternalComponent implements RingInstanceab
public BoundingBox getBoundingBox() { public BoundingBox getBoundingBox() {
BoundingBox singleFinBounds= new BoundingBox( getFinPoints()); BoundingBox singleFinBounds= new BoundingBox().update(getFinPoints());
final double finLength = singleFinBounds.max.x; final double finLength = singleFinBounds.max.x;
final double finHeight = singleFinBounds.max.y; final double finHeight = singleFinBounds.max.y;
BoundingBox compBox = new BoundingBox( getComponentLocations() ); BoundingBox compBox = new BoundingBox().update(getComponentLocations());
BoundingBox finSetBox = new BoundingBox( compBox.min.sub( 0, finHeight, finHeight ), BoundingBox finSetBox = new BoundingBox( compBox.min.sub( 0, finHeight, finHeight ),
compBox.max.add( finLength, finHeight, finHeight )); compBox.max.add( finLength, finHeight, finHeight ));

View File

@ -398,25 +398,37 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
* Return the bounds of the current configuration. The bounds are cached. * Return the bounds of the current configuration. The bounds are cached.
* *
* @return a <code>Collection</code> containing coordinates bounding the rocket. * @return a <code>Collection</code> containing coordinates bounding the rocket.
*
* @deprecated Migrate to FlightConfiguration#BoundingBox, when practical.
*/ */
@Deprecated
public Collection<Coordinate> getBounds() { public Collection<Coordinate> getBounds() {
return getBoundingBox().toCollection();
}
/**
* Return the bounding box of the current configuration.
*
* @return
*/
public BoundingBox getBoundingBox() {
if (rocket.getModID() != boundsModID) { if (rocket.getModID() != boundsModID) {
boundsModID = rocket.getModID(); boundsModID = rocket.getModID();
BoundingBox bounds = new BoundingBox(); BoundingBox bounds = new BoundingBox();
for (RocketComponent component : this.getActiveComponents()) { for (RocketComponent component : this.getActiveComponents()) {
BoundingBox componentBounds = new BoundingBox( component.getComponentBounds() ); BoundingBox componentBounds = new BoundingBox().update(component.getComponentBounds());
bounds.compare( componentBounds ); bounds.update( componentBounds );
} }
cachedLength = bounds.span().x; cachedLength = bounds.span().x;
cachedBounds.compare( bounds ); cachedBounds.update( bounds );
} }
return cachedBounds.toCollection(); return cachedBounds;
} }
/** /**

View File

@ -1,5 +1,6 @@
package net.sf.openrocket.util; package net.sf.openrocket.util;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -8,8 +9,7 @@ public class BoundingBox {
public Coordinate max; public Coordinate max;
public BoundingBox() { public BoundingBox() {
min = Coordinate.MAX.setWeight( 0.0); clear();
max = Coordinate.MIN.setWeight( 0.0);
} }
public BoundingBox( Coordinate _min, Coordinate _max) { public BoundingBox( Coordinate _min, Coordinate _max) {
@ -18,14 +18,9 @@ public class BoundingBox {
this.max = _max.clone(); this.max = _max.clone();
} }
public BoundingBox( Coordinate[] list ) { public void clear() {
this(); min = Coordinate.MAX.setWeight( 0.0);
this.compare( list); max = Coordinate.MIN.setWeight( 0.0);
}
public BoundingBox( Collection<Coordinate> list ) {
this();
this.compare( list.toArray( new Coordinate[0] ));
} }
@Override @Override
@ -33,39 +28,90 @@ public class BoundingBox {
return new BoundingBox( this.min, this.max ); return new BoundingBox( this.min, this.max );
} }
public void compare( Coordinate c ) {
compare_against_min(c); private void update_x_min( final double xVal) {
compare_against_max(c); if( min.x > xVal)
min = min.setX( xVal );
} }
protected void compare_against_min( Coordinate c ) { private void update_y_min( final double yVal) {
if( min.x > c.x ) if( min.y > yVal )
min = min.setX( c.x ); min = min.setY( yVal );
if( min.y > c.y )
min = min.setY( c.y );
if( min.z > c.z )
min = min.setZ( c.z );
} }
protected void compare_against_max( Coordinate c) { private void update_z_min( final double zVal) {
if( max.x < c.x ) if( min.z > zVal )
max = max.setX( c.x ); min = min.setZ( zVal );
if( max.y < c.y ) }
max = max.setY( c.y );
if( max.z < c.z ) private void update_x_max( final double xVal) {
max = max.setZ( c.z ); if( max.x < xVal )
max = max.setX( xVal );
}
private void update_y_max( final double yVal) {
if( max.y < yVal )
max = max.setY( yVal );
}
private void update_z_max( final double zVal) {
if( max.z < zVal )
max = max.setZ( zVal );
}
public BoundingBox update( final double val) {
update_x_min(val);
update_y_min(val);
update_z_min(val);
update_x_max(val);
update_y_max(val);
update_z_max(val);
return this;
}
public void update( Coordinate c ) {
update_x_min(c.x);
update_y_min(c.y);
update_z_min(c.z);
update_x_max(c.x);
update_y_max(c.y);
update_z_max(c.z);
} }
public BoundingBox compare( Coordinate[] list ) { public BoundingBox update( Rectangle2D rect ) {
update_x_min(rect.getMinX());
update_y_min(rect.getMinY());
update_x_max(rect.getMaxX());
update_y_max(rect.getMaxY());
return this;
}
public BoundingBox update( final Coordinate[] list ) {
for( Coordinate c: list ) { for( Coordinate c: list ) {
compare( c ); update( c );
} }
return this; return this;
} }
public void compare( BoundingBox other ) { public BoundingBox update( Collection<Coordinate> list ) {
compare_against_min( other.min); for( Coordinate c: list ) {
compare_against_max( other.max); update( c );
}
return this;
}
public BoundingBox update( BoundingBox other ) {
update_x_min(other.min.x);
update_y_min(other.min.y);
update_z_min(other.min.y);
update_x_max(other.max.x);
update_y_max(other.max.y);
update_z_max(other.max.z);
return this;
} }
public Coordinate span() { return max.sub( min ); } public Coordinate span() { return max.sub( min ); }
@ -81,9 +127,12 @@ public class BoundingBox {
return toReturn; return toReturn;
} }
public Rectangle2D toRectangle() {
return new Rectangle2D.Double(min.x, min.y, (max.x-min.x), (max.y - min.y));
}
@Override @Override
public String toString() { public String toString() {
// return String.format("[( %6.4f, %6.4f, %6.4f) < ( %6.4f, %6.4f, %6.4f)]",
return String.format("[( %g, %g, %g) < ( %g, %g, %g)]", return String.format("[( %g, %g, %g) < ( %g, %g, %g)]",
min.x, min.y, min.z, min.x, min.y, min.z,
max.x, max.y, max.z ); max.x, max.y, max.z );