Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
TransformConfigurable1Dof.cs
Go to the documentation of this file.
1 using System;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Configurables {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "TransformConfigurable1Dof"
14  + ConfigurableComponentMenuPath._Postfix)]
16  IHasSingle {
20  public override string PrototypingTypeName {
21  get { return "Transform" + this._axis_of_configuration + "Configurable"; }
22  }
23 
24  public float ObservationValue {
25  get { return this._observation_value; }
26  private set { this._observation_value = value; }
27  }
28 
32  public Space1 SingleSpace {
33  get { return this._single_value_space; }
34  set { this._single_value_space = value; }
35  }
36 
41  public override void UpdateCurrentConfiguration() {
42  var transform1 = this.transform;
43  var pos = transform1.position;
44  var dir = transform1.forward;
45  var rot = transform1.up;
46  if (this._use_environments_space) {
47  if (this.ParentEnvironment != null) {
48  pos = this.ParentEnvironment.TransformPoint(pos);
49  dir = this.ParentEnvironment.TransformDirection(dir);
50  rot = this.ParentEnvironment.TransformDirection(rot);
51  } else {
52  Debug.LogWarning("ParentEnvironment not found!");
53  }
54  }
55 
56  switch (this._axis_of_configuration) {
57  case Axis.X_:
58  this._observation_value = pos.x;
59  break;
60  case Axis.Y_:
61  this._observation_value = pos.y;
62  break;
63  case Axis.Z_:
64  this._observation_value = pos.z;
65  break;
66  case Axis.Dir_x_:
67  this._observation_value = dir.x;
68  break;
69  case Axis.Dir_y_:
70  this._observation_value = dir.y;
71  break;
72  case Axis.Dir_z_:
73  this._observation_value = dir.z;
74  break;
75  case Axis.Rot_x_:
76  this._observation_value = rot.x;
77  break;
78  case Axis.Rot_y_:
79  this._observation_value = rot.y;
80  break;
81  case Axis.Rot_z_:
82  this._observation_value = rot.z;
83  break;
84  default:
85  throw new ArgumentOutOfRangeException();
86  }
87  }
88 
91  protected override void PreSetup() {
92  if (this._use_bounding_box_for_range) {
93  if (this._bounding_box != null) {
94  var valid_input = new Space1 {
95  _Max_Value =
96  Math.Min(this._bounding_box.Bounds.size.x,
97  Math.Min(this._bounding_box.Bounds.size.y,
98  this._bounding_box.Bounds.size.z))
99  };
100  valid_input._Min_Value = -valid_input._Max_Value;
101  this.SingleSpace = valid_input;
102  }
103  }
104  }
105 
106  public override ISpace ConfigurableValueSpace { get { return this.SingleSpace; } }
107 
113  public override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration) {
114  if (simulator_configuration.ConfigurableValue < this.SingleSpace._Min_Value
115  || simulator_configuration.ConfigurableValue > this.SingleSpace._Max_Value) {
116  Debug.Log($"It does not accept input, outside allowed range {this.SingleSpace._Min_Value} to {this.SingleSpace._Max_Value}");
117  return; // Do nothing
118  }
119 
120  var cv = this.SingleSpace.Round(simulator_configuration.ConfigurableValue);
121 
122  #if NEODROID_DEBUG
123  if (this.Debugging) {
124  Debug.Log("Applying " + simulator_configuration + " To " + this.Identifier);
125  }
126  #endif
127 
128  var transform1 = this.transform;
129  var pos = transform1.position;
130  var dir = transform1.forward;
131  var rot = transform1.up;
132  if (this._use_environments_space) {
133  if (this.ParentEnvironment != null) {
134  this.ParentEnvironment.TransformPoint(ref pos);
135  this.ParentEnvironment.TransformDirection(ref dir);
136  this.ParentEnvironment.TransformDirection(ref rot);
137  } else {
138  Debug.LogWarning("ParentEnvironment not found!");
139  }
140  }
141 
142  switch (this._axis_of_configuration) {
143  case Axis.X_:
144  if (this.RelativeToExistingValue) {
145  pos.Set(cv - pos.x, pos.y, pos.z);
146  } else {
147  pos.Set(cv, pos.y, pos.z);
148  }
149 
150  break;
151  case Axis.Y_:
152  if (this.RelativeToExistingValue) {
153  pos.Set(pos.x, cv - pos.y, pos.z);
154  } else {
155  pos.Set(pos.x, cv, pos.z);
156  }
157 
158  break;
159  case Axis.Z_:
160  if (this.RelativeToExistingValue) {
161  pos.Set(pos.x, pos.y, cv - pos.z);
162  } else {
163  pos.Set(pos.x, pos.y, cv);
164  }
165 
166  break;
167  case Axis.Dir_x_:
168  if (this.RelativeToExistingValue) {
169  dir.Set(cv - dir.x, dir.y, dir.z);
170  } else {
171  dir.Set(cv, dir.y, dir.z);
172  }
173 
174  break;
175  case Axis.Dir_y_:
176  if (this.RelativeToExistingValue) {
177  dir.Set(dir.x, cv - dir.y, dir.z);
178  } else {
179  dir.Set(dir.x, cv, dir.z);
180  }
181 
182  break;
183  case Axis.Dir_z_:
184  if (this.RelativeToExistingValue) {
185  dir.Set(dir.x, dir.y, cv - dir.z);
186  } else {
187  dir.Set(dir.x, dir.y, cv);
188  }
189 
190  break;
191  case Axis.Rot_x_:
192  if (this.RelativeToExistingValue) {
193  rot.Set(cv - rot.x, rot.y, rot.z);
194  } else {
195  rot.Set(cv, rot.y, rot.z);
196  }
197 
198  break;
199  case Axis.Rot_y_:
200  if (this.RelativeToExistingValue) {
201  rot.Set(rot.x, cv - rot.y, rot.z);
202  } else {
203  rot.Set(rot.x, cv, rot.z);
204  }
205 
206  break;
207  case Axis.Rot_z_:
208  if (this.RelativeToExistingValue) {
209  rot.Set(rot.x, rot.y, cv - rot.z);
210  } else {
211  rot.Set(rot.x, rot.y, cv);
212  }
213 
214  break;
215  default:
216  throw new ArgumentOutOfRangeException();
217  }
218 
219  var inv_pos = pos;
220  var inv_dir = dir;
221  var inv_rot = rot;
222  if (this._use_environments_space) {
223  if (this.ParentEnvironment != null) {
224  this.ParentEnvironment.InverseTransformPoint(ref inv_pos);
225  this.ParentEnvironment.InverseTransformDirection(ref inv_dir);
226  this.ParentEnvironment.InverseTransformDirection(ref inv_rot);
227  } else {
228  Debug.LogWarning("ParentEnvironment not found!");
229  }
230  }
231 
232  this.transform.position = inv_pos;
233  this.transform.rotation = Quaternion.identity;
234  this.transform.rotation = Quaternion.LookRotation(inv_dir, inv_rot);
235  }
236 
237  #region Fields
238 
239  [SerializeField] Axis _axis_of_configuration = Axis.X_;
240  [SerializeField] BoundingBox _bounding_box = null;
241  [SerializeField] bool _use_bounding_box_for_range = false;
242  [SerializeField] float _observation_value = 0;
243  [SerializeField] bool _use_environments_space = false;
244  [SerializeField] Space1 _single_value_space = Space1.ZeroOne;
245 
246  #endregion
247  }
248 }
override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration)