Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Position2DSensor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
7 using UnityEngine;
8 
9 namespace droid.Runtime.Prototyping.Sensors.Transform {
13  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
14  + "PositionObserver2D"
15  + SensorComponentMenuPath._Postfix)]
16  [ExecuteInEditMode]
17  [Serializable]
18  public class Position2DSensor : Sensor,
19  IHasDouble {
20  [Header("Observation", order = 103)]
21  [SerializeField]
22  Vector2 _2_d_position;
23 
24  [SerializeField] [SearchableEnum] Dimension2DCombination _dim_combination = Dimension2DCombination.Xz_;
25 
26  [SerializeField] Space2 _position_space = Space2.ZeroOne;
27 
28  [Header("Specific", order = 102)]
29  [SerializeField]
30  ObservationSpace observation_space = ObservationSpace.Environment_;
31 
35  public Vector2 ObservationValue { get { return this._2_d_position; } set { this._2_d_position = value; } }
36 
40  public Space2 DoubleSpace { get { return this._position_space; } }
41 
46  public void SetPosition(Vector3 position) {
47  Vector2 vector2_pos;
48  switch (this._dim_combination) {
49  case Dimension2DCombination.Xy_:
50  vector2_pos = new Vector2(position.x, position.y);
51  break;
52  case Dimension2DCombination.Xz_:
53  vector2_pos = new Vector2(position.x, position.z);
54  break;
55  case Dimension2DCombination.Yz_:
56  vector2_pos = new Vector2(position.y, position.z);
57  break;
58  default: throw new ArgumentOutOfRangeException();
59  }
60 
61  this._2_d_position = this.DoubleSpace.IsNormalised
62  ? this._position_space.ClipNormaliseRound(vector2_pos)
63  : vector2_pos;
64  }
65 
69  public override IEnumerable<float> FloatEnumerable {
70  get { return new[] {this._2_d_position.x, this._2_d_position.y}; }
71  }
72 
76  public override void UpdateObservation() {
77  if (this.ParentEnvironment != null && this.observation_space == ObservationSpace.Environment_) {
78  this.SetPosition(this.ParentEnvironment.TransformPoint(this.transform.position));
79  } else if (this.observation_space == ObservationSpace.Local_) {
80  this.SetPosition(this.transform.localPosition);
81  } else {
82  this.SetPosition(this.transform.position);
83  }
84  }
85 
89  protected override void PreSetup() { }
90 
91  void OnDrawGizmos() {
92  if (this.enabled) {
93  var position = this.transform.position;
94  switch (this._dim_combination) {
95  case Dimension2DCombination.Xy_:
96  Debug.DrawLine(position, position + Vector3.right * 2, Color.green);
97  Debug.DrawLine(position, position + Vector3.up * 2, Color.red);
98  break;
99  case Dimension2DCombination.Xz_:
100  Debug.DrawLine(position, position + Vector3.right * 2, Color.green);
101  Debug.DrawLine(position, position + Vector3.forward * 2, Color.red);
102  break;
103  case Dimension2DCombination.Yz_:
104 
105  Debug.DrawLine(position, position + Vector3.up * 2, Color.green);
106  Debug.DrawLine(position, position + Vector3.forward * 2, Color.red);
107  break;
108  default: //TODO add the Direction cases
109  Gizmos.DrawIcon(position, "console.warnicon", true);
110  break;
111  }
112  }
113  }
114  }
115 }
Vector2 ClipNormaliseRound(Vector2 v)
Definition: Space2.cs:90