-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysutil.py
More file actions
553 lines (469 loc) · 22.6 KB
/
physutil.py
File metadata and controls
553 lines (469 loc) · 22.6 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# physutil.py v1.0
# Copyright (c) 2011 GT Physics Education Research Group
# License: MIT (http://www.opensource.org/licenses/mit-license.php)
# This module is built to simplify and assist high school physics students
# in the construction of models for lab exercises using VPython.
# NOTE: The code is in 3 sections: import / unittest setup, actual module code, and unit test code.
from __future__ import division
import unittest
"""
#
#
# UNIT TESTING / IMPORT SETUP CODE ------------------------------------------------------------
#
#
"""
# Determine whether we are being used as a module or just running unittests (for mock purposes this is important)
if __name__ == "__main__":
# If we are unit testing, set up mock objects (must be done before classes are defined below!)
from visual import vector
class Mock:
def __init__(self, name, *args, **kwargs):
self.name = name
self.called = 0
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
for name in kwargs:
setattr(self, name, kwargs[name])
self.called += 1
return self
def reset(self):
self.called = 0
color = Mock("color")
color.red = "red"
color.green = "green"
color.blue = "blue"
color.yellow = "yellow"
color.orange = "orange"
color.cyan = "cyan"
color.magenta = "magenta"
color.white = "white"
arrow = Mock("arrow")
label = Mock("label")
points = Mock("points")
curve = Mock("curve")
gdisplay = Mock("gdisplay")
gcurve = Mock("gcurve")
gcurve.plots = []
def mockPlot(pos):
gcurve.plots.append(pos)
gcurve.plot = mockPlot
"""
vector = Mock("vector")
def call(x, y, z):
vector.x = x
vector.y = y
vector.z = z
vector.__call__ = call
"""
else:
# These are the actual imports for the utility
from visual import *
from visual.graph import *
"""
#
#
# ACTUAL PHYSUTIL CODE FOLLOWS --------------------------------------------------
#
#
"""
# Initialize window positions for students (if we aren't unit testing)
if __name__ != "__main__":
scene.x = 50
scene.y = 50
# Helper function for returning proper size of something
def obj_size(obj):
if type(obj) == box or type(obj) == pyramid:
return obj.size
elif type(obj) == sphere:
return vector(obj.radius, obj.radius, obj.radius)
class MotionMap:
"""
This class assists students in constructing motion maps
using either arrows (measuring a quantity) or "breadcrumbs"
(with timestamps).
"""
def __init__(self, obj, tf, numMarkers, markerType="arrow",
markerScale=1, markerColor=color.red,
labelMarkerOrder=True, labelMarkerOffset=vector(0,0,0),
dropTime=False, timeOffset=vector(0,0,0)):
# MotionMap
# obj - object to track in mapping / placing markers
# tf - expected tFinal, used to space marker placement over time
# numMarkers - number of markers to place
# markerType - determines type of motionmap; options are "arrow" or "breadcrumbs"
# markerScale - replaces pSize / quantscale from motionmodule.py depending on type
# markerColor - color of markers
# labelMarkerOrder - drop numbers of markers?
# labelMarkerOffset - amount to offset numbering by
# dropTime - boolean determining whether a timestamp should be placed along with the marker
# timeOffset - if dropTime is True, determines the offset, if any, of the label from the marker
self.obj = obj
self.tf = tf
self.numMarkers = numMarkers
self.markerType = markerType
self.markerScale = markerScale
self.markerColor = markerColor
self.labelMarkerOrder = labelMarkerOrder
self.labelMarkerOffset = labelMarkerOffset
self.timeOffset = timeOffset
self.dropTime = dropTime
# Calculate size of interval for each step, set initial step index
try:
self.interval = self.tf / self.numMarkers
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
self.curMarker = 0
def update(self, t, quantity=1):
try:
# Display new arrow if t has broken next threshold
if t > (self.interval * self.curMarker):
# Increment threshold
self.curMarker += 1
# Display marker!
if self.markerType == "arrow":
arrow(pos=self.obj.pos,
axis=self.markerScale*quantity, color=self.markerColor)
elif self.markerType == "breadcrumbs":
points(pos=self.obj.pos,
size=10*self.markerScale*quantity, color=self.markerColor)
#Also display timestamp if requested
if self.dropTime is not False:
epsilon = vector(0,self.markerScale*.5,0)+self.timeOffset
droptimeText = label(pos=self.obj.pos+epsilon, text='t='+str(t)+'s', height=10, box=False)
# Same with order label
if self.labelMarkerOrder is not False:
label(pos=self.obj.pos-vector(0,self.markerScale*.5,0)+self.labelMarkerOffset, text=str(self.curMarker), height=10, box=False)
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
class PhysAxis:
"""
This class assists students in creating dynamic axes for their models.
"""
def __init__(self, obj, numLabels, axisType="x", axis=vector(1,0,0), startPos=None,
length=None, labels = None, labelOrientation="down"):
# PhysAxis
# obj - Object which axis is oriented based on by default
# numLabels - number of labels on axis
# axisType - sets whether this is a default axis of x or y, or an arbitrary axis
# axis - unit vector defining the orientation of the axis to be created IF axisType = "arbitrary"
# startPos - start position for the axis - defaults to (-obj_size(obj).x/2,-4*obj_size(obj).y,0)
# length - length of the axis - defaults to obj_size(obj).x
# labelOrientation - how labels are placed relative to axis markers - "up", "down", "left", or "right"
try:
self.intervalMarkers = []
self.intervalLabels = []
self.labelText = labels
self.obj = obj
self.lastPos = vector(self.obj.pos.x, self.obj.pos.y, self.obj.pos.z)
self.numLabels = numLabels
self.axisType = axisType
self.axis = axis if axisType != "y" else vector(0,1,0)
self.length = length if (length is not None) else obj_size(obj).x
self.startPos = startPos if (startPos is not None) else vector(-obj_size(obj).x/2,-4*obj_size(obj).y,0)
if labelOrientation == "down":
self.labelShift = vector(0,-0.05*self.length,0)
elif labelOrientation == "up":
self.labelShift = vector(0,0.05*self.length,0)
elif labelOrientation == "left":
self.labelShift = vector(-0.1*self.length,0,0)
elif labelOrientation == "right":
self.labelShift = vector(0.1*self.length,0,0)
self.__reorient()
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
def update(self):
try:
# Determine if reference obj. has shifted since last update, if so shift us too
if self.obj.pos != self.lastPos:
diff = self.obj.pos - self.lastPos
for i in range(len(self.intervalMarkers)):
self.intervalMarkers[i].pos += diff
self.intervalLabels[i].pos += diff
self.axisCurve.pos = [x + diff for x in self.axisCurve.pos]
self.lastPos = vector(self.obj.pos.x, self.obj.pos.y, self.obj.pos.z)
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
def reorient(self, axis=None, startPos=None, length=None, labels=None, labelOrientation=None):
try:
# Determine which, if any, parameters are being modified
self.axis = axis if axis is not None else self.axis
self.startPos = startPos if startPos is not None else self.startPos
self.length = length if length is not None else self.length
self.labelText = labels if labels is not None else self.labels
# Re-do label orientation as well, if it has been set
if labelOrientation == "down":
self.labelShift = vector(0,-0.05*self.length,0)
elif labelOrientation == "up":
self.labelShift = vector(0,0.05*self.length,0)
elif labelOrientation == "left":
self.labelShift = vector(-0.1*self.length,0,0)
elif labelOrientation == "right":
self.labelShift = vector(0.1*self.length,0,0)
self.__reorient()
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
def __reorient(self):
# Actual internal axis setup code... determines first whether we are creating or updating
updating = True if len(self.intervalMarkers) > 0 else False
# Then determines the endpoint of the axis and the interval
final = self.startPos + (self.length * self.axis)
interval = (self.length / (self.numLabels-1)) * self.axis
# Loop for each interval marker, setting up or updating the markers and labels
i=0
while i<self.numLabels:
intervalPos = self.startPos+(i*interval)
# Determine text for this label
if self.labelText is not None:
labelText = self.labelText[i]
elif self.axisType == "y":
labelText = "%.2f" % intervalPos.y
else:
labelText = "%.2f" % intervalPos.x
if updating:
self.intervalMarkers[i].pos = intervalPos
self.intervalLabels[i].pos = intervalPos+self.labelShift
self.intervalLabels[i].text = str(labelText)
else:
self.intervalMarkers.append(
points(pos=intervalPos,color=color.yellow,size = 6) )
self.intervalLabels.append(
label(pos=intervalPos+self.labelShift, text=str(labelText),box=False,height = 8) )
i=i+1
# Finally, create / update the line itself!
if updating:
self.axisCurve.pos = [self.startPos,final]
else:
self.axisCurve = curve(pos=[self.startPos,final],color = color.yellow)
class PhysTimer:
"""
This class assists students in creating an onscreen timer display.
"""
def __init__(self, x, y, useScientific=False):
try:
self.useScientific = useScientific
if useScientific is False:
self.timerLabel = label(pos=vector(x,y,0), text='00:00:00:00', box=False)
else:
self.timerLabel = label(pos=vector(x,y,0), text='00E01', box=False)
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
def update(self, t):
try:
# Basically just use sprintf formatting according to either stopwatch or scientific notation
if self.useScientific:
self.timerLabel.text = "%.4E" % t
else:
hours = int(t / 3600)
mins = int((t / 60) % 60)
secs = int(t % 60)
frac = int(round(100 * (t % 1)))
self.timerLabel.text = "%02d:%02d:%02d:%02d" % (hours, mins, secs, frac)
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
class PhysGraph:
"""
This class assists students in creating graphs with advanced functionality.
"""
# Static, pre-determined list of colors from which each line will be generated
graphColors = [color.red, color.green, color.blue, color.yellow,
color.orange, color.cyan, color.magenta, color.white]
def __init__(self, numPlots=1):
try:
# Create our specific graph window
self.graphDisplay = gdisplay(475,350)
self.numPlots = numPlots
# Initialize each plot curve
self.graphs = []
for i in range(numPlots):
self.graphs.append(gcurve(color=PhysGraph.graphColors[i%len(PhysGraph.graphColors)]))
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
def plot(self, independent, *dependents):
try:
if len(dependents) != self.numPlots:
raise Exception("ERROR: Number of dependent parameters given does not match numPlots given at initialization!")
# Plot each line based on its parameter!
for i in range(len(dependents)):
self.graphs[i].plot(pos=(independent,dependents[i]))
except TypeError as err:
print "**********TYPE ERROR**********"
print "Please check that you are not passing in a variable of the wrong type (e.g. a scalar as a vector, or vice-versa)!"
print "******************************"
print err
raise err
"""
#
#
# UNIT TESTING BELOW ----------------------------------------------------------------------
#
#
"""
class TestMotionMap(unittest.TestCase):
def setUp(self):
self.obj = Mock("obj")
self.obj.pos = vector(0,0,0)
self.tf = 10
self.numMarkers = 5
self.timeOffset = vector(1,1,1)
self.markerScale = 2
arrow.reset()
points.reset()
label.reset()
self.map = MotionMap(self.obj, self.tf, self.numMarkers, markerType="arrow",
markerScale=2, markerColor=color.green,
dropTime=True, timeOffset=self.timeOffset)
def test_init(self):
self.assertEqual(self.obj, self.map.obj)
self.assertEqual(self.tf, self.map.tf)
self.assertEqual(self.numMarkers, self.map.numMarkers)
self.assertEqual("arrow", self.map.markerType)
self.assertEqual(self.markerScale, self.map.markerScale)
self.assertEqual(color.green, self.map.markerColor)
self.assertEqual(vector(1,1,1), self.map.timeOffset)
self.assertEqual(True, self.map.dropTime)
self.assertEqual(self.map.interval, self.tf / self.numMarkers)
self.assertEqual(self.map.curMarker, 0)
def test_update(self):
self.map.curMarker = 1
self.map.update(0)
self.assertEqual(arrow.called, 0)
self.assertEqual(points.called, 0)
self.assertEqual(label.called, 0)
self.map.update(3, quantity=2)
self.assertEqual(arrow.called, 1)
self.assertEqual(points.called, 0)
self.assertEqual(label.called, 2)
self.assertEqual(self.map.curMarker, 2)
self.assertEqual(arrow.pos, self.obj.pos)
self.assertEqual(arrow.axis, 4)
self.assertEqual(arrow.color, color.green)
self.assertEqual(label.text, "2")
class TestPhysAxis(unittest.TestCase):
def setUp(self):
self.obj = Mock("obj")
self.obj.pos = vector(0,0,0)
self.numLabels = 5
self.axis = vector(1,1,1)
self.startPos = vector(0,1,0)
self.length = 10
self.labels = ["a", "b", "c", "d", "e"]
self.wrongLabels = ["a"]
self.axisType = "arbitrary"
self.labelOrientation="left"
curve.reset()
self.physAxis = PhysAxis(self.obj, self.numLabels, axisType=self.axisType, axis=self.axis,
startPos=self.startPos, length=self.length, labels = self.labels,
labelOrientation=self.labelOrientation)
def test_init(self):
self.assertEqual(self.physAxis.labelText, self.labels)
self.assertEqual(self.physAxis.obj, self.obj)
self.assertEqual(self.physAxis.lastPos, self.obj.pos)
self.assertEqual(self.physAxis.numLabels, self.numLabels)
self.assertEqual(self.physAxis.axis, self.axis)
self.assertEqual(self.physAxis.length, self.length)
self.assertEqual(self.physAxis.startPos, self.startPos)
self.assertEqual(self.physAxis.axisType, self.axisType)
self.assertEqual(self.physAxis.labelShift, vector(-0.1*self.length, 0, 0))
self.assertEqual(len(self.physAxis.intervalMarkers), self.numLabels)
self.assertEqual(len(self.physAxis.intervalLabels), self.numLabels)
intervalPos = self.startPos+(self.length * self.axis)
self.assertEqual(self.physAxis.intervalMarkers[-1].pos, intervalPos)
self.assertEqual(self.physAxis.intervalLabels[-1].pos, intervalPos+self.physAxis.labelShift)
self.assertEqual(self.physAxis.intervalLabels[-1].text, "e")
self.assertEqual(curve.called, 1)
def test_reorient(self):
newAxis = vector(0,0,1)
startPos = vector(1,0,0)
otherLabels = ["f", "g", "h", "i", "j"]
self.physAxis.reorient(axis=newAxis, startPos=startPos, length=1,
labels=otherLabels, labelOrientation="right")
self.assertEqual(self.physAxis.axis, newAxis)
self.assertEqual(self.physAxis.startPos, startPos)
self.assertEqual(self.physAxis.length, 1)
self.assertEqual(self.physAxis.labelShift, vector(0.1, 0, 0))
intervalPos = startPos+newAxis
self.assertEqual(self.physAxis.intervalMarkers[-1].pos, intervalPos)
self.assertEqual(self.physAxis.intervalLabels[-1].pos, intervalPos+self.physAxis.labelShift)
self.assertEqual(self.physAxis.intervalLabels[-1].text, "j")
self.assertEqual(curve.called, 1)
def test_update(self):
startMarkerPos = vector(self.physAxis.intervalMarkers[-1].pos.x,
self.physAxis.intervalMarkers[-1].pos.y,
self.physAxis.intervalMarkers[-1].pos.z)
startLabelPos = vector(self.physAxis.intervalLabels[-1].pos.x,
self.physAxis.intervalLabels[-1].pos.y,
self.physAxis.intervalLabels[-1].pos.z)
startCurvePos = vector(self.physAxis.axisCurve.pos[0].x,
self.physAxis.axisCurve.pos[0].y,
self.physAxis.axisCurve.pos[0].z)
self.physAxis.update()
self.assertEqual(startMarkerPos, self.physAxis.intervalMarkers[-1].pos)
self.assertEqual(startLabelPos, self.physAxis.intervalLabels[-1].pos)
self.assertEqual(startCurvePos, self.physAxis.axisCurve.pos[0])
self.physAxis.obj.pos = self.physAxis.obj.pos + vector(1,1,1)
self.physAxis.update()
self.assertNotEqual(startMarkerPos, self.physAxis.intervalMarkers[-1].pos)
self.assertNotEqual(startLabelPos, self.physAxis.intervalLabels[-1].pos)
self.assertNotEqual(startCurvePos, self.physAxis.axisCurve.pos[0])
class TestPhysGraph(unittest.TestCase):
def setUp(self):
self.physGraph = PhysGraph(numPlots = 5)
def test_init(self):
self.assertEqual(self.physGraph.graphDisplay.args, (475, 350))
self.assertEqual(self.physGraph.numPlots, 5)
self.assertEqual(len(self.physGraph.graphs), 5)
def test_plot(self):
self.assertRaises(Exception, self.physGraph.plot, vector(0,0,0), vector(1,1,1))
self.physGraph.plot(vector(0,0,0), vector(1,1,1), vector(2,2,2), vector(3,3,3), vector(4,4,4), vector(5,5,5))
self.assertEqual(len(self.physGraph.graphs[-1].plots), 5)
self.assertEqual(self.physGraph.graphs[-1].plots[0], (vector(0,0,0), vector(1,1,1)))
class TestPhysTimer(unittest.TestCase):
def setUp(self):
self.timer = PhysTimer(1,1)
def test_init(self):
self.assertEquals(self.timer.timerLabel.text, "00:00:00:00")
self.assertEquals(self.timer.timerLabel.pos, vector(1,1,0))
def test_update(self):
self.timer.update(3923.65)
self.assertEquals(self.timer.timerLabel.text, "01:05:23:65")
self.timer.useScientific=True
self.timer.update(3923.65)
self.assertEquals(self.timer.timerLabel.text, "3.9237E+03")
# Now set up unittests to be executed when module is run individually
if __name__ == "__main__":
print "Beginning unit tests!"
unittest.main()