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