Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

geop-core-rasterize

This page is a reference to the crate; how a mesh is sent to a viewer is in Lecture 39, and one of the rasterizer’s bugs in Lecture 42.

Turns a Model into a triangle mesh, writes it out as an STL file, and renders it for debugging.

Rasterizing a model

rasterize(model, n) samples every vertex, edge and face of a model into a RasterizedModel, keeping each point, polyline and triangle together with the id of the entity it came from. This is the one place topology becomes sampled geometry. Drawing and picking in the editor’s view, STL export and debug rendering all read the same triangles, so a pick can never disagree with what the viewer drew, and an exported mesh is exactly what is on screen.

n is a quality, not a fixed sample count: it controls how finely geometry that actually curves is approximated, while a straight edge or a flat face stays cheap.

A Rasterizer rasterizes models one after another, as an editor draws the part before a step and the part with it. Each face is meshed on its own once while what it is meshed from stays the same (its surface, and its loops’ pcurves, edges and their ends), and the faces are joined along their edges anew each time, so it draws what rasterize draws, bit for bit. Opening a step meshes again only the faces the steps after it changed.

Triangulating a face

face_triangles_uv (implemented by grid::triangulate_face) triangulates a face’s trimmed region in (u, v) space, then maps each triangle through the surface:

  1. Sample the outer loop and every hole from their pcurves into (u, v) polygons, and drop redundant collinear points.
  2. Pick a uniform grid resolution, doubling it until every cell’s flat approximation is within a curvature-derived tolerance of the true surface. A flat face stays coarse; a curved one gets the resolution its curvature needs.
  3. For each cell, clip the outer loop to it (clip::clip_to_rect), subtract each hole (clip::subtract_convex), and ear-clip what is left (triangulate_polygon).

Both clipping operations are Sutherland–Hodgman half-plane clipping and are computed directly from the boundary’s own edges. No edge is reconstructed, so a mesh edge can never cut across the interior instead of following the trim. Earlier approaches (constrained Delaunay, and bridging holes to the outer loop with a zero-width slit) each produced edges that did not follow the boundary, and were replaced by this simpler one.

This is the one part of the kernel that uses plain f64 rather than interval scalars. A mesh is only a picture of the geometry, never a claim the kernel reasons about, so there is no uncertainty worth tracking.

STL export

stl_triangles(raster, faces) returns the triangles of the given faces as StlTriangles, and write_stl writes them in binary or ASCII (StlFormat). The (u, v) triangulation says nothing about which side is outside, but every face’s surface normal points out of its solid, so each triangle is wound to agree with the normal.

Faces are sampled one at a time, so two faces that share an edge each sample it independently. The mesh is only as watertight as those samplings agree.

Debug rendering

The debug module renders models for a person to look at. A PrimitiveScene collects points, lines, triangles, labels and sampled NURBS curves and surfaces, and saves them as an interactive HTML file (PrimitiveScene::save_to_file). RasterizedModel::scene draws a rasterized model, each face in a color of the caller’s choice. The *_viz examples render curve-curve and curve-surface intersections.

rasterize_topology draws a model’s raw topology rather than its shape. Every vertex, edge, coedge and face is labelled with its id, edges and coedges get direction arrows, faces are semi-transparent with a normal arrow, and coedges on an outer loop and on a hole are drawn in different colours. This helps when debugging topological edits, where which coedge is which matters and an outer loop and a hole look the same as geometry.