Difference between revisions of "HowTo:Use constructive solid geometry"

From POV-Wiki
Jump to navigation Jump to search
(New page: == Constructive Solid Geometry == Constructive Solid Geometry (CSG) is a powerful tool to combine primitive objects to create more complex objects. ==Union== The union object is a way to ...)
 
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Constructive Solid Geometry ==
 
== Constructive Solid Geometry ==
Constructive Solid Geometry (CSG) is a powerful tool to combine primitive objects to create more complex objects.
+
[[Constructive Solid Geometry]] (CSG) is a powerful tool to combine primitive objects to create more complex objects.
  
 
==Union==
 
==Union==
The union object is a way to apply a universal group to a family of objects. Their appearance does not change at all while using this.
+
The [[union]] object is a way to apply a universal group to a family of objects. Their appearance does not change at all while using this.
 
To declare a union object:
 
To declare a union object:
  
#include "colors.inc"
+
<source lang="pov">#include "colors.inc"
camera{  
+
camera{  
  location <0, 1, -10>
+
  location <4, 4, -10>
  look_at 0
+
  look_at 0
  angle 36
+
  angle 36
}
+
}
light_source{ <500, 500, -1000> White }
+
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
+
plane{ y, -1.5
  pigment{ checker Green White }
+
  pigment{ checker Green White }
}
+
}
union{
+
union{
  box{ -1, 1 pigment{ Blue } }
+
  box{ -1, 1 pigment{ Blue } }
  sphere{ 0, 1 pigment{ Red } }
+
  sphere{ 0, 1.375 pigment{ Red } }
}
+
}</source>
  
 
Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects.
 
Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects.
  
#include "colors.inc"
+
<source lang="pov">#include "colors.inc"
camera{
+
camera{
  location <0, 1, -10>
+
  location <4, 4, -10>
  look_at 0
+
  look_at 0
  angle 36
+
  angle 36
}
+
}
light_source{ <500, 500, -1000> White }
+
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
+
plane{ y, -1.5
  pigment{ checker Green White }
+
  pigment{ checker Green White }
}
+
}
union{
+
union{
  box{ -1, 1 }
+
  box{ -1, 1 }
  sphere{ 0, 1 }
+
  sphere{ 0, 1.375 }
  pigment{ Red }
+
  pigment{ Red }
}
+
}</source>
  
The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object.
+
The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. The resultant image, rendered with anti-aliasing, is shown below:
 +
[[Image:Csg_union_example.png|320px|thumb|center|Union Example]]
  
 
== Intersections ==
 
== Intersections ==
Line 46: Line 47:
 
So if we take our previous code and change it around a small bit:
 
So if we take our previous code and change it around a small bit:
  
#include "colors.inc"
+
<source lang="pov">#include "colors.inc"
camera{
+
camera{
  location <0, 1, -10>
+
  location <4, 4, -10>
  look_at 0
+
  look_at 0
  angle 36
+
  angle 36
}
+
}
light_source{ <500, 500, -1000> White }
+
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
+
plane{ y, -1.5
  pigment{ checker Green White }
+
  pigment{ checker Green White }
}
+
}
intersect{
+
intersection{
  box{ -1, 1 }
+
  box{ -1, 1 }
  sphere{ 0, 1 }
+
  sphere{ 0, 1.375 }
  pigment{ Red }
+
  pigment{ Red }
}
+
}</source>
  
What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere's edge.
+
What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere.
 +
 
 +
[[Image:Csg_intersection_example.png|320px|thumb|center|Intersection Example]]
  
 
== Difference ==
 
== Difference ==
Again, as with intersect and union, the difference creates a single object out of many. The difference object though is created by taking the initial object in the list and subtracting from it anything that comes into contact (and intersects) with it. A thing to note, is that any of these can be embedded with each other.
+
Again, as with intersection and union, difference creates a single object out of many. The difference object is created by taking the initial object in the list and cutting away the other listed objects from it. A thing to note is that any of the CSG operations can be embedded with each other:
 +
 
 +
<source lang="pov">#include "colors.inc"
 +
camera{
 +
  location <4, 4, -10>
 +
  look_at 0
 +
  angle 36
 +
}
 +
light_source{ <500, 500, -1000> White }
 +
plane{ y, -1.5
 +
  pigment{ checker Green White }
 +
}
 +
difference{
 +
  intersection{
 +
    box{ -1, 1 }
 +
    sphere{ 0, 1.375 }
 +
    pigment{ Red }
 +
  }
 +
  union{
 +
    cylinder{ -2*y, 2*y, 0.5 }
 +
    cylinder{ -2*x, 2*x, 0.5 }
 +
    cylinder{ -2*z, 2*z, 0.5 }
 +
    pigment{ Blue }
 +
  }
 +
}</source>
  
#include "colors.inc"
+
[[Image:Csg_difference_example.png|320px|thumb|center|Difference Example]]
camera{
 
  location <0, 1, -10>
 
  look_at 0
 
  angle 36
 
}
 
light_source{ <500, 500, -1000> White }
 
plane{ y, -1.5
 
  pigment{ checker Green White }
 
}
 
difference{
 
  intersect{
 
    box{ -1, 1 }
 
    sphere{ 0, 1 }
 
    pigment{ Red }
 
  }
 
  union{
 
    cylinder{ -2*y, 2*y, 0.25 }
 
    cylinder{ -2*x, 2*x, 0.25 }
 
    cylinder{ -2*z, 2*z, 0.25 }
 
    pigment{ Blue }
 
  }
 
}
 
  
 
== Merge ==
 
== Merge ==
Line 96: Line 101:
 
For example, take the following code:
 
For example, take the following code:
  
#include "colors.inc"
+
<source lang="pov">#include "colors.inc"
#include "textures.inc"
+
#include "textures.inc"
camera{
+
camera{
  location <0, 1, -10>
+
  location <0, 2, -10>
  look_at 1*y
+
  look_at 1*y
}
+
}
light_source{ 1000 White }
+
light_source{ 1000 White }
plane{ y, -1 pigment{ White } }
+
plane{ y, -1 pigment{ White } }
union{  
+
union{  
  sphere{ 0, 1 translate 0.5*x }
+
  sphere{ 0, 1 translate 0.5*x }
  sphere{ 0, 1 translate -0.5*x }
+
  sphere{ 0, 1 translate -0.5*x }
  texture{ T_Glass2 }
+
  texture{ Glass2 }
}
+
}
merge{
+
merge{
  sphere{ 0, 1 translate 0.5*x }
+
  sphere{ 0, 1 translate 0.5*x }
  sphere{ 0, 1 translate -0.5*x }
+
  sphere{ 0, 1 translate -0.5*x }
  texture{ T_Glass2 }
+
  texture{ Glass2 }
  translate 2*y
+
  translate 2*y
}
+
}</source>
  
 
On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge.
 
On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge.
 +
[[Image:Csg_merge_example.png|320px|thumb|center|Merge Example]]

Latest revision as of 21:41, 19 July 2009

Constructive Solid Geometry

Constructive Solid Geometry (CSG) is a powerful tool to combine primitive objects to create more complex objects.

Union

The union object is a way to apply a universal group to a family of objects. Their appearance does not change at all while using this. To declare a union object:

#include "colors.inc"
camera{ 
  location <4, 4, -10>
  look_at 0
  angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
  pigment{ checker Green White }
}
union{
  box{ -1, 1 pigment{ Blue } }
  sphere{ 0, 1.375 pigment{ Red } }
}

Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects.

#include "colors.inc"
camera{
  location <4, 4, -10>
  look_at 0
  angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
  pigment{ checker Green White }
}
union{
  box{ -1, 1 }
  sphere{ 0, 1.375 }
  pigment{ Red }
}

The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. The resultant image, rendered with anti-aliasing, is shown below:

Union Example

Intersections

Like union, intersect creates a single object out of many, however it applies a calculation that will only result in what parts of the listed objects that intersect with each other. Intersect inherits from union so anything you can do to a union object you can do in a intersect object.

So if we take our previous code and change it around a small bit:

#include "colors.inc"
camera{
  location <4, 4, -10>
  look_at 0
  angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
  pigment{ checker Green White }
}
intersection{
  box{ -1, 1 }
  sphere{ 0, 1.375 }
  pigment{ Red }
}

What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere.

Intersection Example

Difference

Again, as with intersection and union, difference creates a single object out of many. The difference object is created by taking the initial object in the list and cutting away the other listed objects from it. A thing to note is that any of the CSG operations can be embedded with each other:

#include "colors.inc"
camera{
  location <4, 4, -10>
  look_at 0
  angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
  pigment{ checker Green White }
}
difference{
  intersection{
    box{ -1, 1 }
    sphere{ 0, 1.375 }
    pigment{ Red }
  }
  union{
    cylinder{ -2*y, 2*y, 0.5 }
    cylinder{ -2*x, 2*x, 0.5 }
    cylinder{ -2*z, 2*z, 0.5 }
    pigment{ Blue }
  }
}
Difference Example

Merge

Merge is much like union, with the main difference of it actually treats all objects as one instead of just a list of objects in one family. This is more visible when using semi-clear or fully clear objects, such as water or glass-like objects.

For example, take the following code:

#include "colors.inc"
#include "textures.inc"
camera{
  location <0, 2, -10>
  look_at 1*y
}
light_source{ 1000 White }
plane{ y, -1 pigment{ White } }
union{ 
  sphere{ 0, 1 translate 0.5*x }
  sphere{ 0, 1 translate -0.5*x }
  texture{ Glass2 }
}
merge{
  sphere{ 0, 1 translate 0.5*x }
  sphere{ 0, 1 translate -0.5*x }
  texture{ Glass2 }
  translate 2*y
}

On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge.

Merge Example