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.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
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
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
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
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 }