Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
NearestByTagSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Sensors.Rays {
11  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
12  + "NearestByTag"
13  + SensorComponentMenuPath._Postfix)]
14  public class NearestByTagSensor : Sensor,
16  [SerializeField] Vector3 _direction;
17  [SerializeField] Space3 _direction_space = new Space3(new DistributionSampler(), 10);
18 
19  [Header("Specific", order = 102)]
20  [SerializeField]
21  GameObject _nearest_object;
22 
23  [Header("Observation", order = 103)]
24  [SerializeField]
25  Vector3 _position;
26 
27  [SerializeField] Space3 _position_space = new Space3(new DistributionSampler(), 10);
28  [SerializeField] Vector3 _rotation;
29  [SerializeField] Space3 _rotation_space = new Space3(new DistributionSampler(), 10);
30  [SerializeField] string _tag = "";
31 
35  public override string PrototypingTypeName { get { return "Nearest" + this._tag; } }
36 
40  public Vector3 Position {
41  get { return this._position; }
42  set {
43  this._position = this._position_space.IsNormalised
44  ? this._position_space.ClipNormaliseRound(value)
45  : value;
46  }
47  }
48 
49  public Vector3 Rotation {
50  get { return this._rotation; }
51  set {
52  this._rotation = this._rotation_space.IsNormalised
53  ? this._rotation_space.ClipNormaliseRound(value)
54  : value;
55  }
56  }
57 
58  public Space3 PositionSpace { get { return this._position_space; } }
59  public Space3 DirectionSpace { get { return this._direction_space; } }
60  public Space3 RotationSpace { get { return this._rotation_space; } }
61 
62  public Vector3 Direction {
63  get { return this._direction; }
64  set {
65  this._direction = this._direction_space.IsNormalised
66  ? this._direction_space.ClipNormaliseRound(value)
67  : value;
68  }
69  }
70 
71  public override IEnumerable<float> FloatEnumerable {
72  get {
73  return new[] {
74  this.Position.x,
75  this.Position.y,
76  this.Position.z,
77  this.Direction.x,
78  this.Direction.y,
79  this.Direction.z,
80  this.Rotation.x,
81  this.Rotation.y,
82  this.Rotation.z
83  };
84  }
85  }
86 
87  public override void UpdateObservation() {
88  this._nearest_object = this.FindNearest();
89 
90  if (this.ParentEnvironment != null) {
91  this.Position = this.ParentEnvironment.TransformPoint(this._nearest_object.transform.position);
92  this.Direction = this.ParentEnvironment.TransformDirection(this._nearest_object.transform.forward);
93  this.Rotation = this.ParentEnvironment.TransformDirection(this._nearest_object.transform.up);
94  } else {
95  this.Position = this._nearest_object.transform.position;
96  this.Direction = this._nearest_object.transform.forward;
97  this.Rotation = this._nearest_object.transform.up;
98  }
99  }
100 
101  protected override void PreSetup() { }
102 
103  GameObject FindNearest() {
104  var candidates = FindObjectsOfType<GameObject>();
105  var nearest_object = this.gameObject;
106  var nearest_distance = -1.0;
107  foreach (var candidate in candidates) {
108  if (candidate.CompareTag(this._tag)) {
109  var dist = Vector3.Distance(this.transform.position, candidate.transform.position);
110  if (nearest_distance > dist || nearest_distance < 0) {
111  nearest_distance = dist;
112  nearest_object = candidate;
113  }
114  }
115  }
116 
117  return nearest_object;
118  }
119  }
120 }
Vector3 ClipNormaliseRound(Vector3 v)
Definition: Space3.cs:55