KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > force > ForceSimulator


1 package prefuse.util.force;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 /**
7  * Manages a simulation of physical forces acting on bodies. To create a
8  * custom ForceSimulator, add the desired {@link Force} functions and choose an
9  * appropriate {@link Integrator}.
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

13 public class ForceSimulator {
14
15     private ArrayList JavaDoc items;
16     private ArrayList JavaDoc springs;
17     private Force[] iforces;
18     private Force[] sforces;
19     private int iflen, sflen;
20     private Integrator integrator;
21     private float speedLimit = 1.0f;
22     
23     /**
24      * Create a new, empty ForceSimulator. A RungeKuttaIntegrator is used
25      * by default.
26      */

27     public ForceSimulator() {
28         this(new RungeKuttaIntegrator());
29     }
30
31     /**
32      * Create a new, empty ForceSimulator.
33      * @param integr the Integrator to use
34      */

35     public ForceSimulator(Integrator integr) {
36         integrator = integr;
37         iforces = new Force[5];
38         sforces = new Force[5];
39         iflen = 0;
40         sflen = 0;
41         items = new ArrayList JavaDoc();
42         springs = new ArrayList JavaDoc();
43     }
44
45     /**
46      * Get the speed limit, or maximum velocity value allowed by this
47      * simulator.
48      * @return the "speed limit" maximum velocity value
49      */

50     public float getSpeedLimit() {
51         return speedLimit;
52     }
53     
54     /**
55      * Set the speed limit, or maximum velocity value allowed by this
56      * simulator.
57      * @param limit the "speed limit" maximum velocity value to use
58      */

59     public void setSpeedLimit(float limit) {
60         speedLimit = limit;
61     }
62     
63     /**
64      * Get the Integrator used by this simulator.
65      * @return the Integrator
66      */

67     public Integrator getIntegrator() {
68         return integrator;
69     }
70     
71     /**
72      * Set the Integrator used by this simulator.
73      * @param intgr the Integrator to use
74      */

75     public void setIntegrator(Integrator intgr) {
76         integrator = intgr;
77     }
78     
79     /**
80      * Clear this simulator, removing all ForceItem and Spring instances
81      * for the simulator.
82      */

83     public void clear() {
84         items.clear();
85         Iterator JavaDoc siter = springs.iterator();
86         Spring.SpringFactory f = Spring.getFactory();
87         while ( siter.hasNext() )
88             f.reclaim((Spring)siter.next());
89         springs.clear();
90     }
91     
92     /**
93      * Add a new Force function to the simulator.
94      * @param f the Force function to add
95      */

96     public void addForce(Force f) {
97         if ( f.isItemForce() ) {
98             if ( iforces.length == iflen ) {
99                 // resize necessary
100
Force[] newf = new Force[iflen+10];
101                 System.arraycopy(iforces, 0, newf, 0, iforces.length);
102                 iforces = newf;
103             }
104             iforces[iflen++] = f;
105         }
106         if ( f.isSpringForce() ) {
107             if ( sforces.length == sflen ) {
108                 // resize necessary
109
Force[] newf = new Force[sflen+10];
110                 System.arraycopy(sforces, 0, newf, 0, sforces.length);
111                 sforces = newf;
112             }
113             sforces[sflen++] = f;
114         }
115     }
116     
117     /**
118      * Get an array of all the Force functions used in this simulator.
119      * @return an array of Force functions
120      */

121     public Force[] getForces() {
122         Force[] rv = new Force[iflen+sflen];
123         System.arraycopy(iforces, 0, rv, 0, iflen);
124         System.arraycopy(sforces, 0, rv, iflen, sflen);
125         return rv;
126     }
127     
128     /**
129      * Add a ForceItem to the simulation.
130      * @param item the ForceItem to add
131      */

132     public void addItem(ForceItem item) {
133         items.add(item);
134     }
135     
136     /**
137      * Remove a ForceItem to the simulation.
138      * @param item the ForceItem to remove
139      */

140     public boolean removeItem(ForceItem item) {
141         return items.remove(item);
142     }
143
144     /**
145      * Get an iterator over all registered ForceItems.
146      * @return an iterator over the ForceItems.
147      */

148     public Iterator JavaDoc getItems() {
149         return items.iterator();
150     }
151     
152     /**
153      * Add a Spring to the simulation.
154      * @param item1 the first endpoint of the spring
155      * @param item2 the second endpoint of the spring
156      * @return the Spring added to the simulation
157      */

158     public Spring addSpring(ForceItem item1, ForceItem item2) {
159         return addSpring(item1, item2, -1.f, -1.f);
160     }
161     
162     /**
163      * Add a Spring to the simulation.
164      * @param item1 the first endpoint of the spring
165      * @param item2 the second endpoint of the spring
166      * @param length the spring length
167      * @return the Spring added to the simulation
168      */

169     public Spring addSpring(ForceItem item1, ForceItem item2, float length) {
170         return addSpring(item1, item2, -1.f, length);
171     }
172     
173     /**
174      * Add a Spring to the simulation.
175      * @param item1 the first endpoint of the spring
176      * @param item2 the second endpoint of the spring
177      * @param coeff the spring coefficient
178      * @param length the spring length
179      * @return the Spring added to the simulation
180      */

181     public Spring addSpring(ForceItem item1, ForceItem item2, float coeff, float length) {
182         if ( item1 == null || item2 == null )
183             throw new IllegalArgumentException JavaDoc("ForceItems must be non-null");
184         Spring s = Spring.getFactory().getSpring(item1, item2, coeff, length);
185         springs.add(s);
186         return s;
187     }
188     
189     /**
190      * Get an iterator over all registered Springs.
191      * @return an iterator over the Springs.
192      */

193     public Iterator JavaDoc getSprings() {
194         return springs.iterator();
195     }
196     
197     /**
198      * Run the simulator for one timestep.
199      * @param timestep the span of the timestep for which to run the simulator
200      */

201     public void runSimulator(long timestep) {
202         accumulate();
203         integrator.integrate(this, timestep);
204     }
205     
206     /**
207      * Accumulate all forces acting on the items in this simulation
208      */

209     public void accumulate() {
210         for ( int i = 0; i < iflen; i++ )
211             iforces[i].init(this);
212         for ( int i = 0; i < sflen; i++ )
213             sforces[i].init(this);
214         Iterator JavaDoc itemIter = items.iterator();
215         while ( itemIter.hasNext() ) {
216             ForceItem item = (ForceItem)itemIter.next();
217             item.force[0] = 0.0f; item.force[1] = 0.0f;
218             for ( int i = 0; i < iflen; i++ )
219                 iforces[i].getForce(item);
220         }
221         Iterator JavaDoc springIter = springs.iterator();
222         while ( springIter.hasNext() ) {
223             Spring s = (Spring)springIter.next();
224             for ( int i = 0; i < sflen; i++ ) {
225                 sforces[i].getForce(s);
226             }
227         }
228     }
229     
230 } // end of class ForceSimulator
231
Popular Tags