Examples¶
Here we show a few simple examples on how to use sdf_tools
.
A Sphere…¶
Create the SDF of a single sphere in the middle of a domain:
#! /usr/bin/env python
import sdf_tools
# first we need to allocate a uniform grid
offs = (0., 0., 0.) # start of the grid
dims = (64, 64, 64) # resolution of the grid
exts = (1.0, 1.0, 1.0) # extents of the grid
grid = sdf_tools.Grid.Uniform(dims, offs, exts)
# now we create a sphere description
center = (0.5, 0.5, 0.5)
radius = 0.4
sphere = sdf_tools.Sdf.Sphere(center, radius, inside=True)
# apply the sdf to the grid points
grid.evaluate_sdf(sphere)
# dump the grid to .bov format, can be open with visit or paraview
grid.dump("sphere.bov")
The above script can be run by typing
python 01_sphere.py
The resulting files sphere.bov
and sphere.values
can be visualiszed with
visit.
Binary operations¶
It is possible to apply operations between several shapes.
Let us create the union of a ball and a capsule (see Capsule
).
#! /usr/bin/env python
import sdf_tools
offs = (0., 0., 0.)
dims = (128, 64, 64)
exts = (2.0, 1.0, 1.0)
grid = sdf_tools.Grid.Uniform(dims, offs, exts)
# create 2 shapes
sphere = sdf_tools.Sdf.Sphere(center=(1.0, 0.5, 0.5),
radius=0.4, inside=True)
capsule = sdf_tools.Sdf.Capsule(start=(0.3, 0.5, 0.5),
end =(1.7, 0.5, 0.5),
radius = 0.2, inside=True)
# create a union of the 2 shapes
my_sdf = sdf_tools.Sdf.Union(sphere, capsule)
# evaluate on the grid and dump to file
grid.evaluate_sdf(my_sdf)
grid.dump("union.bov")
More complex geometries¶
It is very easy to extend the previous example to create more complex geometries. An example is shown here:
#! /usr/bin/env python
import sdf_tools
offs = (0., 0., 0.)
dims = (128, 64, 64)
exts = (2.0, 1.0, 1.0)
grid = sdf_tools.Grid.Uniform(dims, offs, exts)
sphere = sdf_tools.Sdf.Sphere(center=(1.0, 0.5, 0.5),
radius=0.4, inside=True)
capsule0 = sdf_tools.Sdf.Capsule(start=(0.3, 0.5, 0.5),
end =(1.7, 0.5, 0.5),
radius = 0.2, inside=True)
capsule1 = sdf_tools.Sdf.Capsule(start=(1.3, 0.5, 0.1),
end =(0.7, 0.5, 0.9),
radius = 0.2, inside=True)
my_sdf = sdf_tools.Sdf.Difference(sdf_tools.Sdf.Union(sphere, capsule0),
capsule1)
grid.evaluate_sdf(my_sdf)
grid.dump("three.bov")