mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-08 04:50:07 +01:00
dc.DrawLinesFromBuffer test and demo added
This commit is contained in:
123
demo/DrawLinesFromBuffer.py
Normal file
123
demo/DrawLinesFromBuffer.py
Normal file
@@ -0,0 +1,123 @@
|
||||
#!/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
|
||||
|
||||
|
||||
pts1 = np.append(x1, y1, 1).astype('int32')
|
||||
pts2 = np.append(x2, y2, 1).astype('int32')
|
||||
|
||||
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>
|
||||
|
||||
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 wxPoint, a TypeError exception is raised.
|
||||
"""
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys,os
|
||||
import run
|
||||
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
||||
|
||||
@@ -50,6 +50,7 @@ _treeList = [
|
||||
'ActivityIndicator',
|
||||
'GenericCheckBox',
|
||||
'CheckListCtrl',
|
||||
'DrawLinesFromBuffer',
|
||||
]),
|
||||
|
||||
# managed windows == things with a (optional) caption you can close
|
||||
@@ -275,6 +276,7 @@ _treeList = [
|
||||
'Cairo_Snippets',
|
||||
'ColourDB',
|
||||
'DragScroller',
|
||||
'DrawLinesFromBuffer',
|
||||
'DrawXXXList',
|
||||
'FileHistory',
|
||||
'FontEnumerator',
|
||||
|
||||
64
unittests/test_dcDrawLinesFromBuffer.py
Normal file
64
unittests/test_dcDrawLinesFromBuffer.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import unittest
|
||||
from unittests import wtc
|
||||
import wx
|
||||
import random
|
||||
try:
|
||||
import numpy as np
|
||||
haveNumpy = True
|
||||
except ImportError:
|
||||
haveNumpy = False
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
w = 600
|
||||
h = 400
|
||||
|
||||
def makeRandomLines():
|
||||
lines = []
|
||||
|
||||
for i in range(num):
|
||||
x1 = random.randint(0, w)
|
||||
y1 = random.randint(0, h)
|
||||
x2 = random.randint(0, w)
|
||||
y2 = random.randint(0, h)
|
||||
lines.append( (x1,y1, x2,y2) )
|
||||
|
||||
return lines
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class dcDrawLists_Tests(wtc.WidgetTestCase):
|
||||
|
||||
@unittest.skipIf(not haveNumpy, "Numpy required for this test")
|
||||
def test_dcDrawLinesFromBuffer(self):
|
||||
pnl = wx.Panel(self.frame)
|
||||
self.frame.SetSize((w,h))
|
||||
dc = wx.ClientDC(pnl)
|
||||
dc.SetPen(wx.Pen("BLACK", 1))
|
||||
|
||||
xs = np.linspace(0, w, w + 1)
|
||||
ys = np.sin(xs / w * 2 * np.pi)
|
||||
|
||||
xs.shape = xs.size, 1
|
||||
ys.shape = ys.size, 1
|
||||
pts = np.append(xs, ys, 1)
|
||||
|
||||
dc.DrawLinesFromBuffer(pts.astype('int32'))
|
||||
self.assertRaises(TypeError, dc.DrawLinesFromBuffer, pts.astype('int64'))
|
||||
self.assertRaises(TypeError, dc.DrawLinesFromBuffer, pts.tolist())
|
||||
del dc
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user