File: DrawLinesFromBuffer.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (131 lines) | stat: -rw-r--r-- 3,710 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python

import time

import wx

npts = 100000

def TestLinesFromBuffer(dc, log):
    global npts
    try:
        import numpy as np
        start = time.time()

        w, h = dc.GetSize()

        vs = np.linspace(0, 1, npts).reshape((npts, 1))

        x1 = np.cos(vs * 12 * np.pi) * w/2 * vs + w/2
        x2 = np.cos(vs * 16 * np.pi) * w/2 * vs + w/2
        y1 = np.sin(vs * 12 * np.pi) * w/2 * vs + h/2
        y2 = np.sin(vs * 16 * np.pi) * w/2 * vs + h/2
        

        # Data has to be the same size as a C integer
        pts1 = np.append(x1, y1, 1).astype('intc')
        pts2 = np.append(x2, y2, 1).astype('intc')

        dc.SetPen(wx.BLACK_PEN)
        t1 = time.time()
        dc.DrawLines(pts1)
        t2 = time.time()
        
        dc.SetPen(wx.RED_PEN)
        t3 = time.time()
        dc.DrawLinesFromBuffer(pts2)
        t4 = time.time()
        
        log.write("%s pts: %s seconds with DrawLines %s seconds with DrawLinesFromBuffer\n" % (npts, t2 - t1, t4 - t3))

    except ImportError:
        log.write("Couldn't import numpy")
        pass



# Class used for all the various sample pages; the mechanics are the same
# for each one with regards to the notebook. The only difference is
# the function we use to draw on it.
class DrawPanel(wx.Panel):
    def __init__(self, parent, drawFun, log):
        wx.Panel.__init__(self, parent, -1)
        self.SetBackgroundColour(wx.WHITE)

        self.log = log
        self.drawFun = drawFun
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnSize(self, evt):
        self.Refresh()

    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.Clear()
        self.drawFun(dc,self.log)


#----------------------------------------------------------------------

def runTest(frame, nb, log):
    global npts
    
    panel = wx.Panel(nb, -1)
    vsizer = wx.BoxSizer(wx.VERTICAL)
    hsizer = wx.BoxSizer(wx.HORIZONTAL)
    hsizer.Add(wx.StaticText(panel, -1, "# of Points"), 0, wx.ALIGN_CENTER|wx.ALL, 5)
    npts_ctrl = wx.TextCtrl(panel, -1, str(npts))
    hsizer.Add(npts_ctrl, 0, wx.ALIGN_CENTER|wx.ALL, 5)
    
    button = wx.Button(panel, -1, "Refresh")
    hsizer.Add(button, 0, wx.ALIGN_CENTER|wx.ALL, 5)
    vsizer.Add(hsizer, 0, wx.ALIGN_CENTER, 0)
    
    win = DrawPanel(panel, TestLinesFromBuffer, log)
    vsizer.Add(win, 1, wx.GROW, 0)
    panel.SetSizer(vsizer)
    
    def update_npts(evt):
        global npts
        
        val = npts_ctrl.GetValue()
        try:
            npts = int(val)
        except ValueError:
            log.write("Error converting %s to an int" % (val))
        win.Refresh()
    button.Bind(wx.EVT_BUTTON, update_npts)
    
    return panel

#----------------------------------------------------------------------


overview = """\

The DrawLinesFromBuffer function has been added to wx.DC to provide 
a way to draw directly from a numpy array or other object that implements
the python buffer protocol.  

<pre>
    DrawLinesFromBuffer(pyBuff)
</pre>

The buffer object needs to provide an array of C integers organized as 
x, y point pairs.  The size of a C integer is platform dependent.
With numpy, the intc data type will provide the appropriate element size.

If called with an object that doesn't support
the python buffer protocol, or if the underlying element size does not
match the size of a C integer, a TypeError exception is raised.  If 
the buffer provided has float data with the same element size as a 
C integer, no error will be raised, but the lines will not be drawn
in the appropriate places.
"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])