## Assignment 4: Rubber Duckie
## DescriptionIn this assignment you will write Python code to generate an STL file for a rubber duck, which you will then 3D print. The duck is described using a set of nine parametric equations for ellipsoids that generate a beak, eyes, head, neck, body, wings, and tail. Your code will create a triangular mesh to approximate the surface of each ellipsoid.If we were to render all nine meshes as surfaces, we would have something that looks like a duck, but it would not be a valid description of a 3D object because the ellipsoid meshes overlap each other rather than being knit together to form a single continuous surface. In practice, most slicing programs can deal with this problem, so you could get away with such an ugly hack, but we're going to do better: we're going to use the CSG (Constructive Solid Geometry) package to unify the meshes.
## The 2D Case: Approximating An EllipseLet's start with a simpler 2D case of an ellipse centered on the origin whose major and minor axes are aligned with the coordinate axes. Such an ellipse can be described by two parametric equations for thex and y coordinates, where the parameter θ
varies from 0 to 2π:
The constants A and B determine the shape of the ellipse: they are the
lengths of the semi-major and semi-minor axes. If A is larger than B
then A is the semi-major axis, otherwise A is the semi-minor axis. If
A equals B, then the shape is a circle and A is the radius. If we
want to move the ellipse off of the origin we can add in offset terms
If we want to tilt the ellipse so its major and minor axes are not aligned with the coordinate axes, we could introduce additional terms in the equations, but this won't be necessary for our purposes.
To draw the ellipse, we use the equations to generate a set of points
(
Since our goal is to generate a triangular mesh, we will instead use the points to generate a set of triangles. Each triangle will have one point at the center of the ellipse; the other two points will approximate the edge:
By using a smaller step size dθ we can generate more points and get a finer approximation to the true curve:
Here is sample code ellipse_example.py to generate the ellipse and write out the STL file ellipse.stl. This code relies on the stlwrite package. Download that file and put it in the same folder as ellipse_example.py After writing out the triangles as an STL file we can examine it in MeshLab. Play with the mode buttons at the top of the MeshLab window to view the triangulation with or without shading.
## The 3D CaseThe higher-dimensional generalization of an ellipse is called an ellipsoid. In 3D an ellipsoid can resemble a football. There are three parameters governing the lengths of the axes, which we'll denote A, B, and C. We'll be using spherical coordinates for our parametric equations, so instead of a single angle θ there are two angles, θ and φ. θ ranges from 0 to 2π as before, while φ ranges from -π/2 to +π/2. You can think of these parameters as longitude and lattitude, with φ=+π/2 being the north pole and φ=-π/2 being the south pole.
The 3D parametric equations for an axis-aligned ellipsoid are:
For any given φ value, the steps of θ trace out an ellipse (a parallel of lattitude) parallel to the x-y plane. For the next lattitude value, φ+dφ, there is another ellipse just below it. If we pick two adjacent points on the first ellipse, and the corresponding two adjacent points on the second ellipse, we'll have a quadrilateral surface patch, as in the diagram above. Any quadrilateral can be converted to two triangles by connecting two diagonally opposite points.
The only exceptions are at the north and south poles, where
φ = ±π/2, so cos(φ) is zero, so
Given this explanation, write a Python function get_ellipsoid that
takes parameters A, B, C, To get you started, we've supplied a file duck.py. It requires the csg package. Download the file csg.zip and extract the contents. Make sure that the csg folder is in the same location as your duck.py file. Also place the file stlwrite.py in that location. Test your function by writing out the triangles for one ellipsoid as an STL file and viewing the file in MeshLab. Note: the order of vertices should be counter-clockwise to indicate which side of the patch is the exterior surface of the object. If you get this order wrong, Meshlab will render the patch as flat black instead of shaded.
Common programming error: when testing two points to see if they are
identical, you cannot rely on ## Describing the DuckHere are the parameter values for the nine ellipsoids that describe the duck:
You should write all nine ellipsoids into the same STL file. We have provided a function write_simple_duck() that does this. The result should look something like this:
## Customize Your DuckModify the parameters a bit to customize your duck. You can change the shape of existing body parts, and even add new body parts such as a hat.Examine your simple_duck.stl file in MeshLab to make sure that the triangularization is correct. Look for holes in the mesh (missing triangles), or triangles that are all black, indicating that the surface normal is pointing in the wrong direction because the vertices are listed in the wrong order. You will need to fix these problems before proceeding to the next step.
## Generating a Proper Duck MeshTo generate a proper duck we must take the surface union of all the ellipses to get a single continuous surface. We have provided a function write_good_duck() to do that.
## Placing Your Duck on a PedestalWe are going to use SolidWorks to place the duck on a pedestal that you design, with your initials cut into it.Updated: In SolidWorks, go to File > Open, set the file type to be "Mesh files", and select your STL file. Before clicking OK, click on the Options button and make sure that "Import as" setting is Graphics (the default), not Solid Body or Surface. Then click OK. Create an extruded base as the pedestal. Since your duck has a curved bottom, you should have the base extend up slightly into the bottom of the duck so that the two bodies are connected and the duck is properly supported. Cut your initials into the base; the letter height should be 15 mm. Scale your duck so that the max dimension (including the pedestal) is roughly 3 inches. The exact scale is up to you. Remember to scale about the origin, the same as for the molecule.
## Whistle OptionWe can turn the duck into a "duck whistle" by inserting a pre-made whistle part, which you can download as Whistle.SLDPRT.In the assembly, add the whistle part and position it so it sticks into the back end of the duck, as shown: . Save your SolidWorks part as a new STL file. Be sure to set the the STL option to place all parts of the assembly in a single STL file.
## Print Your DuckWe are doing to use the resin printer instead of the Mosaic Array for the duck project. The resin printer offers finer resolution. Use this Google Drive link to submit your duck for printing. Include your Andrew id at the start of the filename, e.g., dst_duck01.stl. Place the file in the Submissions folder. When it is printing it will be moved to the "In Progress" folder, and when finished it will be moved to the "Completed" folder.
## What to Hand InCollect the following into a zip file and submit via Canvas:- Your Python source code.
- The STL file that is the output of your Python code.
- Your SolidWorks part file and the STL file you generated from that.
- A screen capture showing a rendering of your generated duck STL file. You can use Meshlab for the rendering, or you can use some other program that displays STL files.
Dave Touretzky Last modified: Sat Feb 13 14:34:11 EST 2016 |