From efbea6a275ba3a9afeb16db80f8eca9675471d79 Mon Sep 17 00:00:00 2001 From: arjones6 Date: Wed, 15 Dec 2021 11:07:18 -0500 Subject: [PATCH] dc.DrawLinesFromBuffer demo and test to intc data type. Updated documentation. --- demo/DrawLinesFromBuffer.py | 14 +++++++++++--- etg/dc.py | 14 +++++++++++++- unittests/test_dcDrawLinesFromBuffer.py | 5 +++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/demo/DrawLinesFromBuffer.py b/demo/DrawLinesFromBuffer.py index f4c948fc..1761e3b7 100644 --- a/demo/DrawLinesFromBuffer.py +++ b/demo/DrawLinesFromBuffer.py @@ -22,8 +22,9 @@ def TestLinesFromBuffer(dc, log): 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') + # 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() @@ -110,9 +111,16 @@ the python buffer protocol. DrawLinesFromBuffer(pyBuff) +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 wxPoint, a TypeError exception is raised. +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. """ diff --git a/etg/dc.py b/etg/dc.py index ccdc3b10..f4727c28 100644 --- a/etg/dc.py +++ b/etg/dc.py @@ -481,7 +481,19 @@ def run(): c.addPyMethod('DrawLinesFromBuffer', '(self, pyBuff)', doc="""\ Implementation of DrawLines that can use numpy arrays, or anything else that uses the - python buffer protocol, directly. + python buffer protocol directly without any element conversion. This provides a + significant performance increase over the standard DrawLines function. + + The pyBuff argument 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. :param pyBuff: A python buffer containing integer pairs """, diff --git a/unittests/test_dcDrawLinesFromBuffer.py b/unittests/test_dcDrawLinesFromBuffer.py index 20552341..3bed6053 100644 --- a/unittests/test_dcDrawLinesFromBuffer.py +++ b/unittests/test_dcDrawLinesFromBuffer.py @@ -48,8 +48,9 @@ class dcDrawLists_Tests(wtc.WidgetTestCase): ys.shape = ys.size, 1 pts = np.append(xs, ys, 1) - dc.DrawLinesFromBuffer(pts.astype('int32')) - self.assertRaises(TypeError, dc.DrawLinesFromBuffer, pts.astype('int64')) + dc.DrawLinesFromBuffer(pts.astype('intc')) + self.assertRaises(TypeError, dc.DrawLinesFromBuffer, + pts.astype('int64') if np.intc(1).nbytes != np.int64(1).nbytes else pts.astype('int32')) self.assertRaises(TypeError, dc.DrawLinesFromBuffer, pts.tolist()) del dc