GitHub PullRequestReviewCommentEvent at 2014-03-18 17:33:56
Repository: vispy
Actor: rougier
Payload: {"comment":{"url":"https://api.github.com/repos/vispy/vispy/pulls/comments/10715222","id":10715222,"diff_hunk":"@@ -0,0 +1,218 @@\n+#!/usr/bin/env python\n+# -*- coding: utf-8 -*-\n+# vispy: gallery 60\n+\n+\"\"\"\n+Dynamic planar graph layout.\n+\"\"\"\n+\n+import numpy as np\n+from vispy import gloo\n+from vispy import app\n+from vispy.gloo import gl\n+from vispy.util.transforms import ortho\n+\n+import scipy.spatial\n+\n+class Unstructured2d(app.Canvas):\n+\n+    def __init__(self, \n+            x=None, y=None, u=None, v=None, \n+            colormap=None, data_lim=None, \n+            dir_x_right=True, dir_y_top=True, \n+            **kwargs):\n+        # Initialize the canvas for real\n+        app.Canvas.__init__(self, **kwargs)\n+#         self.size = 512, 512\n+#         self.position = 50, 50\n+        \n+        self.create_shader(colormap)\n+        self.create_mesh(x, y, u, v)\n+        self.program.bind(self.vbo)\n+        if data_lim is not None:\n+            self._data_lim = data_lim\n+        else:\n+            self._data_lim = [[x.min(), x.max()], [y.min(), y.max()]]\n+        self._dir_x_right = dir_x_right\n+        self._dir_y_top = dir_y_top\n+\n+\n+        # self.timer = app.Timer(.01)\n+        # self.timer.connect(self.on_timer)\n+        # self.timer.start()\n+#         self.resize(*self.size)\n+\n+    def create_shader(self, colormap):\n+        if len(colormap.shape) == 2:\n+            args = dict(\n+                n_dims=\"1\",\n+                tex_t=\"float\",\n+                texture2D_arg=\"vec2(v_texcoord, 0.)\")\n+        else:\n+            args = dict(\n+                n_dims=\"2\",\n+                tex_t=\"vec2\",\n+                texture2D_arg=\"v_texcoord\")\n+        vertex = \"\"\"\n+            uniform mat4 model;\n+            uniform mat4 view;\n+            uniform mat4 projection;\n+            uniform sampler2D texture;\n+            \n+            attribute vec2 position;\n+            attribute {tex_t} texcoord;\n+            \n+            varying {tex_t} v_texcoord;\n+            void main()\n+            {{\n+                gl_Position = projection * vec4(position, 0.0, 1.0);\n+                v_texcoord = texcoord;\n+            }}\n+        \"\"\".format(**args)\n+        \n+        fragment = \"\"\"\n+            uniform sampler2D texture;\n+            varying {tex_t} v_texcoord;\n+            void main()\n+            {{\n+                gl_FragColor = texture2D(texture, {texture2D_arg});\n+            }}\n+        \"\"\".format(**args)\n+\n+        self.program = gloo.Program(vertex, fragment)\n+        if len(colormap.shape) == 2:\n+            self.program['texture'] = colormap[None, :, :]\n+        else:\n+            self.program['texture'] = colormap\n+        self.program['texture'].interpolation = gl.GL_LINEAR\n+        self.view = np.eye(4, dtype=np.float32)\n+        self.model = np.eye(4, dtype=np.float32)\n+        self.projection = np.eye(4, dtype=np.float32)\n+        self.program['model'] = self.model\n+        self.program['view'] = self.view\n+        \n+    def create_mesh(self, x, y, u, v):\n+        tri = scipy.spatial.Delaunay(np.column_stack([x, y]))\n+        edges = tri.simplices.astype(np.uint32)\n+        uv = []\n+        for c in [u, v]:\n+            if c is not None:\n+                c = c.astype('f4')\n+                c = .5 + .5 * c / np.abs(c).max()\n+                uv.append(c)\n+        data = np.column_stack([\n+                x.astype('f4'), \n+                y.astype('f4')] + uv\n+            ).view(\n+            dtype = [('position', 'f4', 2),\n+                     ('texcoord', 'f4', 2 if v is not None else 1),\n+                    ])\n+        self.vbo = gloo.VertexBuffer(data)\n+        self.index = gloo.IndexBuffer(edges)\n+\n+\n+    def on_initialize(self, event):\n+        gl.glClearColor(1, 1, 1, 1)\n+        gl.glDisable(gl.GL_DEPTH_TEST)\n+        gl.glEnable(gl.GL_BLEND)\n+        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)\n+\n+    def on_key_press(self, event):\n+        if event.text == ' ':\n+            if self.timer.running:\n+                self.timer.stop()\n+            else:\n+                self.timer.start()","path":"examples/demo/unstructured_2d.py","position":125,"original_position":125,"commit_id":"0c126bed6e627b047d0b5baad2cea683047a02ce","original_commit_id":"0c126bed6e627b047d0b5baad2cea683047a02ce","user":{"login":"rougier","id":327203,"avatar_url":"https://avatars.githubusercontent.com/u/327203?","gravatar_id":"05fc6835e821894b1ff75c46391eed7b","url":"https://api.github.com/users/rougier","html_url":"https://github.com/rougier","followers_url":"https://api.github.com/users/rougier/followers","following_url":"https://api.github.com/users/rougier/following{/other_user}","gists_url":"https://api.github.com/users/rougier/gists{/gist_id}","starred_url":"https://api.github.com/users/rougier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rougier/subscriptions","organizations_url":"https://api.github.com/users/rougier/orgs","repos_url":"https://api.github.com/users/rougier/repos","events_url":"https://api.github.com/users/rougier/events{/privacy}","received_events_url":"https://api.github.com/users/rougier/received_events","type":"User","site_admin":false},"body":"unnecessary (you have no timer anyway)","created_at":"2014-03-18T17:33:56Z","updated_at":"2014-03-18T17:33:56Z","html_url":"https://github.com/vispy/vispy/pull/203#discussion_r10715222","pull_request_url":"https://api.github.com/repos/vispy/vispy/pulls/203","_links":{"self":{"href":"https://api.github.com/repos/vispy/vispy/pulls/comments/10715222"},"html":{"href":"https://github.com/vispy/vispy/pull/203#discussion_r10715222"},"pull_request":{"href":"https://api.github.com/repos/vispy/vispy/pulls/203"}}}}