whispervis/propagation.go

114 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
"time"
"github.com/divan/three"
"github.com/status-im/simulation/propagation"
)
var (
2018-10-27 09:52:10 +02:00
BlinkedEdgeMaterial = NewBlinkedEdgeMaterial()
BlinkedNodeMaterial = NewBlinkedNodeMaterial()
)
// AnimatePropagation visualizes propagation of message based on plog.
func (w *WebGLScene) AnimatePropagation(plog *propagation.Log) {
w.rt.EnableRendering()
for i, ts := range plog.Timestamps {
duration := time.Duration(time.Duration(ts) * time.Millisecond)
nodes := plog.Nodes[i]
edges := plog.Links[i]
2018-10-27 09:45:12 +02:00
delay := time.Duration(w.blink) * time.Millisecond
fn := func() {
// blink nodes for this timestamp
for _, idx := range nodes {
2018-10-27 16:04:54 +02:00
node := w.nodes[idx]
w.BlinkNode(node)
go time.AfterFunc(delay, func() { w.UnblinkNode(node) })
}
// blink links for this timestamp
for _, idx := range edges {
2018-10-27 16:04:54 +02:00
edge := w.lines[idx]
w.BlinkEdge(edge)
go time.AfterFunc(delay, func() { w.UnblinkEdge(edge) })
}
}
go time.AfterFunc(duration, fn)
w.rt.EnableRendering() // prevent thorttler from enabling during long animations
}
}
2018-10-27 09:45:12 +02:00
// AnimateOneStep blinks permantently nodes and edges for the given step of plog.
// TODO(divan): "Animate-" is probably not the best name here, come up with something
// better (this function doesn't *animate* anything so far)
func (w *WebGLScene) AnimateOneStep(plog *propagation.Log, step int) {
nodes := plog.Nodes[step]
edges := plog.Links[step]
nodesToBlink := make(map[int]struct{})
for _, idx := range nodes {
nodesToBlink[idx] = struct{}{}
}
// blink nodes for this timestamp
for i, _ := range w.nodes {
2018-10-27 16:04:54 +02:00
node := w.nodes[i]
2018-10-27 09:45:12 +02:00
if _, ok := nodesToBlink[i]; ok {
2018-10-27 16:04:54 +02:00
w.BlinkNode(node)
2018-10-27 09:45:12 +02:00
} else {
2018-10-27 16:04:54 +02:00
w.UnblinkNode(node)
2018-10-27 09:45:12 +02:00
}
}
edgesToBlink := make(map[int]struct{})
for _, idx := range edges {
edgesToBlink[idx] = struct{}{}
}
for i, _ := range w.lines {
2018-10-27 16:04:54 +02:00
edge := w.lines[i]
2018-10-27 09:45:12 +02:00
if _, ok := edgesToBlink[i]; ok {
2018-10-27 16:04:54 +02:00
w.BlinkEdge(edge)
2018-10-27 09:45:12 +02:00
} else {
2018-10-27 16:04:54 +02:00
w.UnblinkEdge(edge)
2018-10-27 09:45:12 +02:00
}
}
}
// BlinkNode animates a single node blinking. Node specified by its idx.
2018-10-27 09:45:12 +02:00
// TODO(divan): consider renaming it to Highlight or something.
2018-10-27 16:04:54 +02:00
func (w *WebGLScene) BlinkNode(node *Mesh) {
2018-10-27 09:52:10 +02:00
node.Set("material", BlinkedNodeMaterial)
2018-10-27 09:45:12 +02:00
}
2018-10-27 16:04:54 +02:00
func (w *WebGLScene) UnblinkNode(node *Mesh) {
2018-10-27 09:45:12 +02:00
node.Object.Set("material", DefaultNodeMaterial)
}
// BlinkEdge animates a single edge blinking. Edge specified by its idx.
2018-10-27 16:04:54 +02:00
func (w *WebGLScene) BlinkEdge(edge *Line) {
2018-10-27 09:52:10 +02:00
edge.Set("material", BlinkedEdgeMaterial)
2018-10-27 09:45:12 +02:00
}
2018-10-27 16:04:54 +02:00
func (w *WebGLScene) UnblinkEdge(edge *Line) {
edge.Set("material", DefaultEdgeMaterial)
}
2018-10-27 09:52:10 +02:00
// NewBlinkedEdgeMaterial creates a new default material for the graph blinked edge lines.
func NewBlinkedEdgeMaterial() three.Material {
params := three.NewMaterialParameters()
params.Color = three.NewColorRGB(255, 0, 0)
2018-10-27 09:52:10 +02:00
params.Transparent = false
return three.NewLineBasicMaterial(params)
}
2018-10-27 09:52:10 +02:00
// NewBlinkedNodeMaterial creates a new default material for the graph blinked node.
func NewBlinkedNodeMaterial() three.Material {
params := three.NewMaterialParameters()
params.Color = three.NewColorRGB(255, 0, 0) // red
2018-10-27 09:52:10 +02:00
params.Transparent = false
return three.NewMeshPhongMaterial(params)
}