Support functions in a model editor
Posted by Aardvajk, 18 May 2014 · 464 views
Adding a rather nifty set of functionality to Charm, the model editor, this morning. Generating shapes based on support functions rather than explicit geometry.
For those not familiar, a support function is a function that, given a shape and a normal, returns the point on the shape that is farthest in the direction of the normal. For a sphere, for example, this is just pos + (normal * radius) and so on.
Nice thing about support functions is they can be combined to make new, more complex shapes. If you add the result of one support function to another, you get a shape based on "sweeping" the second shape around the surface of the first. The rounded cube in the image above is based on a support function for a cube, with the support function for a smaller sphere added to the result.
The other useful operation is to take the result of two support functions, then return whichever result is farthest along the normal. This produces a sort of convex "shrink-wrap" result. If you had a disc and a point, for example, both offset vertically away from each other, the result would be a cone. You could then add the sphere support to this result, to get a rounded cone, and so on.
This is all well studied and documented, and indeed forms the heart of the GJK distance algorithm but something I can now do in Charm is provide a generic support function to a method and it produces the resulting shape as a convex hull.
Its quite simple to implement. I just initially sweep around a unit sphere with whatever granularity and get a list of the points, then stitch the faces together as if I was creating a sphere. Then I just do the equivalent of the "weld" method that most 3D software supports, merging all the equivalent vertices together and updating the face indices. Then I just do a final sweep and remove any degenerate triangles.
So what I hope to do is build some kind of interactive interface where you can choose from a basic set of primitive supports (cube, sphere, point, disk etc) but then use the UI to express different combinations - add, maximal, etc. Then it should be possible to create a hull of almost any convex imaginable with a few simple clicks and setups in the editor.
Not seen this in other modellers although I'm not very experienced with them. I have a minor issue to sort out as well as the shape above isn't quite symmetric and you can see the top face has one too many triangles so need to figure out what is happening there, but generally speaking, a great result for about an hour and a half's work
Here's a very hi-resolution version of the rounded cube, using a very high normal count.
Thanks for reading.