{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import stl.mesh\n",
"import numpy\n",
"import trimesh\n",
"import random\n",
"\n",
"import meshutil\n",
"import examples"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#mesh = examples.ram_horn()\n",
"#mesh = examples.twist()\n",
"#mesh = examples.twist_nonlinear()"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"def gen_twisted_boundary(count=4, dx0=2, dz=0.2, ang=0.1):\n",
" b = numpy.array([\n",
" [0, 0, 0],\n",
" [1, 0, 0],\n",
" [1, 0, 1],\n",
" [0, 0, 1],\n",
" ], dtype=numpy.float64) - [0.5, 0, 0.5]\n",
" # this function almost needs some sort of 'continuous'\n",
" # property about it - that it is not tied to an integral\n",
" # number of iterations of a transform, but can smoothly\n",
" # generate it at any resolution.\n",
" # but... can I do that? do all transforms accomodate that?\n",
" # some should (rotation, translation), for others it makes\n",
" # no sense (mirroring)\n",
" #\n",
" # Generate 'seed' transformations:\n",
" xfs = [meshutil.Transform().translate(dx0, 0, 0).rotate([0,1,0], numpy.pi * 2 * i / count)\n",
" for i in range(count)]\n",
" # (we'll increment the transforms in xfs as we go)\n",
" while True:\n",
" xfs_new = []\n",
" bs = []\n",
" for i, xf in enumerate(xfs):\n",
" # Generate a boundary from running transform:\n",
" b_i = xf.apply_to(b)\n",
" bs.append(b_i)\n",
" # Increment transform i:\n",
" xf2 = xf.rotate([0,1,0], ang)\n",
" xfs_new.append(xf2)\n",
" xfs = xfs_new\n",
" yield bs"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"# This is to see how well it works to compose generators:\n",
"def gen_inc_y(gen, dy=0.1):\n",
" xf = meshutil.Transform()\n",
" for bs in gen:\n",
" bs2 = [xf.apply_to(b) for b in bs]\n",
" yield bs2\n",
" xf = xf.translate(0, dy, 0)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"# and to string together boundaries from a generator:\n",
"def gen2mesh(gen, count):\n",
" # Get first list of boundaries:\n",
" bs_last = next(gen)\n",
" # TODO: Begin and end with close_boundary_simple\n",
" mesh = meshutil.FaceVertexMesh.Empty()\n",
" for i,bs_cur in enumerate(gen):\n",
" for j,b in enumerate(bs_cur):\n",
" m = meshutil.join_boundary_simple(bs_last[j], b)\n",
" mesh = mesh.concat(m)\n",
" bs_last = bs_cur\n",
" if i > count:\n",
" break\n",
" return mesh"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"gen = gen_inc_y(gen_twisted_boundary())\n",
"mesh = gen2mesh(gen, 100)\n",
"# TODO: Fix winding order"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# what even is this?\n",
"\"\"\"\n",
"dx0=2\n",
"frames=100\n",
"b = numpy.array([\n",
" [0, 0, 0],\n",
" [1, 0, 0],\n",
" [1, 0, 1],\n",
" [0, 0, 1],\n",
"], dtype=numpy.float64) - [0.5, 0.5, 0]\n",
"ang = -numpy.pi*2 / frames\n",
"# negative because of winding order annoyance\n",
"mesh = meshutil.FaceVertexMesh.Empty()\n",
"xf = meshutil.Transform() \\\n",
" .translate(dx0, 0, 0)\n",
"b0 = xf.apply_to(b)\n",
"for layer in range(frames):\n",
" b_sub0 = xf.apply_to(b)\n",
" incr = meshutil.Transform().rotate([0,0,1], ang)\n",
" b_sub1 = xf.compose(incr).apply_to(b)\n",
" m = meshutil.join_boundary_simple(b_sub0, b_sub1)\n",
" mesh = mesh.concat(m)\n",
" xf = xf.compose(incr)\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#mesh = torus_xy()"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"face_normals didn't match triangles, ignoring!\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Work around some annoying-ass trimesh/threejs bug:\n",
"wtf = meshutil.Transform().scale(0.1).translate(5,0,0)\n",
"center = meshutil.Transform().translate(-0.5, -0.5, -0.5)\n",
"base = meshutil.cube(open_xz=False).transform(center)\n",
"base = base.transform(wtf)\n",
"# to enable:\n",
"mesh = mesh.concat(base)\n",
"\n",
"mesh_fname = \"twist.stl\"\n",
"mesh.to_stl_mesh().save(mesh_fname)\n",
"\n",
"# TODO: Just use the mesh data directly (no sense in saving & re-loading)\n",
"m = trimesh.load_mesh(mesh_fname)\n",
"#m.show()\n",
"scene = trimesh.Scene([m])\n",
"scene.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}