Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
InstantUniformLidarSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Sensors.Rays {
10  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
11  + "InstantUniformLidar"
12  + SensorComponentMenuPath._Postfix)]
15  [SerializeField] RaycastHit _hit;
16 
17  [SerializeField] bool _is_2_d = false;
18 
19  [Header("Observation", order = 103)]
20  [SerializeField]
21  float[] _obs_array;
22 
23  [SerializeField] Space1 _space = new Space1 {_Min_Value = 0, _Max_Value = 5f};
24 
27  public override string PrototypingTypeName { get { return "Adjacency"; } }
28 
31  public RaycastHit Hit { get { return this._hit; } set { this._hit = value; } }
32 
33  public override IEnumerable<float> FloatEnumerable { get { return this.ObservationArray; } }
34 
37  public float[] ObservationArray {
38  get { return this._obs_array; }
39  private set { this._obs_array = value; }
40  }
41 
42  public Space1[] ObservationSpace { get { return new[] {this._space}; } }
43 
46  protected override void PreSetup() {
47  if (this._is_2_d) {
48  this.ObservationArray = new float[8];
49  } else {
50  this.ObservationArray = new float[27];
51  }
52  }
53 
56  public override void UpdateObservation() {
57  if (this._is_2_d) {
58  var vals = new float[8];
59  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.forward,
60  Vector3.forward,
61  out this._hit,
62  this._space._Max_Value)) {
63  vals[0] = 1;
64  } else {
65  vals[0] = this._space._Min_Value;
66  }
67 
68  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.left,
69  Vector3.left,
70  out this._hit,
71  this._space._Max_Value)) {
72  vals[1] = this._space.ClipNormaliseRound(this._hit.distance);
73  } else {
74  vals[1] = this._space._Min_Value;
75  }
76 
77  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.right,
78  Vector3.right,
79  out this._hit,
80  this._space._Max_Value)) {
81  vals[2] = this._space.ClipNormaliseRound(this._hit.distance);
82  } else {
83  vals[2] = this._space._Min_Value;
84  }
85 
86  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.back,
87  Vector3.back,
88  out this._hit,
89  this._space._Max_Value)) {
90  vals[3] = this._space.ClipNormaliseRound(this._hit.distance);
91  } else {
92  vals[3] = this._space._Min_Value;
93  }
94 
95  if (Physics.Raycast(this.transform.position
96  + this._space._Min_Value * (Vector3.forward + Vector3.left).normalized,
97  (Vector3.forward + Vector3.left).normalized,
98  out this._hit,
99  this._space._Max_Value)) {
100  vals[4] = this._space.ClipNormaliseRound(this._hit.distance);
101  } else {
102  vals[4] = this._space._Min_Value;
103  }
104 
105  if (Physics.Raycast(this.transform.position
106  + this._space._Min_Value * (Vector3.forward + Vector3.right).normalized,
107  (Vector3.forward + Vector3.right).normalized,
108  out this._hit,
109  this._space._Max_Value)) {
110  vals[5] = this._space.ClipNormaliseRound(this._hit.distance);
111  } else {
112  vals[5] = this._space._Min_Value;
113  }
114 
115  if (Physics.Raycast(this.transform.position
116  + this._space._Min_Value * (Vector3.back + Vector3.left).normalized,
117  (Vector3.back + Vector3.left).normalized,
118  out this._hit,
119  this._space._Max_Value)) {
120  vals[6] = this._space.ClipNormaliseRound(this._hit.distance);
121  } else {
122  vals[6] = this._space._Min_Value;
123  }
124 
125  if (Physics.Raycast(this.transform.position
126  + this._space._Min_Value * (Vector3.back + Vector3.right).normalized,
127  (Vector3.back + Vector3.right).normalized,
128  out this._hit,
129  this._space._Max_Value)) {
130  vals[7] = this._space.ClipNormaliseRound(this._hit.distance);
131  } else {
132  vals[7] = this._space._Min_Value;
133  }
134 
135  this.ObservationArray = vals;
136  } else {
137  var vals = new float[27];
138  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.forward,
139  Vector3.forward,
140  out this._hit,
141  this._space._Max_Value)) {
142  vals[0] = 1;
143  } else {
144  vals[0] = this._space._Min_Value;
145  }
146 
147  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.left,
148  Vector3.left,
149  out this._hit,
150  this._space._Max_Value)) {
151  vals[1] = this._space.ClipNormaliseRound(this._hit.distance);
152  } else {
153  vals[1] = this._space._Min_Value;
154  }
155 
156  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.right,
157  Vector3.right,
158  out this._hit,
159  this._space._Max_Value)) {
160  vals[2] = this._space.ClipNormaliseRound(this._hit.distance);
161  } else {
162  vals[2] = this._space._Min_Value;
163  }
164 
165  if (Physics.Raycast(this.transform.position + this._space._Min_Value * Vector3.back,
166  Vector3.back,
167  out this._hit,
168  this._space._Max_Value)) {
169  vals[3] = this._space.ClipNormaliseRound(this._hit.distance);
170  } else {
171  vals[3] = this._space._Min_Value;
172  }
173 
174  if (Physics.Raycast(this.transform.position
175  + this._space._Min_Value * (Vector3.forward + Vector3.left).normalized,
176  (Vector3.forward + Vector3.left).normalized,
177  out this._hit,
178  this._space._Max_Value)) {
179  vals[4] = this._space.ClipNormaliseRound(this._hit.distance);
180  } else {
181  vals[4] = this._space._Min_Value;
182  }
183 
184  if (Physics.Raycast(this.transform.position
185  + this._space._Min_Value * (Vector3.forward + Vector3.right).normalized,
186  (Vector3.forward + Vector3.right).normalized,
187  out this._hit,
188  this._space._Max_Value)) {
189  vals[5] = this._space.ClipNormaliseRound(this._hit.distance);
190  } else {
191  vals[5] = this._space._Min_Value;
192  }
193 
194  if (Physics.Raycast(this.transform.position
195  + this._space._Min_Value * (Vector3.back + Vector3.left).normalized,
196  (Vector3.back + Vector3.left).normalized,
197  out this._hit,
198  this._space._Max_Value)) {
199  vals[6] = this._space.ClipNormaliseRound(this._hit.distance);
200  } else {
201  vals[6] = this._space._Min_Value;
202  }
203 
204  if (Physics.Raycast(this.transform.position
205  + this._space._Min_Value * (Vector3.back + Vector3.right).normalized,
206  (Vector3.back + Vector3.right).normalized,
207  out this._hit,
208  this._space._Max_Value)) {
209  vals[7] = this._space.ClipNormaliseRound(this._hit.distance);
210  } else {
211  vals[7] = this._space._Min_Value;
212  }
213 
214  //TODO:Missing combinations Vector3.down+Vector3.left...
215 
216  this.ObservationArray = vals;
217  }
218  }
219 
220  #if UNITY_EDITOR
221  [SerializeField] Color _color = Color.green;
222 
223  void OnDrawGizmosSelected() {
224  if (this.enabled) {
225  var position = this.transform.position;
226  Debug.DrawLine(position, position - Vector3.forward * this._space._Max_Value, this._color);
227  Debug.DrawLine(position, position - Vector3.left * this._space._Max_Value, this._color);
228  Debug.DrawLine(position, position - Vector3.right * this._space._Max_Value, this._color);
229  Debug.DrawLine(position, position - Vector3.back * this._space._Max_Value, this._color);
230  Debug.DrawLine(position,
231  position - (Vector3.forward + Vector3.left).normalized * this._space._Max_Value,
232  this._color);
233  Debug.DrawLine(position,
234  position - (Vector3.forward + Vector3.right).normalized * this._space._Max_Value,
235  this._color);
236  Debug.DrawLine(position,
237  position - (Vector3.back + Vector3.left).normalized * this._space._Max_Value,
238  this._color);
239  Debug.DrawLine(position,
240  position - (Vector3.back + Vector3.right).normalized * this._space._Max_Value,
241  this._color);
242  if (!this._is_2_d) {
243  var position1 = this.transform.position;
244  Debug.DrawLine(position1, position1 - Vector3.up * this._space._Max_Value, this._color);
245  Debug.DrawLine(position1, position1 - Vector3.down * this._space._Max_Value, this._color);
246 
247  Debug.DrawLine(position1,
248  position1 - (Vector3.up + Vector3.left).normalized * this._space._Max_Value,
249  this._color);
250  Debug.DrawLine(position1,
251  position1 - (Vector3.up + Vector3.right).normalized * this._space._Max_Value,
252  this._color);
253  Debug.DrawLine(position1,
254  position1 - (Vector3.up + Vector3.forward).normalized * this._space._Max_Value,
255  this._color);
256  Debug.DrawLine(position1,
257  position1 - (Vector3.up + Vector3.back).normalized * this._space._Max_Value,
258  this._color);
259 
260  Debug.DrawLine(position1,
261  position1 - (Vector3.down + Vector3.left).normalized * this._space._Max_Value,
262  this._color);
263 
264  Debug.DrawLine(position1,
265  position1 - (Vector3.down + Vector3.right).normalized * this._space._Max_Value,
266  this._color);
267 
268  Debug.DrawLine(position1,
269  position1 - (Vector3.down + Vector3.forward).normalized * this._space._Max_Value,
270  this._color);
271 
272  Debug.DrawLine(position1,
273  position1 - (Vector3.down + Vector3.back).normalized * this._space._Max_Value,
274  this._color);
275 
276  Debug.DrawLine(position1,
277  position1
278  - (Vector3.down + Vector3.forward + Vector3.left).normalized
279  * this._space._Max_Value,
280  this._color);
281 
282  Debug.DrawLine(position1,
283  position1
284  - (Vector3.down + Vector3.forward + Vector3.right).normalized
285  * this._space._Max_Value,
286  this._color);
287  Debug.DrawLine(position1,
288  position1
289  - (Vector3.down + Vector3.back + Vector3.left).normalized * this._space._Max_Value,
290  this._color);
291 
292  Debug.DrawLine(position1,
293  position1
294  - (Vector3.down + Vector3.back + Vector3.right).normalized * this._space._Max_Value,
295  this._color);
296 
297  Debug.DrawLine(position1,
298  position1
299  - (Vector3.up + Vector3.forward + Vector3.left).normalized * this._space._Max_Value,
300  this._color);
301 
302  Debug.DrawLine(position1,
303  position1
304  - (Vector3.up + Vector3.forward + Vector3.right).normalized * this._space._Max_Value,
305  this._color);
306  Debug.DrawLine(position1,
307  position1
308  - (Vector3.up + Vector3.back + Vector3.left).normalized * this._space._Max_Value,
309  this._color);
310 
311  Debug.DrawLine(position1,
312  position1
313  - (Vector3.up + Vector3.back + Vector3.right).normalized * this._space._Max_Value,
314  this._color);
315  }
316  }
317  }
318  #endif
319  }
320 }