fixed
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / GraphSimplifier.java
index b9dde99a0bebca9132e71e67bea25ef1c77e2cb1..decac481de29b8b9505ee924152e96f41c71259a 100644 (file)
@@ -15,15 +15,20 @@ import javax.measure.quantity.Area;
 import javax.measure.quantity.Length;
 import javax.measure.quantity.Quantity;
 import javax.measure.unit.SI;
+import javax.swing.JFrame;
+import javax.swing.JSplitPane;
 
 import org.jscience.physics.amount.Amount;
 
-import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain;
+import com.billkuker.rocketry.motorsim.grain.EndBurner;
 import com.billkuker.rocketry.motorsim.visual.Chart;
 
 public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
        Method f;
-
+       
+       private static final int CHOOSE = 10;
+       private static final int EVEN = 10;
+       
        private class Entry {
                Amount<X> x;
                Amount<Y> y;
@@ -35,7 +40,7 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
 
                @Override
                public int compareTo(DDEntry o) {
-                       return dd.compareTo(o.dd);
+                       return o.dd.compareTo(dd);
                }
        }
 
@@ -56,7 +61,7 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
                f = source.getClass().getMethod(method, Amount.class);
 
                Vector<Entry> oldEntries = new Vector<Entry>();
-
+               Entry max = null;
                while (domain.hasNext()) {
                        Amount<X> x = domain.next();
                        Amount<Y> y = (Amount<Y>) f.invoke(source, x);
@@ -64,6 +69,8 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
                        e.x = x;
                        e.y = y;
                        oldEntries.add(e);
+                       if ( max == null || e.y.isGreaterThan(max.y) )
+                               max = e;
                }
 
                List<DDEntry> byDD = new Vector<DDEntry>();
@@ -73,11 +80,21 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
                        Entry low = oldEntries.elementAt(i - 1);
                        Entry middle = oldEntries.elementAt(i);
                        Entry high = oldEntries.elementAt(i + 1);
+                       
+                       //if this is one of the N even stepped
+                       //samples include it.
+                       if ( i % (oldEntries.size()/EVEN) == 0 ){
+                               out.put(middle.x, middle.y);
+                       }
 
-                       Amount<?> d1, d2, dd;
+                       Amount d1, d2, dd;
 
                        d1 = middle.y.minus(low.y).divide(middle.x.minus(low.x));
                        d2 = high.y.minus(middle.y).divide(high.x.minus(middle.x));
+                       
+                       //dd = (d1.isGreaterThan(d2))?d1:d2;
+                       
+                       
                        dd = d2.minus(d1).divide(high.x.minus(low.x));
 
                        DDEntry dde = new DDEntry();
@@ -91,29 +108,49 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
 
                Collections.sort(byDD);
 
+               //always include the first, MAX and last
                out.put(oldEntries.elementAt(0).x, oldEntries.elementAt(0).y);
+               out.put(max.x, max.y);
+               int last = oldEntries.size() - 1;
+               out.put(oldEntries.elementAt(last).x, oldEntries.elementAt(last).y);
+               
                int count = 0;
                for (DDEntry dde : byDD) {
+                       if ( out.containsKey(dde.x) )
+                               continue;
                        out.put(dde.x, byX.get(dde.x));
-                       if (++count >= 20)
+                       if (++count >= CHOOSE)
                                break;
                }
-               int last = oldEntries.size() - 1;
-               out.put(oldEntries.elementAt(last).x, oldEntries.elementAt(last).y);
+               
+               
 
        }
 
        public static void main(String args[]) throws Exception {
+               /*
                CoredCylindricalGrain g = new CoredCylindricalGrain();
                g.setLength(Amount.valueOf(70, SI.MILLIMETER));
                g.setOD(Amount.valueOf(30, SI.MILLIMETER));
                g.setID(Amount.valueOf(10, SI.MILLIMETER));
+               */
+               EndBurner g = new EndBurner();
+               g.setLength(Amount.valueOf(70, SI.MILLIMETER));
+               g.setoD(Amount.valueOf(30, SI.MILLIMETER));
+               g.setPuntDepth(Amount.valueOf(10, SI.MILLIMETER));
+               g.setPuntDiameter(Amount.valueOf(10, SI.MILLIMETER));
+               
+               JFrame f = new JFrame();
+               f.setSize(1024, 768);
+               f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+               JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
+               f.add(jsp);
 
                Chart<Length, Area> c = new Chart<Length, Area>(SI.MILLIMETER,
                                SI.MILLIMETER.pow(2).asType(Area.class), g, "surfaceArea");
                c.setDomain(c.new IntervalDomain(Amount.valueOf(0, SI.CENTIMETER), g
                                .webThickness()));
-               c.show();
+               jsp.setTopComponent(c);
 
                GraphSimplifier<Length, Area> gs = new GraphSimplifier(g,
                                "surfaceArea", c.new IntervalDomain(Amount.valueOf(0,
@@ -122,6 +159,12 @@ public class GraphSimplifier<X extends Quantity, Y extends Quantity> {
                Chart<Length, Area> d = new Chart<Length, Area>(SI.MILLIMETER,
                                SI.MILLIMETER.pow(2).asType(Area.class), gs, "value");
                d.setDomain(gs.getDomain());
-               d.show();
+               jsp.setBottomComponent(d);
+               
+
+               
+               f.setVisible(true);
+               jsp.setDividerLocation(.5);
+               jsp.revalidate();
        }
 }