Constructive Solid Geometry

I will post about intersections in the near future, and I think it is instructive to explain how easy it is these days to visualize intersections of sets. For instance, an infamous Calculus problem asks to compute the volume of the intersection of two cylinders of equal radius whose axes intersect at a right angle:

Cylinder 2

This image shows the union of the two cylinders. It can be rendered in PoVRay using the following code:

#declare color1 = rgb <0.824802, 0.572810, 0.332621>;
#declare color2 = rgb <0.288963, 0.391247, 0.546639>;

union {
	object {
		cylinder {<3,0,0>,-<3,0,0>, 1}
		pigment {color color1}
	}
	object {
		cylinder {<0,3,0>,-<0,3,0>, 1}
		pigment {color color2}
	}
	scale .4
	rotate <90,30,0>
	rotate <-30,0,0>
}

To make this a complete scene file, you need to add before this camera and light source settings, for instance:

global_settings { assumed_gamma 2.2 }

#include "colors.inc"

camera {
	location <0,0,-4>
	angle 40
	right x*image_width/image_height    
	up y
	look_at <0,0,0>
}

#default {
	finish {
		ambient .1
		diffuse .3
		specular .2 
	}
}

light_source {
	<3,3,-8> color White
}
light_source {
	<3,-3,-8> color White
}
light_source {
	<-3,3,-8> color White
}
light_source {
	<-3,-3,-8> color White
}

background {color White}

Cylinder 2i

To render the intersection above, you just need to replace union by intersection.
While amusing, this doesn’t help to solve the problem. The main idea is to slice the two cylinders with planes parallel to the two axes of the cylinders, and to realize that you will get squares, because both cylinders are cut into parallel strips. One way to visualize that is by slicing the two cylinders with thin slabs, and throwing away every other slab, like so:

Cylinder 2b

The PoVRay code for this uses a simple loop and intersects the two cylinders with a union of slabs. Note that the plane primitive represents a half space, given by normal vector and distance to the origin. So a slab is nothing but the difference of two half planes:

intersection {
	object {
		cylinder {<3,0,0>,-<3,0,0>, 1}
		pigment {color color1}
	}
	object {
		cylinder {<0,3,0>,-<0,3,0>, 1}
		pigment {color color2}
	}
	union {
		#declare height = -1;
		#declare step=.05;
		#while (height <1)
			difference {
				plane {<0,0,1>,height+step/2}
				plane {<0,0,1>,height}
				pigment {color color3}
			}
			#declare height = height+step;
		#end
	}
}

Finally, let’s try to incorporate more of the two cylinders so that it becomes clear in a single image that we are intersecting cylinders. We do this by adding the differences between one cylinder and a slightly larger copy of the other cylinder as follows:

intersection {
	union {
		intersection {
			object {
				cylinder {<3,0,0>,-<3,0,0>, 1}
				pigment {color color1}
			}
			object {
				cylinder {<0,3,0>,-<0,3,0>, 1}
				pigment {color color2}
			}
		}	
		intersection {
			object {
				cylinder {<3,0,0>,-<3,0,0>, 1+.3}
				pigment {color color1}
				inverse
			}
			object {
				cylinder {<0,3,0>,-<0,3,0>, 1}
				pigment {color color2}
			}
		} 
	 	intersection {
			object {
				cylinder {<3,0,0>,-<3,0,0>, 1}
				pigment {color color1}			
			}
			object {
				cylinder {<0,3,0>,-<0,3,0>, 1+.3}
				pigment {color color2}
				inverse
			}
		}
	}
	union {
		#declare height = -1;
		#declare step=.1;
		#while (height <1)
			difference {
				plane {<0,0,1>,height+step/2}
				plane {<0,0,1>,height}
				pigment {color color3}
			}
			#declare height = height+step;
		#end
	}
}

Cylinder 2c

I imagine Archimedes would have been pleased.

Advertisements

One thought on “Constructive Solid Geometry”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s