{ "cells": [ { "cell_type": "code", "execution_count": 591, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 591, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import stl.mesh\n", "import numpy\n", "import trimesh\n", "import random\n", "import itertools\n", "\n", "import meshutil\n", "import meshgen\n", "import examples\n", "import cage\n", "\n", "import importlib\n", "importlib.reload(examples)" ] }, { "cell_type": "code", "execution_count": 592, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2]" ] }, "execution_count": 592, "metadata": {}, "output_type": "execute_result" } ], "source": [ "importlib.reload(cage)\n", "c = cage.Cage.from_arrays([[0,0,0], [1,1,1]], [[2,2,2]])\n", "c.splits" ] }, { "cell_type": "code", "execution_count": 593, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1.]])" ] }, "execution_count": 593, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.verts[1:-1,:]" ] }, { "cell_type": "code", "execution_count": 594, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0.]\n", " [1. 1. 1.]]\n", "[[2. 2. 2.]]\n" ] } ], "source": [ "for i in c.polys():\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 595, "metadata": {}, "outputs": [], "source": [ "center = meshutil.Transform().translate(-0.5, -0.5, 0)\n", "cage0 = cage.Cage.from_arrays([\n", " [0, 0, 0],\n", " [1, 0, 0],\n", " [1, 1, 0],\n", " [0, 1, 0],\n", "])#.transform(center)" ] }, { "cell_type": "code", "execution_count": 596, "metadata": {}, "outputs": [], "source": [ "cs,_,_ = cage0.subdivide_deprecated()\n", "c1,c2,c3,c4 = cs" ] }, { "cell_type": "code", "execution_count": 597, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0. , 0. , 0. ],\n", " [0.5, 0. , 0. ],\n", " [0.5, 0.5, 0. ],\n", " [0. , 0.5, 0. ]])" ] }, "execution_count": 597, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1.verts" ] }, { "cell_type": "code", "execution_count": 598, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.5, 0. , 0. ],\n", " [1. , 0. , 0. ],\n", " [1. , 0.5, 0. ],\n", " [0.5, 0.5, 0. ]])" ] }, "execution_count": 598, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c2.verts" ] }, { "cell_type": "code", "execution_count": 599, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.5, 0.5, 0. ],\n", " [1. , 0.5, 0. ],\n", " [1. , 1. , 0. ],\n", " [0.5, 1. , 0. ]])" ] }, "execution_count": 599, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c3.verts" ] }, { "cell_type": "code", "execution_count": 600, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0. , 0.5, 0. ],\n", " [0.5, 0.5, 0. ],\n", " [0.5, 1. , 0. ],\n", " [0. , 1. , 0. ]])" ] }, "execution_count": 600, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c4.verts" ] }, { "cell_type": "code", "execution_count": 601, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([3, 1, 2, 1], dtype=uint8),\n", " array([1, 3, 1, 2], dtype=uint8),\n", " array([2, 1, 3, 1], dtype=uint8),\n", " array([1, 2, 1, 3], dtype=uint8)]" ] }, "execution_count": 601, "metadata": {}, "output_type": "execute_result" } ], "source": [ "overlap = cage0.classify_overlap(cs)\n", "overlap" ] }, { "cell_type": "code", "execution_count": 602, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cage 0:\n", " Vertex 0: type=3 for position [0. 0. 0.]\n", " Vertex 1: type=1 for position [0.5 0. 0. ]\n", " Vertex 2: type=2 for position [0.5 0.5 0. ]\n", " Vertex 3: type=1 for position [0. 0.5 0. ]\n", "Cage 1:\n", " Vertex 0: type=1 for position [0.5 0. 0. ]\n", " Vertex 1: type=3 for position [1. 0. 0.]\n", " Vertex 2: type=1 for position [1. 0.5 0. ]\n", " Vertex 3: type=2 for position [0.5 0.5 0. ]\n", "Cage 2:\n", " Vertex 0: type=2 for position [0.5 0.5 0. ]\n", " Vertex 1: type=1 for position [1. 0.5 0. ]\n", " Vertex 2: type=3 for position [1. 1. 0.]\n", " Vertex 3: type=1 for position [0.5 1. 0. ]\n", "Cage 3:\n", " Vertex 0: type=1 for position [0. 0.5 0. ]\n", " Vertex 1: type=2 for position [0.5 0.5 0. ]\n", " Vertex 2: type=1 for position [0.5 1. 0. ]\n", " Vertex 3: type=3 for position [0. 1. 0.]\n" ] } ], "source": [ "# Pretty-print a little:\n", "for i,(c,ols) in enumerate(zip(cs, overlap)):\n", " print(\"Cage {}:\".format(i))\n", " for j,(vert,ol) in enumerate(zip(c.verts, ols)):\n", " print(\" Vertex {}: type={} for position {}\".format(j, ol, vert))" ] }, { "cell_type": "code", "execution_count": 603, "metadata": {}, "outputs": [], "source": [ "cf = cage.CageFork(None,\n", " numpy.array([[0, 0, 0], [0.5, 0, 0], [1, 0, 0], [1, 0.5, 0],\n", " [1, 1, 0], [0.5, 1, 0], [0, 1, 0], [0, 0.5, 0]]) + [0, 0, 2],\n", " [[7, 0, 1], [1, 2, 3], [3, 4, 5], [5, 6, 7]]\n", " )\n", "mesh2 = cf.transition_from(cage0)" ] }, { "cell_type": "code", "execution_count": 604, "metadata": {}, "outputs": [], "source": [ "import cProfile, pstats" ] }, { "cell_type": "code", "execution_count": 605, "metadata": {}, "outputs": [], "source": [ "#mesh = examples.ram_horn()\n", "#mesh = examples.twist()\n", "#mesh = examples.twist_nonlinear()\n", "#mesh = examples.twist_from_gen()\n", "#mesh = examples.twisty_torus(1000)\n", "#mesh = examples.ram_horn2()\n", "#mesh = examples.branch_test()\n", "mesh = examples.ram_horn_branch()" ] }, { "cell_type": "code", "execution_count": 606, "metadata": {}, "outputs": [], "source": [ "def gen_wave(freq=2.0, a=2.0, d_ph=0.05, count=80):\n", " ph = 0.0\n", " while True:\n", " xs = numpy.linspace(-10, 10, count)\n", " ys = numpy.zeros(count)\n", " zs = (numpy.sin(xs*freq + a*numpy.sin(ph)*numpy.pi) + 2)/2\n", " b1 = numpy.stack((xs,ys,zs), axis=-1)\n", " b2 = numpy.stack((xs,ys,zs - 2), axis=-1)\n", " yield [numpy.vstack((b1, b2[::-1,:]))]\n", " ph += d_ph\n", "\n", "#gen = meshgen.gen_inc_y(gen_wave())\n", "#mesh = meshgen.gen2mesh(gen, 200, False, False, True, True)\n", "# The closing method in gen2mesh is inappropriate because\n", "# my boundary is not convex!" ] }, { "cell_type": "code", "execution_count": 607, "metadata": {}, "outputs": [], "source": [ "def spiral3():\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", " b *= 0.3\n", " b = meshutil.subdivide_boundary(b)\n", " b = meshutil.subdivide_boundary(b)\n", " bs = [b]\n", " # since it needs a generator:\n", " gen1 = itertools.repeat(bs)\n", " gen2 = meshgen.gen_twisted_boundary(gen1, ang=-0.2, dx0=0.5)\n", " gen3 = meshgen.gen_twisted_boundary(gen2, ang=0.07, dx0=1)\n", " gen4 = meshgen.gen_twisted_boundary(gen3, ang=-0.03, dx0=3)\n", " gen = meshgen.gen_inc_y(gen4, dy=0.1)\n", " mesh = meshgen.gen2mesh(\n", " gen, count=500, flip_order=True, close_first=True, close_last=True)\n", " return mesh\n", "\n", "def profile():\n", " pr = cProfile.Profile()\n", " pr.enable()\n", " spiral3()\n", " pr.disable()\n", " ps = pstats.Stats(pr)\n", " return ps\n", "\n", "#ps = profile()\n", "#ps.sort_stats('tottime').print_stats()" ] }, { "cell_type": "code", "execution_count": 608, "metadata": {}, "outputs": [], "source": [ "# Work around some annoying-ass trimesh/threejs bug:\n", "wtf = meshutil.Transform().scale(0.1).translate(15,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", "mesh_out = mesh\n", "# to enable:\n", "#mesh_out = mesh_out.concat(base)\n", "\n", "mesh_fname = \"branch.stl\"\n", "mesh_out.to_stl_mesh().save(mesh_fname)" ] }, { "cell_type": "code", "execution_count": 609, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 609, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = trimesh.Trimesh(vertices=mesh_out.v, faces=mesh_out.f)\n", "# Also acceptable:\n", "#m = trimesh.load_mesh(mesh_fname)\n", "\n", "#m.show()\n", "scene = trimesh.Scene([m])\n", "scene.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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 }