1 package net.sf.openrocket.database;
4 import java.io.IOException;
6 import java.util.ArrayList;
8 import net.sf.openrocket.file.MotorLoader;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.Motor;
11 import net.sf.openrocket.util.JarUtil;
12 import net.sf.openrocket.util.MathUtil;
16 * A class that contains single instances of {@link Database} for specific purposes.
18 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20 public class Databases {
22 /* Static implementations of specific databases: */
26 public static final Database<Motor> MOTOR = new Database<Motor>(new MotorLoader());
30 * A database of bulk materials (with bulk densities).
32 public static final Database<Material> BULK_MATERIAL = new Database<Material>();
34 * A database of surface materials (with surface densities).
36 public static final Database<Material> SURFACE_MATERIAL = new Database<Material>();
38 * A database of linear material (with length densities).
40 public static final Database<Material> LINE_MATERIAL = new Database<Material>();
44 // TODO: HIGH: loading the thrust curves and other databases
48 MOTOR.loadJarDirectory("datafiles/thrustcurves/", ".*\\.[eE][nN][gG]$");
49 } catch (IOException e) {
50 System.out.println("Could not read thrust curves from JAR: "+e.getMessage());
52 // Try to find directory as a system resource
54 URL url = ClassLoader.getSystemResource("datafiles/thrustcurves/");
57 dir = JarUtil.urlToFile(url);
58 } catch (Exception e1) {
59 dir = new File("datafiles/thrustcurves/");
63 MOTOR.loadDirectory(dir, ".*\\.[eE][nN][gG]$");
64 } catch (IOException e1) {
65 System.out.println("Could not read thrust curves from directory either.");
66 throw new RuntimeException(e1);
71 // TODO: HIGH: Move materials into data files
74 BULK_MATERIAL.add(new Material.Bulk("Acrylic", 1190));
75 BULK_MATERIAL.add(new Material.Bulk("Balsa", 170));
76 BULK_MATERIAL.add(new Material.Bulk("Birch", 670));
77 BULK_MATERIAL.add(new Material.Bulk("Cardboard", 680));
78 BULK_MATERIAL.add(new Material.Bulk("Carbon fiber", 1780));
79 BULK_MATERIAL.add(new Material.Bulk("Cork", 240));
80 BULK_MATERIAL.add(new Material.Bulk("Fiberglass", 1850));
81 BULK_MATERIAL.add(new Material.Bulk("Kraft phenolic",950));
82 BULK_MATERIAL.add(new Material.Bulk("Maple", 755));
83 BULK_MATERIAL.add(new Material.Bulk("Paper (office)",820));
84 BULK_MATERIAL.add(new Material.Bulk("Pine", 530));
85 BULK_MATERIAL.add(new Material.Bulk("Plywood (birch)",630));
86 BULK_MATERIAL.add(new Material.Bulk("Polycarbonate (Lexan)",1200));
87 BULK_MATERIAL.add(new Material.Bulk("Polystyrene", 1050));
88 BULK_MATERIAL.add(new Material.Bulk("PVC", 1390));
89 BULK_MATERIAL.add(new Material.Bulk("Spruce", 450));
90 BULK_MATERIAL.add(new Material.Bulk("Quantum tubing",1050));
92 SURFACE_MATERIAL.add(new Material.Surface("Ripstop nylon", 0.067));
93 SURFACE_MATERIAL.add(new Material.Surface("Mylar", 0.021));
94 SURFACE_MATERIAL.add(new Material.Surface("Polyethylene (thin)", 0.015));
95 SURFACE_MATERIAL.add(new Material.Surface("Polyethylene (heavy)", 0.040));
96 SURFACE_MATERIAL.add(new Material.Surface("Silk", 0.060));
97 SURFACE_MATERIAL.add(new Material.Surface("Paper (office)", 0.080));
98 SURFACE_MATERIAL.add(new Material.Surface("Cellophane", 0.018));
99 SURFACE_MATERIAL.add(new Material.Surface("Cr\u00eape paper", 0.025));
101 LINE_MATERIAL.add(new Material.Line("Thread (heavy-duty)", 0.0003));
102 LINE_MATERIAL.add(new Material.Line("Elastic cord (round 2mm, 1/16 in)",0.0018));
103 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 6mm, 1/4 in)", 0.0043));
104 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 12mm, 1/2 in)", 0.008));
105 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 19mm, 3/4 in)", 0.0012));
106 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 25mm, 1 in)", 0.0016));
107 LINE_MATERIAL.add(new Material.Line("Braided nylon (2 mm, 1/16 in)", 0.001));
108 LINE_MATERIAL.add(new Material.Line("Braided nylon (3 mm, 1/8 in)", 0.0035));
109 LINE_MATERIAL.add(new Material.Line("Tubular nylon (11 mm, 7/16 in)", 0.013));
110 LINE_MATERIAL.add(new Material.Line("Tubular nylon (14 mm, 9/16 in)", 0.016));
111 LINE_MATERIAL.add(new Material.Line("Tubular nylon (25 mm, 1 in)", 0.029));
116 * Find a material from the database with the specified type and name. Returns
117 * <code>null</code> if the specified material could not be found.
119 * @param type the material type.
120 * @param name the material name in the database.
121 * @return the material, or <code>null</code> if not found.
123 public static Material findMaterial(Material.Type type, String name) {
124 Database<Material> db;
130 db = SURFACE_MATERIAL;
136 throw new IllegalArgumentException("Illegal material type: "+type);
139 for (Material m: db) {
140 if (m.getName().equalsIgnoreCase(name)) {
149 * Find a material from the database or return a new material if the specified
150 * material with the specified density is not found.
152 * @param type the material type.
153 * @param name the material name.
154 * @param density the density of the material.
155 * @return the material object from the database or a new material.
157 public static Material findMaterial(Material.Type type, String name, double density) {
158 Database<Material> db;
164 db = SURFACE_MATERIAL;
170 throw new IllegalArgumentException("Illegal material type: "+type);
173 for (Material m: db) {
174 if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
178 return Material.newMaterial(type, name, density);
184 * Return all motor in the database matching a search criteria. Any search criteria that
185 * is null or NaN is ignored.
187 * @param type the motor type, or null.
188 * @param manufacturer the manufacturer, or null.
189 * @param designation the designation, or null.
190 * @param diameter the diameter, or NaN.
191 * @param length the length, or NaN.
192 * @return an array of all the matching motors.
194 public static Motor[] findMotors(Motor.Type type, String manufacturer, String designation, double diameter, double length) {
195 ArrayList<Motor> results = new ArrayList<Motor>();
197 for (Motor m: MOTOR) {
198 boolean match = true;
199 if (type != null && type != m.getMotorType())
201 else if (manufacturer != null && !manufacturer.equalsIgnoreCase(m.getManufacturer()))
203 else if (designation != null && !designation.equalsIgnoreCase(m.getDesignation()))
205 else if (!Double.isNaN(diameter) && (Math.abs(diameter - m.getDiameter()) > 0.0015))
207 else if (!Double.isNaN(length) && (Math.abs(length - m.getLength()) > 0.0015))
214 return results.toArray(new Motor[0]);