Debian Codegraph @

examples/python/waterline/waterline_9_conecutter_anim.py

raw
1 from opencamlib import ocl, camvtk
2 import time
3 import math
4 import os
5
6 def calcWaterline(zh, cutter,s):
7 wl = ocl.Waterline()
8 #wl = ocl.AdaptiveWaterline()
9 wl.setSTL(s)
10 wl.setCutter(cutter)
11 wl.setZ(zh)
12 wl.setSampling(0.02)
13 wl.setThreads(2)
14 t_before = time.time()
15 wl.run()
16 t_after = time.time()
17 calctime = t_after-t_before
18 print(" Waterline done in ", calctime," s")
19 out = []
20 out.append(wl.getLoops())
21 out.append(wl.getXFibers())
22 out.append(wl.getYFibers())
23 return out
24
25 def drawFiber(myscreen, f, fibercolor=camvtk.red):
26 inter = f.getInts()
27 for i in inter:
28 if not i.empty():
29 ip1 = f.point( i.lower )
30 ip2 = f.point( i.upper )
31 ballradius = 0.0005
32 myscreen.addActor( camvtk.Line(p1=(ip1.x,ip1.y,ip1.z),p2=(ip2.x,ip2.y,ip2.z), color=fibercolor) )
33 myscreen.addActor( camvtk.Sphere(center=(ip1.x,ip1.y,ip1.z),radius=ballradius, color=camvtk.clColor( i.lower_cc) ) )
34 myscreen.addActor( camvtk.Sphere(center=(ip2.x,ip2.y,ip2.z),radius=ballradius, color=camvtk.clColor( i.upper_cc) ) )
35 cc1 = i.lower_cc
36 cc2 = i.upper_cc
37 myscreen.addActor( camvtk.Sphere(center=(cc1.x,cc1.y,cc1.z),radius=ballradius, color=camvtk.lgreen ) )
38 myscreen.addActor( camvtk.Sphere(center=(cc2.x,cc2.y,cc2.z),radius=ballradius, color=camvtk.lgreen ) )
39 # cutter circle
40 #c1 = camvtk.Circle(center=(ip1.x,ip1.y,ip1.z), radius = 0.3/2, color=fibercolor)
41 #myscreen.addActor(c1)
42 #c2 = camvtk.Circle(center=(ip2.x,ip2.y,ip2.z), radius = 0.3/2, color=fibercolor)
43 #myscreen.addActor(c2)
44
45 def drawLoops(myscreen, loops, loopColor=camvtk.yellow):
46 nloop=0
47 zofz = 0.01
48 for lop in loops:
49 n = 0
50 N = len(lop)
51 first_point=ocl.Point(-1,-1,5)
52 previous=ocl.Point(-1,-1,5)
53 for p in lop:
54 if n==0: # don't draw anything on the first iteration
55 previous=p
56 first_point = p
57 elif n== (N-1): # the last point
58 myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z+zofz),p2=(p.x,p.y,p.z+zofz),color=loopColor) ) # the normal line
59 # and a line from p to the first point
60 myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z+zofz),p2=(first_point.x,first_point.y,first_point.z+zofz),color=loopColor) )
61 else:
62 myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z+zofz),p2=(p.x,p.y,p.z+zofz),color=loopColor) )
63 previous=p
64 n=n+1
65 zofz = zofz +0.01
66 print("rendered loop ",nloop, " with ", len(lop), " points at zofz=",zofz)
67 if len(lop)==2:
68 for p in lop:
69 print(p)
70 myscreen.addActor( camvtk.Sphere(center=(p.x,p.y,p.z+zofz),radius=0.0005, color=camvtk.pink ) )
71 nloop = nloop+1
72
73 def drawScreen(a,b,c,filename,write_flag):
74 print(ocl.version())
75 myscreen = camvtk.VTKScreen()
76 #a = ocl.Point(0,1,0.3)
77 myscreen.addActor(camvtk.Point(center=(a.x,a.y,a.z), color=(1,0,1)))
78 #b = ocl.Point(1,0.5,0.3)
79 myscreen.addActor(camvtk.Point(center=(b.x,b.y,b.z), color=(1,0,1)))
80 #c = ocl.Point(-0.1,0.3,0.0)
81 myscreen.addActor(camvtk.Point(center=(c.x,c.y,c.z), color=(1,0,1)))
82 myscreen.addActor( camvtk.Line(p1=(a.x,a.y,a.z),p2=(c.x,c.y,c.z)) )
83 myscreen.addActor( camvtk.Line(p1=(c.x,c.y,c.z),p2=(b.x,b.y,b.z)) )
84 myscreen.addActor( camvtk.Line(p1=(a.x,a.y,a.z),p2=(b.x,b.y,b.z)) )
85 t = ocl.Triangle(b,c,a)
86 s = ocl.STLSurf()
87 s.addTriangle(t) # a one-triangle STLSurf
88 zheights=[-0.3, -0.2, -0.1, -0.05, 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.26, 0.27, 0.28, 0.29 ] # the z-coordinates for the waterlines
89 zheights=[-0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, -0.05, 0.0, 0.05, 0.1, 0.15, 0.2, 0.28 ]
90 zheights=[ -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.28]
91
92 zheights=[]
93 Nmax=20
94 zmin=-0.5
95 zmax= -0.05
96 dz = (zmax-zmin)/float(Nmax-1)
97 z = zmin
98 for n in range(Nmax):
99 zheights.append(z)
100 z=z+dz
101
102 zheights=[]
103 zheights.append(-0.25)
104 #zheights=[ -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25]
105 #zheights=[ 0.1]
106
107 length = 10
108 diam = 0.6
109 cutter1 = ocl.CylCutter( diam , length )
110 cutter2 = ocl.BallCutter( diam , length )
111 cutter3 = ocl.BullCutter( diam , diam/5, length )
112 cutter4 = ocl.ConeCutter( diam , math.pi/5, length )
113
114 for zh in zheights:
115 #loops = calcWaterline(zh, cutter1, s)
116 #drawLoops(myscreen, loops[0], camvtk.yellow)
117
118 #loops = calcWaterline(zh, cutter2, s)
119 #drawLoops(myscreen, loops[0], camvtk.green)
120 #loops = calcWaterline(zh, cutter3, s)
121 #drawLoops(myscreen, loops[0], camvtk.yellow)
122
123 loops = calcWaterline(zh, cutter4, s)
124 drawLoops(myscreen, loops[0], camvtk.pink)
125
126 #for f in loops[1]:
127 # drawFiber(myscreen, f, camvtk.red)
128 #for f in loops[2]:
129 # drawFiber(myscreen, f, camvtk.lblue)
130
131
132
133
134 print("done.")
135 myscreen.camera.SetPosition(1, -1, 3)
136 myscreen.camera.SetFocalPoint(0.5, 0.5, 0)
137 camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5))
138 camvtk.drawOCLtext(myscreen)
139 myscreen.render()
140
141 """
142 w2if = vtk.vtkWindowToImageFilter()
143 w2if.SetInput(myscreen.renWin)
144 lwr = vtk.vtkPNGWriter()
145 lwr.SetInput( w2if.GetOutput() )
146 w2if.Modified()
147 lwr.SetFileName(filename)
148 if write_flag:
149 lwr.Write()
150 print("wrote ",filename)
151 """
152
153 time.sleep(1)
154 #myscreen.iren.Start()
155 #raw_input("Press Enter to terminate")
156
157
158 if __name__ == "__main__":
159 ztri = 0.3 # this is the shallow case
160 #ztri = 0.8 # this produces the steep case where we hit the circular rim
161 ztri_lo = 0.1
162 Nmax = 300
163 ymax = 0.5
164 dy = ymax/float(Nmax)
165 current_dir = os.getcwd()
166 for n in range(0,Nmax):
167 a = ocl.Point(0,1,0.3)
168 b = ocl.Point(1,0.5,0.3)
169 c = ocl.Point(-0.1,n*dy,0.0)
170
171 filename = current_dir + "/frames/conecutter_"+ ('%05d' % n)+".png"
172 time.sleep(1)
173 drawScreen(a,b,c,filename, 1)
174