File: windbarb_quiver.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (96 lines) | stat: -rw-r--r-- 2,944 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
# vispy: gallery 30
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
""" Display Windbarbs.
"""

import itertools

import numpy as np
from vispy import app
from vispy.visuals import WindbarbVisual
from vispy.visuals.transforms import NullTransform


class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, title="Windbarb plot", keys="interactive",
                            size=(830, 430))

        self.windbarb_length = 25
        self.grid_spacing = 50

        self.grid_coords = None
        self.line_vertices = None
        self.last_mouse = (0, 0)

        self.generate_grid()

        direction_vectors = (self.grid_coords - self.last_mouse).astype(
            np.float32)
        direction_vectors[:] /= 10
        direction_vectors[:, 1] *= -1

        self.visual = WindbarbVisual(pos=self.grid_coords,
                                     wind=direction_vectors,
                                     trig=False,
                                     edge_color='black',
                                     face_color='black',
                                     size=self.windbarb_length)

        self.visual.events.update.connect(lambda evt: self.update())
        self.visual.transform = NullTransform()

        self.show()

    def generate_grid(self):
        n = self.grid_spacing / 2.
        num_cols = int(self.physical_size[0] / n * 2)
        num_rows = int(self.physical_size[1] / n * 2)
        coords = []

        # Generate grid
        for i, j in itertools.product(range(num_rows), range(num_cols)):
            x = n + (n * 2 * j)
            y = n + (n * 2 * i)

            coords.append((x, y))

        self.grid_coords = np.array(coords)

    def on_resize(self, event):
        self.generate_grid()
        self.rotate_arrows(np.array(self.last_mouse))

        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)
        self.visual.transforms.configure(canvas=self, viewport=vp)

    def rotate_arrows(self, point_towards):
        direction_vectors = (self.grid_coords - point_towards).astype(
            np.float32)
        direction_vectors[:] /= 10
        direction_vectors[:, 1] *= -1

        self.visual.set_data(
            pos=self.grid_coords,
            wind=direction_vectors,
            size=self.windbarb_length,
        )

    def on_mouse_move(self, event):
        self.last_mouse = event.pos
        self.rotate_arrows(np.array(event.pos))

    def on_draw(self, event):
        self.context.clear(color='white')
        self.visual.draw()


if __name__ == '__main__':
    canvas = Canvas()
    canvas.measure_fps()
    app.run()