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