Skip to content

Commit c97884e

Browse files
Merge pull request #126 from BruceSherwood/Fix_triangle_quad_bug
Fix bug in replacing a triangle/quad vertex
2 parents 55b4e4e + 712978e commit c97884e

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

labextension/vpython/src/glowcommlab.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ var textattrs = ['text', 'align', 'caption', 'title', 'title_align', 'xtitle', '
448448
// patt gets idx and attr code; vpatt gets x,y,z of a vector
449449
var patt = /(\d+)(.)(.*)/
450450
var vpatt = /([^,]*),([^,]*),(.*)/
451+
var quadpatt = /([^,]*),([^,]*),(.*)/
451452
var plotpatt = /([^,]*),([^,]*)/
452453

453454
function decode(data) {
@@ -476,6 +477,15 @@ function decode(data) {
476477
if (vecattrs.indexOf(attr) > -1) {
477478
val = m[3].match(vpatt)
478479
val = vec(Number(val[1]), Number(val[2]), Number(val[3]))
480+
} else if (attr == 'vs') {
481+
var vs
482+
val = m[3].match(quadpatt)
483+
if (val === null) {
484+
val = m[3].match(vpatt)
485+
vs = [Number(val[1]), Number(val[2]), Number(val[3])]
486+
} else {
487+
vs = [Number(val[1]), Number(val[2]), Number(val[3]), Number(val[4])]
488+
}
479489
} else if (textattrs.indexOf(attr) > -1) {
480490
// '\n' doesn't survive JSON transmission, so in vpython.py we replace '\n' with '<br>'
481491
val = m[3].replace(/<br>/g, "\n")
@@ -977,6 +987,9 @@ function handle_attrs(dattrs) {
977987
} else {
978988
if (triangle_quad.indexOf(attr) !== -1) {
979989
obj[attr] = glowObjs[val]
990+
} else if (attr = 'vs') {
991+
if (val.length == 3) obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]] ]
992+
else obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]], glowObjs[val[3]] ]
980993
} else {
981994
obj[attr] = val
982995
}

vpython/vpython.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
from math import sqrt, tan, pi
77

8-
from time import clock
98
import time
9+
try:
10+
clock = time.perf_counter # time.clock is deprecated in Python 3.3, gone in 3.8
11+
except:
12+
clock = time.clock
1013
import sys
1114
from . import __version__, __gs_version__
1215
from ._notebook_helpers import _isnotebook
@@ -127,6 +130,13 @@ def _encode_attr2(sendval, val, ismethods):
127130
for p in val:
128131
s += "{:.16G},{:.16G},".format(p[0], p[1])
129132
s = s[:-1]
133+
elif sendval in ['v0', 'v1', 'v2', 'v3']: # val is the vertex object referenced by triangle or quad
134+
s += str(val.idx)
135+
elif sendval == 'vs':
136+
if len(val) == 3:
137+
s += "{d},{d},{d}".format(val[0].idx, val[1].idx, val[2].idx)
138+
else:
139+
s += "{d},{d},{d},{d}".format(val[0].idx, val[1].idx, val[2].idx, val[3].idx)
130140
elif ismethods:
131141
#if sendval in ['follow', 'waitfor', 'delete']: val = str(val) # scene.camera.follow(object idx)
132142
s += str(val)
@@ -1643,32 +1653,32 @@ def v0(self):
16431653
return self._v0
16441654
@v0.setter
16451655
def v0(self, value):
1646-
self._v0 = value
16471656
if not self._constructing:
16481657
self._v0.decrementTriangleCount() ## current v0 now used less
16491658
self.addattr('v0')
1659+
self._v0 = value
16501660
self._v0.incrementTriangleCount() ## new v0 now used more
16511661

16521662
@property
16531663
def v1(self):
16541664
return self._v1
16551665
@v1.setter
16561666
def v1(self, value):
1657-
self._v1 = value
16581667
if not self._constructing:
16591668
self._v1.decrementTriangleCount() ## current v1 now used less
16601669
self.addattr('v1')
1670+
self._v1 = value
16611671
self._v1.incrementTriangleCount() ## new v1 now used more
16621672

16631673
@property
16641674
def v2(self):
16651675
return self._v2
16661676
@v2.setter
16671677
def v2(self, value):
1668-
self._v2 = value
16691678
if not self._constructing:
16701679
self._v2.decrementTriangleCount() ## current v2 now used less
16711680
self.addattr('v2')
1681+
self._v2 = value
16721682
self._v2.incrementTriangleCount() ## new v2 now used more
16731683

16741684
@property
@@ -1713,10 +1723,10 @@ def v3(self):
17131723
return self._v3
17141724
@v3.setter
17151725
def v3(self, value):
1716-
self._v3 = value
17171726
if not self._constructing:
17181727
self._v3.decrementTriangleCount() ## current v3 now used less
17191728
self.addattr('v3')
1729+
self._v3 = value
17201730
self._v3.incrementTriangleCount() ## new v3 now used more
17211731

17221732
@property

vpython/vpython_libraries/glowcomm.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@
395395
// patt gets idx and attr code; vpatt gets x,y,z of a vector
396396
var patt = /(\d+)(.)(.*)/
397397
var vpatt = /([^,]*),([^,]*),(.*)/
398+
var quadpatt = /([^,]*),([^,]*),(.*)/
398399
var plotpatt = /([^,]*),([^,]*)/
399400

400401
function decode(data) {
@@ -423,6 +424,15 @@
423424
if (vecattrs.indexOf(attr) > -1) {
424425
val = m[3].match(vpatt)
425426
val = vec(Number(val[1]), Number(val[2]), Number(val[3]))
427+
} else if (attr == 'vs') {
428+
var vs
429+
val = m[3].match(quadpatt)
430+
if (val === null) {
431+
val = m[3].match(vpatt)
432+
vs = [Number(val[1]), Number(val[2]), Number(val[3])]
433+
} else {
434+
vs = [Number(val[1]), Number(val[2]), Number(val[3]), Number(val[4])]
435+
}
426436
} else if (textattrs.indexOf(attr) > -1) {
427437
// '\n' doesn't survive JSON transmission, so in vpython.py we replace '\n' with '<br>'
428438
val = m[3].replace(/<br>/g, "\n")
@@ -910,6 +920,9 @@
910920
} else {
911921
if (triangle_quad.indexOf(attr) !== -1) {
912922
obj[attr] = glowObjs[val]
923+
} else if (attr = 'vs') {
924+
if (val.length == 3) obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]] ]
925+
else obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]], glowObjs[val[3]] ]
913926
} else {
914927
obj[attr] = val
915928
}

vpython/vpython_libraries/glowcomm.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ var textattrs = ['text', 'align', 'caption', 'title', 'title_align', 'xtitle', '
425425
// patt gets idx and attr code; vpatt gets x,y,z of a vector
426426
var patt = /(\d+)(.)(.*)/
427427
var vpatt = /([^,]*),([^,]*),(.*)/
428+
var quadpatt = /([^,]*),([^,]*),(.*)/
428429
var plotpatt = /([^,]*),([^,]*)/
429430

430431
function decode(data) {
@@ -453,6 +454,15 @@ function decode(data) {
453454
if (vecattrs.indexOf(attr) > -1) {
454455
val = m[3].match(vpatt)
455456
val = vec(Number(val[1]), Number(val[2]), Number(val[3]))
457+
} else if (attr == 'vs') {
458+
var vs
459+
val = m[3].match(quadpatt)
460+
if (val === null) {
461+
val = m[3].match(vpatt)
462+
vs = [Number(val[1]), Number(val[2]), Number(val[3])]
463+
} else {
464+
vs = [Number(val[1]), Number(val[2]), Number(val[3]), Number(val[4])]
465+
}
456466
} else if (textattrs.indexOf(attr) > -1) {
457467
// '\n' doesn't survive JSON transmission, so in vpython.py we replace '\n' with '<br>'
458468
val = m[3].replace(/<br>/g, "\n")
@@ -953,6 +963,9 @@ function handle_attrs(dattrs) {
953963
} else {
954964
if (triangle_quad.indexOf(attr) !== -1) {
955965
obj[attr] = glowObjs[val]
966+
} else if (attr = 'vs') {
967+
if (val.length == 3) obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]] ]
968+
else obj[attr]['vs'] = [ glowObjs[val[0]], glowObjs[val[1]], glowObjs[val[2]], glowObjs[val[3]] ]
956969
} else {
957970
obj[attr] = val
958971
}

0 commit comments

Comments
 (0)