View Javadoc

1   /*
2    * @(#) LinearInterpolation.java Mar 4, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.animation.interpolation;
11  
12  import nl.tudelft.simulation.language.d3.DirectedPoint;
13  
14  /***
15   * A LinearInterpolation <br>
16   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
17   * University of Technology </a>, the Netherlands. <br>
18   * See for project information <a
19   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
20   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
21   * License (GPL) </a>, no warranty <br>
22   * 
23   * @version 1.0 Mar 4, 2004 <br>
24   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
25   *         Jacobs </a>
26   */
27  public class LinearInterpolation implements InterpolationInterface
28  {
29  	/*** the start time */
30  	protected double startTime = Double.NaN;
31  
32  	/*** the end time */
33  	protected double endTime = Double.NaN;
34  
35  	/*** the origin */
36  	private DirectedPoint origin = null;
37  
38  	/*** the destination */
39  	private DirectedPoint destination = null;
40  
41  	/***
42  	 * constructs a new LinearInterpolation
43  	 * 
44  	 * @param startTime the startingTime
45  	 * @param endTime the endTime
46  	 * @param origin the origin
47  	 * @param destination the destination
48  	 */
49  	public LinearInterpolation(final double startTime, final double endTime,
50  			final DirectedPoint origin, final DirectedPoint destination)
51  	{
52  		super();
53  		if (endTime < startTime)
54  		{
55  			throw new IllegalArgumentException("endTime < startTime");
56  		}
57  		this.startTime = startTime;
58  		this.endTime = endTime;
59  		this.origin = (DirectedPoint) origin.clone();
60  		this.destination = (DirectedPoint) destination.clone();
61  	}
62  
63  	/***
64  	 * @see nl.tudelft.simulation.dsol.animation.interpolation.InterpolationInterface
65  	 *      #getLocation(double)
66  	 */
67  	public DirectedPoint getLocation(final double time)
68  	{
69  		if (time <= this.startTime)
70  		{
71  			return this.origin;
72  		}
73  		if (time >= this.endTime)
74  		{
75  			return this.destination;
76  		}
77  		double fraction = (time - this.startTime)
78  				/ (this.endTime - this.startTime);
79  		double x = this.origin.x + (this.destination.x - this.origin.x)
80  				* fraction;
81  		double y = this.origin.y + (this.destination.y - this.origin.y)
82  				* fraction;
83  		double z = this.origin.z + (this.destination.z - this.origin.z)
84  				* fraction;
85  		double rotY = this.origin.getRotY()
86  				+ (this.destination.getRotY() - this.origin.getRotY())
87  				* fraction;
88  		double rotZ = this.origin.getRotZ()
89  				+ (this.destination.getRotZ() - this.origin.getRotZ())
90  				* fraction;
91  		double rotX = this.origin.getRotX()
92  				+ (this.destination.getRotX() - this.origin.getRotX())
93  				* fraction;
94  		return new DirectedPoint(x, y, z, rotX, rotY, rotZ);
95  	}
96  }