[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. * @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 @Deprecated
public Collection<Coordinate> getBounds() { public Collection<Coordinate> getBounds() {
@ -414,28 +414,28 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
/** /**
* Return the bounding box of the current configuration. * Return the bounding box of the current configuration.
* *
* @return * @return the rocket's bounding box (under the selected configuration)
*/ */
public BoundingBox getBoundingBox() { public BoundingBox getBoundingBox() {
if (rocket.getModID() != boundsModID) { if (rocket.getModID() != boundsModID) {
boundsModID = rocket.getModID(); calculateBounds();
}
return cachedBounds;
}
private void calculateBounds(){
BoundingBox bounds = new BoundingBox(); BoundingBox bounds = new BoundingBox();
for (RocketComponent component : this.getActiveComponents()) { for (RocketComponent component : this.getActiveComponents()) {
BoundingBox componentBounds = new BoundingBox().update(component.getComponentBounds()); BoundingBox componentBounds = new BoundingBox().update(component.getComponentBounds());
bounds.update( componentBounds ); bounds.update( componentBounds );
} }
boundsModID = rocket.getModID();
cachedLength = bounds.span().x; cachedLength = bounds.span().x;
cachedBounds.update( bounds ); cachedBounds.update( bounds );
} }
return cachedBounds;
}
/** /**
* Returns the length of the rocket configuration, from the foremost bound X-coordinate * Returns the length of the rocket configuration, from the foremost bound X-coordinate
* to the aft-most X-coordinate. The value is cached. * to the aft-most X-coordinate. The value is cached.
@ -443,9 +443,9 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
* @return the length of the rocket in the X-direction. * @return the length of the rocket in the X-direction.
*/ */
public double getLength() { public double getLength() {
if (rocket.getModID() != boundsModID) if (rocket.getModID() != boundsModID) {
getBounds(); // Calculates the length calculateBounds();
}
return cachedLength; return cachedLength;
} }