Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
EulerRotationConfigurable.cs
Go to the documentation of this file.
1 using System;
7 using UnityEngine;
8 
9 namespace droid.Runtime.Prototyping.Configurables {
13  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
14  + "EulerRotation"
15  + ConfigurableComponentMenuPath._Postfix)]
17  IHasTriple {
18  [Header("Observation", order = 103)]
19  [SerializeField]
20  Quaternion _euler_rotation = Quaternion.identity;
21 
22  [SerializeField] bool _use_environments_space = false;
23 
26  string _x;
27 
30  string _y;
31 
34  string _z;
35 
38  string _w;
39 
40  [SerializeField]
41  Space3 _euler_space =
42  new Space3(new DistributionSampler(), 2) {
43  _Min_Values = Vector3.zero,
44  _Max_Values = new Vector3(360f, 360f, 360f)
45  };
46 
47  public Space3 TripleSpace { get { return this._euler_space; } }
48 
52  protected override void PreSetup() {
53  this._x = this.Identifier + "X_";
54  this._y = this.Identifier + "Y_";
55  this._z = this.Identifier + "Z_";
56  this._w = this.Identifier + "W_";
57  }
58 
62  protected override void RegisterComponent() {
63  this.ParentEnvironment =
64  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this);
65  this.ParentEnvironment =
66  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._x);
67  this.ParentEnvironment =
68  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._y);
69  this.ParentEnvironment =
70  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._z);
71  this.ParentEnvironment =
72  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._w);
73  }
74 
78  protected override void UnRegisterComponent() {
79  if (this.ParentEnvironment == null) {
80  return;
81  }
82 
83  this.ParentEnvironment.UnRegister(this);
84  this.ParentEnvironment.UnRegister(this, this._x);
85  this.ParentEnvironment.UnRegister(this, this._y);
86  this.ParentEnvironment.UnRegister(this, this._z);
87  this.ParentEnvironment.UnRegister(this, this._w);
88  }
89 
90  public override ISpace ConfigurableValueSpace { get; }
91 
95  public override void UpdateCurrentConfiguration() {
96  if (this._use_environments_space && this.ParentEnvironment != null) {
97  this._euler_rotation = this.ParentEnvironment.TransformRotation(this.transform.rotation);
98  } else {
99  this._euler_rotation = this.transform.rotation;
100  }
101  }
102 
103  public override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration) {
104  var rot = this.transform.rotation;
105  if (this._use_environments_space) {
106  rot = this.ParentEnvironment.TransformRotation(this.transform.rotation);
107  }
108 
109  var v = simulator_configuration.ConfigurableValue;
110  if (this.TripleSpace.DecimalGranularity >= 0) {
111  v = (int)Math.Round(v, this.TripleSpace.DecimalGranularity);
112  }
113 
114  if (this.TripleSpace._Min_Values[0].CompareTo(this.TripleSpace._Max_Values[0]) != 0) {
115  #if NEODROID_DEBUG
116  //TODO NOT IMPLEMENTED CORRECTLY VelocitySpace should not be index but should check all pairwise values, TripleSpace._Min_Values == TripleSpace._Max_Values
117  if (v < this.TripleSpace._Min_Values[0] || v > this.TripleSpace._Max_Values[0]) {
118  Debug.Log($"Configurable does not accept input {v}, outside allowed range {this.TripleSpace._Min_Values[0]} to {this.TripleSpace._Max_Values[0]}");
119  return; // Do nothing
120  }
121  #endif
122  }
123 
124  #if NEODROID_DEBUG
125  if (this.Debugging) {
126  Debug.Log($"Applying {v} to {simulator_configuration.ConfigurableName} configurable");
127  }
128  #endif
129  var rote = rot.eulerAngles;
130 
131  if (this.RelativeToExistingValue) {
132  if (simulator_configuration.ConfigurableName == this._x) {
133  rot.eulerAngles = new Vector3(v - rote.x, rote.y, rote.z);
134  } else if (simulator_configuration.ConfigurableName == this._y) {
135  rot.eulerAngles = new Vector3(rote.x, v - rote.y, rote.z);
136  } else if (simulator_configuration.ConfigurableName == this._z) {
137  rot.eulerAngles = new Vector3(rote.x, rote.y, v - rote.z);
138  }
139  } else {
140  if (simulator_configuration.ConfigurableName == this._x) {
141  rot.eulerAngles = new Vector3(v, rote.y, rote.z);
142  } else if (simulator_configuration.ConfigurableName == this._y) {
143  rot.eulerAngles = new Vector3(rote.x, v, rote.z);
144  } else if (simulator_configuration.ConfigurableName == this._z) {
145  rot.eulerAngles = new Vector3(rote.x, rote.y, v);
146  }
147  }
148 
149  if (this._use_environments_space) {
150  rot = this.ParentEnvironment.InverseTransformRotation(rot);
151  }
152 
153  this.transform.rotation = rot;
154  }
155 
160  public override Configuration[] SampleConfigurations() {
161  var sample = this.TripleSpace.Sample();
162 
163  return new[] {
164  new Configuration(this._x, sample.x),
165  new Configuration(this._y, sample.y),
166  new Configuration(this._z, sample.z)
167  };
168  }
169 
170  Vector3 IHasTriple.ObservationValue { get { return this._euler_rotation.eulerAngles; } }
171  }
172 }
override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration)