Successfully made a twisty-torus

This commit is contained in:
Chris Hodapp 2019-10-04 23:51:40 +02:00
parent b8a4cdd152
commit 1a01905d0c
2 changed files with 107 additions and 126 deletions

File diff suppressed because one or more lines are too long

View File

@ -126,40 +126,107 @@ def twist_nonlinear(dx0 = 2, dz=0.2, count=3, scale=0.99, layers=100):
mesh = mesh.concat(meshutil.close_boundary_simple(b_sub1[::-1,:])) mesh = mesh.concat(meshutil.close_boundary_simple(b_sub1[::-1,:]))
return mesh return mesh
# Wrap some boundary around a (sorta) torus that is along XY. # Generate a frame with 'count' boundaries in the XZ plane.
# Each one rotates by 'ang' as it moves by 'dz'.
# dx0 is center-point distance from each to the origin.
def gen_twisted_boundary(count=4, dx0=2, dz=0.2, ang=0.1):
b = numpy.array([
[0, 0, 0],
[1, 0, 0],
[1, 0, 1],
[0, 0, 1],
], dtype=numpy.float64) - [0.5, 0, 0.5]
# Generate 'seed' transformations:
xfs = [meshutil.Transform().translate(dx0, 0, 0).rotate([0,1,0], numpy.pi * 2 * i / count)
for i in range(count)]
# (we'll increment the transforms in xfs as we go)
while True:
xfs_new = []
bs = []
for i, xf in enumerate(xfs):
# Generate a boundary from running transform:
b_i = xf.apply_to(b)
bs.append(b_i)
# Increment transform i:
xf2 = xf.rotate([0,1,0], ang)
xfs_new.append(xf2)
xfs = xfs_new
yield bs
# This is to see how well it works to compose generators:
def gen_inc_y(gen, dy=0.1):
xf = meshutil.Transform()
for bs in gen:
bs2 = [xf.apply_to(b) for b in bs]
yield bs2
xf = xf.translate(0, dy, 0)
# Wrap a boundary generator around a (sorta) torus that is along XY.
# producing a mesh. # producing a mesh.
# 'frames' sets resolution, 'dx0' sets radius. # 'frames' sets resolution, 'rad' sets radius (the boundary's origin
# 'b' can be None, and then a 1x1 boundary in XZ is used, # sweeps through this radius - it's not 'inner' or 'outer' radius).
# centered at (0,0,0). If one is supplied, it should also #
# be oriented roughly along XZ. # generator should produce lists of boundaries which are oriented
def torus_xy(bs=None, dx0=2, frames=100): # roughly in XZ. This will get 'frames' elements from it if
if b is None: # possible.
b = numpy.array([ def gen_torus_xy(gen, rad=2, frames=100):
[0, 0, 0], ang = numpy.pi*2 / frames
[1, 0, 0], xf = meshutil.Transform().translate(rad, 0, 0)
[1, 0, 1], for i,bs in enumerate(gen):
[0, 0, 1], if i >= frames:
], dtype=numpy.float64) - [0.5, 0, 0.5] break
ang = -numpy.pi*2 / frames bs2 = [xf.apply_to(b) for b in bs]
# negative because of winding order annoyance yield bs2
xf = xf.rotate([0,0,1], ang)
# String together boundaries from a generator.
# If count is nonzero, run only this many iterations.
def gen2mesh(gen, count=0, flip_order=False, loop=False):
# Get first list of boundaries:
bs_first = next(gen)
bs_last = bs_first
# TODO: Begin and end with close_boundary_simple
mesh = meshutil.FaceVertexMesh.Empty() mesh = meshutil.FaceVertexMesh.Empty()
xf = meshutil.Transform() \ for i,bs_cur in enumerate(gen):
.translate(dx0, 0, 0) if count > 0 and i >= count:
b0 = xf.apply_to(b) break
for layer in range(frames): for j,b in enumerate(bs_cur):
b_sub0 = xf.apply_to(b) if flip_order:
incr = meshutil.Transform().rotate([0,0,1], ang) m = meshutil.join_boundary_simple(b, bs_last[j])
b_sub1 = xf.compose(incr).apply_to(b) else:
m = meshutil.join_boundary_simple(b_sub0, b_sub1) m = meshutil.join_boundary_simple(bs_last[j], b)
mesh = mesh.concat(m) mesh = mesh.concat(m)
xf = xf.compose(incr) bs_last = bs_cur
if loop:
for b0,b1 in zip(bs_last, bs_first):
if flip_order:
m = meshutil.join_boundary_simple(b1, b0)
else:
m = meshutil.join_boundary_simple(b0, b1)
mesh = mesh.concat(m)
return mesh return mesh
def twist_from_gen():
gen = gen_inc_y(gen_twisted_boundary())
mesh = gen2mesh(gen, 100, True)
return mesh
# frames = How many step to build this from:
# turn = How many full turns to make in inner twist
# count = How many inner twists to have
def twisty_torus(frames = 200, turns = 4, count = 4, rad = 4):
# In order to make this line up properly:
angle = numpy.pi * 2 * turns / frames
gen = gen_torus_xy(gen_twisted_boundary(count=count, ang=angle), rad=rad, frames=frames)
return gen2mesh(gen, 0, flip_order=True, loop=True)
def main(): def main():
fns = { fns = {
ram_horn: "ramhorn.stl", ram_horn: "ramhorn.stl",
twist: "twist.stl", twist: "twist.stl",
twist_nonlinear: "twist_nonlinear.stl", twist_nonlinear: "twist_nonlinear.stl",
twist_from_gen: "twist_from_gen.stl",
twisty_torus: "twisty_torus.stl",
} }
for f in fns: for f in fns:
fname = fns[f] fname = fns[f]