Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ScreenSpaceBoundingBoxSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Sensors.BoundingBox {
10  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
11  + "Experimental/ScreenSpaceBoundingBox"
12  + SensorComponentMenuPath._Postfix)]
13  [ExecuteInEditMode]
14  //[ExecuteAlways]
15  [RequireComponent(typeof(Utilities.GameObjects.BoundingBoxes.BoundingBox))]
17  IHasString {
21  public override string PrototypingTypeName { get { return "BoundingBox"; } }
22 
23  Utilities.GameObjects.BoundingBoxes.BoundingBox _bounding_box = null;
24  [SerializeField] UnityEngine.Camera _camera = null;
25  [SerializeField] Rect _out_rect = new Rect();
26 
30  protected override void PreSetup() {
31  this._bounding_box = this.GetComponent<Utilities.GameObjects.BoundingBoxes.BoundingBox>();
32  }
33 
34  [SerializeField] bool NormaliseObservation = true;
35  public override IEnumerable<float> FloatEnumerable { get { return new List<float>(); } }
36 
40  public override void UpdateObservation() {
41  if (this._camera) {
42  var rect = this._bounding_box.ScreenSpaceBoundingRect(this._camera);
43 
44  if (this.NormaliseObservation) {
45  float w;
46  float h;
47  var target = this._camera.targetTexture;
48 
49  if (target) {
50  w = target.width;
51  h = target.height;
52  } else {
53  var r = this._camera.pixelRect;
54  w = r.width;
55  h = r.height;
56  }
57 
58  this._out_rect = rect.Normalise(w, h);
59  } else {
60  this._out_rect = rect;
61  }
62  }
63 
64  var str_rep =
65  $"{{\"x\":{this._out_rect.x},\n\"y\":{this._out_rect.y},\n\"w\":{this._out_rect.width},\n\"h\":{this._out_rect.height}}}";
66 
67  this.ObservationValue = str_rep;
68  }
69 
73  public string ObservationValue { get; set; }
74 
75  public override string ToString() { return this.ObservationValue; }
76  }
77 }