Browse Source

Fixed plotting of FSA events

Yentl Van Tendeloo 8 years ago
parent
commit
844d0421e8
1 changed files with 38 additions and 35 deletions
  1. 38 35
      interface/FSA/main.py

+ 38 - 35
interface/FSA/main.py

@@ -220,21 +220,27 @@ class InterfaceCore():
             self.mv.instantiate_block(str(name), "State")
             r = canvas.create_oval(lower(x), lower(y), upper(x), upper(y), fill="white")
             b = (lower(x), lower(y), upper(x), upper(y), str(name))
-            self.drawn.add(b)
             self.refs[str(name)] = [r]
             name += 1
 
     def find(self, location):
+        def sqrt(a):
+            return a**0.5
+
         x, y = location
+        matches = []
         for e in self.drawn:
-            if (e[0] <= x and
-                e[1] <= y and
-                e[2] >= x and
-                e[3] >= y):
-                    return e[4]
+            x1, y1, x2, y2, x0, y0 = e[0], e[1], e[2], e[3], x, y
+            distance = abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)
+            matches.append((distance, e[4]))
+
+        if matches:
+            minimal_distance, name = sorted(matches)[0]
+            print("Distance to " + name + " is " + str(minimal_distance))
+            if minimal_distance < 10:
+                return name
 
-        print("Found nothing at that location!")
-        return []
+        return None
 
     def draw(self, start, end):
         source = self.find(start)
@@ -246,6 +252,7 @@ class InterfaceCore():
             global name
             self.mv.instantiate_link(str(name), "Transition", source, target)
             self.refs[str(name)] = [canvas.create_line(start[0], start[1], end[0], end[1], fill="black", arrow=LAST)]
+            self.drawn.add([start[0], start[1], end[0], end[1], str(name)])
             name += 1
 
 core = InterfaceCore()
@@ -298,41 +305,37 @@ canvas.bind("<ButtonRelease-3>", release)
 
 visual = Toplevel(root)
 
-probes = {}
-values = {}
-
 # Example:
-#    simulation = [(1, {"a": 1, "b": 2}), (2, {"a": 3}), (3, {"a": 4, "b": 6})]
+#    simulation = [(1, "A"), (3, "B"), (4, "A")]
+simulation = [(1, "A"), (3, "B"), (4, "A")]
 
 frame = Frame(visual)
 frame.pack(fill=BOTH,expand=True)
 
-def update_graphs():
-    while simulation:
-        t, results = simulation.pop(0)
-        for k, v in results.items():
-            if k in probes:
-                fcanvas, a = probes[k]
-            else:
-                f = Figure()
-                a = f.add_subplot(111)
-                a.plot([], [])
-                print(k)
-                f.suptitle(k)
-
-                fcanvas = FigureCanvasTkAgg(f, frame.interior)
-                fcanvas.show()
-                fcanvas.get_tk_widget().pack()
+f = Figure()
+a = f.add_subplot(111)
+a.plot([], [])
+f.suptitle("Events")
 
-                probes[k] = (fcanvas, a)
-                values[k] = ([], [])
+fcanvas = FigureCanvasTkAgg(f, frame)
+fcanvas.show()
+fcanvas.get_tk_widget().pack()
 
-            values[k][0].append(t)
-            values[k][1].append(v)
+reverse_lookup = {}
 
-            a.clear()
-            a.plot(values[k][0], values[k][1], linestyle="none", marker="o")
-            fcanvas.draw()
+def update_graphs():
+    times = [x[0] for x in simulation]
+    events = [reverse_lookup.setdefault(x[1], len(reverse_lookup)) for x in simulation]
+    lookup = [None] * len(reverse_lookup)
+    for k, v in reverse_lookup.items():
+        lookup[v] = k
+    a.clear()
+    a.plot(times, events, linestyle="none", marker="o")
+    print("Lookup: " + str(lookup))
+    print("Events:" + str(events))
+    f.get_axes()[0].set_yticks(range(len(lookup)))
+    f.get_axes()[0].set_yticklabels(lookup)
+    fcanvas.draw()
     root.after(100, update_graphs)
 
 root.after(100, update_graphs)