1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
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  		
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  		
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  		
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  		
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  }