Skip to content

Commit 31397e4

Browse files
committed
Improve compound
1 parent 574919e commit 31397e4

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

vpython/vpython.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ class standardAttributes(baseObj):
522522
['visible'],
523523
[]],
524524
'compound':[['pos', 'color', 'trail_color'],
525-
['axis', 'size', 'up'],
525+
['axis', 'size', 'up', 'origin'],
526526
['visible', 'opacity','shininess', 'emissive',
527527
'make_trail', 'trail_type', 'interval', 'texture',
528528
'retain', 'trail_color', 'trail_radius', 'obj_idxs', 'pickable'],
@@ -1528,32 +1528,34 @@ def size(self,value): # compound axis and size don't interact
15281528
if not self._constructing:
15291529
self.addattr('size')
15301530

1531-
def _world_zaxis(self):
1532-
axis = self._axis
1533-
up = norm(self._up)
1534-
if abs(axis.dot(up)) / sqrt(axis.mag2) > 0.98:
1535-
if abs(norm(axis).dot(vector(-1,0,0))) > 0.98:
1536-
z_axis = axis.cross(vector(0,0,1)).norm()
1537-
else:
1538-
z_axis = axis.cross(vector(-1,0,0)).norm()
1539-
else:
1540-
z_axis = axis.cross(up).norm()
1541-
return z_axis
1531+
@property
1532+
def origin(self):
1533+
return self._origin
1534+
@origin.setter
1535+
def origin(self,value): # compound origin cannot be reset
1536+
if not self._constructing:
1537+
raise AttributeError('The compound "origin" attribute is read-only; change "pos" instead.')
1538+
self._origin = value
15421539

15431540
def world_to_compound(self, v):
1544-
axis = self._axis
1545-
z_axis = self._world_zaxis()
1546-
y_axis = z_axis.cross(axis).norm()
1547-
x_axis = axis.norm()
1548-
v = v - self._pos
1549-
return vector(v.dot(x_axis), v.dot(y_axis), v.dot(z_axis))
1541+
v = v-self._pos
1542+
x_axis = self._axis.hat
1543+
y_axis = self._up.hat
1544+
z_axis = x_axis.cross(y_axis)
1545+
ox = self._size0.x/self._size.x # _size0 is the original size
1546+
oy = self._size0.y/self._size.y
1547+
oz = self._size0.z/self._size.z
1548+
return self._origin + vector(v.dot(x_axis)*ox, v.dot(y_axis)*oy, v.dot(z_axis)*oz)
15501549

15511550
def compound_to_world(self, v):
1552-
axis = self._axis
1553-
z_axis = self._world_zaxis()
1554-
y_axis = z_axis.cross(axis).norm()
1555-
x_axis = axis.norm()
1556-
return self._pos+(v.x*x_axis) + (v.y*y_axis) + (v.z*z_axis)
1551+
v = v-self._origin
1552+
x_axis = self._axis.hat
1553+
y_axis = self._up.hat
1554+
z_axis = x_axis.cross(y_axis)
1555+
ox = self._size.x/self._size0.x # _size0 is the original size
1556+
oy = self._size.y/self._size0.y
1557+
oz = self._size.z/self._size0.z
1558+
return self._pos + v.x*ox*x_axis + v.y*oy*y_axis + v.z*oz*z_axis
15571559

15581560
class vertex(standardAttributes):
15591561
def __init__(self, **args):
@@ -3104,6 +3106,7 @@ def handle_event(self, evt): ## events and scene info updates
31043106
self.mouse.setpick( evt )
31053107
self._waitfor = True # what pick is looking for
31063108
elif ev == '_compound': # compound, text, extrusion
3109+
print('compound event return')
31073110
obj = self._compound
31083111
p = evt['pos']
31093112
if obj._objName == 'text':
@@ -3115,7 +3118,7 @@ def handle_event(self, evt): ## events and scene info updates
31153118
# on_change functions that detect changes in e.g. obj.pos.y
31163119
obj._pos.value = list_to_vec(p)
31173120
s = evt['size']
3118-
obj._size.value = list_to_vec(s)
3121+
obj._size.value = obj._size0 = list_to_vec(s)
31193122
obj._axis.value = obj._size._x*norm(obj._axis)
31203123
obj._up.value = list_to_vec(evt['up'])
31213124
self._waitfor = True # what compound and text and extrusion are looking for in _wait()

vpython/vpython_libraries/glow.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)