Friday, October 28, 2011

Industrial Pumpkin Carving with Mathematica:

The art of pumpkin carving is hard to master, yet once a year parents in many countries are asked to perform this traditional and messy form of art.


It’s time for a change in this old tradition. In fact, our colleague Jon McLoone already made a significant advance in pumpkin carving, mainly using implicit functions and RegionPlot3D.


This year, I decided to make a contribution of my own that is more interactive and easier to use, with Mathematica or Mathematica Home Edition, of course.


Let’s start with a list. These are the things you need for traditional pumpkin carving.



  1. A nice looking pumpkin

  2. Carving tools of your choice: from a spoon and knife (if you are a true

    professional) to an industrial 36,000 rpm power rotary tool (seriously, I know someone who uses one)

  3. A bunch of candles to be placed inside the pumpkin

  4. A pattern for the carving on paper


For industrial Mathematica pumpkin carving, you need these tools.



  1. B-spline curve, surface, and function

  2. Color processing functions

  3. Morphological image processing functions

  4. ParametricPlot3D with Texture

  5. A pattern for the carving as a bitmap


Intrigued? Let us begin.


First of all, we need to design the pumpkin itself. How about an industrial approach? In many manufacturing industries, B-spline or NURBS surfaces are the de facto standard for shape design.


This is a profile curve of a horizontal cross section of a pumpkin.


Finding the profile curve of a horizontal cross section of a pumpkin


Setting the profile curve of a horizontal cross section of a pumpkin into an image


Profile curve of a horizontal cross section of a pumpkin


By controlling n and d, you can manipulate the number of ribs and the depth of the grooves on the pumpkin’s surface.


Here is a portion of the curve magnified to show you how the control points are configured. Note that control points at the grooves are duplicated to create sharp creases.


Controlling n and d to manipulate the number of ribs and the depth of the grooves on the pumpkin's surface


Portion of the curve magnified to show how the control points are configured


To generate a surface, we need a vertical profile, too. A simple Manipulate gives you an easy way to create one.


Using Manipulate to create the vertical profile


A Manipulate graph creating the vertical profile of the pumpkin


To create a surface from the horizontal cross section curve and the vertical B-spline curve, we simulate a surface of revolution by inserting cross section control points perpendicular to the rotational axis—in this case, the z axis.


Creating a surface from the horizontal and vertical sections


This is the resulting surface.


Building the 3D model of the pumpkin


3D rendering of a pumpkin


With this interface, you can create pumpkin surfaces dynamically.






To view the full content of this page, please enable JavaScript in your browser.
Learn more here.




To view this content, please install Wolfram CDF Player. You can install the free CDF Player here.







If you download the CDF at the bottom of the post and open it in Mathematica, you can use your own pumpkin shape for the rest of this post. By clicking the Set the values button, the values of the variables pumpkinProfile and pumpkinPoints will be set to the profile curve control points and the current configuration of surface control points, respectively. (This will not work in your browser; you must download the CDF at the end of this post to try it out.)


Now it is time for the carving pattern. We will start with any grayscale bitmap image with square dimensions. Black pixels will represent carved areas, whereas white will be background. You can find many patterns on the web. You may need to use ColorNegate to invert the colors. I found this one.


Setting the pattern to be put on the pumpkin


A pattern is valid only if the background has no disconnected components. Otherwise your carved area will have dangling pieces. You can test a pattern by applying MorphologicalComponents and checking whether there is only one component.


Applying MorphologicalComponents and checking whether there is only one component


True


We’ll texture-map the carving image onto the pumpkin surface, but first we have to adjust the image somewhat so that it is suitable for use as a texture. First, we resize it and pad it so that it becomes a 200 x 200 bitmap image. Then the image needs to be padded so that the texture will cover the whole pumpkin surface.


Adjusting the image so that it is suitable for use as a texture


Now we need to turn the pattern into a texture map. To make the carving realistic, we will use a texture map with an alpha channel. Where the alpha channel value is 0, the corresponding pixel will be completely transparent, thus creating the effect of a carved pixel. The rest of the area will be colored orange using some color channel arithmetic.


Turning the pattern into a texture map


Here is the result with our input.


generatePumpkinTexture[pattern, pumpkinProfile]


Orange texture map to be used on the pumpkin


Now it’s carving time! Currently, BSplineSurface does not support textures directly. So we rely instead on BSplineFunction and ParametricPlot3D. The good news is that the arguments and options of BSplineFunction are fully compatible with those of BSplineSurface, so the switch requires no extra editing work.


Combining all the functions and textures to make the final pumpkin


The texture is applied only to the outer shell of the pumpkin using FaceForm. “RenderingOptions” is used to avoid possible erroneous rendering of textures with alpha channels on some kinds of hardware. Now this is how our carved pumpkin looks.


carvedPumpkin[pumpkinPoints, pumpkinProfile, pattern, Automatic]


Fully carved pumpkin


Let’s make it a bit spookier—by implementing flickering candles and a darker environment.






To view the full content of this page, please enable JavaScript in your browser.
Learn more here.




To view this content, please install Wolfram CDF Player. You can install the free CDF Player here.







I asked talented Wolfram colleagues to help me with some good carving patterns. Here are examples from Tim Shedelbower, Jennifer Peterson, and Vitaliy Kaurov. Thank you, everyone!


Six examples of a carved pumpkin


Download this post as a Computable Document Format (CDF) file.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...