| 1 |
from opencamlib import ocl, camvtk |
| 2 |
import time |
| 3 |
import math |
| 4 |
|
| 5 |
def calcWaterline(zh, cutter,s): |
| 6 |
#wl = ocl.Waterline() |
| 7 |
wl = ocl.AdaptiveWaterline() |
| 8 |
wl.setSTL(s) |
| 9 |
wl.setCutter(cutter) |
| 10 |
wl.setZ(zh) |
| 11 |
wl.setSampling(0.01) |
| 12 |
wl.setThreads(4) |
| 13 |
t_before = time.time() |
| 14 |
wl.run() |
| 15 |
t_after = time.time() |
| 16 |
calctime = t_after-t_before |
| 17 |
print(" Waterline done in ", calctime," s") |
| 18 |
out = [] |
| 19 |
out.append(wl.getLoops()) |
| 20 |
out.append(wl.getXFibers()) |
| 21 |
out.append(wl.getYFibers()) |
| 22 |
return out |
| 23 |
|
| 24 |
def drawFiber(myscreen, f, fibercolor=camvtk.red): |
| 25 |
inter = f.getInts() |
| 26 |
for i in inter: |
| 27 |
if not i.empty(): |
| 28 |
ip1 = f.point( i.lower ) |
| 29 |
ip2 = f.point( i.upper ) |
| 30 |
ballradius = 0.0005 |
| 31 |
myscreen.addActor( camvtk.Line(p1=(ip1.x,ip1.y,ip1.z),p2=(ip2.x,ip2.y,ip2.z), color=fibercolor) ) |
| 32 |
myscreen.addActor( camvtk.Sphere(center=(ip1.x,ip1.y,ip1.z),radius=ballradius, color=camvtk.clColor( i.lower_cc) ) ) |
| 33 |
myscreen.addActor( camvtk.Sphere(center=(ip2.x,ip2.y,ip2.z),radius=ballradius, color=camvtk.clColor( i.upper_cc) ) ) |
| 34 |
cc1 = i.lower_cc |
| 35 |
cc2 = i.upper_cc |
| 36 |
myscreen.addActor( camvtk.Sphere(center=(cc1.x,cc1.y,cc1.z),radius=ballradius, color=camvtk.lgreen ) ) |
| 37 |
myscreen.addActor( camvtk.Sphere(center=(cc2.x,cc2.y,cc2.z),radius=ballradius, color=camvtk.lgreen ) ) |
| 38 |
# cutter circle |
| 39 |
#c1 = camvtk.Circle(center=(ip1.x,ip1.y,ip1.z), radius = 0.3/2, color=fibercolor) |
| 40 |
#myscreen.addActor(c1) |
| 41 |
#c2 = camvtk.Circle(center=(ip2.x,ip2.y,ip2.z), radius = 0.3/2, color=fibercolor) |
| 42 |
#myscreen.addActor(c2) |
| 43 |
|
| 44 |
def drawLoops(myscreen, loops, loopColor=camvtk.yellow): |
| 45 |
nloop=0 |
| 46 |
zofz = 0.00 |
| 47 |
for lop in loops: |
| 48 |
n = 0 |
| 49 |
N = len(lop) |
| 50 |
first_point=ocl.Point(-1,-1,5) |
| 51 |
previous=ocl.Point(-1,-1,5) |
| 52 |
for p in lop: |
| 53 |
if n==0: # don't draw anything on the first iteration |
| 54 |
previous=p |
| 55 |
first_point = p |
| 56 |
elif n== (N-1): # the last point |
| 57 |
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 |
| 58 |
# and a line from p to the first point |
| 59 |
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) ) |
| 60 |
else: |
| 61 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z+zofz),p2=(p.x,p.y,p.z+zofz),color=loopColor) ) |
| 62 |
previous=p |
| 63 |
n=n+1 |
| 64 |
zofz = zofz +0.00 |
| 65 |
print("rendered loop ",nloop, " with ", len(lop), " points at zofz=",zofz) |
| 66 |
if len(lop)==2: |
| 67 |
for p in lop: |
| 68 |
print(p) |
| 69 |
myscreen.addActor( camvtk.Sphere(center=(p.x,p.y,p.z+zofz),radius=0.0005, color=camvtk.pink ) ) |
| 70 |
nloop = nloop+1 |
| 71 |
|
| 72 |
if __name__ == "__main__": |
| 73 |
print(ocl.version()) |
| 74 |
myscreen = camvtk.VTKScreen() |
| 75 |
a = ocl.Point(0,1,0.3) |
| 76 |
myscreen.addActor(camvtk.Point(center=(a.x,a.y,a.z), color=(1,0,1))) |
| 77 |
b = ocl.Point(1,0.5,0.3) |
| 78 |
myscreen.addActor(camvtk.Point(center=(b.x,b.y,b.z), color=(1,0,1))) |
| 79 |
c = ocl.Point(-0.1,0.3,0.0) |
| 80 |
myscreen.addActor(camvtk.Point(center=(c.x,c.y,c.z), color=(1,0,1))) |
| 81 |
myscreen.addActor( camvtk.Line(p1=(a.x,a.y,a.z),p2=(c.x,c.y,c.z)) ) |
| 82 |
myscreen.addActor( camvtk.Line(p1=(c.x,c.y,c.z),p2=(b.x,b.y,b.z)) ) |
| 83 |
myscreen.addActor( camvtk.Line(p1=(a.x,a.y,a.z),p2=(b.x,b.y,b.z)) ) |
| 84 |
t = ocl.Triangle(b,c,a) |
| 85 |
s = ocl.STLSurf() |
| 86 |
s.addTriangle(t) # a one-triangle STLSurf |
| 87 |
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 |
| 88 |
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 ] |
| 89 |
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] |
| 90 |
|
| 91 |
zheights=[] |
| 92 |
Nmax=10 |
| 93 |
zmin=-0.5 |
| 94 |
zmax= 0.30 |
| 95 |
dz = (zmax-zmin)/float(Nmax-1) |
| 96 |
z = zmin |
| 97 |
for n in range(Nmax): |
| 98 |
zheights.append(z) |
| 99 |
z=z+dz |
| 100 |
|
| 101 |
#zheights=[] |
| 102 |
""" |
| 103 |
zheights.append(0.29) |
| 104 |
zheights.append(0.28) |
| 105 |
zheights.append(0.27) |
| 106 |
zheights.append(0.26) |
| 107 |
zheights.append(0.25) |
| 108 |
""" |
| 109 |
#zheights=[ -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25] |
| 110 |
#zheights=[ 0.1] |
| 111 |
|
| 112 |
length = 10 |
| 113 |
diam = 0.6 |
| 114 |
cutter1 = ocl.CylCutter( diam , length ) |
| 115 |
cutter2 = ocl.BallCutter( diam , length ) |
| 116 |
cutter3 = ocl.BullCutter( diam , diam/5, length ) |
| 117 |
cutter4 = ocl.ConeCutter( diam , math.pi/5, length ) |
| 118 |
cutter5 = ocl.CylConeCutter(diam/float(3),diam,math.pi/float(9)) |
| 119 |
|
| 120 |
for zh in zheights: |
| 121 |
loops = calcWaterline(zh, cutter5, s) |
| 122 |
drawLoops(myscreen, loops[0], camvtk.red) |
| 123 |
|
| 124 |
#loops = calcWaterline(zh, cutter2, s) |
| 125 |
#drawLoops(myscreen, loops[0], camvtk.green) |
| 126 |
#loops = calcWaterline(zh, cutter3, s) |
| 127 |
#drawLoops(myscreen, loops[0], camvtk.yellow) |
| 128 |
|
| 129 |
#loops = calcWaterline(zh, cutter4, s) |
| 130 |
#drawLoops(myscreen, loops[0], camvtk.pink) |
| 131 |
|
| 132 |
#for f in loops[1]: |
| 133 |
# drawFiber(myscreen, f, camvtk.red) |
| 134 |
#for f in loops[2]: |
| 135 |
# drawFiber(myscreen, f, camvtk.lblue) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
print("done.") |
| 141 |
myscreen.camera.SetPosition(1, -1, 3) |
| 142 |
myscreen.camera.SetFocalPoint(0.5, 0.5, 0) |
| 143 |
camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5)) |
| 144 |
camvtk.drawOCLtext(myscreen) |
| 145 |
myscreen.render() |
| 146 |
myscreen.iren.Start() |
| 147 |
#raw_input("Press Enter to terminate") |