From: Shaun P. <sha...@an...> - 2003-05-12 05:26:11
|
On Sat, 2003-05-10 at 16:21, Gary Ruben wrote: > Hi fellow VPython Users, > A few times in my code I have wanted to show a trail behind some moving object. > For example, you might want to see the path of the free end of the > double pendulum in the doublependulum.py example. To do this, you could add the > following code: > > <code> > trailLength = 10000 # no of points in trail > trail = curve(color=(0.5,0.5,0.5)) > > # the existing while loop > while 1: > . > . other code here > . > posnew = frame2.pos+frame2.axis*L2 > trail.append(pos=posnew) > # Now keep the trail length finite > if len(trail.pos) > trailLength: > trail.pos = trail.pos[20:] > > t = t+dt > > </code> > > However, I have encountered a problem with doing this which I assume is to do > with the curve being held in a Numeric array structure. If I understand Numeric > correctly, trail.pos = trail.pos[20:] won't cause a copy or truncation, just a > change to the offset to the start of the array, so the array/curve object will > continue to grow despite only showing the last 10000 points (in this example). > If a copy could be forced, the curve object would have to be reassociated with > the new array object, so I don't think a copy is occurring. A copy would be > very inefficient anyway. > Actually, when I run this example, everything seems to work OK, but in another > example I have, if I leave it running, the vpython window eventually locks up > and the lockup is associated with the code I have shown. I'm running Win98. > So, has anyone got a more efficient way to limit the length of a curve to some > maximum? Do others ever do this sort of thing and if so, do they agree that it > would be nice to have a property such as maxlength added to the curve object? > > Gary Under the current implementation the curve grows without bound, although a maximum of 1024 points are shown on the screen. Once this limit is exceeded only every N point is shown, N being (length/1024)+1 The memory model btw is that once the array limit of points is reached, a new array twice the size is created and all the points are copied to it. A maxlength property might be a solution but once you have reached the limit you would take a performance hit as you would have to loop though the points making point[N] = point[N+1] before appending the new point. But then again this may be a solution. As part of my Masters project I have created a new VPython object called <points> These are sets of discrete points rendered as points on the screen and are intended for use in the simulation of particle systems. A problem I encountered during the implementation of this shape is similar to yours. My problem is "What do with unnecesary points?". The obvious answer is to delete them, but the points model was based upon the curve object (without the connecting lines) and therefore didn't have a delete method. So I added one. When a point is deleted I do what I described above eg Loop from the deletion point to the end of the set replacing the position and colour of point[N] with point[N+1], and modifying the length attribute as well. It doesn't cause the same performance hit, as points are appened and deleted at different time and rates, but could be used to simulate exactly what you want to do eg posnew = frame2.pos+frame2.axis*L2 if (len(trail.pos) == trailLength): trail.delete(0) trail.append(pos=posnew) |