DGP - bug fix for MassObject/Shock Cord

This commit is contained in:
Doug Pedrick 2012-01-23 03:39:47 +00:00
parent 6bdefe103c
commit b5642117ce
2 changed files with 70 additions and 17 deletions

View File

@ -9,7 +9,9 @@ import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler; import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import net.sf.openrocket.rocketcomponent.MassComponent; import net.sf.openrocket.rocketcomponent.MassComponent;
import net.sf.openrocket.rocketcomponent.MassObject;
import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.ShockCord;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import java.util.HashMap; import java.util.HashMap;
@ -17,7 +19,7 @@ import java.util.HashMap;
/** /**
* A SAX handler for Rocksim's MassObject XML type. * A SAX handler for Rocksim's MassObject XML type.
*/ */
class MassObjectHandler extends PositionDependentHandler<MassComponent> { class MassObjectHandler extends PositionDependentHandler<MassObject> {
/** /**
* The Rocksim Mass length fudge factor. Rocksim completely exaggerates the length of a mass object to the point * The Rocksim Mass length fudge factor. Rocksim completely exaggerates the length of a mass object to the point
@ -33,6 +35,21 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
*/ */
private final MassComponent mass; private final MassComponent mass;
/**
* Reference to answer for getComponent().
*/
private MassObject current;
/**
* Parent.
*/
private RocketComponent parent;
/**
* 0 == General, 1 == Shock Cord
*/
private int typeCode = 0;
/** /**
* Constructor. * Constructor.
*l *l
@ -46,9 +63,8 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
throw new IllegalArgumentException("The parent component of a mass component may not be null."); throw new IllegalArgumentException("The parent component of a mass component may not be null.");
} }
mass = new MassComponent(); mass = new MassComponent();
if (isCompatible(c, MassComponent.class, warnings)) { current = mass;
c.addChild(mass); parent = c;
}
} }
@Override @Override
@ -62,7 +78,7 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
super.closeElement(element, attributes, content, warnings); super.closeElement(element, attributes, content, warnings);
try { try {
if (RocksimCommonConstants.LEN.equals(element)) { if (RocksimCommonConstants.LEN.equals(element)) {
mass.setLength(Double.parseDouble(content) / (RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR)); mass.setLength(Double.parseDouble(content) / (RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
} }
if (RocksimCommonConstants.KNOWN_MASS.equals(element)) { if (RocksimCommonConstants.KNOWN_MASS.equals(element)) {
mass.setComponentMass(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS); mass.setComponentMass(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
@ -75,20 +91,56 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
//Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object. //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
super.setCG(0); super.setCG(0);
} }
if (RocksimCommonConstants.TYPE_CODE.equals(element)) {
typeCode = Integer.parseInt(content);
}
if (RocksimCommonConstants.MATERIAL.equals(element)) {
setMaterialName(content);
}
} }
catch (NumberFormatException nfe) { catch (NumberFormatException nfe) {
warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number."); warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
} }
} }
@Override
public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
if (typeCode == 0) { //General Mass Object
if (isCompatible(parent, MassComponent.class, warnings)) {
parent.addChild(mass);
}
super.endHandler(element, attributes, content, warnings);
}
else if (typeCode == 1) { //Shock Cord
ShockCord cord = new ShockCord();
current = cord;
if (isCompatible(parent, ShockCord.class, warnings)) {
parent.addChild(cord);
}
super.endHandler(element, attributes, content, warnings);
cord.setName(mass.getName());
setOverride(cord, mass.isMassOverridden(), mass.getOverrideMass(), mass.getOverrideCGX());
cord.setRadialDirection(mass.getRadialDirection());
cord.setRadialPosition(mass.getRadialPosition());
cord.setRadius(mass.getRadius());
//Rocksim does not distinguish between total length of the cord and the packed length. Fudge the
//packed length and set the real length.
cord.setCordLength(mass.getLength());
cord.setLength(cord.getCordLength()/MASS_LEN_FUDGE_FACTOR);
}
}
/** /**
* Get the component this handler is working upon. * Get the component this handler is working upon. This changes depending upon the type of mass object.
* *
* @return a component * @return a component
*/ */
@Override @Override
public MassComponent getComponent() { public MassObject getComponent() {
return mass; return current;
} }
/** /**
@ -98,17 +150,17 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
* @param position the OpenRocket position * @param position the OpenRocket position
*/ */
public void setRelativePosition(RocketComponent.Position position) { public void setRelativePosition(RocketComponent.Position position) {
mass.setRelativePosition(position); current.setRelativePosition(position);
} }
/** /**
* Get the required type of material for this component. Does not apply to MassComponents. * Get the required type of material for this component. Does not apply to MassComponents, but does apply to Shock Cords.
* *
* @return BULK * @return LINE
*/ */
@Override @Override
public Material.Type getMaterialType() { public Material.Type getMaterialType() {
return Material.Type.BULK; return Material.Type.LINE;
} }
} }

View File

@ -38,8 +38,9 @@ public class MassObjectHandlerTest extends RocksimTestBase {
BodyTube tube = new BodyTube(); BodyTube tube = new BodyTube();
MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet()); MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
MassComponent component = (MassComponent) getField(handler, "mass"); MassComponent mass = (MassComponent) getField(handler, "mass");
assertContains(component, tube.getChildren()); MassComponent current = (MassComponent) getField(handler, "current");
Assert.assertEquals(mass, current);
} }
/** /**
@ -70,10 +71,10 @@ public class MassObjectHandlerTest extends RocksimTestBase {
handler.closeElement("Len", attributes, "-1", warnings); handler.closeElement("Len", attributes, "-1", warnings);
Assert.assertEquals(0d, component.getLength(), 0.001); Assert.assertEquals(0d, component.getLength(), 0.001);
handler.closeElement("Len", attributes, "10", warnings); handler.closeElement("Len", attributes, "10", warnings);
Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH) Assert.assertEquals(0.01
, component.getLength(), 0.001); , component.getLength(), 0.001);
handler.closeElement("Len", attributes, "10.0", warnings); handler.closeElement("Len", attributes, "10.0", warnings);
Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH) Assert.assertEquals(0.01
, component.getLength(), 0.001); , component.getLength(), 0.001);
handler.closeElement("Len", attributes, "foo", warnings); handler.closeElement("Len", attributes, "foo", warnings);
Assert.assertEquals(1, warnings.size()); Assert.assertEquals(1, warnings.size());
@ -120,6 +121,6 @@ public class MassObjectHandlerTest extends RocksimTestBase {
*/ */
@org.junit.Test @org.junit.Test
public void testGetMaterialType() throws Exception { public void testGetMaterialType() throws Exception {
Assert.assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType()); Assert.assertEquals(Material.Type.LINE, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
} }
} }