Boolean Operations with Solids in Mathematica
1
Boolean Operations with Solids in Mathematica Robert Kragler Weingarten University of Applied Sciences D-88241 Weingarten, Germany
[email protected] http://portal.hs-weingarten.de/web/kragler/mathematica Abstract. The intention of this paper is to investigate the mathematical algorithms which are involved in Constructive Solid Geometry (CSG). Besides basic primitives more sophisticated solid are created, such as superquadrics, polyhedra and 3D objects either defined by closed algebraic surfaces or Boolean functions. These 3D objects are subjected to geometric transformations (scaling, rotation, translation) and thus can be deformed, oriented and positioned at any spatial location. By means of various Boolean operators applied to these solids it is possible to combine them to more complex bodies. Operations typical for CSG systems such as extrusion and sweeping are discussed too; they admit creatation of 3D solids from 2D Boolean functions by extrusion. Skewed objects such as prisms etc. will be generated by sweeping techniques.
Initialization
Introduction CAD systems (see [1], [2a] such as solid modeling systems or volume modeler essentially apply Boolean operations. These mathematical methods used for the construction of 3D solids play an important part in CAD systems for simulation of components too. As regards to the mathematical description of 3D objects several possibilities exist :
Boundary Representation (B-rep) : In this case a 3D object is defined by its confining surfaces. B-rep models are preferentially applied for visualization of 3D computer graphics and also in CAD programs because they can be processed algorithmically fast. Furthermore, this method lends itself to the description of volume models too; the object is described by its confining surfaces only where it has to be ensured by an algorithm that the hull is closed. [2b]
Constructive Solid Geometry (CSG) : In this approach [2c] a 3D object is represented by combination of primitive bodies (such as sphere, cuboid, cylinder etc.) by means of Boolean algebra. The button Eval allows evaluation of a closed input cell (option CellOpenFalse will hide the Mathematica code) which immediately follows the Eval button. The illustration below shows the action of Boolean operations on a cube A and sphere B : Eval
2 Robert Kragler
union AB :
Union merges objects (A= cube, B= sphere) into a single object. intersection AB : Intersection retains only those parts which are common to both objects (A, B). difference A(¬B) : Difference cuts parts of object B from A (here subtraction of sphere from cube) neg. intersection ¬AB : Intersection of B (sphere) with ¬A (negated cube) results in the sphere minus the intersection (A B). This modeling technique of primitive bodies is applied in CAD programs and gives rise to the creation of more complicated solids by application of Boolean operations in order to combine various objects. Hence, objects generated with CSG might look rather complex but are nothing more than 3D objects skillfully combined with each other.
Geometric transformations The impact of geometric transformations (which are the result of scaling, rotation and translation of the solid in 3D space) will be illustrated for the simplest solid which is a cuboid : defineBooleanCuboid[{x_,y_,z_}][r_:1][scale_:{1,1,1}, trans_:{0,0,0}][{arg1_,arg2_List}]:= Module[{}, {coordS1,coordS} = coordTrans[{x,y,z}][trans, {arg1,arg2}]; y x boolean =(( Abs[ scale1 ]< r && Abs[ scale2 ] < r && z Abs[ scale3 ] < r )/.coordS1)/.coordS//N//Chop;
Return[boolean] ] Apart from a sphere all solids can be subjected to a rotation with respect to the origin either with (i) (, Q) where is the rotation angle and Q is the rotation axis, or equivalently (ii) P1 , P2 i.e. from axis P1 into direction of axis P2 . In addition a translation by vector t (default {0,0,0}) from the origin and scaling by s (default {1,1,1}) along the the coordinate axes are applicable to every solid. Mathematica provides several transformation functions for translation and rotation
Boolean Operations with Solids in Mathematica
3
gives a transformation representing a translation by a vector t. RotationTransform[,axis] transformation representing 3D rotation with angle around the direction axis axis RotationTransformP1 , P2}] rotates vector P1 into direction of vector P2 . TranslationTransformt]
The geometric transformations (rotation, translation) are incorporated in coordTrans. In this article, for sake of notational compactness, shortcuts will be used throughout such as fS ( = FullSimplify ) , fS1 ( = FullSimplify[…,{x,y,z} Reals] ), tF ( = TraditionalForm ), mF ( = MatrixForm ), mS ( = ComplexExpand[…, TargetFunctions{Re,Im}]), lEx ( = LogicalExpand ). Moreover, square1, square2 will remove square roots in inequalities and Boolean functions. In general, solids (in standard position) are defined in terms of (relatively simple) Boolean functions which turns out to be combinations of inequalities for the coordinates. Here we investigate a cuboid (with equal sides) cube defineBooleanCuboid x, y, z11, 1, 1, 0, 0, 0 0, 0, 0, 1 fS1 tF x 1 y 1 z 1
But, for an arbitrary spatial positioning (different from the coordinate origin) the coordinates {x,y,z} of the Boolean function will be modified due to a geometric transformation which combines scaling, rotation and translation. This modification (done by coordTrans) will be demonstrated for the cube above which is now scaled by s x , s y , sz 4, 2, 1, rotated around the z-axis by 4 and moved from the origin to t 1, 2, 3.
T0 TranslationTransform 1, 2, 31 mF ; R0 RotationTransform 4, 1, 0, 0 1 mF ; invM0 InverseT0.R0 mS mF; mF invM0, T0, R0
1
0
0
1
0
1
1
5
2 1
2 1
0
2
0
0
2 1
2
0
2
1
,
1 0 0 0
0 1 0 0
0 0 1 0
1 2 , 3 1
1
0
0
1
0 0
2 1
0
0
1
0
2 1
2
2
0
0
0 1
Hence, the coordinates are replaced by EvaluateThread x, y, z, 1 invM0., , , 1 Most . Thread , , x, y, z ColumnForm
4 Robert Kragler
x 1 x y z
5 2 1
2
y 2 y
2
z 2 z 2
Thus, the simple Boolean function of the cube given above now becomes : cuboid defineBooleanCuboid x, y, z14, 2, 1, 1, 2, 3 4, 1, 0, 0 . approxRoot Rationalize tF x 1 4
1
1
y
2
2
z
2
5
1
2
y 2
z 2
1
1
2
The result of the geometric transformation applied to the cuboid is visualized below :
Solids The solids provided through the package SolidModeling.m comprise several classes of 3D objects with names defineBoolean .
(i) Basic primitives Primitives (sphere, ellipsoid, cuboid, cylinder, cone, pyramid, toroid, hyperboloid of 1- and 2sheets and paraboloid) are Sphere with radius r = 2 centered at {0,0,0} Ellipsoid rotated from P1 ={0, 0, 1} into direction P2 ={1, -2, 1} and translated by t={0, 0, 1} Cube rotated from P1 ={0,0,1} into P2 ={0,1,1} Elliptic cylinder rotated by
3 4
around axis Q={0,1,0}
Elliptic cone rotated by 3 around axis Q={1,0,0} Elliptical toroid rotated by
4
around axis Q={0,1,0}
Boolean Operations with Solids in Mathematica
5
Elliptical hyperboloid of 1-sheet rotated from P1 ={0,0,1} into P2 ={1,0,0} Elliptical hyperboloid of 2-sheets rotated from P1 ={0,0,1} into P2 ={1,0,1} Elliptical paraboloid rotated from P1 ={0,0,1} into P2 2 , 0, 1 3
Rectangular pyramid rotated by
4
around axis Q={0,0,1} and tilted with respect to the
axis defined by Q1={0,0,0} and Q2={1,0,7} The Boolean functions for each solid generated above are visualized with RegionPlot3D (or with its abbreviation plotSolid). The button Eval displays all basic solids (but it is required to evaluate the Boolean functions of the preceding subsection).
(ii) Superquadric solids In addition to the primitive ellipsoid, hyperboloid, paraboloid and toroid there are so-called superquadrics defineBooleanSuper implemented whose shape can be modeled by two supplementary squareness parameters 1 , 2 ).
The exponents 1 , 2 ) characterize the “squareness” of superquadrics and generally assume noninteger values which modify the global properties of quadrics into super-quadrics. 1 is the lateral squareness parameter (in N-S direction) whereas 2 corresponds to the longitudinal squareness parameter (in E-W direction).
Boolean function for superellipsoid may serve as an example and is discussed in some detail. Parameters 1 and 2 characterize lateral/longitudinal squareness of the superellipsoid.
6 Robert Kragler
defineBooleanSuperellipsoid x_, y_, z_r_: 1, 1_: 1, 2_: 1 scale_: 1, 1, 1, trans_: 0, 0, 0arg1_, arg2_List : Module, coordS1, coordS
coordTrans x, y, zscale, trans, arg1, arg2; boolean
Abs scale1 x
22
Abs scale2 y
22 21
coordS1 . coordS N Chop;
Abs scale3 z
21
r
.
Returnboolean
Superellipsoids : (sell1) with radius r = 4 and squareness parameters 1 , 2 3.5, 0.25), scaling s x , s y , sz 1, 1.5, 1 in standard position ;
(sell2) with radius r = 4 and 1 , 2 0.5, 3), scalings x , s y, sz 1, 1.5, 1 rotated by
3
along {1,-1,2} axis.
Supertoroids : (stor1) with tube radius r = .1 , torus radius R=5, squareness parameters 1 , 2 1 2, 4), scaling 4, 3, 1 in standard position; (stor2) with radii (r, R) = ( .5, 3), 1 , 2 1 4, 1), scalings x , s y , sz 2, 1, .5 rotated by
2
along {0,1,0} axis.
Superhyperboloids of 1- and 2-sheets : (shyp1) with cutoff q1 , q2 3, 2, 1-sheet, radius r =1, squareness parameters 1 , 2 1,
1 , 3
scaling 1, 2, 2, rotated from {0,0,1} into {1,-1,0};
(shyp2) with cutoff q1 , q2 4, 4, 1-sheet, radius r = 4, squareness parameters
1 , 2 2 , 3, scaling 5 1, 1, 2, rotated from {0,0,1} into {1,0,1}; 5
1
(shyp3) with cutoff q1 , q2 3, 2, 2-sheets, radius r = 10 , squareness parameters 1
1 , 2 4, 3 , scaling 1, 2, 2, rotated from {0,0,1} into {1,-1,0}; 1
(shyp4) with cutoff q1 , q2 4, 4, 2-sheets, radius r = 20 , squareness parameters 1
1 , 2 2 , 3, scaling 5
1 5
2, 2, 4, rotated from {0,0,1} into {1,0,1}.
Superparaboloids :
(spara1) with cutoff q2 6, radius r = 0, squareness parameters 1 , 2 10 , 2, 1
scaling 1, 1, 1, standard position;
(spar2) with cutoff q2 5, radius r = 5, squareness parameters 1 , 2 4, 2 , scaling 7
1, 2 , 1, translation t={0,0,-2}, rotated from {0,0,1} into {0, - 2 , 1}. 3
1
Again, the Boolean functions obtained above are visualized with plotSolid. ( Eval )
Boolean Operations with Solids in Mathematica
7
Out[26]=
(iii) Polyhedral solids è PolyedronData The built-in procedure PolyhedronData[poly,”property”] is a very general and comprehensive procedure for altogether 195 polyhedra providing for each polyhedron its properties respectively lists of polyhedra classes. If the option ”RegionFunction” is chosen one obtains for a subset of 151 polyhedra the corresponding Boolean functions in standard position. Hence, the Boolean functions of a tetrahedron is generated by : PolyhedronData "Tetrahedron ", "RegionFunction "x, y, z lEx tF
3 z
4 4
2 04
3 2 x z 3
3 z8
6 x3
2
2 4 y 1 4 6 x 3
2 y
3 z 3
2
A complete overview of polyhedra covered by PolyhedronData is given by the following Manipulate program ( Eval ) which delivers apart from visualizing the polyhedron chosen additional properties; default value is the polyhedron name.
8 Robert Kragler
poly property
Octahedron Name
octahedron 2 2 x 2 y 2 z 0 && 2 x 2y
2 2 y 2 z &&
2 2 x 2 z && 2 x y
2 x z
2 2 y && 2 y z
2 2 z && 2 z
2 2 x 2 y &&
2 2 x && 2 x y z
2
The Boolean function (if it exists for the specific polyhedron selected) is accessible for further treatment through the (internal) variable booleFct . è defineBooleanPolyhedron All 151 polyhedra for which a Boolean function exists can be used as solids in the same way as the basic primitives given in the previous section. In the spirit of previous procedures defineBooleanPolyhedron is defined : defineBooleanPolyhedron[polyhedron_,{x_,y_,z_}][{Q1_List,Q2_List },r_:1][scale_:{1,1,1},trans_:{0,0,0}][{arg1_, arg2_List}]:= Module[{booleanFct}, {coordS1,coordS} = coordTrans[trans,{arg1,arg2}]; booleanFct = PolyhedronData[polyhedron,"RegionFunction"]/. y x z {#1 ,#2 ,#3 , FunctionIdentity}; scale1
scale2
scale3
boolean = booleanFct/.coordS1/.coordS//N//Chop; Return[boolean] ]
Octogonal prism rotated by
2
4
along the axis {0,1,0} and hexagonal antiprism rotated by
along the axis {1,0,0} and scaled 3, 2, 4. (Eval )
Boolean Operations with Solids in Mathematica
9
(iv) Solids defined through algebraic surfaces In addition to previous solids more interesting 3D objects can be created by means of cubic, quartic or sextic surfaces defined by inequalities, for example : x y z 1 or xn yn zn x2 y2 z2 12 with n = 4, 6
The resulting solids are (multi-connected or dis-connected) 3D objects such as ( Eval )
Out[32]=
Boolean operations for CSG An algebraic approach of Boolean operations applied to solids (but in the context of offsetting and
10 Robert Kragler
blending) was given by T. Sturm [3]. The intention of the current paper, however, is to investigate the result of Boolean operations in the context of constructive solid geometry (CSG) and is more in the spirit of the workshop given by Yu-Sung Cheng [4]. The Boolean operations acting on Boolean functions gi are based on the built-in Boolean operators provided by Mathematica. The package SolidModeling provides the following Boolean operators which can be applied directly to solids.
CSG operations : union, difference, intersection, symmDifference, nor, xnor, nand, complement union[g1_, difference[g1_, intersection[g1_, symmDifference[g1_, nor[g1_, xnor[g1_, nand[g1_, complement[g1_]
g2_] g2_] g2_] g2_] g2_] g2_] g2_] := ¬
:= := := := := := := g1
g1 g1 g1 g1 g1 g1 g1
g2 ¬ g2 g2 g2 g2 g2 g2
(* Or (||,)
*)
(* (* (* (* (* (*
*) *) *) *) *) *)
And (&&,) Xor () Nor, Xnor, Nand, Not,¬
Combinations of Boolean expressions (which consist of inequalities or other Boolean functions) can be simplified by applying LogicalExpand (for which the shortcut lEx is used for sake of notational compactness).
3d Cutter The procedures defineBooleanCutter, cutDir and cutY are contained in the package SolidModeling. defineBooleanCutter[{x_, y_, z_}][r_:1][scale_:{1,1,1}, trans_:{0,0,0}][{arg1_,arg2_List}] := Module[{}, {coordS1,coordS}=coordTrans[{x,y,z}][scale,trans,{arg1,arg2}]; x boolean =(( scale1 < r)/.coordS1)/.coordS//N //Chop; Return[boolean] ] cutDir[{x_, y_, z_}][_:0, xt_:0]:= defineBooleanCutter[{x,y,z}][1][{1,1,1},{xt,0,0}][{,{0,0,1}}] cutY[{x_, y_, z_}][{arg1_, arg2_List}]:= defineBooleanCutter[{x, y, z}][0][{1,1,1},{0,0,0}][{arg1,arg2}]
? defineBooleanCutter cutDir cutY cutter Usage of gcut = cutDir[{x,y,z}][-/4,-1.5] performs a cut through a given volume body with respect to angle 4 at distance x = -1.5 from the origin. Visualization shows the application of the fBoolean operations ( union, difference, intersection and negated difference) onto the two primitives defineBooleanCuboid and defineBooleanSphere. Here, C denotes a cube centered in the origin and S a sphere trans-
Boolean Operations with Solids in Mathematica
11
lated from the origin to position {1,1,1}. ( Eval )
Further Boolean operations ( complement, xor, nor, xnor ) are illustrated by ( Eval )
Finally, the same figures as above but with diagonal cutter gcut applied : ( Eval )
Combination of primitives to more complex objects Suitable combinations of solids and application of Boolean operations such as union, intersection and negation are used to construct more complex solids.
Cube with 3 holes drilled along major axes Instead of x2 y2 3 x2 z2 3 y2 z2 3 gCube2R gCube3R a convolution can be used :
12 Robert Kragler
Foldintersection , x2 y2 3,
x2 z2 3, y2 z2 3, gCube2R, gCube3R
Cylinder, cube, sphere, ellipsoid and cone Subsequently, from primitives such as cube, sphere, 2 cones and 3 cylinders oriented along the coordinate axes, more complex 3d objects are constructed. (Eval )
Out[150]=
Shells made from ellipsoids and hyperboloids Shells created from quadric primitives are obtained by the Boolean operator difference[g2,g1] together with a suitable cutoff. The first example is an elliptic shell
3
2
2
1 5
x2 y2 z2 3
y 0 with wall thickness
0.32 ) resulting from the difference of the larger solid ell2 and the smaller one
ell1. Instead of y > 0 the Boolean cutoff function cutY is used. The inequality of an elliptic shell is given by : ( Eval ) The second example shows a shell constructed from a hyperboloid of 1-sheet which is defined by
Boolean Operations with Solids in Mathematica
13
the inequality 1 x2 y2 z2 1 with cutoff between -5 < z < +5 . The resulting hyperbolic shell is given by the inequality : ( Eval ) The third example is a kind of ‘shell’ defined by intersection of two inequalities with different pnorms : 1 Norm[{x,y,z},n] Norm[{x,y,z},m] 1 . The shell resulting from the intersection of a 2-norm with 4-norm gives rise to the following inequality (Eval ) Hence, the three different shells created are : ( Eval )
Intersection of a sphere, cylinders and hyperboloid è The first example is the construction of a barrel which is definded as intersection of a sphere with radius r :
x2
y2
z2
6 and a cylinder in x-direction with radius
6
y2
z2
3 2 ( Eval )
2 . The defining inequality is
è The second example results from intersection of a sphere with a hyperbolid (of 1-sheet) which leads to the following inequalities: x2 y2 z2 1 x2 y2 z2 ( Eval )
è The third example investigatesintersection resp. union of two cylinders oriented along x- and yaxis. The inequalities are : y2 z2 3 and x2 z2 3. ( Eval ) Finally, the resulting Boolean functions give rise to the following solids ( Eval )
Out[100]=
Creation of arbitrary solids by Extrusion In the context of CSG extrusion is defined as dimensional rising of an element (e.g. a planar curve
14 Robert Kragler
or surface) by means of parallel translation into space. Extrusion of a (closed) surface gives rise to a generalized cylinder whose cross-section contains this surface. Extrusion is a method widely used in solid modeling to create objects of a fixed cross-sectional profile. The main advantage is its ability to create very complex cross-sections
BooleanFunction[k,n] Mathematica contains the procedure BooleanFunction[k,n] which represents the k-th Boolean function in n variables and is equivalent to the function
BooleanFunctionIntegerDigitsk, 2, 2n where n assumes the smallest value n
which fulfills k < 22 . Another useful built-in procedure is BooleanConvert. Several interesting shapes are generated with a particular choice of fk, BooleanFunctionIntegerDigitsk, 2, 22 , if for the coordinates (,)inequalities are substituted. ? BooleanFunction BooleanConvert By means of BooleanConvert the Boolean expression is converted into a Boolean function which consists of combination of inequalities as given below. With {k,kRange} the sequence of Boolean functions is arranged in complementary pairs. ( Eval ) kRange Table, 15 , , 1, 7 Flatten; Table"k", PaddedForm k, 2, BooleanConvert
BooleanFunction k, 2x2 y 1, x y2 0 0 z 5,
k, kRange ColumnForm
Extrusion is achieved by intersection of each Boolean function with the inequality 0