-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
147 lines (126 loc) · 5.61 KB
/
util.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright 2015, Graeme Ball
# Copyright 2012, The Regents of University of California
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import wx
import OpenGL.GL as GL
import threading
# key codes: 70=F, 76=L, 84=T, 66=B (as in First, Last, Top, Bottom)
KEY_MOTION_MAP = {
70: (0, -1, 0, 0, 0),
76: (0, 1, 0, 0, 0),
66: (0, 0, -1, 0, 0),
84: (0, 0, 1, 0, 0),
wx.WXK_DOWN: (0, 0, 0, -1, 0),
wx.WXK_UP: (0, 0, 0, 1, 0),
wx.WXK_LEFT: (0, 0, 0, 0, -1),
wx.WXK_RIGHT: (0, 0, 0, 0, 1),
}
# Convert wavelength (nm) into (R,G,B) tuple for approx color.
def waveToRGB(wave):
"""
Convert wavelength (nm) into (R,G,B) tuple for approx color.
"""
# "python style switch" ... using a dictionary - bizarre!
RGBtuple = {
wave<420 : (0.5,0,1),
420<=wave<470 : (0,0,1),
470<=wave<500 : (0,1,1),
500<=wave<560 : (0,1,0),
560<=wave<590 : (1,1,0),
590<=wave<620 : (1,0.5,0),
620<=wave<670 : (1,0,0),
670<=wave : (0.5,0,0)
}[1]
return RGBtuple
## Copied from the OMX version.
def addLabeledInput(parent, sizer, id = -1, label = '',
defaultValue = '', size = (-1, -1), minSize = (-1, -1),
shouldRightAlignInput = False, border = 0, labelHeightAdjustment = 0,
controlType = None, helperString = '', flags = wx.ALL,
style = 0):
if controlType is None:
controlType = wx.TextCtrl
rowSizer = wx.BoxSizer(wx.HORIZONTAL)
rowSizer.SetMinSize(minSize)
rowSizer.Add(wx.StaticText(parent, -1, label), 0, wx.TOP, labelHeightAdjustment)
if helperString != '':
addHelperString(parent, rowSizer, helperString, labelHeightAdjustment,
wx.TOP)
if shouldRightAlignInput:
# Add an empty to suck up horizontal space
rowSizer.Add((10, -1), 1, wx.EXPAND | wx.ALL, 0)
control = controlType(parent, id, defaultValue, size = size, style = style)
rowSizer.Add(control)
sizer.Add(rowSizer, 0, flags, border)
return control
## Add some explanatory text to the given sizer.
def addHelperString(parent, sizer, text, border = 0, flags = wx.ALL):
label = wx.StaticText(parent, -1, " (What is this?)")
label.SetForegroundColour((100, 100, 255))
label.SetToolTipString(text)
sizer.Add(label, 0, flags, border)
## Create a new menu item and insert it into the given menu.
def addMenuItem(parent, menu, label, action):
item = wx.MenuItem(menu, -1, label)
parent.Bind(wx.EVT_MENU, action, id = item.GetId())
menu.AppendItem(item)
## Save an array as an image. Copied from
# http://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image
def imsave(filename, array, vmin=None, vmax=None, cmap=None, format=None, origin=None):
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure(figsize=array.shape[::-1], dpi=1, frameon=False)
canvas = FigureCanvas(fig)
fig.figimage(array, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
fig.savefig(filename, dpi=1, format=format)
## Save out the current OpenGL view as an image.
def saveGLView(filename):
view = GL.glGetIntegerv(GL.GL_VIEWPORT)
GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1)
GL.glReadBuffer(GL.GL_BACK_LEFT)
pixels = GL.glReadPixels(0, 0, view[2], view[3], GL.GL_RGB, GL.GL_UNSIGNED_BYTE)
image = wx.ImageFromData(int(view[2]), int(view[3]), pixels)
image.SaveFile(filename, wx.BITMAP_TYPE_PNG)
## Call the passed-in function in a new thread.
def callInNewThread(function):
def wrappedFunc(*args, **kwargs):
threading.Thread(target = function, args = args, kwargs = kwargs).start()
return wrappedFunc
## Decorator function used to ensure that a given function is only called
# in wx's main thread.
def callInMainThread(func):
def wrappedFunc(*args, **kwargs):
wx.CallAfter(func, *args, **kwargs)
return wrappedFunc
printLock = threading.Lock()
## Simple function for debugging when dealing with multiple threads, since
# otherwise Python's "print" builtin isn't threadsafe.
def threadPrint(*args):
with printLock:
print " ".join([str(s) for s in args])