Jekyll2020-05-07T13:25:33+00:00https://rustytriangles.github.io//feed.xmlRusty TrianglesRusty Triangles is the github playground for Mike Garrity.Polyhedra2020-05-07T12:22:44+00:002020-05-07T12:22:44+00:00https://rustytriangles.github.io//jekyll/update/2020/05/07/polyhedra<p>I love polyhedra. When I needed test cases for printing at <a href="https://formlabs.com/software/">Formlabs</a>,
I would often write a script to generate some interesting polyhedra. As a result, I have a crazy
collection of these motley scripts scattered all over my laptop. I finally found the time to sweep
them all together into one place.</p>
<p><img src="/images/polyhedra_screenshot.png" alt="Polyhedra" /></p>
<p>I’ve put the result <a href="https://github.com/rustytriangles/stl-polyhedra">here</a>.</p>
<p>It’s written in Rust, with one function for each shape. So it’s relatively
easy to extend as I add more. Hopefully this will prevent the spread of these
scripts in the future.</p>
<p>The output is STL, which is an awfully wonky format. But it is the lingua
franca for 3D printing, so…</p>
<p>The basic idea is that you do something like:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>stl-polyhedra <span class="nt">-p</span> rhombicuboctahedron <span class="nt">-o</span> output.stl
</code></pre></div></div>
<p>There is a <code class="language-plaintext highlighter-rouge">-h</code> option for getting help, and a <code class="language-plaintext highlighter-rouge">-l</code> option for getting a list
of all of the polyhedra it knows about.</p>I love polyhedra. When I needed test cases for printing at Formlabs, I would often write a script to generate some interesting polyhedra. As a result, I have a crazy collection of these motley scripts scattered all over my laptop. I finally found the time to sweep them all together into one place.Hello Teapots!2020-05-01T12:01:44+00:002020-05-01T12:01:44+00:00https://rustytriangles.github.io//jekyll/update/2020/05/01/teapots<p>Someone I work with once said that instead of “Hello World!”, I write “Hello Teapot!”.</p>
<p>The teapot is basically what I use to get a feel for any graphics system or technical computing
platform. For example, the <a href="https://www.mathworks.com/help/matlab/visualize/displaying-complex-three-dimensional-objects.html">MATLAB teapot demo</a>
is the first thing I wrote when I joined the MathWorks, as told in <a href="https://blogs.mathworks.com/graphics/2014/10/03/welcome/">my first post</a> on the graphics
blog there.</p>
<p>So here are a couple of recent teapots to get things rolling here at Rusty Triangles.</p>
<h2 id="rust">Rust</h2>
<p>The <a href="https://github.com/rustytriangles/rust-teapot">first one</a> is written in Rust and uses <a href="https://github.com/gfx-rs/wgpu-rs">wgpu-rs</a> for the rendering.
This was the first Rust I’d written and is probably a bit ugly in how it’s slicing the multidimensional arrays.
I like wgpu-rs. In particular, it’s shader support is pretty nice.</p>
<p><img src="/images/rust_teapot_screenshot.png" alt="Rust Teapot" /></p>
<h2 id="webgl">WebGL</h2>
<p>The <a href="https://github.com/rustytriangles/webgl-teapot">second one</a> is written in JavaScript and uses WebGL.
This is using <a href="https://www.electronjs.org/">electron</a> to run as a local app. Numerics in JavaScript are
always a bit weird. I used <a href="http://glmatrix.net/">gl-matrix</a> to deal with some of the worst of that. Your
shaders are always an issue with WebGL. In this case they’re just strings. That means you’re probably going
to have typos and cryptic error messags at startup. On the other hand, I’m more comfortable with JS than
Rust at this point, and having <a href="https://mochajs.org/">mocha</a> & <a href="https://istanbul.js.org/">istanbul</a>
made things go pretty quickly. I have learned more about Rust unittests since I wrote the one above.</p>
<p><img src="/images/webgl_teapot_screenshot.png" alt="WebGL Teapot" /></p>
<h2 id="the-math">The Math</h2>
<p>I dug into the details of the math behind the teapot in <a href="https://blogs.mathworks.com/graphics/2015/05/12/patch-work/">another MATLAB Graphics blog post</a>.
Basically, the teapot is composed of 32 cubic Bézier patches. A cubic Bézier patch has 16 control points
arranged in a 4x4 grid. To get a point on the patch, you substitute U & V into a 4x4 matrix composed of two copies
of the Bernstein basis polynomial and multiply that by the control points.</p>
<p>You can take the first partials of the polynomials to get a pair of tangent vectors. The cross product
of those gives you a normal vector at that point. However, as you can see in the first screenshot, this
has an issue at the very top and very bottom of the teapot. There are dark “nipples” there. What’s
happening there is that four of the control points are coincident. As a result, one of the partial
derivative is zero length. There are a few good ways to work around that, but I often don’t bother.</p>Someone I work with once said that instead of “Hello World!”, I write “Hello Teapot!”.