Link Search Menu Expand Document

Picking & Selection

OCCTSwiftViewport resolves a tap or click through two complementary paths: a GPU pick pass that reads a single RG32Uint texel to identify which body and primitive was hit, and a CPU raycast (SceneRaycast) that computes a precise world-space intersection point. Both paths are controlled from ViewportController.


Enabling the GPU pick pass

Picking is off by default. Turn it on when you create your ViewportConfiguration:

var config = ViewportConfiguration.cad
config.pickingConfiguration = PickingConfiguration(isEnabled: true)

let controller = ViewportController(configuration: config)

The renderer allocates the RG32Uint pick texture (8 bytes per pixel: 16.6 MB at 1920 × 1080) and runs its 1× pick pass only when picking is enabled, keeping the default render path lean.


Pick tolerance for thin edges and vertices

By default a pick reads exactly one pixel, so a 1 px edge or a vertex marker needs a pixel-perfect click. Set pickRadius to search a small square around the cursor instead:

config.pickingConfiguration = PickingConfiguration(isEnabled: true, pickRadius: 3)

Within that 7 x 7 neighbourhood the pick prefers a vertex over an edge over a face, and the nearest hit among equals, so clicking near an edge on top of a face selects the edge. 2–4 px suits most CAD work; the value is clamped to 32.


Picking without a view

The same pick pass runs headlessly. OffscreenRenderer stamps the same RG32Uint texture and decodes it with the same PickResult, so a server-side renderer, a snapshot tool or a test harness can ask what is under a pixel without a live viewport:

let renderer = OffscreenRenderer()!
let options = OffscreenRenderOptions(matching: controller, width: 1024, height: 768)

let hit = renderer.performPick(bodies: bodies, at: SIMD2(512, 384), options: options)
let box = renderer.performRegionPick(
    bodies: bodies, rect: CGRect(x: 100, y: 100, width: 300, height: 200), options: options)

For the same scene and camera the result equals the live performPick and performRegionPick. options.pickRadius and options.selectionFilter play the roles of PickingConfiguration.pickRadius and ViewportController.selectionFilter. To pick many pixels of one scene, call renderPickMap(bodies:options:) once and use the returned OffscreenPickMap. See Offscreen Rendering.


Reading a pick result

After a tap, the renderer reads the pick texture and populates ViewportController.pickResult (a @Published property). Subscribe with Combine or read it directly in SwiftUI:

// SwiftUI: react inside .onChange
MetalViewportView(controller: controller, bodies: bodies)
    .onChange(of: controller.pickResult) { _, result in
        guard let result else {
            // tap landed on background
            return
        }
        print("hit body:", result.bodyID)
        print("kind:", result.kind)          // .face / .edge / .vertex
        print("primitive index:", result.triangleIndex)
    }

PickResult fields:

Property Type Meaning
bodyID String The id string of the picked ViewportBody
bodyIndex Int Zero-based draw-order index
kind PrimitiveKind .face, .edge, or .vertex
triangleIndex Int Primitive index within the body (triangle for face picks, segment for edge picks, point for vertex picks)
pickLayer PickLayer .userGeometry or .widget
pickValue UInt64 Raw RG32Uint texel, G << 32 \| R (R: objectIndex; G bits 0–29: primitiveID, bits 30–31: kind)

pickResult carries .userGeometry picks. Widget-layer picks (manipulator handles, etc.) land separately in widgetPickResult and are never mixed into the selection stream.

Callback alternative

If you prefer a callback over Combine, assign onPick:

controller.onPick = { result in
    if let result {
        highlightBody(id: result.bodyID)
    }
}

Body-level selection

ViewportController maintains selectedBodyIDs: Set<String>, which the renderer uses to draw a highlight outline around selected bodies. The outline’s colour and pixel width come from ViewportConfiguration.selectionOutline (and .hoverOutline for the hovered body). Select programmatically or mirror a pick result:

// Single select
controller.selectBody("bracket", toggle: false)

// Multi-select (toggle mode)
controller.selectBody("bracket", toggle: true)

// Mirror GPU pick → body selection
controller.onPick = { result in
    guard let result else { return }
    controller.selectBody(result.bodyID, toggle: false)
}

// Clear
controller.deselectAll()
// or
controller.clearSelection()   // also clears pickResult

Selected and hovered bodies also get a faint blue wash over the body itself, separate from the outline. Its colour and strength come from ViewportConfiguration.selectionTint and .hoverTint (SelectionTintStyle), so an app with an orange selection colour can match it, or turn the wash off for outline-only selection. OffscreenRenderOptions carries the same two fields.

var config = ViewportConfiguration.cad
let orange = SIMD3<Float>(1.0, 0.6, 0.1)
config.selectionOutline = SelectionOutlineStyle(color: orange, width: 3)
config.selectionTint = SelectionTintStyle(color: orange, strength: 0.2)  // match the outline
config.hoverTint = .off                                                  // no wash on hover

PickResultFilter chains

A PickResultFilter is a composable predicate that runs on a decoded PickResult after the GPU pass. A result that fails the filter is treated as a miss: since the GPU resolves exactly one primitive per pixel, there is no fallback candidate.

(Called SelectionFilter until v1.1.30; that spelling survives as a deprecated typealias. See Picking & Selection for why it was renamed.)

Assign one to ViewportController.selectionFilter:

// Faces only
controller.selectionFilter = .faces

// Edges or vertices
controller.selectionFilter = .edges.or(.vertices)

// Faces, excluding a construction body
controller.selectionFilter = .faces.and(.excludingBodyIDs(["ground_plane"]))

// Custom predicate
controller.selectionFilter = PickResultFilter { result in
    result.kind == .face && result.bodyID.hasPrefix("solid_")
}

// Remove the filter (accept everything)
controller.selectionFilter = nil

Built-in factory filters:

Filter Accepts
.all Everything
.nothing Nothing
.faces .face primitives
.edges .edge primitives
.vertices .vertex primitives
.kind(_:) Exact PrimitiveKind
.kinds(_:) Set of PrimitiveKind
.bodyIDs(_:) Allow-listed body IDs
.excludingBodyIDs(_:) Deny-listed body IDs
.bodyIndices(_:) Allow-listed draw-order indices
.layer(_:) Specific PickLayer

Compose with .and(_:), .or(_:), .negated, PickResultFilter.all(of:), and PickResultFilter.any(of:).


Excluding bodies from picks with isPickable

Set ViewportBody.isPickable = false to keep a body visible but invisible to the GPU pick pass. This is the right way to handle datum planes, grid meshes, or any decorative geometry that should never steal a tap from real parts:

let groundPlane = ViewportBody(
    id: "ground",
    vertexData: planeVerts,
    indices: planeIndices,
    isPickable: false       // drawn, but excluded from pick texture
)

The body’s objectIndex still advances in the draw order so other indices remain stable. You do not need a PickResultFilter as well: isPickable: false is cheaper because it skips the pick sub-passes entirely.

To take only some of a body’s edge segments out of picking, set ViewportBody.edgeDrawRange: the segments outside it are neither drawn nor pickable, and the ones inside keep their absolute index in triangleIndex. See Per-segment edge colour and a visible range.


CPU raycast with SceneRaycast

The GPU pick pass identifies which body and primitive was hit but carries no world-space position. Use SceneRaycast when you need the actual hit point in 3D (for measurements, snapping, or distance-aware filtering).

Constructing a ray from a tap

Convert a screen-space tap location to NDC, then use Ray.fromCamera:

// screenPoint: CGPoint in the viewport's coordinate system (origin top-left)
// viewportSize: CGSize of the MetalViewportView
func makeRay(from screenPoint: CGPoint,
             viewportSize: CGSize,
             controller: ViewportController) -> Ray {
    let aspectRatio = Float(viewportSize.width / viewportSize.height)

    // NDC: x in [-1, 1] right, y in [-1, 1] up
    let ndcX = Float(screenPoint.x / viewportSize.width)  * 2.0 - 1.0
    let ndcY = 1.0 - Float(screenPoint.y / viewportSize.height) * 2.0
    let ndc = SIMD2<Float>(ndcX, ndcY)

    return Ray.fromCamera(
        ndc: ndc,
        cameraState: controller.cameraState,
        aspectRatio: aspectRatio
    )
}

Ray.fromCamera handles both perspective and orthographic projections automatically.

Casting the ray

Pass the ray, your body array, and a bounding-box cache. SceneRaycast.cast runs AABB broadphase (slab method) then Möller–Trumbore narrowphase on survivors, returning the nearest hit:

func castRay(_ ray: Ray,
             bodies: [ViewportBody],
             controller: ViewportController) -> RaycastHit? {
    // Build a simple AABB cache from each body's bounding box
    var bbCache: [String: BoundingBox] = [:]
    for body in bodies {
        bbCache[body.id] = body.boundingBox
    }

    return SceneRaycast.cast(
        ray: ray,
        bodies: bodies,
        boundingBoxCache: bbCache,
        clipPlanes: controller.clipPlanes  // skip geometry a section plane removed
    )
}

For a one-off cast, SceneRaycast.cast(ray:bodies:clipPlanes:) builds the cache for you.

Like the GPU pick pass, the cast skips bodies with isPickable == false, and it also skips widgets: bodies on the .overlay render layer or the .widget pick layer (gizmos, datum planes), so a raycast lands on the model rather than on a widget in front of it. Pass includeWidgets: true or includeNonPickable: true to test them too.

RaycastHit gives you bodyID: String, point: SIMD3<Float> (world-space), and distance: Float from the ray origin.

Full tap-to-world-point example

func onTap(at screenPoint: CGPoint,
           viewportSize: CGSize,
           controller: ViewportController,
           bodies: [ViewportBody]) {
    let aspectRatio = Float(viewportSize.width / viewportSize.height)
    let ndcX = Float(screenPoint.x / viewportSize.width)  * 2.0 - 1.0
    let ndcY = 1.0 - Float(screenPoint.y / viewportSize.height) * 2.0
    let ray = Ray.fromCamera(
        ndc: SIMD2<Float>(ndcX, ndcY),
        cameraState: controller.cameraState,
        aspectRatio: aspectRatio
    )

    var bbCache: [String: BoundingBox] = [:]
    for body in bodies { bbCache[body.id] = body.boundingBox }

    if let hit = SceneRaycast.cast(ray: ray, bodies: bodies, boundingBoxCache: bbCache) {
        print("hit \(hit.bodyID) at \(hit.point), distance \(hit.distance)")
    }
}

Direct ray construction

You can also build a Ray directly for programmatic use (e.g. automated testing):

let ray = Ray(
    origin: SIMD3<Float>(0, 10, 0),
    direction: SIMD3<Float>(0, -1, 0)   // normalized automatically
)

Projecting world points back to screen

Use ProjectionUtility to go the other direction, world position to screen coordinates, for annotation placement or custom overlays:

let state = controller.cameraState
let vpMatrix = state.projectionMatrix(aspectRatio: aspectRatio) * state.viewMatrix

if let screenPt = ProjectionUtility.worldToScreen(
    point: hit.point,
    vpMatrix: vpMatrix,
    viewportSize: viewportSize
) {
    // screenPt is a CGPoint with origin at top-left, Y-down
    drawAnnotation(at: screenPt)
}

worldToScreen returns nil when the point is behind the camera (in orthographic, nearer than the near plane). worldToNDC gives the same result in NDC space when you need to stay in normalized coordinates.


Two-stream pick routing

Bodies tagged with pickLayer: .widget bypass the selectionFilter and land in widgetPickResult instead of pickResult. This lets manipulator widgets (e.g. from OCCTSwiftAIS) have their own pick stream without colliding with user geometry selection:

let handle = ViewportBody(
    id: "translate_x",
    vertexData: arrowVerts,
    indices: arrowIndices,
    pickLayer: .widget   // routed to controller.widgetPickResult
)

faceIndices: mapping triangles to BREP faces

When your geometry comes from a B-Rep tessellation, supply faceIndices to map each triangle back to its source face ID. This lets you identify which BREP face was tapped from a .face PickResult:

let body = ViewportBody(
    id: "solid",
    vertexData: meshVerts,
    indices: meshIndices,
    faceIndices: perTriangleFaceIDs   // [Int32], one entry per triangle
)

// In the pick handler:
controller.onPick = { result in
    guard let result, result.kind == .face else { return }
    let faceID = body.faceIndices[result.triangleIndex]
    print("tapped BREP face", faceID)
}

faceIndices is optional (defaults to []). When empty, triangleIndex is still valid as a raw triangle index within the body’s indices buffer.

triangleIndex is the input triangle index under every renderingQuality, including the tessellated (.enhanced) and mesh-shader (.maximum) pipelines, so this lookup works at any quality.

Pick limits

The GPU pick texture is RG32Uint, so triangleIndex is always exact:

  • 4,294,967,295 visible bodies per frame (PickResult.maxPickableBodies), counted in draw order.
  • 1,073,741,824 primitives per kind per body (PickResult.maxPrimitivesPerKind).

Both are far beyond what the renderer can hold, so there is nothing to guard: a tap, a performPick call or a region pick on a million-triangle STL reports the true triangle, edge or point index, and faceIndices[result.triangleIndex], edgeIndices[...] and vertexIndices[...] can be indexed directly.

Before issue #160 the target was R32Uint, which capped a frame at 65,536 pickable bodies and a body at 16,384 primitives per kind; a bigger body reported the index modulo 16,384 with isPrimitiveIndexAmbiguous == true. If you wrote code for that (checking the flag, calling ViewportBody.resolveWrappedTriangleIndex(_:ray:)), you can delete it: the flag is now always false and both are deprecated no-ops. PickResult.rawValue (a lossy UInt32) is deprecated in favour of pickValue.

The price is the texture: 8 bytes per pixel instead of 4, so 16.6 MB at 1920 × 1080 and 66.4 MB at 3840 × 2160, and 8 bytes per pixel read back for each pick. See Picking: Cost.