| 1 |
from opencamlib import ocl, camvtk |
| 2 |
import time |
| 3 |
|
| 4 |
# note 2018.08: won't run becayse we don't have ocl.Weave() |
| 5 |
|
| 6 |
def generateRange(zmin,zmax,zNmax): |
| 7 |
dz = (float(zmax)-float(zmin))/(zNmax-1) |
| 8 |
zvals=[] |
| 9 |
for n in range(0,zNmax): |
| 10 |
zvals.append(zmin+n*dz) |
| 11 |
return zvals |
| 12 |
|
| 13 |
|
| 14 |
if __name__ == "__main__": |
| 15 |
print(ocl.version()) |
| 16 |
myscreen = camvtk.VTKScreen() |
| 17 |
#stl = camvtk.STLSurf("../../stl/demo.stl") |
| 18 |
stl = camvtk.STLSurf("../../../stl/gnu_tux_mod.stl") |
| 19 |
myscreen.addActor(stl) |
| 20 |
stl.SetWireframe() |
| 21 |
#stl.SetSurface() |
| 22 |
stl.SetColor(camvtk.cyan) |
| 23 |
polydata = stl.src.GetOutput() |
| 24 |
s = ocl.STLSurf() |
| 25 |
camvtk.vtkPolyData2OCLSTL(polydata, s) |
| 26 |
print("STL surface read,", s.size(), "triangles") |
| 27 |
bounds = s.getBounds() |
| 28 |
print("STLSurf.Bounds()=", bounds) |
| 29 |
cutter = ocl.CylCutter(1.6,5) |
| 30 |
#cutter = ocl.BallCutter(0.3,5) |
| 31 |
|
| 32 |
print(cutter) |
| 33 |
xmin=-1 |
| 34 |
xmax=15 |
| 35 |
N=100 |
| 36 |
ymin=-1 |
| 37 |
ymax=15 |
| 38 |
yvals = generateRange(ymin,ymax,N) |
| 39 |
xvals = generateRange(xmin,xmax,N) |
| 40 |
#print xvals |
| 41 |
zmin = -0.1 |
| 42 |
zmax = 2.75 |
| 43 |
zNmax = 5 |
| 44 |
zvals = generateRange(zmin,zmax,zNmax) |
| 45 |
print(" calculating waterlines at ", len(zvals)," different z-heights") |
| 46 |
#print zvals |
| 47 |
bpcx = ocl.BatchPushCutter() |
| 48 |
bpcy = ocl.BatchPushCutter() |
| 49 |
bpcx.setXDirection() |
| 50 |
bpcy.setYDirection() |
| 51 |
bpcx.setSTL(s) |
| 52 |
bpcy.setSTL(s) |
| 53 |
bpcx.setCutter(cutter) |
| 54 |
bpcy.setCutter(cutter) |
| 55 |
# create fibers |
| 56 |
nfibers=0 |
| 57 |
for zh in zvals: |
| 58 |
for y in yvals: |
| 59 |
f1 = ocl.Point(xmin,y,zh) # start point of fiber |
| 60 |
f2 = ocl.Point(xmax,y,zh) # end point of fiber |
| 61 |
f = ocl.Fiber( f1, f2) |
| 62 |
bpcx.appendFiber(f) |
| 63 |
nfibers=nfibers+1 |
| 64 |
for x in xvals: |
| 65 |
f1 = ocl.Point(x,ymin,zh) # start point of fiber |
| 66 |
f2 = ocl.Point(x,ymax,zh) # end point of fiber |
| 67 |
f = ocl.Fiber( f1, f2) |
| 68 |
bpcy.appendFiber(f) |
| 69 |
nfibers=nfibers+1 |
| 70 |
|
| 71 |
# run |
| 72 |
t_before = time.time() |
| 73 |
bpcx.run() |
| 74 |
bpcy.run() |
| 75 |
t_after = time.time() |
| 76 |
push_calctime = t_after-t_before |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
clpoints = bpcx.getCLPoints() |
| 81 |
clp2=bpcy.getCLPoints() |
| 82 |
clpoints+=clp2 |
| 83 |
print("got ", len(clpoints), " CL-points") |
| 84 |
print("rendering raw CL-points.") |
| 85 |
|
| 86 |
# draw the CL-points |
| 87 |
#camvtk.drawCLPointCloud(myscreen, clpoints) |
| 88 |
#for p in clpoints: |
| 89 |
# myscreen.addActor( camvtk.Sphere(center=(p.x,p.y,p.z),radius=0.02, color=camvtk.clColor( p.cc() ) ) ) |
| 90 |
|
| 91 |
|
| 92 |
xfibers = bpcx.getFibers() |
| 93 |
yfibers = bpcy.getFibers() |
| 94 |
fibers=xfibers+yfibers |
| 95 |
# sort fibers into different z-levels |
| 96 |
sorted_fibers=[] |
| 97 |
for zh in zvals: |
| 98 |
zlevel_fibers=[] |
| 99 |
for f in fibers: |
| 100 |
if f.p1.z == zh: |
| 101 |
zlevel_fibers.append(f) |
| 102 |
sorted_fibers.append(zlevel_fibers) |
| 103 |
print(" found ",len(sorted_fibers)," z-levels") |
| 104 |
|
| 105 |
n=0 |
| 106 |
loops = [] |
| 107 |
t2_before = time.time() |
| 108 |
for zlev_fibers in sorted_fibers: |
| 109 |
print(" z-level ",n," at z=", zvals[n], " has ", len(zlev_fibers), "fibers") |
| 110 |
n=n+1 |
| 111 |
w = ocl.Weave() |
| 112 |
for f in zlev_fibers: |
| 113 |
w.addFiber(f) |
| 114 |
print(" build()...",) |
| 115 |
w.build() |
| 116 |
print("done") |
| 117 |
print(" split()...",) |
| 118 |
subw = w.get_components() |
| 119 |
print(" graph has ", len(subw)," sub-weaves") |
| 120 |
m=0 |
| 121 |
for sw in subw: |
| 122 |
print(m," face_traverse...",) |
| 123 |
t_before = time.time() |
| 124 |
sw.face_traverse() |
| 125 |
t_after = time.time() |
| 126 |
calctime = t_after-t_before |
| 127 |
print("done in ", calctime," s") |
| 128 |
w_loop = sw.getLoops() |
| 129 |
for lop in w_loop: |
| 130 |
loops.append(lop) |
| 131 |
m=m+1 |
| 132 |
t2_after = time.time() |
| 133 |
print(" found", len(loops)," loops") |
| 134 |
print(" loop extraction took ", t2_after-t2_before," seconds") |
| 135 |
# draw the loops |
| 136 |
for lop in loops: |
| 137 |
n = 0 |
| 138 |
N = len(lop) |
| 139 |
first_point=ocl.Point(-1,-1,5) |
| 140 |
previous=ocl.Point(-1,-1,5) |
| 141 |
for p in lop: |
| 142 |
if n==0: # don't draw anything on the first iteration |
| 143 |
previous=p |
| 144 |
first_point = p |
| 145 |
elif n== (N-1): # the last point |
| 146 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) ) # the normal line |
| 147 |
# and a line from p to the first point |
| 148 |
myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z),p2=(first_point.x,first_point.y,first_point.z),color=camvtk.yellow) ) |
| 149 |
else: |
| 150 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) ) |
| 151 |
previous=p |
| 152 |
n=n+1 |
| 153 |
|
| 154 |
t2 = camvtk.Text() |
| 155 |
stltext = "%s\n%i triangles\n%i waterlines\n%i Fibers\n%i CL-points\n%i pushCutter() calls\n%0.1f seconds\n%0.3f us/call" \ |
| 156 |
% ( str(cutter), s.size(), len(zvals), nfibers, len(clpoints), bpc.nCalls, push_calctime, 1e6* push_calctime/bpc.nCalls ) |
| 157 |
t2.SetText(stltext) |
| 158 |
t2.SetPos( (50, myscreen.height-200) ) |
| 159 |
myscreen.addActor( t2) |
| 160 |
|
| 161 |
|
| 162 |
print("done.") |
| 163 |
myscreen.camera.SetPosition(0.5, 3, 2) |
| 164 |
myscreen.camera.SetFocalPoint(0.5, 0.5, 0) |
| 165 |
camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5)) |
| 166 |
camvtk.drawOCLtext(myscreen) |
| 167 |
|
| 168 |
myscreen.render() |
| 169 |
|
| 170 |
myscreen.iren.Start() |
| 171 |
#raw_input("Press Enter to terminate") |