Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
EulerTransformConfigurable.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  + "EulerTransform"
14  + ConfigurableComponentMenuPath._Postfix)]
17  string _dir_x;
18  string _dir_y;
19  string _dir_z;
20 
21  string _x;
22  string _y;
23  string _z;
24 
25  string _rot_x;
26  string _rot_y;
27  string _rot_z;
28 
29  [Header("Observation", order = 103)]
30  [SerializeField]
31  Vector3 _position;
32 
33  [SerializeField] Vector3 _direction;
34  [SerializeField] Vector3 _rotation;
35  [SerializeField] bool _use_environments_space = false;
36 
40  public override string PrototypingTypeName { get { return "EulerTransformConfigurable"; } }
41 
44  public Vector3 Position { get { return this._position; } set { this._position = value; } }
45 
46  public Vector3 Direction { get { return this._direction; } set { this._direction = value; } }
47 
48  public Vector3 Rotation { get { return this._rotation; } set { this._rotation = value; } }
49 
50  public Space3 PositionSpace { get; } = new Space3();
51  public Space3 DirectionSpace { get; } = new Space3();
52  public Space3 RotationSpace { get; } = new Space3();
53 
54  public override ISpace ConfigurableValueSpace { get; }
55 
58  public override void UpdateCurrentConfiguration() {
59  if (this._use_environments_space) {
60  this.Position = this.ParentEnvironment.TransformPoint(this.transform.position);
61  this.Direction = this.ParentEnvironment.TransformDirection(this.transform.forward);
62  this.Rotation = this.ParentEnvironment.TransformDirection(this.transform.up);
63  } else {
64  var transform1 = this.transform;
65  this.Position = transform1.position;
66  this.Direction = transform1.forward;
67  this.Rotation = transform1.up;
68  }
69  }
70 
73  protected override void RegisterComponent() {
74  this.ParentEnvironment =
75  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this);
76  this.ParentEnvironment =
77  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._x);
78  this.ParentEnvironment =
79  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._y);
80  this.ParentEnvironment =
81  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._z);
82  this.ParentEnvironment =
83  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._dir_x);
84  this.ParentEnvironment =
85  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._dir_y);
86  this.ParentEnvironment =
87  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._dir_z);
88  this.ParentEnvironment =
89  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rot_x);
90  this.ParentEnvironment =
91  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rot_y);
92  this.ParentEnvironment =
93  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rot_z);
94  }
95 
99  protected override void PreSetup() {
100  this._x = this.Identifier + "X_";
101  this._y = this.Identifier + "Y_";
102  this._z = this.Identifier + "Z_";
103  this._dir_x = this.Identifier + "DirX_";
104  this._dir_y = this.Identifier + "DirY_";
105  this._dir_z = this.Identifier + "DirZ_";
106  this._rot_x = this.Identifier + "RotX_";
107  this._rot_y = this.Identifier + "RotY_";
108  this._rot_z = this.Identifier + "RotZ_";
109  }
110 
114  protected override void UnRegisterComponent() {
115  if (this.ParentEnvironment == null) {
116  return;
117  }
118 
119  this.ParentEnvironment.UnRegister(this, this._x);
120  this.ParentEnvironment.UnRegister(this, this._y);
121  this.ParentEnvironment.UnRegister(this, this._z);
122  this.ParentEnvironment.UnRegister(this, this._dir_x);
123  this.ParentEnvironment.UnRegister(this, this._dir_y);
124  this.ParentEnvironment.UnRegister(this, this._dir_z);
125  this.ParentEnvironment.UnRegister(this, this._rot_x);
126  this.ParentEnvironment.UnRegister(this, this._rot_y);
127  this.ParentEnvironment.UnRegister(this, this._rot_z);
128  }
129 
134  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
135  var transform1 = this.transform;
136  var pos = transform1.position;
137  var dir = transform1.forward;
138  var rot = transform1.up;
139  if (this._use_environments_space) {
140  pos = this.ParentEnvironment.TransformPoint(pos);
141  dir = this.ParentEnvironment.TransformDirection(dir);
142  rot = this.ParentEnvironment.TransformDirection(rot);
143  }
144 
145  var v = configuration.ConfigurableValue;
146  if (this.PositionSpace.DecimalGranularity >= 0) {
147  v = (int)Math.Round(v, this.PositionSpace.DecimalGranularity);
148  }
149 
150  if (this.PositionSpace._Min_Values[0].CompareTo(this.PositionSpace._Max_Values[0]) != 0) {
151  //TODO NOT IMPLEMENTED CORRECTLY VelocitySpace should not be index but should check all pairwise values, PositionSpace._Min_Values == PositionSpace._Max_Values, and use other space aswell!
152  if (v < this.PositionSpace._Min_Values[0] || v > this.PositionSpace._Max_Values[0]) {
153  Debug.Log(string.Format("Configurable does not accept input{2}, outside allowed range {0} to {1}",
154  this.PositionSpace._Min_Values[0],
155  this.PositionSpace._Max_Values[0],
156  v));
157  return; // Do nothing
158  }
159  }
160 
161  #if NEODROID_DEBUG
162  if (this.Debugging) {
163  Debug.Log("Applying " + v + " To " + this.Identifier);
164  }
165  #endif
166  if (this.RelativeToExistingValue) {
167  if (configuration.ConfigurableName == this._x) {
168  pos.Set(v - pos.x, pos.y, pos.z);
169  } else if (configuration.ConfigurableName == this._y) {
170  pos.Set(pos.x, v - pos.y, pos.z);
171  } else if (configuration.ConfigurableName == this._z) {
172  pos.Set(pos.x, pos.y, v - pos.z);
173  } else if (configuration.ConfigurableName == this._dir_x) {
174  dir.Set(v - dir.x, dir.y, dir.z);
175  } else if (configuration.ConfigurableName == this._dir_y) {
176  dir.Set(dir.x, v - dir.y, dir.z);
177  } else if (configuration.ConfigurableName == this._dir_z) {
178  dir.Set(dir.x, dir.y, v - dir.z);
179  } else if (configuration.ConfigurableName == this._rot_x) {
180  rot.Set(v - rot.x, rot.y, rot.z);
181  } else if (configuration.ConfigurableName == this._rot_y) {
182  rot.Set(rot.x, v - rot.y, rot.z);
183  } else if (configuration.ConfigurableName == this._rot_z) {
184  rot.Set(rot.x, rot.y, v - rot.z);
185  }
186  } else {
187  if (configuration.ConfigurableName == this._x) {
188  pos.Set(v, pos.y, pos.z);
189  } else if (configuration.ConfigurableName == this._y) {
190  pos.Set(pos.x, v, pos.z);
191  } else if (configuration.ConfigurableName == this._z) {
192  pos.Set(pos.x, pos.y, v);
193  } else if (configuration.ConfigurableName == this._dir_x) {
194  dir.Set(v, dir.y, dir.z);
195  } else if (configuration.ConfigurableName == this._dir_y) {
196  dir.Set(dir.x, v, dir.z);
197  } else if (configuration.ConfigurableName == this._dir_z) {
198  dir.Set(dir.x, dir.y, v);
199  } else if (configuration.ConfigurableName == this._rot_x) {
200  rot.Set(v, rot.y, rot.z);
201  } else if (configuration.ConfigurableName == this._rot_y) {
202  rot.Set(rot.x, v, rot.z);
203  } else if (configuration.ConfigurableName == this._rot_z) {
204  rot.Set(rot.x, rot.y, v);
205  }
206  }
207 
208  var inv_pos = pos;
209  var inv_dir = dir;
210  var inv_rot = rot;
211  if (this._use_environments_space) {
212  inv_pos = this.ParentEnvironment.InverseTransformPoint(pos);
213  inv_dir = this.ParentEnvironment.InverseTransformDirection(dir);
214  inv_rot = this.ParentEnvironment.InverseTransformDirection(rot);
215  }
216 
217  this.transform.position = inv_pos;
218  this.transform.rotation = Quaternion.identity;
219  this.transform.rotation = Quaternion.LookRotation(inv_dir, inv_rot);
220  }
221 
226  public override Configuration[] SampleConfigurations() {
227  return new[] {new Configuration(this._rot_x, Space1.ZeroOne.Sample())};
228  }
229  }
230 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)