| 1 |
from opencamlib import ocl, camvtk |
| 2 |
import math |
| 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 |
def getWaterline(s, cutter, zh, sampling): |
| 27 |
wl = ocl.Waterline() |
| 28 |
#wl.setThreads(1) # single thread for easier debug |
| 29 |
wl.setSTL(s) |
| 30 |
wl.setCutter(cutter) |
| 31 |
wl.setZ(zh) |
| 32 |
wl.setSampling(sampling) |
| 33 |
wl.run() |
| 34 |
loops = wl.getLoops() |
| 35 |
return loops |
| 36 |
|
| 37 |
def getPathsY(s,cutter,sampling,y): |
| 38 |
#apdc = ocl.PathDropCutter() |
| 39 |
apdc = ocl.AdaptivePathDropCutter() |
| 40 |
apdc.setSTL(s) |
| 41 |
apdc.setCutter(cutter) |
| 42 |
apdc.setZ( -20 ) |
| 43 |
apdc.setSampling(sampling) |
| 44 |
apdc.setMinSampling(sampling/700) |
| 45 |
path = ocl.Path() |
| 46 |
p1 = ocl.Point(-1.52*cutter.getDiameter() , y,-111) # start-point of line |
| 47 |
p2 = ocl.Point(+1.52*cutter.getDiameter(), y,-111) # end-point of line |
| 48 |
l = ocl.Line(p1,p2) # line-object |
| 49 |
path.append( l ) |
| 50 |
apdc.setPath( path ) |
| 51 |
apdc.run() |
| 52 |
return apdc.getCLPoints() |
| 53 |
|
| 54 |
def getPathsX(s,cutter,sampling,x): |
| 55 |
#apdc = ocl.PathDropCutter() |
| 56 |
apdc = ocl.AdaptivePathDropCutter() |
| 57 |
apdc.setSTL(s) |
| 58 |
apdc.setCutter(cutter) |
| 59 |
apdc.setZ( -20 ) |
| 60 |
apdc.setSampling(sampling) |
| 61 |
apdc.setMinSampling(sampling/700) |
| 62 |
path = ocl.Path() |
| 63 |
p1 = ocl.Point(x, -1.52*cutter.getDiameter() , -111) # start-point of line |
| 64 |
p2 = ocl.Point(x, +1.52*cutter.getDiameter(), -111) # end-point of line |
| 65 |
l = ocl.Line(p1,p2) # line-object |
| 66 |
path.append( l ) |
| 67 |
apdc.setPath( path ) |
| 68 |
apdc.run() |
| 69 |
return apdc.getCLPoints() |
| 70 |
|
| 71 |
if __name__ == "__main__": |
| 72 |
print(ocl.version()) # revision() |
| 73 |
myscreen = camvtk.VTKScreen() |
| 74 |
#stl = camvtk.STLSurf("../../stl/demo.stl") |
| 75 |
#stl = camvtk.STLSurf("../../stl/30sphere.stl") |
| 76 |
#myscreen.addActor(stl) |
| 77 |
|
| 78 |
base=0.1 |
| 79 |
tip=10 |
| 80 |
a=ocl.Point(base,0,-tip) |
| 81 |
myscreen.addActor(camvtk.Point(center=(a.x,a.y,a.z), color=(1,0,1))); |
| 82 |
b=ocl.Point(-base,0,-tip) |
| 83 |
myscreen.addActor(camvtk.Point(center=(b.x,b.y,b.z), color=(1,0,1))); |
| 84 |
c=ocl.Point(0,0,0) |
| 85 |
myscreen.addActor( camvtk.Point(center=(c.x,c.y,c.z), color=(1,0,1))); |
| 86 |
#myscreen.addActor( camvtk.Line(p1=(1,0,0),p2=(0,0,0.3)) ) |
| 87 |
#myscreen.addActor( camvtk.Line(p1=(0,0,0.3),p2=(0,1,0)) ) |
| 88 |
#myscreen.addActor( camvtk.Line(p1=(1,0,0),p2=(0,1,0)) ) |
| 89 |
t = ocl.Triangle(a,b,c) |
| 90 |
s = ocl.STLSurf() |
| 91 |
s.addTriangle(t) |
| 92 |
|
| 93 |
print("STL surface read,", s.size(), "triangles") |
| 94 |
|
| 95 |
Nwaterlines = 40 |
| 96 |
zh=[-0.15*x for x in range(Nwaterlines)] |
| 97 |
#zh=[15] |
| 98 |
diam = 3.01 |
| 99 |
length = 50 |
| 100 |
loops = [] |
| 101 |
sampling = 0.1 |
| 102 |
|
| 103 |
#cutter = ocl.CylCutter( diam , length ) |
| 104 |
#cutter = ocl.BallCutter( diam , length ) |
| 105 |
#cutter = ocl.BullCutter( diam , diam/5, length ) |
| 106 |
#cutter = ocl.ConeCutter(diam, math.pi/3, length) |
| 107 |
#cutter = ocl.CylConeCutter(diam/float(3),diam,math.pi/float(9)) |
| 108 |
#cutter = ocl.BallConeCutter(diam/float(2.3),diam,math.pi/float(5)) |
| 109 |
#cutter = ocl.BullConeCutter(diam/1.5, diam/10, diam, math.pi/10) |
| 110 |
cutter = ocl.ConeConeCutter(diam/2,math.pi/3,diam,math.pi/6) |
| 111 |
|
| 112 |
print(cutter) |
| 113 |
#raw_input("Press Enter to terminate") |
| 114 |
|
| 115 |
ptsy_all = [] |
| 116 |
ptsx_all = [] |
| 117 |
yvals=[] |
| 118 |
Nmax=15 |
| 119 |
for i in range(Nmax): |
| 120 |
yvals.append( diam* float(i)/float(Nmax) ) |
| 121 |
yvals.append( -diam* float(i)/float(Nmax) ) |
| 122 |
|
| 123 |
for y in yvals: #[diam*0.4, diam*0.2, 0, -diam*0.2,diam*(-0.4)]: |
| 124 |
ptsy = getPathsY(s,cutter,sampling, y) |
| 125 |
ptsx = getPathsX(s,cutter,sampling, y) |
| 126 |
ptsy_all.append(ptsy) |
| 127 |
ptsx_all.append(ptsx) |
| 128 |
|
| 129 |
#print " got ",len(pts)," cl-points" |
| 130 |
#for p in pts: |
| 131 |
# print(p.x," ",p.y," ",p.z) |
| 132 |
#exit() |
| 133 |
|
| 134 |
loops = [] |
| 135 |
for z in zh: |
| 136 |
|
| 137 |
z_loops = getWaterline(s, cutter, z, sampling) |
| 138 |
for l in z_loops: |
| 139 |
loops.append(l) |
| 140 |
|
| 141 |
|
| 142 |
#for l in line: |
| 143 |
|
| 144 |
#drawLoops(myscreen, line, camvtk.cyan) |
| 145 |
#for l in cutter_loops: |
| 146 |
# loops.append(l) |
| 147 |
|
| 148 |
print("All waterlines done. Got", len(loops)," loops in total.") |
| 149 |
# draw the loops |
| 150 |
drawLoops(myscreen, loops, camvtk.cyan) |
| 151 |
drawLoops(myscreen, ptsy_all, camvtk.pink) |
| 152 |
drawLoops(myscreen, ptsx_all, camvtk.lblue) |
| 153 |
|
| 154 |
print("done.") |
| 155 |
myscreen.camera.SetPosition(15, 13, 7) |
| 156 |
myscreen.camera.SetFocalPoint(5, 5, 0) |
| 157 |
camvtk.drawArrows(myscreen,center=(0,0,3)) |
| 158 |
camvtk.drawOCLtext(myscreen) |
| 159 |
myscreen.render() |
| 160 |
myscreen.iren.Start() |
| 161 |
#raw_input("Press Enter to terminate") |