[refactor] FlightConfiguration now optionally caches its calculated bounds.

This commit is contained in:
Daniel_M_Williams 2018-09-16 19:51:52 -04:00
parent 5e3d230724
commit 7d813b4e55

View File

@ -404,7 +404,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
*
* @return a <code>Collection</code> containing coordinates bounding the rocket.
*
* @deprecated Migrate to FlightConfiguration#BoundingBox, when practical.
* @deprecated Migrate to <FlightConfiguration>.getBoundingBox(), when practical.
*/
@Deprecated
public Collection<Coordinate> getBounds() {
@ -414,26 +414,26 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
/**
* Return the bounding box of the current configuration.
*
* @return
* @return the rocket's bounding box (under the selected configuration)
*/
public BoundingBox getBoundingBox() {
if (rocket.getModID() != boundsModID) {
boundsModID = rocket.getModID();
calculateBounds();
}
return cachedBounds;
}
BoundingBox bounds = new BoundingBox();
private void calculateBounds(){
BoundingBox bounds = new BoundingBox();
for (RocketComponent component : this.getActiveComponents()) {
BoundingBox componentBounds = new BoundingBox().update(component.getComponentBounds());
bounds.update( componentBounds );
}
cachedLength = bounds.span().x;
cachedBounds.update( bounds );
for (RocketComponent component : this.getActiveComponents()) {
BoundingBox componentBounds = new BoundingBox().update(component.getComponentBounds());
bounds.update( componentBounds );
}
return cachedBounds;
boundsModID = rocket.getModID();
cachedLength = bounds.span().x;
cachedBounds.update( bounds );
}
/**
@ -443,9 +443,9 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
* @return the length of the rocket in the X-direction.
*/
public double getLength() {
if (rocket.getModID() != boundsModID)
getBounds(); // Calculates the length
if (rocket.getModID() != boundsModID) {
calculateBounds();
}
return cachedLength;
}