View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.translators;
21  
22  import java.util.List;
23  
24  import cz.cuni.amis.pogamut.base3d.worldview.object.Velocity;
25  import cz.cuni.amis.pogamut.ut2004.utils.UnrealUtils;
26  import eis.iilang.Function;
27  import eis.iilang.Numeral;
28  import eis.iilang.Parameter;
29  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
30  import nl.tudelft.goal.EIS2Java.translation.Java2Parameter;
31  import nl.tudelft.goal.EIS2Java.translation.Parameter2Java;
32  
33  public class VelocityTranslator implements Java2Parameter<Velocity>, Parameter2Java<Velocity> {
34  
35  	public static final String VELOCITY_KEYWORD = "velocity";
36  
37  	@Override
38  	public Parameter[] translate(Velocity o) throws TranslationException {
39  		return new Parameter[] { new Function(VELOCITY_KEYWORD,
40  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getX())),
41  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getY())),
42  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getZ())) )
43  			};
44  	}
45  
46  	@Override
47  	public Class<? extends Velocity> translatesFrom() {
48  		return Velocity.class;
49  	}
50  
51  	@Override
52  	public Velocity translate(Parameter p) throws TranslationException {
53  
54  		if (!(p instanceof Function)) {
55  			String message = String.format("A velocity must be a function, received: %s.", p);
56  			throw new TranslationException(message);
57  		}
58  
59  		Function f = (Function) p;
60  
61  		// Check function name
62  		if (!f.getName().equals(VELOCITY_KEYWORD)) {
63  			String message = String.format("A velocity needs to start with %s, not: %s in %s.", VELOCITY_KEYWORD,
64  					f.getName(), f);
65  			throw new TranslationException(message);
66  		}
67  		List<Parameter> parameters = f.getParameters();
68  
69  		// Check number of parameters
70  		if (parameters.size() != 3) {
71  			String message = String.format("Expected exactly 3 parameters when parsing %s", f);
72  			throw new TranslationException(message);
73  		}
74  
75  		// Check type of parameters
76  		for (Parameter parameter : parameters) {
77  			if (!(parameter instanceof Numeral)) {
78  				String message = String.format("All arguments of %s should be numerical.", f);
79  				throw new TranslationException(message);
80  			}
81  		}
82  
83  		// Transform to location.
84  		double[] c = new double[3];
85  		for (int i = 0; i < c.length; i++) {
86  			Parameter parameter = parameters.get(i);
87  			c[i] = ((Numeral) parameter).getValue().doubleValue();
88  		}
89  		return new Velocity(c[0], c[1], c[2]);
90  
91  	}
92  
93  	@Override
94  	public Class<Velocity> translatesTo() {
95  		return Velocity.class;
96  
97  	}
98  
99  }