Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RotationConfigurable.cs
Go to the documentation of this file.
1 using System;
6 using UnityEngine;
7 using Random = UnityEngine.Random;
8 
9 namespace droid.Runtime.Prototyping.Configurables {
13  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
14  + "Rotation"
15  + ConfigurableComponentMenuPath._Postfix)]
18  [SerializeField] Space4 _quad_space = Space4.MinusOneOne;
19  [SerializeField] bool _use_environments_space = false;
20 
21  [Header("Observation", order = 103)]
22  [SerializeField]
23  Quaternion observation_value = Quaternion.identity;
24 
27  string _x;
28 
31  string _y;
32 
35  string _z;
36 
39  string _w;
40 
44  public Quaternion ObservationValue { get { return this.observation_value; } }
45 
49  public Space4 QuadSpace { get { return this._quad_space; } }
50 
54  protected override void PreSetup() {
55  this._x = this.Identifier + "X_";
56  this._y = this.Identifier + "Y_";
57  this._z = this.Identifier + "Z_";
58  this._w = this.Identifier + "W_";
59  }
60 
64  protected override void RegisterComponent() {
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, this._x);
84  this.ParentEnvironment.UnRegister(this, this._y);
85  this.ParentEnvironment.UnRegister(this, this._z);
86  this.ParentEnvironment.UnRegister(this, this._w);
87  }
88 
89  public override ISpace ConfigurableValueSpace { get; }
90 
94  public override void UpdateCurrentConfiguration() {
95  if (this._use_environments_space && this.ParentEnvironment != null) {
96  this.observation_value = this.ParentEnvironment.TransformRotation(this.transform.rotation);
97  } else {
98  this.observation_value = this.transform.rotation;
99  }
100  }
101 
102  public override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration) {
103  var rot = this.transform.rotation;
104  if (this.ParentEnvironment && this._use_environments_space) {
105  rot = this.ParentEnvironment.TransformRotation(this.transform.rotation);
106  }
107 
108  var v = simulator_configuration.ConfigurableValue;
109  if (this.QuadSpace.DecimalGranularity >= 0) {
110  v = (int)Math.Round(v, this.QuadSpace.DecimalGranularity);
111  }
112 
113  #if NEODROID_DEBUG
114  if (this.Debugging) {
115  Debug.Log($"Applying {v} to {simulator_configuration.ConfigurableName} configurable");
116  }
117  #endif
118 
119  if (this.RelativeToExistingValue) {
120  if (simulator_configuration.ConfigurableName == this._x) {
121  if (this.QuadSpace._Min_Values.x.CompareTo(this.QuadSpace._Max_Values.x) != 0) {
122  if (v < this.QuadSpace._Min_Values.x || v > this.QuadSpace._Max_Values.x) {
123  Debug.Log($"ConfigurableX does not accept input {v}, outside allowed range "
124  + $"{this.QuadSpace._Min_Values.x} to {this.QuadSpace._Max_Values.x}");
125  return; // Do nothing
126  }
127  }
128 
129  rot.Set(rot.x - v, rot.y, rot.z, rot.w);
130  } else if (simulator_configuration.ConfigurableName == this._y) {
131  if (this.QuadSpace._Min_Values.y.CompareTo(this.QuadSpace._Max_Values.y) != 0) {
132  if (v < this.QuadSpace._Min_Values.y || v > this.QuadSpace._Max_Values.y) {
133  Debug.Log($"ConfigurableY does not accept input {v}, outside allowed range "
134  + $"{this.QuadSpace._Min_Values.y} to {this.QuadSpace._Max_Values.y}");
135  return; // Do nothing
136  }
137  }
138 
139  rot.Set(rot.x, rot.y - v, rot.z, rot.w);
140  } else if (simulator_configuration.ConfigurableName == this._z) {
141  if (this.QuadSpace._Min_Values.z.CompareTo(this.QuadSpace._Max_Values.z) != 0) {
142  if (v < this.QuadSpace._Min_Values.z || v > this.QuadSpace._Max_Values.z) {
143  Debug.Log($"ConfigurableZ does not accept input {v}, outside allowed range "
144  + $"{this.QuadSpace._Min_Values.z} to {this.QuadSpace._Max_Values.z}");
145  return; // Do nothing
146  }
147  }
148 
149  rot.Set(rot.x, rot.y, rot.z - v, rot.w);
150  } else if (simulator_configuration.ConfigurableName == this._w) {
151  if (this.QuadSpace._Min_Values.w.CompareTo(this.QuadSpace._Max_Values.w) != 0) {
152  if (v < this.QuadSpace._Min_Values.w || v > this.QuadSpace._Max_Values.w) {
153  Debug.Log($"ConfigurableW does not accept input {v}, outside allowed range "
154  + $"{this.QuadSpace._Min_Values.w} to {this.QuadSpace._Max_Values.w}");
155  return; // Do nothing
156  }
157  }
158 
159  rot.Set(rot.x, rot.y, rot.z, rot.w - v);
160  }
161  } else {
162  if (simulator_configuration.ConfigurableName == this._x) {
163  if (this.QuadSpace._Min_Values.x.CompareTo(this.QuadSpace._Max_Values.x) != 0) {
164  if (v < this.QuadSpace._Min_Values.x || v > this.QuadSpace._Max_Values.x) {
165  Debug.Log($"ConfigurableX does not accept input {v}, outside allowed range "
166  + $"{this.QuadSpace._Min_Values.x} to {this.QuadSpace._Max_Values.x}");
167  return; // Do nothing
168  }
169  }
170 
171  rot.Set(v, rot.y, rot.z, rot.w);
172  //rot.x = v;
173  } else if (simulator_configuration.ConfigurableName == this._y) {
174  if (this.QuadSpace._Min_Values.y.CompareTo(this.QuadSpace._Max_Values.y) != 0) {
175  if (v < this.QuadSpace._Min_Values.y || v > this.QuadSpace._Max_Values.y) {
176  Debug.Log($"ConfigurableY does not accept input {v}, outside allowed range "
177  + $"{this.QuadSpace._Min_Values.y} to {this.QuadSpace._Max_Values.y}");
178  return; // Do nothing
179  }
180  }
181 
182  rot.Set(rot.x, v, rot.z, rot.w);
183  //rot.y = v;
184  } else if (simulator_configuration.ConfigurableName == this._z) {
185  if (this.QuadSpace._Min_Values.z.CompareTo(this.QuadSpace._Max_Values.z) != 0) {
186  if (v < this.QuadSpace._Min_Values.z || v > this.QuadSpace._Max_Values.z) {
187  Debug.Log($"ConfigurableZ does not accept input {v}, outside allowed range "
188  + $"{this.QuadSpace._Min_Values.z} to {this.QuadSpace._Max_Values.z}");
189  return; // Do nothing
190  }
191  }
192 
193  rot.Set(rot.x, rot.y, v, rot.w);
194  //rot.z = v;
195  } else if (simulator_configuration.ConfigurableName == this._w) {
196  if (this.QuadSpace._Min_Values.w.CompareTo(this.QuadSpace._Max_Values.w) != 0) {
197  if (v < this.QuadSpace._Min_Values.w || v > this.QuadSpace._Max_Values.w) {
198  Debug.Log($"ConfigurableW does not accept input {v}, outside allowed range "
199  + $"{this.QuadSpace._Min_Values.w} to {this.QuadSpace._Max_Values.w}");
200  return; // Do nothing
201  }
202  }
203 
204  rot.Set(rot.x, rot.y, rot.z, v);
205  //rot.w = v;
206  }
207  }
208 
209  if (this.ParentEnvironment && this._use_environments_space) {
210  rot = this.ParentEnvironment.InverseTransformRotation(rot);
211  }
212 
213  this.transform.rotation = rot;
214  }
215 
220  public override Configuration[] SampleConfigurations() {
221  var sample = this.QuadSpace.Sample();
222 
223  var r = Random.Range(0, 4);
224  switch (r) {
225  case 0:
226  return new[] {new Configuration(this._x, sample.x)};
227 
228  case 1:
229  return new[] {new Configuration(this._y, sample.y)};
230 
231  case 2:
232  return new[] {new Configuration(this._z, sample.z)};
233 
234  case 3:
235  return new[] {new Configuration(this._w, sample.w)};
236 
237  default:
238  throw new IndexOutOfRangeException();
239  }
240  }
241  }
242 }
override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration)