Debian Codegraph @

examples/python/waterline/waterline_2_tux_adapt.py

raw
1 from opencamlib import ocl, camvtk
2 import time
3
4 def drawLoops(myscreen, loops, loopcolor):
5 nloop = 0
6 for lop in loops:
7 n = 0
8 N = len(lop)
9 first_point=ocl.Point(-1,-1,5)
10 previous=ocl.Point(-1,-1,5)
11 for p in lop:
12 if n==0: # don't draw anything on the first iteration
13 previous=p
14 first_point = p
15 elif n== (N-1): # the last point
16 myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=loopcolor) ) # the normal line
17 # and a line from p to the first point
18 myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z),p2=(first_point.x,first_point.y,first_point.z),color=loopcolor) )
19 else:
20 myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=loopcolor) )
21 previous=p
22 n=n+1
23 print("rendered loop ",nloop, " with ", len(lop), " points")
24 nloop = nloop+1
25
26
27 if __name__ == "__main__":
28 print(ocl.version())
29 myscreen = camvtk.VTKScreen()
30 #stl = camvtk.STLSurf("../../../stl/demo.stl")
31 stl = camvtk.STLSurf("../../../stl/gnu_tux_mod.stl")
32 #stl = camvtk.STLSurf("../../../stl/waterline1.stl")
33 myscreen.addActor(stl)
34 stl.SetWireframe() # render tux as wireframe
35 #stl.SetSurface() # render tux as surface
36 stl.SetColor(camvtk.cyan)
37 polydata = stl.src.GetOutput()
38 s = ocl.STLSurf()
39 camvtk.vtkPolyData2OCLSTL(polydata, s)
40 print("STL surface read,", s.size(), "triangles")
41 zh=1.75145
42 diam = 1.4
43 length = 500
44 loops = []
45
46 #cutter = ocl.CylCutter( diam , length )
47 cutter = ocl.BallCutter( diam , length )
48 #cutter = ocl.BullCutter( diam , diam/5, length )
49
50 wl = ocl.Waterline()
51 wl.setSTL(s)
52 wl.setCutter(cutter)
53 wl.setZ(zh)
54 wl.setSampling(0.5)
55 #wl.setThreads(5)
56 t_before = time.time()
57 wl.run()
58 t_after = time.time()
59 calctime = t_after-t_before
60 print(" Waterline done in ", calctime," s")
61 cutter_loops = wl.getLoops()
62 for l in cutter_loops:
63 loops.append(l)
64
65 aloops = []
66 awl = ocl.AdaptiveWaterline()
67 awl.setSTL(s)
68 awl.setCutter(cutter)
69 awl.setZ(zh)
70 awl.setSampling(0.1)
71 awl.setMinSampling(0.01)
72 #wl.setThreads(5)
73 t_before = time.time()
74 awl.run()
75 t_after = time.time()
76 calctime = t_after-t_before
77 print(" AdaptiveWaterline done in ", calctime," s")
78 acutter_loops = awl.getLoops()
79 for l in acutter_loops:
80 aloops.append(l)
81
82
83 print("All waterlines done. Got", len(loops)," loops in total.")
84 # draw the loops
85 drawLoops(myscreen, loops, camvtk.yellow)
86 drawLoops(myscreen, aloops, camvtk.red)
87
88 print("done.")
89 myscreen.camera.SetPosition(15, 13, 7)
90 myscreen.camera.SetFocalPoint(5, 5, 0)
91 camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5))
92 camvtk.drawOCLtext(myscreen)
93 myscreen.render()
94 myscreen.iren.Start()
95 #raw_input("Press Enter to terminate")