Constructive Solid Geometry
Description
3D Solid modeling using combining and transforming primite shapes. The solids can be exported as stl files.
The source code is hosted on https://codeberg.org/her01n/constructive-solid-geometry.
Example
(import constructive-solid-geometry) (export-stl "example.stl" (difference (box 5 4 1) (translate (vector 5 0 0) (cylinder 2 1))))
Reference
All identifiers are imported from constructive-solid-geometry module.
(import constructive-solid-geometry)
Points and vectors in 3D space are represented as standard vector, with three elements for x, y, z coordinates.
Primitives
[procedure] (box width length height)An orthogonal cuboid with one corner at origin and the other at [width, length, height] coordinates.
[procedure] (box u v)An orthogonal cuboid with one corner at u and the other at v.
[procedure] (box? value)True if value is a box.
[procedure] (width box)[procedure] (box-length box)
[procedure] (height box)
Returns the parameters of the box. box-length is chosen to not conflict with base scheme function length.
[procedure] (cylinder radius height)Constructs a cylinder primitive. The cylinder extends in z-axis direction, with the base center at origin, and the other base center at [0, 0, height].
[procedure] (cylinder? value)True if the value is a cylinder.
[procedure] (radius cylinder)[procedure] (height cylinder)
Returns the parameters of the cylinder primitive.
[procedure] (sphere radius)Returns a sphere solid with the center at origin and the given radius.
[procedure] (sphere? value)True, if the value is a sphere.
[procedure] (radius sphere)Return the sphere's radius.
[procedure] (cone bottom-radius top-radius height)Construct a truncated cone - a frustum. One base is at z = 0 plane with the center at the origin and radius bottom-radius. Cone extends for height in positive z direction, ending with the other base with top-radius. One of bottom-radius or top-radius may be zero. When equal, the shape is identical to cylinder.
[procedure] (cone? value)Checks if the value is cone.
[procedure] (bottom-radius cone)[procedure] (top-radius cone)
[procedure] (height cone)
Extracts the cone's parameters.
Transformations
[procedure] (rotate axis angle child)Rotate the child around the axis. The axis is specified as a vector of three coordinates, the amplitude of the vector is ignored. Angle is expressed in radians.
[procedure] (rotate? value)True if the value is a rotation transformation.
[procedure] (axis rotation)[procedure] (angle rotation)
[procedure] (child rotation)
Access rotation parameters.
[procedure] (translate translation child)Move the child in the coordinate system. translation is a vector of three numbers.
[procedure] (translate? value)Check if the value is translation transformation.
[procedure] (translation translate)[procedure] (child translate)
Get the parameters of translation transformation.
Operations
[procedure] (union child ...)Merge children. This is a boolean or operation.
[procedure] (union? value)True if the value is an union operation.
[procedure] (children union)Returns the children of the union.
[procedure] (difference upper lower)Subtract lower solid from upper solid. Returns a part of upper that is not part of lower.
[procedure] (difference? value)Checks if value is a difference operation.
[procedure] (upper difference)[procedure] (lower difference)
Get the operands of the difference operation.
[procedure] (intersection child ...)Construct an intersection of all the children. This is a boolean and operation.
[procedure] (intersection? value)Checks if the value is an intersection operation.
[procedure] (children intersection)Returns the children of the intersection.
Export
[procedure] (export-stl filename solid)Calculates the triangular surface mesh, and writes it in STL format to a file.
Other
[procedure] (solid? value)Returns true if the value is a solid: either primitive, transformation or operation.
[procedure] (inside? point solid)Returns true if the point is inside a solid. If it point lies on the surface of the solid, the return value is unspecified.
Author
Michal Herko
License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. A full copy of the GPL license can be found at <http://www.gnu.org/licenses/>.