Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PositionConfigurable.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  + "Position"
15  + ConfigurableComponentMenuPath._Postfix)]
17  IHasTriple {
18  [Header("Observation", order = 103)]
19  [SerializeField]
20  Vector3 _position = Vector3.zero;
21 
22  [SerializeField] bool _use_environments_space = false;
23 
26  string _x;
27 
30  string _y;
31 
34  string _z;
35 
36  [SerializeField] Space3 _triple_space = Space3.ZeroOne;
37 
41  public Vector3 ObservationValue { get { return this._position; } }
42 
46  public Space3 TripleSpace { get { return this._triple_space; } }
47 
51  protected override void PreSetup() {
52  this._x = this.Identifier + "X_";
53  this._y = this.Identifier + "Y_";
54  this._z = this.Identifier + "Z_";
55  }
56 
60  protected override void RegisterComponent() {
61  this.ParentEnvironment =
62  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this);
63  this.ParentEnvironment =
64  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._x);
65  this.ParentEnvironment =
66  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._y);
67  this.ParentEnvironment =
68  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._z);
69  }
70 
74  protected override void UnRegisterComponent() {
75  if (this.ParentEnvironment == null) {
76  return;
77  }
78 
79  this.ParentEnvironment.UnRegister(this);
80  this.ParentEnvironment.UnRegister(this, this._x);
81  this.ParentEnvironment.UnRegister(this, this._y);
82  this.ParentEnvironment.UnRegister(this, this._z);
83  }
84 
85  public override ISpace ConfigurableValueSpace { get; }
86 
90  public override void UpdateCurrentConfiguration() {
91  if (this._use_environments_space) {
92  this._position = this.ParentEnvironment.TransformPoint(this.transform.position);
93  } else {
94  this._position = this.transform.position;
95  }
96  }
97 
102  public override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration) {
103  var pos = this.transform.position;
104  if (this._use_environments_space) {
105  pos = this.ParentEnvironment.TransformPoint(this.transform.position);
106  }
107 
108  var v = simulator_configuration.ConfigurableValue;
109  if (this.TripleSpace.DecimalGranularity >= 0) {
110  v = (int)Math.Round(v, this.TripleSpace.DecimalGranularity);
111  }
112 
113  if (this.TripleSpace._Min_Values[0].CompareTo(this.TripleSpace._Max_Values[0]) != 0) {
114  //TODO NOT IMPLEMENTED CORRECTLY VelocitySpace should not be index but should check all pairwise values, TripleSpace._Min_Values == TripleSpace._Max_Values
115  if (v < this.TripleSpace._Min_Values[0] || v > this.TripleSpace._Max_Values[0]) {
116  Debug.Log($"Configurable does not accept input{v}, outside allowed range {this.TripleSpace._Min_Values[0]} to {this.TripleSpace._Max_Values[0]}");
117  return; // Do nothing
118  }
119  }
120 
121  #if NEODROID_DEBUG
122  if (this.Debugging) {
123  Debug.Log($"Applying {v} to {simulator_configuration.ConfigurableName} configurable");
124  }
125  #endif
126 
127  if (this.RelativeToExistingValue) {
128  if (simulator_configuration.ConfigurableName == this._x) {
129  pos.Set(v - pos.x, pos.y, pos.z);
130  } else if (simulator_configuration.ConfigurableName == this._y) {
131  pos.Set(pos.x, v - pos.y, pos.z);
132  } else if (simulator_configuration.ConfigurableName == this._z) {
133  pos.Set(pos.x, pos.y, v - pos.z);
134  }
135  } else {
136  if (simulator_configuration.ConfigurableName == this._x) {
137  pos.Set(v, pos.y, pos.z);
138  } else if (simulator_configuration.ConfigurableName == this._y) {
139  pos.Set(pos.x, v, pos.z);
140  } else if (simulator_configuration.ConfigurableName == this._z) {
141  pos.Set(pos.x, pos.y, v);
142  }
143  }
144 
145  var inv_pos = pos;
146  if (this._use_environments_space) {
147  inv_pos = this.ParentEnvironment.InverseTransformPoint(inv_pos);
148  }
149 
150  this.transform.position = inv_pos;
151  }
152 
153  public override Configuration[] SampleConfigurations() {
154  var sample = this.TripleSpace.Sample();
155  var r = Random.Range(0, 3);
156  switch (r) {
157  case 0:
158  return new[] {new Configuration(this._x, sample.x)};
159 
160  case 1:
161  return new[] {new Configuration(this._y, sample.y)};
162 
163  case 2:
164  return new[] {new Configuration(this._z, sample.z)};
165 
166  default:
167  throw new IndexOutOfRangeException();
168  }
169  }
170  }
171 }
override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration)