Skip to content

Commit efe6e10

Browse files
Merge pull request #67 from vpython/Change_glowcomm_to_use_VPython_syntax
Change glowcomm to use VPython syntax, which simplifies things
2 parents 7c92ae7 + fd8a4e7 commit efe6e10

File tree

4 files changed

+65
-110
lines changed

4 files changed

+65
-110
lines changed

labextension/vpython/src/glowcommlab.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,22 @@ function send() { // periodically send events and update_canvas and request obje
167167

168168

169169
// *************************************************************************************************** //
170-
// THE REST OF THIS FILE IS IDENTICAL IN glowcomm.html AND glowcomm.js (except for the last few lines) //
171-
172-
// Should eventually have glowcomm.html and glowcom.js both import this common component.
170+
// THE REST OF THIS FILE IS NEARLY IDENTICAL IN glowcomm.html AND glowcomm.js AND glowcommlab.js //
171+
172+
// Should eventually have glowcomm.html, glowcom.js, and glowcommlab.js all import this common component.
173+
174+
window.__GSlang = "vpython"
175+
box = vp_box
176+
sphere = vp_sphere
177+
simple_sphere = vp_simple_sphere
178+
cylinder = vp_cylinder
179+
pyramid = vp_pyramid
180+
cone = vp_cone
181+
helix = vp_helix
182+
ellipsoid = vp_ellipsoid
183+
ring = vp_ring
184+
arrow = vp_arrow
185+
compound = vp_compound
173186

174187
function msclock() {
175188
"use strict";
@@ -275,6 +288,7 @@ function update_canvas() { // mouse location and other stuff
275288

276289
/*
277290
var request = new XMLHttpRequest()
291+
278292
function send_to_server(data, callback) { // send to HTTP server
279293
var data= JSON.stringify(data)
280294
request.open('get', data, true)
@@ -287,6 +301,7 @@ function send_to_server(data, callback) { // send to HTTP server
287301
}
288302
request.send()
289303
}
304+
290305
function ok(req) { ; }
291306
*/
292307

@@ -372,7 +387,7 @@ function process_waitfor(event) {
372387
}
373388

374389
function process_binding(event) { // event associated with a previous bind command
375-
"use strict";
390+
"use strict";
376391
event.bind = true
377392
process(event)
378393
}
@@ -625,8 +640,6 @@ function handle_cmds(dcmds) {
625640
ptlist.push( o2vec3(val[kk]) )
626641
}
627642
cfg[attr] = ptlist
628-
} else if (attr === "axis" && obj == 'arrow') {
629-
cfg['axis_and_length'] = o2vec3(val)
630643
} else if (vlst.indexOf(attr) !== -1) {
631644
cfg[attr] = o2vec3(val)
632645
} else if (triangle_quad.indexOf(attr) !== -1) {
@@ -775,7 +788,6 @@ function handle_cmds(dcmds) {
775788
catch(err) {
776789
console.log("glowcomm canvas contextmenu event : ",err.message);
777790
}
778-
779791
break
780792
// Display frames per second and render time:
781793
//$("<div id='fps'/>").appendTo(glowObjs[idx].title)
@@ -988,17 +1000,6 @@ function handle_attrs(dattrs) {
9881000
ptlist.push( val[kk] )
9891001
}
9901002
obj[attr] = ptlist
991-
} else if (attr === 'axis') {
992-
// For axis and up, Python maintains them to be perpendicular, so avoid
993-
// having GlowScript do it again, which can have bad results.
994-
window.__adjustupaxis = false
995-
if (obj instanceof arrow) attr = 'axis_and_length'
996-
obj[attr] = val
997-
window.__adjustupaxis = true
998-
} else if (attr === 'up') {
999-
window.__adjustupaxis = false
1000-
obj[attr] = val
1001-
window.__adjustupaxis = true
10021003
} else {
10031004
obj[attr] = val
10041005
}

vpython/vpython.py

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -629,25 +629,24 @@ def setup(self, args):
629629
else: raise AttributeError(a+' must be a vector')
630630
del args[a]
631631

632-
vectorInteractions = [ ('size','axis'), ('axis','size'), ('axis','up'), ('up','axis')]
633-
634-
# override defaults for vector attributes with side effects
632+
# Track side effects of modifying size, axis, or up
635633
# For consistency with GlowScript, axis is listed before up in the attrLists,
636634
# so that setting axis may affect up, but then setting up can affect axis afterwards.
637-
attrs = standardAttributes.attrLists[objName][1]
635+
attrs = standardAttributes.attrLists[objName][1] # vector attributes with interactions
638636
for a in attrs:
639637
if a in args:
640638
val = args[a]
641639
if isinstance(val, vector):
642-
setattr(self, a, vector(val)) ## use setter to take care of side effects; copy of val
640+
setattr(self, a, vector(val))
643641
if a not in argsToSend:
644642
argsToSend.append(a)
645-
for vi in vectorInteractions:
646-
if vi[0] == a:
647-
if vi[1] not in argsToSend:
648-
argsToSend.append(vi[1])
649-
elif objName == 'points' and a == 'size': ## in this case size is a scalar
650-
argsToSend.append(a)
643+
if a == 'size':
644+
self._axis = self._axis.norm()*val.x
645+
elif a == 'axis':
646+
self._size.x = mag(val)
647+
self.axis = val # this will have the side effect of modifying up
648+
elif a == 'up':
649+
self.up = val # this will have the side effect of modifying axis
651650
else: raise AttributeError(a+' must be a vector')
652651
del args[a]
653652

@@ -695,10 +694,8 @@ def setup(self, args):
695694
elif isinstance(aval, vertex):
696695
aval = aval.idx
697696
if objName in nosize and a == 'size': continue # do not send superfluous size
698-
#cmd["attrs"].append({"attr":a, "value": aval})
699697
cmd[a] = aval
700698

701-
702699
# set canvas
703700
if self.canvas is None: ## not specified in constructor
704701
self.canvas = canvas.get_selected()
@@ -757,8 +754,6 @@ def up(self):
757754
def up(self,value):
758755
self._save_oldup = adjust_axis(self._up, value, self._axis, self._save_oldup) # this sets self._axis and self._up
759756
if not self._constructing:
760-
# must update both axis and up when either is changed
761-
self.addattr('axis')
762757
self.addattr('up')
763758

764759
@property
@@ -768,14 +763,8 @@ def axis(self):
768763
def axis(self,value):
769764
self._save_oldaxis = adjust_up(self._axis, value, self._up, self._save_oldaxis) # this sets self._axis and self._up
770765
if not self._constructing:
771-
# must update both axis and up when either is changed
772766
self.addattr('axis')
773-
self.addattr('up')
774-
m = value.mag
775-
if abs(self._size._x - m) > 0.0001*self._size._x: # need not update size if very small change
776-
self._size._x = m
777-
if not self._constructing:
778-
self.addattr('size')
767+
self._size._x = value.mag # changing axis length changes size.x
779768

780769
@property
781770
def size(self):
@@ -788,11 +777,7 @@ def size(self,value):
788777
a = self._axis.norm() * value.x
789778
if mag(self._axis) == 0:
790779
a = vector(value.x,0,0)
791-
v = self._axis
792-
if not v.equals(a):
793-
self._axis.value = a
794-
if not self._constructing:
795-
self.addattr('axis')
780+
self._axis.value = a # changing size changes length of axis
796781

797782
@property
798783
def length(self):
@@ -1263,37 +1248,6 @@ def __init__(self, **args):
12631248

12641249
super(arrow, self).setup(args)
12651250

1266-
@property
1267-
def size(self):
1268-
return self._size
1269-
@size.setter
1270-
def size(self,value): # no need to send to browser both arrow.size and arrow.axis
1271-
self._size.value = value
1272-
if not self._constructing:
1273-
self.addattr('size')
1274-
a = self._axis.norm() * value.x
1275-
if mag(self._axis) == 0:
1276-
a = vector(value.x,0,0)
1277-
v = self._axis
1278-
if not v.equals(a):
1279-
self._axis.value = a
1280-
1281-
@property
1282-
def axis(self):
1283-
return self._axis
1284-
@axis.setter
1285-
def axis(self,value): # no need to send to browser both arrow.size and arrow.axis
1286-
oldaxis = norm(self._axis)
1287-
self._axis.value = value
1288-
m = value.mag
1289-
if abs(self._size._x - m) > 0.0001*self._size._x: # need not update size if very small change
1290-
self._size._x = m
1291-
self._save_oldaxis = adjust_up(norm(oldaxis), self._axis, self._up, self._save_oldaxis)
1292-
if not self._constructing:
1293-
# must update both axis and up when either is changed
1294-
self.addattr('axis')
1295-
self.addattr('up')
1296-
12971251
@property
12981252
def shaftwidth(self):
12991253
return self._shaftwidth

vpython/vpython_libraries/glowcomm.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,22 @@
112112
}
113113

114114
// *************************************************************************************************** //
115-
// THE REST OF THIS FILE IS IDENTICAL IN glowcomm.html AND glowcomm.js (except for the last few lines) //
116-
117-
// Should eventually have glowcomm.html and glowcom.js both import this common component.
115+
// THE REST OF THIS FILE IS NEARLY IDENTICAL IN glowcomm.html AND glowcomm.js AND glowcommlab.js //
116+
117+
// Should eventually have glowcomm.html, glowcom.js, and glowcommlab.js all import this common component.
118+
119+
window.__GSlang = "vpython"
120+
box = vp_box
121+
sphere = vp_sphere
122+
simple_sphere = vp_simple_sphere
123+
cylinder = vp_cylinder
124+
pyramid = vp_pyramid
125+
cone = vp_cone
126+
helix = vp_helix
127+
ellipsoid = vp_ellipsoid
128+
ring = vp_ring
129+
arrow = vp_arrow
130+
compound = vp_compound
118131

119132
function msclock() {
120133
"use strict";
@@ -319,7 +332,7 @@
319332
}
320333

321334
function process_binding(event) { // event associated with a previous bind command
322-
"use strict";
335+
"use strict";
323336
event.bind = true
324337
process(event)
325338
}
@@ -572,8 +585,6 @@
572585
ptlist.push( o2vec3(val[kk]) )
573586
}
574587
cfg[attr] = ptlist
575-
} else if (attr === "axis" && obj == 'arrow') {
576-
cfg['axis_and_length'] = o2vec3(val)
577588
} else if (vlst.indexOf(attr) !== -1) {
578589
cfg[attr] = o2vec3(val)
579590
} else if (triangle_quad.indexOf(attr) !== -1) {
@@ -921,17 +932,6 @@
921932
ptlist.push( val[kk] )
922933
}
923934
obj[attr] = ptlist
924-
} else if (attr === 'axis') {
925-
// For axis and up, Python maintains them to be perpendicular, so avoid
926-
// having GlowScript do it again, which can have bad results.
927-
window.__adjustupaxis = false
928-
if (obj instanceof arrow) attr = 'axis_and_length'
929-
obj[attr] = val
930-
window.__adjustupaxis = true
931-
} else if (attr === 'up') {
932-
window.__adjustupaxis = false
933-
obj[attr] = val
934-
window.__adjustupaxis = true
935935
} else {
936936
obj[attr] = val
937937
}

vpython/vpython_libraries/glowcomm.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,22 @@ function send() { // periodically send events and update_canvas and request obje
162162
}
163163

164164
// *************************************************************************************************** //
165-
// THE REST OF THIS FILE IS IDENTICAL IN glowcomm.html AND glowcomm.js (except for the last few lines) //
166-
167-
// Should eventually have glowcomm.html and glowcom.js both import this common component.
165+
// THE REST OF THIS FILE IS NEARLY IDENTICAL IN glowcomm.html AND glowcomm.js AND glowcommlab.js //
166+
167+
// Should eventually have glowcomm.html, glowcom.js, and glowcommlab.js all import this common component.
168+
169+
window.__GSlang = "vpython"
170+
box = vp_box
171+
sphere = vp_sphere
172+
simple_sphere = vp_simple_sphere
173+
cylinder = vp_cylinder
174+
pyramid = vp_pyramid
175+
cone = vp_cone
176+
helix = vp_helix
177+
ellipsoid = vp_ellipsoid
178+
ring = vp_ring
179+
arrow = vp_arrow
180+
compound = vp_compound
168181

169182
function msclock() {
170183
"use strict";
@@ -369,7 +382,7 @@ function process_waitfor(event) {
369382
}
370383

371384
function process_binding(event) { // event associated with a previous bind command
372-
"use strict";
385+
"use strict";
373386
event.bind = true
374387
process(event)
375388
}
@@ -622,8 +635,6 @@ function handle_cmds(dcmds) {
622635
ptlist.push( o2vec3(val[kk]) )
623636
}
624637
cfg[attr] = ptlist
625-
} else if (attr === "axis" && obj == 'arrow') {
626-
cfg['axis_and_length'] = o2vec3(val)
627638
} else if (vlst.indexOf(attr) !== -1) {
628639
cfg[attr] = o2vec3(val)
629640
} else if (triangle_quad.indexOf(attr) !== -1) {
@@ -984,17 +995,6 @@ function handle_attrs(dattrs) {
984995
ptlist.push( val[kk] )
985996
}
986997
obj[attr] = ptlist
987-
} else if (attr === 'axis') {
988-
// For axis and up, Python maintains them to be perpendicular, so avoid
989-
// having GlowScript do it again, which can have bad results.
990-
window.__adjustupaxis = false
991-
if (obj instanceof arrow) attr = 'axis_and_length'
992-
obj[attr] = val
993-
window.__adjustupaxis = true
994-
} else if (attr === 'up') {
995-
window.__adjustupaxis = false
996-
obj[attr] = val
997-
window.__adjustupaxis = true
998998
} else {
999999
obj[attr] = val
10001000
}

0 commit comments

Comments
 (0)