|
From: Mike M. <mmu...@dg...> - 2000-11-27 10:29:03
|
Hi,
enclosed is a class WireBox which implements as box which edges. That is I
tried to have an object that behaves just like box() but instead of colored
side plains it only has a wire frame of the edges.
The code works pretty much as I expected it to (just copy into an empty
file and run it to see what I mean). The only problem is speed and memory
consumption which is several orders of magnitude higher than for box().
Since I need several hundred or more boxes speed is kind of a problem.
Is there as possibility to include an edged box whether:
- by adding edgeColor and edgeRadius to the box object (default edgeColor =
box().color --> no edge) and color = none to make the filled box() invisible
or
- by the means of an new object that inherits from box() but has the
additional features (edgeColor, edgeRadius, color = none).
Also making my implementation faster would be an option.
Any ideas or hints are be appreciated.
Thanks.
Mike
import time
from visual import *
class WireBox:
"""Wire frame box.
Edges are implemented as curves.
"""
def __init__(self, pos=(0,0,0), length=1, height=1, width=1,
color=color.white, radius = 0.005, visible = 1):
# use of __dict__ because of __setattr__
# self.__dict__['frame'] = frame() instead of self.frame = frame()
# --> avoid recursive refrences to __setattr__
self.__dict__['frame'] = frame()
self.__dict__['pos'] = pos
self.__dict__['length'] = length
self.__dict__['height'] = height
self.__dict__['width'] = width
self.__dict__['color'] = color
self.__dict__['radius'] = radius
self.__dict__['visible'] = visible
self.__dict__['x1'] = self.pos[0] - self.length/2.0
self.__dict__['x2'] = self.pos[0] + self.length/2.0
self.__dict__['y1'] = self.pos[1] - self.height/2.0
self.__dict__['y2'] = self.pos[1] + self.height/2.0
self.__dict__['z1'] = self.pos[2] - self.width/2.0
self.__dict__['z2'] = self.pos[2] + self.width/2.0
#lower plane
self.__dict__['lplane'] = curve(frame = self.frame, pos=[(self.x1,
self.y1, self.z1),
(self.x2, self.y1, self.z1),
(self.x2, self.y1, self.z2),
(self.x1, self.y1, self.z2),
(self.x1, self.y1, self.z1)],
color = self.color, visible = self.visible,
radius = self.radius)
#upper plane
self.__dict__['uplane'] = curve(frame = self.frame, pos=[(self.x1,
self.y2, self.z1),
(self.x2, self.y2, self.z1),
(self.x2, self.y2, self.z2),
(self.x1, self.y2, self.z2),
(self.x1, self.y2, self.z1)],
color = self.color, visible = self.visible,
radius = self.radius)
#vertical connector 1
self.__dict__['v1'] = curve(frame = self.frame, pos=[(self.x1,
self.y1, self.z1),
(self.x1, self.y2, self.z1)],
color = self.color, visible = self.visible,
radius = self.radius)
#vertical connector 2
self.__dict__['v2'] = curve(frame = self.frame, pos=[(self.x2,
self.y1, self.z1),
(self.x2, self.y2, self.z1)],
color = self.color, visible = self.visible,
radius = self.radius)
#vertical connector 3
self.__dict__['v3'] = curve(frame = self.frame, pos=[(self.x2,
self.y1, self.z2),
(self.x2, self.y2, self.z2)],
color = self.color, visible = self.visible,
radius = self.radius)
#vertical connector 4
self.__dict__['v4'] = curve(frame = self.frame, pos=[(self.x1,
self.y1, self.z2),
(self.x1, self.y2, self.z2)],
color = self.color, visible = self.visible,
radius = self.radius)
def __setattr__(self, attrname, value):
if attrname == 'visible':
self.lplane.visible = self.uplane.visible = value
self.v1.visible = self.v2.visible = value
self.v3.visible = self.v4.visible = value
elif attrname == 'radius':
self.lplane.radius = self.uplane.radius = value
self.v1.radius = self.v2.radius = value
self.v3.radius = self.v4.radius = value
elif attrname == 'color':
self.lplane.color = self.uplane.color = value
self.v1.color = self.v2.color = value
self.v3.color = self.v4.color = value
else:
raise AttributeError, attrname
if __name__ == "__main__":
"""
Testing some different radii, colors, and visibilities
for filled and empty wire frame box.
"""
myBox = box()
myWire = WireBox(color = color.red)
myWire.radius = 0.05
time.sleep(1)
myBox.visible = 0
time.sleep(1)
myWire.visible = 0
time.sleep(1)
myBox.visible = 0
myWire.visible = 1
myWire.radius = 0.01
myWire.color = color.green
time.sleep(1)
myBox.visible = 1
myWire.visible = 0
time.sleep(1)
myBox.visible = 1
myWire.visible = 1
|