View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name
3    * Copyright (C) 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package org.jcurl.demo.util;
20  
21  import org.jcurl.core.api.RockDouble;
22  import org.jcurl.core.api.RockSet;
23  import org.jcurl.core.api.RockSetUtils;
24  import org.jcurl.core.api.TrajectorySet;
25  import org.jcurl.core.api.RockType.Pos;
26  import org.jcurl.core.api.RockType.Vel;
27  
28  /**
29   * Extract locations from a (non-discrete) {@link TrajectorySet} and walk on in
30   * real time.
31   * 
32   * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
33   * @version $Id:RealTimePlayer.java 378 2007-01-24 01:18:35Z mrohrmoser $
34   */
35  public class RealTimePlayer implements Runnable {
36  
37  	private final TrajectorySet src;
38  
39  	private double t0Last;
40  
41  	private final double t0Start;
42  
43  	private volatile double timeScale;
44  
45  	private final long timeSleep = 25;
46  
47  	private double tNow;
48  
49  	public RealTimePlayer(final double t0, final double scale,
50  			final TrajectorySet src) {
51  		t0Start = t0Last = tNow = t0;
52  		timeScale = scale;
53  		this.src = src;
54  	}
55  
56  	/**
57  	 * @return Returns the timeScale.
58  	 */
59  	public double getTimeScale() {
60  		return timeScale;
61  	}
62  
63  	/**
64  	 * 
65  	 * @see java.lang.Runnable#run()
66  	 */
67  	public void run() {
68  		try {
69  			final RockSet<Pos> pos = RockSetUtils.allHome(null);
70  			final RockSet<Vel> speed = new RockSet<Vel>(new RockDouble<Vel>());
71  			final long start = System.currentTimeMillis();
72  			for (;;) {
73  				final long dt = System.currentTimeMillis() - start;
74  				tNow = t0Last + dt * timeScale * 1e-3;
75  				// get the position
76  				src.setCurrentTime(tNow);
77  				if (0 == RockSet.nonZero(src.getCurrentVel())) {
78  					t0Last = t0Start;
79  					break;
80  				}
81  				try {
82  					Thread.sleep(timeSleep);
83  				} catch (final InterruptedException e) {
84  					t0Last = tNow;
85  					break;
86  				}
87  			}
88  		} catch (final Exception e) {
89  			System.err.println(e.toString());
90  			e.printStackTrace(System.err);
91  		}
92  	}
93  
94  	/**
95  	 * @param timeScale
96  	 *            The timeScale to set.
97  	 */
98  	public void setTimeScale(final double timeScale) {
99  		t0Last = tNow;
100 		this.timeScale = timeScale;
101 	}
102 }