File: fiber_10_offsets.py

package info (click to toggle)
opencamlib 2023.01.11-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,800 kB
  • sloc: cpp: 8,722; python: 5,530; sh: 604; javascript: 310; makefile: 209
file content (116 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from opencamlib import ocl, camvtk
import time

# 2018.08: Weave not wrapped..

def generateRange(zmin,zmax,zNmax):
    if zNmax>1:
        dz = (float(zmax)-float(zmin))/(zNmax-1)
    else:
        dz = 0
    zvals=[]
    for n in range(0,zNmax):
        zvals.append(zmin+n*dz)
    return zvals
    
def waterline(cutter, s, zh, tol = 0.1 ):
    bpc = ocl.BatchPushCutter()
    bpc.setSTL(s)
    bpc.setCutter(cutter)
    bounds = s.getBounds()
    xmin= bounds[0] - 2*cutter.getRadius()
    xmax= bounds[1] + 2*cutter.getRadius()
    ymin= bounds[2] - 2*cutter.getRadius()
    ymax= bounds[3] + 2*cutter.getRadius()
    Nx= int( (xmax-xmin)/tol )
    Ny= int( (ymax-ymin)/tol )
    xvals = generateRange(xmin,xmax,Nx)
    yvals = generateRange(ymin,ymax,Ny)
    for y in yvals:
        f1 = ocl.Point(xmin,y,zh) # start point of fiber
        f2 = ocl.Point(xmax,y,zh)  # end point of fiber
        f =  ocl.Fiber( f1, f2)
        bpc.appendFiber(f)
    for x in xvals:
        f1 = ocl.Point(x,ymin,zh) # start point of fiber
        f2 = ocl.Point(x,ymax,zh)  # end point of fiber
        f =  ocl.Fiber( f1, f2)
        bpc.appendFiber(f)
    bpc.run()
    fibers = bpc.getFibers() # get fibers
    w = ocl.Weave()
    for f in fibers:
        w.addFiber(f)

    print(" build()...",)
    w.build()
    print("done")
    print(" split()...",)
    subw = w.get_components()
    print("done. graph has", len(subw),"sub-weaves")
    m=0
    loops = []
    for sw in subw:
        print(m," face_traverse...",)
        t_before = time.time()
        sw.face_traverse()
        t_after = time.time()
        calctime = t_after-t_before
        print("done in ", calctime," s.")
        sw_loops = sw.getLoops()
        for lop in sw_loops:
            loops.append(lop)
        m=m+1
    return loops
    
if __name__ == "__main__":  
    print(ocl.version())
    myscreen = camvtk.VTKScreen()
    #stl = camvtk.STLSurf("../../stl/demo.stl")
    stl = camvtk.STLSurf("../../../stl/gnu_tux_mod.stl")
    myscreen.addActor(stl)
    stl.SetWireframe()
    #stl.SetSurface()
    stl.SetColor(camvtk.cyan)
    polydata = stl.src.GetOutput()
    s = ocl.STLSurf()
    camvtk.vtkPolyData2OCLSTL(polydata, s)
    print("STL surface read,", s.size(), "triangles")
    zh=1.9
    cutter_diams = generateRange(0.1, 6, 5)
    loops = []
    length = 20 # cutter length
    for diam in cutter_diams:
        cutter = ocl.CylCutter( diam, length )
        cutter_loops = waterline(cutter, s, zh, 0.05 )
        for l in cutter_loops:
            loops.append(l)

    print("All waterlines done. Got", len(loops)," loops in total.")
    # draw the loops
    for lop in loops:
        n = 0
        N = len(lop)
        first_point=ocl.Point(-1,-1,5)
        previous=ocl.Point(-1,-1,5)
        for p in lop:
            if n==0: # don't draw anything on the first iteration
                previous=p 
                first_point = p
            elif n== (N-1): # the last point
                myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) ) # the normal line
                # and a line from p to the first point
                myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z),p2=(first_point.x,first_point.y,first_point.z),color=camvtk.yellow) )
            else:
                myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) )
                previous=p
            n=n+1

    print("done.")
    myscreen.camera.SetPosition(0.5, 3, 2)
    myscreen.camera.SetFocalPoint(0.5, 0.5, 0)
    camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5))
    camvtk.drawOCLtext(myscreen)
    myscreen.render()    
    myscreen.iren.Start()
    #raw_input("Press Enter to terminate")