From: Poul R. <Pou...@sk...> - 2011-10-12 20:29:46
|
I have slightly modified the faces_heightfield example in order to add another surface (see below). That works fine but how can I get rid of the old surface (not just make it invisible)? My long-term-purpose is to make an animation (the zvalues depending on time). Poul Riis ## Demonstrates some techniques for working with "faces", and ## shows how to build a height field (a common feature request) ## with it. ## David Scherer July 2001 ## Revised January 2010 by Bruce Sherwood to use faces.smooth() function ## introduced with VPython 5.2 ## Revised March 2010 by Bruce Sherwood to use faces.make_normals() and ## faces.make_twosided() functions introduced with VPython 5.3 from visual import * from time import * class Model: def __init__(self): self.frame = frame() self.model = faces(frame=self.frame, color=color.cyan) self.vertices = [] def FacetedTriangle(self, v1, v2, v3, color=color.white): """Add a triangle to the model""" for v in (v1,v2,v3): self.vertices.append(v) def FacetedPolygon(self, *v): """Appends a planar polygon of any number of vertices to the model""" for t in range(len(v)-2): self.FacetedTriangle( v[0], v[t+1], v[t+2] ) def DrawNormals(self, scale): pos = self.model.pos normal = self.model.normal for i in range(len(pos)): arrow(pos=pos[i], axis=normal[i]*scale) class Mesh (Model): def __init__(self, xvalues, yvalues, zvalues): Model.__init__(self) points = zeros( xvalues.shape + (3,), float ) points[...,0] = xvalues points[...,1] = yvalues points[...,2] = zvalues for i in range(zvalues.shape[0]-1): for j in range(zvalues.shape[1]-1): self.FacetedPolygon( points[i,j], points[i,j+1], points[i+1,j+1], points[i+1,j] ) self.model.pos = self.vertices self.model.make_normals() self.model.smooth() self.model.make_twosided() ## Graph a function of two variables (a height field) x = arange(-1,1,2./20) y = arange(-1,1,2./20) z = zeros( (len(x),len(y)), float ) x,y = x[:,None]+z, y+z Mesh( x, (sin(x*pi)+sin(y*pi))*0.2, y ) sleep(1) Mesh( x, (sin(x*pi)+sin(2*y*pi))*0.2, y ) |
From: Poul R. <Pou...@sk...> - 2011-10-13 06:24:50
|
Bru...@nc... writes: >To hide a Visual object just make it invisible: ball.visible = False. >This does not delete the object from computer memory, and you can make >it visible again later. > >If however you later re-use the name ball, for example by creating a >new object and naming it ball, Python will be free to release the >memory used by the object formerly named ball (assuming no other names >currently refer to that object). If the object is visible when you >re-use the name ball, the original object will not be deleted from >computer memory, and it will remain visible in the window. > >After you make an object named ball invisible, you can delete it >immediately with del ball. Oh, yes, that's from the manual. Nevertheless, I cannot make it work (see below). Poul Riis ## Demonstrates some techniques for working with "faces", and ## shows how to build a height field (a common feature request) ## with it. ## David Scherer July 2001 ## Revised January 2010 by Bruce Sherwood to use faces.smooth() function ## introduced with VPython 5.2 ## Revised March 2010 by Bruce Sherwood to use faces.make_normals() and ## faces.make_twosided() functions introduced with VPython 5.3 from visual import * from time import * class Model: def __init__(self): self.frame = frame() self.model = faces(frame=self.frame, color=color.cyan) self.vertices = [] def FacetedTriangle(self, v1, v2, v3, color=color.white): """Add a triangle to the model""" for v in (v1,v2,v3): self.vertices.append(v) def FacetedPolygon(self, *v): """Appends a planar polygon of any number of vertices to the model""" for t in range(len(v)-2): self.FacetedTriangle( v[0], v[t+1], v[t+2] ) def DrawNormals(self, scale): pos = self.model.pos normal = self.model.normal for i in range(len(pos)): arrow(pos=pos[i], axis=normal[i]*scale) class Mesh (Model): def __init__(self, xvalues, yvalues, zvalues): Model.__init__(self) points = zeros( xvalues.shape + (3,), float ) points[...,0] = xvalues points[...,1] = yvalues points[...,2] = zvalues for i in range(zvalues.shape[0]-1): for j in range(zvalues.shape[1]-1): self.FacetedPolygon( points[i,j], points[i,j+1], points[i+1,j+1], points[i+1,j] ) self.model.pos = self.vertices self.model.make_normals() self.model.smooth() self.model.make_twosided() ## Graph a function of two variables (a height field) x = arange(-1,1,2./20) y = arange(-1,1,2./20) z = zeros( (len(x),len(y)), float ) x,y = x[:,None]+z, y+z surface=Mesh( x, (sin(x*pi)+sin(y*pi))*0.2, y ) sleep(1) surface.visible=False del surface surface=Mesh( x, (sin(x*pi)+sin(2*y*pi))*0.2, y ) |
From: Bruce S. <Bru...@nc...> - 2011-10-13 18:19:15
|
The problem is that you haven't made the objects invisible before deleting them. You have the statement "surface.visible = False", but surface is a Mesh object, which doesn't have a visible attribute. If you instead say "surface.model.visible = False", you make the faces object invisible, and then "del surface" works. Bruce Sherwood On Thu, Oct 13, 2011 at 12:24 AM, Poul Riis <Pou...@sk...> wrote: > > > Bru...@nc... writes: > To hide a Visual object just make it invisible: ball.visible = False. > This does not delete the object from computer memory, and you can make > it visible again later. > If however you later re-use the name ball, for example by creating a > new object and naming it ball, Python will be free to release the > memory used by the object formerly named ball (assuming no other names > currently refer to that object). If the object is visible when you > re-use the name ball, the original object will not be deleted from > computer memory, and it will remain visible in the window. > After you make an object named ball invisible, you can delete it > immediately with del ball. > Oh, yes, that's from the manual. Nevertheless, I cannot make it work (see > below). > Poul Riis > ## Demonstrates some techniques for working with "faces", and > ## shows how to build a height field (a common feature request) > ## with it. > ## David Scherer July 2001 > ## Revised January 2010 by Bruce Sherwood to use faces.smooth() function > ## introduced with VPython 5.2 > ## Revised March 2010 by Bruce Sherwood to use faces.make_normals() and > ## faces.make_twosided() functions introduced with VPython 5.3 > from visual import * > from time import * > class Model: > def __init__(self): > self.frame = frame() > self.model = faces(frame=self.frame, color=color.cyan) > self.vertices = [] > def FacetedTriangle(self, v1, v2, v3, color=color.white): > """Add a triangle to the model""" > for v in (v1,v2,v3): > self.vertices.append(v) > def FacetedPolygon(self, *v): > """Appends a planar polygon of any number of vertices to the model""" > for t in range(len(v)-2): > self.FacetedTriangle( v[0], v[t+1], v[t+2] ) > def DrawNormals(self, scale): > pos = self.model.pos > normal = self.model.normal > for i in range(len(pos)): > arrow(pos=pos[i], axis=normal[i]*scale) > class Mesh (Model): > def __init__(self, xvalues, yvalues, zvalues): > Model.__init__(self) > points = zeros( xvalues.shape + (3,), float ) > points[...,0] = xvalues > points[...,1] = yvalues > points[...,2] = zvalues > for i in range(zvalues.shape[0]-1): > for j in range(zvalues.shape[1]-1): > self.FacetedPolygon( points[i,j], points[i,j+1], > points[i+1,j+1], points[i+1,j] ) > > self.model.pos = self.vertices > self.model.make_normals() > self.model.smooth() > self.model.make_twosided() > ## Graph a function of two variables (a height field) > x = arange(-1,1,2./20) > y = arange(-1,1,2./20) > z = zeros( (len(x),len(y)), float ) > x,y = x[:,None]+z, y+z > surface=Mesh( x, (sin(x*pi)+sin(y*pi))*0.2, y ) > sleep(1) > surface.visible=False > del surface > surface=Mesh( x, (sin(x*pi)+sin(2*y*pi))*0.2, y ) > > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure contains a > definitive record of customers, application performance, security > threats, fraudulent activity and more. Splunk takes this data and makes > sense of it. Business sense. IT sense. Common sense. > http://p.sf.net/sfu/splunk-d2d-oct > _______________________________________________ > Visualpython-users mailing list > Vis...@li... > https://lists.sourceforge.net/lists/listinfo/visualpython-users > > |
From: Poul R. <Pou...@sk...> - 2011-10-14 08:22:59
|
Now it works! (please try my further developed example below). However, it is far too slow. Is there any possibility to speed it up? How can I add colors depending on the z-value? Poul Riis from visual import * from time import * class Model: def __init__(self): self.frame = frame() self.model = faces(frame=self.frame, color=color.blue) self.vertices = [] def FacetedTriangle(self, v1, v2, v3, color=color.red): """Add a triangle to the model""" for v in (v1,v2,v3): self.vertices.append(v) def FacetedPolygon(self, *v): """Appends a planar polygon of any number of vertices to the model""" for t in range(len(v)-2): self.FacetedTriangle( v[0], v[t+1], v[t+2] ) self.FacetedTriangle( v[0], v[t+2], v[t+1] ) def DrawNormals(self, scale): pos = self.model.pos normal = self.model.normal for i in range(len(pos)): arrow(pos=pos[i], axis=normal[i]*scale) class Mesh (Model): def __init__(self, xvalues, yvalues, zvalues): Model.__init__(self) points = zeros( xvalues.shape + (3,), float ) points[...,0] = xvalues points[...,1] = yvalues points[...,2] = zvalues for i in range(zvalues.shape[0]-1): for j in range(zvalues.shape[1]-1): self.FacetedPolygon( points[i,j], points[i,j+1], points[i+1,j+1], points[i+1,j] ) self.model.pos = self.vertices self.model.make_normals() self.model.smooth() self.model.make_twosided() dxy=0.02 # Length of x-y-intervals x = arange(-1,1+dxy,dxy) y = arange(-1,1+dxy,dxy) d=0.8 # Distance between two oscillators dh=d/2 # Half that distance lambdah=d/5 # Wavelength k=2*pi/lambdah T=10 # Oscillator period A=0.025 # Oscillator amplitude omega=2*pi/T m1=12 #Number of wavelengths from oscillator 1 to point of constructive interference m2=10 #Number of wavelengths from oscillator 2 to point of constructive interference n=m1-m2 m=m2 xci=n*(n+2*m)*lambdah**2/2/d yci=sqrt((d**2-(n*lambdah)**2)*(((n+2*m)*lambdah)**2-d**2))/2/d print('Point of constructive interference: (',xci,yci,')') print(sqrt((xci-dh)**2+yci**2)/lambdah) print(sqrt((xci+dh)**2+yci**2)/lambdah) N1=m*20 N2=(m+n)*20 a1points=[] a2points=[] for i in range(0,N1+1): a1points.append((i*(xci-dh)/N1+dh,2*A*cos(i/N1*m*2*pi),1-i*yci/N1)) for i in range(0,N2+1): a2points.append((i*(xci+dh)/N2-dh,2*A*cos(i/N2*(m+n)*2*pi),1-i*yci/N2)) def f(x,y,t): return A*(sin(k*sqrt((x-dh)*(x-dh)+y*y)-omega*t)+sin(k*sqrt((x+dh)*(x+dh)+y*y)-omega*t)) source1=sphere(radius=0.02,pos=(dh,0,1),color=color.yellow) source2=sphere(radius=0.02,pos=(-dh,0,1),color=color.red) z = zeros( (len(x),len(y)), float ) x,y = x[:,None]+z, y+z curve(pos=a1points,color=color.red) curve(pos=a2points,color=color.yellow) Ncp=20 ncmax=floor(d/lambdah) if ncmax*lambdah==d: ncmax=ncmax-1 for nc in range(1,ncmax+1): bhyp=sqrt(-(nc*lambdah/2)**2+(d/2)**2) ahyp=bhyp*nc/sqrt(-nc*nc+(d/lambdah)**2) print('ahyp,bhyp:',ahyp,bhyp) txmax=acosh(1/ahyp) tymax=acosh(2/bhyp) tmax=txmax if tymax<txmax: tmax=tymax hyppoints=[] for i in range(0,Ncp+1): t=i*tmax/Ncp hyppoints.append((ahyp*cosh(t),2*A,-bhyp*sinh(t)+1)) curve(pos=hyppoints,color=color.green) hyppoints=[] for i in range(0,Ncp+1): t=i*tmax/Ncp hyppoints.append((-ahyp*cosh(t),2*A,-bhyp*sinh(t)+1)) curve(pos=hyppoints,color=color.green) hyppoints=[] hyppoints.append((0,2*A,-1)) hyppoints.append((0,2*A,1)) curve(pos=hyppoints,color=color.green) for i in range(0,100): surface=Mesh( x, f(x,y-1,i) , y ) #sleep(0.01) surface.model.visible=False del surface source1.pos=(-dh,-A*sin(omega*i),1) source2.pos=(dh,-A*sin(omega*i),1) i=100 surface=Mesh( x, f(x,y-1,i) , y ) |
From: Bruce S. <Bru...@nc...> - 2011-10-14 18:39:23
|
Your program is too long and complex for me to comment on the speed issues. Concerning color, I'm not sure exactly what your issue is. You can calculate colors and then use them; for example you could do something like mycolor = (1,1,k*z), where k*z is between 0 and 1, and then set some object's color attribute to mycolor. Also note that the faces object lets you specify the colors of all vertices individually. A general comment about speed is that if you can structure a program to use the parallelism of numpy arrays you can get large speedups in the computations. Among the example programs that use this technique are gas, stars, and toroid_drag. However, numpy can be rather hard to use. Bruce Sherwood On Fri, Oct 14, 2011 at 2:22 AM, Poul Riis <Pou...@sk...> wrote: > Now it works! (please try my further developed example below). > However, it is far too slow. > Is there any possibility to speed it up? > How can I add colors depending on the z-value? > > Poul Riis > |
From: Poul R. <Pou...@sk...> - 2011-10-14 19:13:49
|
Bru...@nc... writes: >Also note that the >faces object lets you specify the colors of all vertices individually. Yes, I know, but even the original example 'faces_heightfield' is too complicated for me to see how I can add these colors. Can someone give me just a little hint? Poul Riis |
From: Bruce S. <Bru...@nc...> - 2011-10-14 19:51:02
|
I'm not sure what it is that you're asking about colors. Here is the key part of the documenation on the faces object: You can append additional vertices to an existing faces object with one of the following four forms (assuming the object is named f): f.append(pos=(x,y,z)) f.append(pos=(x,y,z), normal=(nx,ny,nz)) f.append(pos=(x,y,z), normal=(nx,ny,nz), color=(r,g,b)) f.append(pos=(x,y,z), normal=(nx,ny,nz), red=r, green=g, blue=b) Another convenient way to construct a faces object is to create a list or numpy array of positions, then create the faces object with the pos attribute set to the list or array. Then use make_normals(), make_twosided(), and smooth() to complete the task. On Fri, Oct 14, 2011 at 1:13 PM, Poul Riis <Pou...@sk...> wrote: > Bru...@nc... writes: > Also note that the > faces object lets you specify the colors of all vertices individually. > Yes, I know, but even the original example 'faces_heightfield' is too > complicated for me to see how I can add these colors. Can someone give me > just a little hint? > Poul Riis |
From: Poul R. <Pou...@sk...> - 2011-10-14 19:16:29
|
Bru...@nc... writes: >A general comment about speed is that if you can structure a program >to use the parallelism of numpy arrays you can get large speedups in >the computations Would it be possible that someone could rewrite the faces_heightfield-example using the numpy arrays? Poul Riis |
From: Bruce S. <Bru...@nc...> - 2011-10-12 21:28:11
|
>From the documentation section "Delete an object": To hide a Visual object just make it invisible: ball.visible = False. This does not delete the object from computer memory, and you can make it visible again later. If however you later re-use the name ball, for example by creating a new object and naming it ball, Python will be free to release the memory used by the object formerly named ball (assuming no other names currently refer to that object). If the object is visible when you re-use the name ball, the original object will not be deleted from computer memory, and it will remain visible in the window. After you make an object named ball invisible, you can delete it immediately with del ball. On Wed, Oct 12, 2011 at 2:29 PM, Poul Riis <Pou...@sk...> wrote: > > I have slightly modified the faces_heightfield example in order to add > another surface (see below). > That works fine but how can I get rid of the old surface (not just make it > invisible)? > My long-term-purpose is to make an animation (the zvalues depending on > time). > > Poul Riis |