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.eis2java.exception.TranslationException;
27  import eis.eis2java.translation.Java2Parameter;
28  import eis.eis2java.translation.Parameter2Java;
29  import eis.iilang.Function;
30  import eis.iilang.Numeral;
31  import eis.iilang.Parameter;
32  
33  
34  public class VelocityTranslator implements Java2Parameter<Velocity>, Parameter2Java<Velocity> {
35  
36  	public static final String VELOCITY_KEYWORD = "velocity";
37  
38  	@Override
39  	public Parameter[] translate(Velocity o) throws TranslationException {
40  		return new Parameter[] { new Function(VELOCITY_KEYWORD,
41  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getX())),
42  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getY())),
43  				new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getZ())) )
44  			};
45  	}
46  
47  	@Override
48  	public Class<? extends Velocity> translatesFrom() {
49  		return Velocity.class;
50  	}
51  
52  	@Override
53  	public Velocity translate(Parameter p) throws TranslationException {
54  
55  		if (!(p instanceof Function)) {
56  			String message = String.format("A velocity must be a function, received: %s.", p);
57  			throw new TranslationException(message);
58  		}
59  
60  		Function f = (Function) p;
61  
62  		// Check function name
63  		if (!f.getName().equals(VELOCITY_KEYWORD)) {
64  			String message = String.format("A velocity needs to start with %s, not: %s in %s.", VELOCITY_KEYWORD,
65  					f.getName(), f);
66  			throw new TranslationException(message);
67  		}
68  		List<Parameter> parameters = f.getParameters();
69  
70  		// Check number of parameters
71  		if (parameters.size() != 3) {
72  			String message = String.format("Expected exactly 3 parameters when parsing %s", f);
73  			throw new TranslationException(message);
74  		}
75  
76  		// Check type of parameters
77  		for (Parameter parameter : parameters) {
78  			if (!(parameter instanceof Numeral)) {
79  				String message = String.format("All arguments of %s should be numerical.", f);
80  				throw new TranslationException(message);
81  			}
82  		}
83  
84  		// Transform to location.
85  		double[] c = new double[3];
86  		for (int i = 0; i < c.length; i++) {
87  			Parameter parameter = parameters.get(i);
88  			c[i] = ((Numeral) parameter).getValue().doubleValue();
89  		}
90  		return new Velocity(c[0], c[1], c[2]);
91  
92  	}
93  
94  	@Override
95  	public Class<Velocity> translatesTo() {
96  		return Velocity.class;
97  
98  	}
99  
100 }