Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
TextScatterPlotDisplayer.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Linq;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Displayers.ScatterPlots {
11  [ExecuteInEditMode]
12  [AddComponentMenu(DisplayerComponentMenuPath._ComponentMenuPath
13  + "ScatterPlot"
14  + DisplayerComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(ParticleSystem))]
17  [SerializeField] Gradient _gradient;
18  ParticleSystem _particle_system;
19 
20  ParticleSystem.MainModule _particle_system_main_module;
21  ParticleSystemRenderer _particle_system_renderer;
22 
23  [SerializeField]
24  ParticleSystemSimulationSpace _particle_system_simulation_space = ParticleSystemSimulationSpace.World;
25 
26  ParticleSystem.Particle[] _particles;
27  [SerializeField] float _size = 0.6f;
28 
29  List<string> _vs = new List<string>();
30 
31  protected override void Setup() {
32  this._particle_system = this.GetComponent<ParticleSystem>();
33  var em = this._particle_system.emission;
34  em.enabled = false;
35  em.rateOverTime = 0;
36  var sh = this._particle_system.shape;
37  sh.enabled = false;
38 
39  this._particle_system_main_module = this._particle_system.main;
40  this._particle_system_main_module.loop = false;
41  this._particle_system_main_module.playOnAwake = false;
42  this._particle_system_main_module.simulationSpace = this._particle_system_simulation_space;
43  this._particle_system_main_module.simulationSpeed = 0;
44  this._particle_system_main_module.startSize = this._size;
45 
46  this._particle_system_renderer = this.GetComponent<ParticleSystemRenderer>();
47  //this._particle_system_renderer.renderMode = ParticleSystemRenderMode.Mesh;
48  this._particle_system_renderer.alignment = ParticleSystemRenderSpace.World;
49 
50  if (this._gradient == null) {
51  this._gradient = new Gradient {
52  colorKeys = new[] {
53  new GradientColorKey(new Color(1, 0, 0), 0f),
54  new GradientColorKey(new Color(0, 1, 0), 1f)
55  }
56  };
57  }
58  }
59 
60  public override void Display(Double value) {
61  #if NEODROID_DEBUG
62  if (this.Debugging) {
63  Debug.Log("Applying the double " + value + " To " + this.name);
64  }
65  #endif
66 
67  this._Values = new[] {value.ToString(CultureInfo.InvariantCulture)};
68  this.PlotSeries(this._Values);
69  }
70 
71  public override void Display(float[] values) {
72  #if NEODROID_DEBUG
73  if (this.Debugging) {
74  var s = "";
75  foreach (var value in values) {
76  s += $"{value},";
77  }
78 
79  Debug.Log("Applying the float array " + s + " To " + this.name);
80  }
81  #endif
82  this._Values = values.Select(v => v.ToString(CultureInfo.InvariantCulture)).ToArray();
83  this.PlotSeries(values);
84  }
85 
86  public override void Display(String values) {
87  #if NEODROID_DEBUG
88  if (this.Debugging) {
89  Debug.Log("Applying the float array " + values + " To " + this.name);
90  }
91  #endif
92 
93  this._vs.Clear();
94  foreach (var value in values.Split(',')) {
95  this._vs.Add(value);
96  }
97 
98  this._Values = this._vs.ToArray();
99  this.PlotSeries(this._Values);
100  }
101 
102  public override void Display(Vector3 value) { throw new NotImplementedException(); }
103  public override void Display(Vector3[] value) { this.ScatterPlot(value); }
104 
105  public override void Display(Points.ValuePoint points) { this.PlotSeries(new[] {points}); }
106 
107  public override void Display(Points.ValuePoint[] points) {
108  if (this._particles == null || this._particles.Length != points.Length) {
109  this._particles = new ParticleSystem.Particle[points.Length];
110  }
111 
112  #if NEODROID_DEBUG
113  if (this.Debugging) {
114  var points_str = points.Aggregate("",
115  (current, point) =>
116  current
117  + $"({point._Pos.ToString()}, {point._Val},{point._Size})"
118  + ", ");
119  Debug.Log("Applying the points " + points_str + " to " + this.name);
120  }
121  #endif
122 
123  var i = 0;
124  foreach (var point in points) {
125  this._particles[i].remainingLifetime = 100000;
126  this._particles[i].position = point._Pos;
127  var clamped = Math.Min(Math.Max(0.0f, point._Val), 1.0f);
128  this._particles[i].startColor = this._gradient.Evaluate(clamped);
129  this._particles[i].startSize = point._Size;
130  i++;
131  }
132 
133  this._particle_system.SetParticles(this._particles, points.Length);
134  }
135 
136  public override void Display(Points.StringPoint point) { throw new NotImplementedException(); }
137  public override void Display(Points.StringPoint[] points) { throw new NotImplementedException(); }
138 
139  //public override void Display(Object o) { throw new NotImplementedException(); }
140 
141  public override void Display(float values) {
142  #if NEODROID_DEBUG
143  if (this.Debugging) {
144  Debug.Log("Applying the float " + values + " To " + this.name);
145  }
146  #endif
147 
148  this._Values = new[] {values.ToString(CultureInfo.InvariantCulture)};
149  this.PlotSeries(this._Values);
150  }
151 
155  public void ScatterPlot(Vector3[] points) {
156  if (this._particles == null || this._particles.Length != points.Length) {
157  this._particles = new ParticleSystem.Particle[points.Length];
158  }
159 
160  #if NEODROID_DEBUG
161  if (this.Debugging) {
162  var points_str = points.Aggregate("", (current, point) => current + point.ToString() + ", ");
163  Debug.Log("Applying the points " + points_str + " To " + this.name);
164  }
165  #endif
166 
167  var i = 0;
168  var l = (float)points.Length;
169  foreach (var point in points) {
170  this._particles[i].remainingLifetime = 100000;
171  this._particles[i].position = point;
172  var clamped = Math.Min(Math.Max(0.0f, i / l), 1.0f);
173  this._particles[i].startColor = this._gradient.Evaluate(clamped);
174  this._particles[i].startSize = 1f;
175  i++;
176  }
177 
178  this._particle_system.SetParticles(this._particles, points.Length);
179  }
180 
181  public void PlotSeries(float[] points) {
182  if (this._particles == null || this._particles.Length != points.Length) {
183  this._particles = new ParticleSystem.Particle[points.Length];
184  }
185 
186  #if NEODROID_DEBUG
187  if (this.Debugging) {
188  Debug.Log("Applying the series " + points + " To " + this.name);
189  }
190  #endif
191 
192  var i = 0;
193  foreach (var point in points) {
194  this._particles[i].remainingLifetime = 100000;
195  this._particles[i].position = Vector3.one * i;
196  var clamped = Math.Min(Math.Max(0.0f, point), 1.0f);
197  this._particles[i].startColor = this._gradient.Evaluate(clamped);
198  this._particles[i].startSize = 1f;
199  i++;
200  }
201 
202  this._particle_system.SetParticles(this._particles, points.Length);
203  }
204 
205  public void PlotSeries(string[] points) {
206  if (this._particles == null || this._particles.Length != points.Length) {
207  this._particles = new ParticleSystem.Particle[points.Length];
208  }
209 
210  #if NEODROID_DEBUG
211  if (this.Debugging) {
212  Debug.Log("Applying the series " + points + " To " + this.name);
213  }
214  #endif
215 
216  var i = 0;
217  foreach (var point in points) {
218  this._particles[i].remainingLifetime = 100000;
219  this._particles[i].position = Vector3.one * i;
220  this._particles[i].startSize = 1f;
221  i++;
222  }
223 
224  this._particle_system.SetParticles(this._particles, points.Length);
225  }
226 
230  public override void PlotSeries(Points.ValuePoint[] points) {
231  var alive = this._particle_system.GetParticles(this._particles);
232  if (alive < points.Length) {
233  this._particles = new ParticleSystem.Particle[points.Length];
234  }
235 
236  var i = 0;
237  foreach (var point in points) {
238  this._particles[i].remainingLifetime = 100000;
239  this._particles[i].position = point._Pos;
240  this._particles[i].startColor = this._gradient.Evaluate(point._Val);
241  this._particles[i].startSize = point._Size;
242  i++;
243  }
244 
245  this._particle_system.SetParticles(this._particles, points.Length);
246  }
247  }
248 }