{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import meshutil\n", "import stl.mesh\n", "import numpy\n", "import trimesh" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "b0 = numpy.array([\n", " [0, 0, 0],\n", " [1, 0, 0],\n", " [1, 1, 0],\n", " [0, 1, 0],\n", "], dtype=numpy.float64) - [0.5, 0.5, 0]\n", "xf0_to_1 = meshutil.Transform().translate(0,0,1)\n", "b1 = xf0_to_1.apply_to(b0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "mesh_fname = \"cube_test.stl\"\n", "\n", "mesh = meshutil.join_boundary_simple(b0, b1)\n", "mesh = mesh.concat(meshutil.close_boundary_simple(b0))\n", "for i in range(4):\n", " # Opening boundary:\n", " b = b1\n", " xf = meshutil.Transform() \\\n", " .translate(0,0,-1) \\\n", " .scale(0.5) \\\n", " .translate(0.25,0.25,1) \\\n", " .rotate([0,0,1], i*numpy.pi/2)\n", " for layer in range(30):\n", " b_sub0 = xf.apply_to(b)\n", " incr = meshutil.Transform() \\\n", " .scale(0.9) \\\n", " .rotate([-1,0,1], 0.3) \\\n", " .translate(0,0,0.8)\n", " b_sub1 = incr.compose(xf).apply_to(b)\n", " m = meshutil.join_boundary_simple(b_sub0, b_sub1)\n", " mesh = mesh.concat(m)\n", " xf = incr.compose(xf)\n", " # Close final boundary:\n", " mesh = mesh.concat(meshutil.close_boundary_simple(b_sub1[::-1,:]))\n", " # ::-1 is to reverse the boundary's order to fix winding order.\n", " # Not sure of the \"right\" way to fix winding order here.\n", " # The boundary vertices go in an identical order... it's just\n", " # that clockwise/counter-clockwise flip.\n", "\n", "# I keep confusing the 'incremental' transform with the\n", "# transform to get b_open in the first place\n", " \n", "# rotate([-1,0,1], ang).rotate([0,0,1], i*numpy.pi/2)\n", "\n", "# I don't need to subdivide *geometry*.\n", "# I need to subdivide *space* and then put geometry in it.\n", "\n", "# 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", "mesh.to_stl_mesh().save(mesh_fname)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "face_normals didn't match triangles, ignoring!\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 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 }