commit c8f021682125dbdaa0fee78230cde27ae01a0e31 Author: Chris Hodapp Date: Sun Sep 15 19:35:46 2019 +0200 Initial commit of scratch diff --git a/Scratch.ipynb b/Scratch.ipynb new file mode 100644 index 0000000..962b069 --- /dev/null +++ b/Scratch.ipynb @@ -0,0 +1,301 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import meshutil\n", + "import stl.mesh" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "c = meshutil.open_cube()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "mesh = stl.mesh.Mesh(c)\n", + "mesh_fname = \"cube_test.stl\"\n", + "mesh.save(mesh_fname)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "import trimesh" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "face_normals didn't match triangles, ignoring!\n", + "/home/hodapp/.local/lib/python3.6/site-packages/IPython/core/display.py:694: UserWarning: Consider using IPython.display.IFrame instead\n", + " warnings.warn(\"Consider using IPython.display.IFrame instead\")\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m = trimesh.load_mesh(mesh_fname)\n", + "m.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 +} diff --git a/meshutil.py b/meshutil.py new file mode 100644 index 0000000..d10ed78 --- /dev/null +++ b/meshutil.py @@ -0,0 +1,17 @@ +import stl.mesh +import numpy +import quaternion + +def open_cube(): + data = numpy.zeros(8, dtype=stl.mesh.Mesh.dtype) + v = data["vectors"] + v[0] = [[0,0,0], [1,0,0], [1,1,0]] + v[1] = [[0,0,0], [1,1,0], [0,1,0]] + v[2] = [[1,0,0], [1,0,1], [1,1,1]] + v[3] = [[1,0,0], [1,1,1], [0,0,0]] + v[4] = [[1,0,1], [0,0,1], [0,1,1]] + v[5] = [[1,0,1], [0,1,1], [0,0,1]] + v[6] = [[0,0,1], [0,0,0], [0,1,0]] + v[7] = [[0,0,1], [0,1,0], [0,1,1]] + # Winding order might be wrong. + return data diff --git a/quat.py b/quat.py new file mode 100644 index 0000000..d8cbb90 --- /dev/null +++ b/quat.py @@ -0,0 +1,24 @@ +import numpy +import quaternion + +def conjugate_by(vec, quat): + """Turn 'vec' to a quaternion, conjugate it by 'quat', and return it.""" + q2 = quat * vec2quat(vec) * quat.conjugate() + return quaternion.as_float_array(q2)[:,1:] + +def rotation_quaternion(axis, angle): + """Returns a quaternion for rotating by some axis and angle. + + Inputs: + axis -- numpy array of shape (3,), with axis to rotate around + angle -- angle in radians by which to rotate + """ + qc = numpy.cos(angle / 2) + qs = numpy.sin(angle / 2) + qv = qs * axis + return numpy.quaternion(qc, qv[0], qv[1], qv[2]) + +def vec2quat(vs): + qs = numpy.zeros(vs.shape[0], dtype=numpy.quaternion) + quaternion.as_float_array(qs)[:,1:4] = vs + return qs