From b9dcc908b66c2431a37b6899204d95aafdf168ab Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 10 Nov 2022 00:08:27 +0100 Subject: [PATCH] [#1792] Don't clamp the transition thickness upon import --- .../openrocket/importt/DocumentConfig.java | 15 ++++++---- .../file/openrocket/importt/DoubleSetter.java | 28 +++++++++++++------ .../rocketcomponent/SymmetricComponent.java | 23 ++++++++++----- .../rocketcomponent/Transition.java | 26 ++++++++++++++--- 4 files changed, 66 insertions(+), 26 deletions(-) diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/DocumentConfig.java b/core/src/net/sf/openrocket/file/openrocket/importt/DocumentConfig.java index 3fe4093e6..775c1c47f 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/DocumentConfig.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/DocumentConfig.java @@ -171,9 +171,10 @@ class DocumentConfig { // SymmetricComponent setters.put("SymmetricComponent:thickness", new DoubleSetter( - Reflection.findMethod(SymmetricComponent.class, "setThickness", double.class), + Reflection.findMethod(SymmetricComponent.class, "setThickness", double.class, boolean.class), "filled", - Reflection.findMethod(SymmetricComponent.class, "setFilled", boolean.class))); + Reflection.findMethod(SymmetricComponent.class, "setFilled", boolean.class), + false)); // LaunchLug setters.put("LaunchLug:instancecount", new IntSetter( @@ -218,13 +219,15 @@ class DocumentConfig { Reflection.findMethod(Transition.class, "setShapeParameter", double.class))); setters.put("Transition:foreradius", new DoubleSetter( - Reflection.findMethod(Transition.class, "setForeRadius", double.class), + Reflection.findMethod(Transition.class, "setForeRadius", double.class, boolean.class), "auto", " ", - Reflection.findMethod(Transition.class, "setForeRadiusAutomatic", boolean.class))); + Reflection.findMethod(Transition.class, "setForeRadiusAutomatic", boolean.class), + false)); setters.put("Transition:aftradius", new DoubleSetter( - Reflection.findMethod(Transition.class, "setAftRadius", double.class), + Reflection.findMethod(Transition.class, "setAftRadius", double.class, boolean.class), "auto", " ", - Reflection.findMethod(Transition.class, "setAftRadiusAutomatic", boolean.class))); + Reflection.findMethod(Transition.class, "setAftRadiusAutomatic", boolean.class), + false)); setters.put("Transition:foreshoulderradius", new DoubleSetter( Reflection.findMethod(Transition.class, "setForeShoulderRadius", double.class))); diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/DoubleSetter.java b/core/src/net/sf/openrocket/file/openrocket/importt/DoubleSetter.java index 5525f8086..b12263666 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/DoubleSetter.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/DoubleSetter.java @@ -2,7 +2,6 @@ package net.sf.openrocket.file.openrocket.importt; import java.util.Arrays; import java.util.HashMap; -import java.util.Objects; import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.WarningSet; @@ -20,6 +19,7 @@ class DoubleSetter implements Setter { private final Reflection.Method specialMethod; private final double multiplier; private String separator; + private Object[] extraParameters = null; /** * Set only the double value. @@ -45,22 +45,24 @@ class DoubleSetter implements Setter { this.specialMethod = null; this.multiplier = mul; } - + /** * Set the double value, or if the value equals the special string, use the * special setter and set it to true. - * + * * @param set double setter. * @param special special string * @param specialMethod boolean setter. + * @param parameters (optional) extra parameter set to use for the setter method. */ public DoubleSetter(Reflection.Method set, String special, - Reflection.Method specialMethod) { + Reflection.Method specialMethod, Object... parameters) { this.setMethod = set; this.configGetter = null; this.specialString = special; this.specialMethod = specialMethod; this.multiplier = 1.0; + this.extraParameters = parameters; } /** @@ -73,11 +75,13 @@ class DoubleSetter implements Setter { * @param set double setter. * @param special special string * @param specialMethod boolean setter. + * @param parameters (optional) extra parameter set to use for the setter method. */ public DoubleSetter(Reflection.Method set, String special, String separator, - Reflection.Method specialMethod) { + Reflection.Method specialMethod, Object... parameters) { this(set, special, specialMethod); this.separator = separator; + this.extraParameters = parameters; } @@ -118,11 +122,17 @@ class DoubleSetter implements Setter { try { double d = Double.parseDouble(data); - if (configGetter == null) { - setMethod.invoke(c, d * multiplier); - } else { + Object obj = c; + if (configGetter != null) { FlightConfigurableParameterSet config = (FlightConfigurableParameterSet) configGetter.invoke(c); - Object obj = config.getDefault(); + obj = config.getDefault(); + } + if (extraParameters != null) { + Object[] parameters = new Object[extraParameters.length + 1]; + parameters[0] = d * multiplier; + System.arraycopy(extraParameters, 0, parameters, 1, extraParameters.length); + setMethod.invoke(obj, parameters); + } else { setMethod.invoke(obj, d * multiplier); } } catch (NumberFormatException e) { diff --git a/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java b/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java index 0427ea927..322a6245b 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java +++ b/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java @@ -103,15 +103,15 @@ public abstract class SymmetricComponent extends BodyComponent implements BoxBou public double getThickness() { if (filled) return Math.max(getForeRadius(), getAftRadius()); - return Math.min(thickness, Math.max(getForeRadius(), getAftRadius())); + return thickness; } - - + /** - * Set the component wall thickness. Values greater than the maximum radius are not - * allowed, and will result in setting the thickness to the maximum radius. + * Set the component wall thickness. If doClamping is true, values greater than + * the maximum radius will be clamped the thickness to the maximum radius. + * @param doClamping If true, the thickness will be clamped to the maximum radius. */ - public void setThickness(double thickness) { + public void setThickness(double thickness, boolean doClamping) { for (RocketComponent listener : configListeners) { if (listener instanceof SymmetricComponent) { ((SymmetricComponent) listener).setThickness(thickness); @@ -120,13 +120,22 @@ public abstract class SymmetricComponent extends BodyComponent implements BoxBou if ((this.thickness == thickness) && !filled) return; - this.thickness = MathUtil.clamp(thickness, 0, Math.max(getForeRadius(), getAftRadius())); + this.thickness = doClamping ? MathUtil.clamp(thickness, 0, Math.max(getForeRadius(), getAftRadius())) : thickness; filled = false; fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE); clearPreset(); } + /** + * Set the component wall thickness. Values greater than the maximum radius are not + * allowed, and will result in setting the thickness to the maximum radius. + */ + public void setThickness(double thickness) { + setThickness(thickness, true); + } + + /** * Returns whether the component is set as filled. If it is set filled, then the * wall thickness will have no effect. diff --git a/core/src/net/sf/openrocket/rocketcomponent/Transition.java b/core/src/net/sf/openrocket/rocketcomponent/Transition.java index c63dff741..83d18081f 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/Transition.java +++ b/core/src/net/sf/openrocket/rocketcomponent/Transition.java @@ -102,7 +102,12 @@ public class Transition extends SymmetricComponent implements InsideColorCompone return foreRadius; } - public void setForeRadius(double radius) { + /** + * Set the new fore radius, with option to clamp the thickness to the new radius if it's too large. + * @param radius new radius + * @param doClamping whether to clamp the thickness + */ + public void setForeRadius(double radius, boolean doClamping) { for (RocketComponent listener : configListeners) { if (listener instanceof Transition) { ((Transition) listener).setForeRadius(radius); @@ -115,13 +120,17 @@ public class Transition extends SymmetricComponent implements InsideColorCompone this.autoForeRadius = false; this.foreRadius = Math.max(radius, 0); - if (this.thickness > this.foreRadius && this.thickness > this.aftRadius) + if (doClamping && this.thickness > this.foreRadius && this.thickness > this.aftRadius) this.thickness = Math.max(this.foreRadius, this.aftRadius); clearPreset(); fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE); } + public void setForeRadius(double radius) { + setForeRadius(radius, true); + } + @Override public boolean isForeRadiusAutomatic() { return autoForeRadius; @@ -170,7 +179,12 @@ public class Transition extends SymmetricComponent implements InsideColorCompone return aftRadius; } - public void setAftRadius(double radius) { + /** + * Set the new aft radius, with option to clamp the thickness to the new radius if it's too large. + * @param radius new radius + * @param doClamping whether to clamp the thickness + */ + public void setAftRadius(double radius, boolean doClamping) { for (RocketComponent listener : configListeners) { if (listener instanceof Transition) { ((Transition) listener).setAftRadius(radius); @@ -183,13 +197,17 @@ public class Transition extends SymmetricComponent implements InsideColorCompone this.autoAftRadius2 = false; this.aftRadius = Math.max(radius, 0); - if (this.thickness > this.foreRadius && this.thickness > this.aftRadius) + if (doClamping && this.thickness > this.foreRadius && this.thickness > this.aftRadius) this.thickness = Math.max(this.foreRadius, this.aftRadius); clearPreset(); fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE); } + public void setAftRadius(double radius) { + setAftRadius(radius, true); + } + @Override public boolean isAftRadiusAutomatic() { return autoAftRadius2;