Skip to content

1. A first surface ​

The tutorial puts a body into a simulation grid. The body is dragon.stl, a binary STL of 6588 facets. This chapter loads it, reads what FOSSIL computed on loading, finds out why it is not a closed surface, and writes it back, once as one solid and once split into named patches. Three short programs, run one after the other.

Load, read, save ​

f90
use fossil, only : surface_stl_object
use penf,   only : I4P, R8P
use vecfor, only : vector_R8P

implicit none
type(surface_stl_object) :: surface
type(vector_R8P)         :: bmin, bmax

call surface%load_from_file(file_name='dragon.stl', guess_format=.true.) ! analyze runs inside
  • use fossil gives the four public types (surface_stl_object, facet_object, distance_field_object, curve_object) and the named constants. Kinds come from PENF (I4P, R8P), vectors from VecFor (vector_R8P).
  • load_from_file(file_name=, guess_format=.true.) reads an ASCII or a binary STL: with guess_format the format is detected from the file size. Without it the file is read as ASCII unless is_ascii=.false.. The optional status is STATUS_FILE_NOT_FOUND for a missing file and STATUS_INVALID_INPUT for NaN or Inf coordinates.
  • Loading runs analyze: bounding box, volume, centroid, facet connectivity, the AABB tree (a SAH BVH by default) and the pseudo-normals the signed distance needs. Every later query relies on it.
f90
bmin = surface%get_bmin() ; bmax = surface%get_bmax()
print '(A,I0)',       'facets      ', surface%get_facets_number()
print '(A,3F10.4)',   'box min     ', bmin%x, bmin%y, bmin%z
print '(A,3F10.4)',   'box max     ', bmax%x, bmax%y, bmax%z
print '(A,F12.4)',    'volume      ', surface%get_volume()
print '(A,L1,A,L1)',  'watertight  ', surface%is_watertight(), '   manifold ', surface%is_manifold()
  • The components of surface_stl_object are private. They are read through accessors: get_facets_number, get_bmin, get_bmax, get_volume, get_centroid, get_non_manifold_edges_number, and facet_at(i) for one facet.
  • is_watertight() is true when every edge has exactly two facets, is_manifold() when the surface is also free of edges with three or more facets.
f90
call surface%save_into_file(file_name='dragon-ascii.stl', is_ascii=.true.)
print '(A)', 'saved dragon-ascii.stl'
  • save_into_file(file_name=, is_ascii=) writes the surface, as ASCII by default (is_ascii=.false. writes binary). status reports STATUS_FILE_OPEN_FAIL when the file cannot be created.
$ tut01_first_surface
facets      6588
box min       -10.8287   -4.1883    5.2821
box max         9.6461    4.9512   19.7413
volume          465.9287
watertight  F   manifold F
saved dragon-ascii.stl

The dragon spans about 20.5 × 9.1 × 14.5 CAD units and encloses a volume of 465.93. It is neither watertight nor manifold.

the dragon as loaded, 6588 facets, inside the bounding box analyze computes

The bounding box is axis-aligned and belongs to the facets as they are: analyze recomputes it after any change. The animation turns the dragon about the vertical axis through the centre of its box, 30 degrees per frame, and analyzes it again each time. The box changes from 20.47 × 9.14 to 9.14 × 20.47 at a quarter turn and back; the volume stays 465.93.

the dragon turned about the vertical axis in steps of 30 degrees, with the axis-aligned bounding box analyze recomputes at each step: its x and y sides swap between 20.47 and 9.14 while the height 14.46 and the volume 465.93 stay the same

Why it is not closed ​

A facet knows its neighbours: fcon_edge(e) is the facet across edge e (edge 1 runs from vertex 1 to vertex 2, edge 2 from 2 to 3, edge 3 from 3 to 1), or 0 when the edge has no single neighbour. The second program reads one facet, then counts the edges that make the surface open or non-manifold.

f90
use fossil,                    only : surface_stl_object, FEATURE_EDGE_BOUNDARY, FEATURE_EDGE_NONMANIFOLD
use fossil_facet_object,       only : facet_object
use penf,                      only : I4P, R8P
use vecfor,                    only : vector_R8P

implicit none
type(surface_stl_object)          :: surface
type(facet_object), pointer       :: f
type(vertex_pool_object), pointer :: pool
type(vector_R8P), allocatable     :: a(:), b(:)
integer(I4P), allocatable         :: edges(:,:), kinds(:)
integer(I4P)                      :: i, v, open_edges, u
character(96)                     :: caption

call surface%load_from_file(file_name='dragon.stl', guess_format=.true.)
f => surface%facet_at(1_I4P)                                            ! a pointer into the surface, read-only use
do v = 1, 3
   print '(A,I0,A,3F9.4)', 'facet 1 vertex ', v, '   ', f%vertex(v)%x, f%vertex(v)%y, f%vertex(v)%z
enddo
print '(A,3F9.4)', 'facet 1 normal     ', f%normal%x, f%normal%y, f%normal%z
print '(A,3I6)',   'facet 1 neighbours ', f%fcon_edge                    ! across edges 1-2, 2-3, 3-1; 0: none
f90
call surface%feature_edges(edges=edges, kinds=kinds, angle=180._R8P)    ! no sharp edge at 180 degrees
print '(A,I0)', 'boundary edges     ', count(kinds == FEATURE_EDGE_BOUNDARY)      ! one facet
print '(A,I0)', 'non-manifold edges ', count(kinds == FEATURE_EDGE_NONMANIFOLD)   ! three or more
open_edges = 0
do i = 1, surface%get_facets_number()
   f => surface%facet_at(i)
   if (any(f%fcon_edge == 0_I4P)) open_edges = open_edges + 1             ! no single neighbour across an edge
enddo
print '(A,I0)', 'facets on them     ', open_edges
print '(A,I0,A,I0)', 'shells             ', surface%get_shells_number(), ', bodies ', surface%get_bodies_number()
  • facet_at(i) returns a pointer to facet i (null() out of range). Its public components are the data analyze filled: vertex(3), normal, centroid, fcon_edge(3), the pseudo-normals. Read them; change the surface only through its procedures.
  • feature_edges(edges=, kinds=, angle=) lists the feature edges by kind. With angle=180 no edge counts as sharp, so the list holds the boundary edges (FEATURE_EDGE_BOUNDARY, one facet) and the non-manifold edges (FEATURE_EDGE_NONMANIFOLD, three or more facets). Chapter 12 uses the same routine for sharp edges.
  • get_shells_number() counts the edge-connected sets of facets, get_bodies_number() the bodies (an outer shell with the cavities inside it).
$ tut01_open_edges
facet 1 vertex 1      6.9477   1.0539  13.9143
facet 1 vertex 2      6.8777   1.4739  13.9180
facet 1 vertex 3      6.8427   1.0817  13.7563
facet 1 normal       -0.8356  -0.1440   0.5301
facet 1 neighbours   4901  2872    45
boundary edges     0
non-manifold edges 61
facets on them     221
shells             83, bodies 40

The dragon has no boundary edge: it is not open. It fails is_watertight because of 61 non-manifold edges, where 221 facets meet three or four at an edge, and it is 83 overlapping shells grouped into 40 bodies. Chapter 2 shows where these defects come from and how many of them sanitize removes.

the dragon drawn translucent, with the 221 facets that lie on its 61 non-manifold edges in ember: small clusters scattered over the head, the body and the tail, most of them inside the surface

Several solids in one file ​

An ASCII STL can hold several solid <name> … endsolid blocks: exporters write one per boundary condition (wall, inlet, outlet). FOSSIL keeps them as named patches. The third program names three parts of the dragon by the x of the facet centroids, writes them as three blocks and reads the file back.

f90
use fossil,              only : surface_stl_object
use fossil_facet_object, only : facet_object
use penf,                only : I4P, R8P

implicit none
type(surface_stl_object)    :: surface, back
type(facet_object), pointer :: f
integer(I4P), allocatable   :: ids(:)
real(R8P),    allocatable   :: xc(:)
integer(I4P)                :: i, p, n, u

call surface%load_from_file(file_name='dragon.stl', guess_format=.true.)
n = surface%get_facets_number()
allocate(xc(n))
do i = 1, n
   f => surface%facet_at(i) ; xc(i) = f%centroid%x
enddo
ids = [(i, i = 1, n)]
call surface%set_patch(facets=ids, name='middle')                        ! every facet, then two subsets
call surface%set_patch(facets=pack(ids, xc < -4._R8P), name='front')
call surface%set_patch(facets=pack(ids, xc >  4._R8P), name='rear')
call surface%save_into_file(file_name='dragon-patches.stl', is_ascii=.true.) ! one solid block per patch
f90
call back%load_from_file(file_name='dragon-patches.stl', guess_format=.true.)
print '(A,I0)', 'patches read back ', back%get_patches_number()
do p = 1, back%get_patches_number()
   print '(A,I0,A,A8,I6,A)', 'patch ', p, '  ', back%patch_name(p), &
                             count(back%facet_patch(ids) == p), ' facets'
enddo
  • set_patch(facets=, name=) assigns facets to the patch name, created if new. Every facet starts in patch 1, named after the file header; a patch left without facets writes no block.
  • save_into_file(is_ascii=.true.) writes one solid block per patch. A binary STL has no place for names: it is read as one patch.
  • load_from_file opens a patch per block: get_patches_number, patch_name(i) and facet_patch(i) read them back. load_from_files reads one file per patch. The patch of each facet then travels through the queries of chapter 8 (query_many(patches=)).
$ tut01_patches
patches read back 3
patch 1    middle  3036 facets
patch 2     front  2238 facets
patch 3      rear  1314 facets

the dragon read back from dragon-patches.stl, each facet coloured by its patch: front in teal (2238 facets: the head and the forelegs), middle in cream (3036), rear in ember (1314: the tail)

See the cookbook recipe wall, inlet and outlet from one STL and the reference page Named patches.

What you learned

load_from_file reads an STL and analyzes it; accessors, facet_at and the validity predicates tell what kind of surface you have; feature_edges finds where it is open or non-manifold; set_patch and the ASCII writer keep named patches. Reference: surface_stl_object.

Next: 2. Repairing the surface.