Skip to content

Regions: abutting operands share one interface (the later face wins within a tolerance), and a near-gap check #325

Done

Description

@rmrsk

Regions whose operands abut exactly (two cubes sharing a face) have two coincident faces at the contact: the earlier operand's and the later one's. The queries then report the one interface as two different surfaces depending on the side asked from, so its surface id, label and part change across it. Requested 2026-11-10, after #321 (PR #323).

Today: with A first and B on top in an Overlay, closestSurface(p, A) names A's own face (on a tie the region's own surface wins) and closestSurface(p, B) names B's. firstRegionChange() keeps the earlier crossing on a tie too. The workaround is geometric: let the earlier operand reach a little into the later one, so that its face lies under the later one and only the later face remains. SurfaceSelector::interface(A, B) already catches both faces, so a boundary condition by interface is right either way.

Proposal: the later operand's face wins at a coincident face, within a tolerance. This is what a slight overlap gives, without changing the geometry: an Overlay can then be written with abutting parts.

  • closestSurface(p, region): where the region's own value and a later region's agree within the tolerance, the later one's face is the surface, so both sides name the same surface, label and part.
  • firstRegionChange(): where leaving the earlier solid and entering the later one happen at the same t within the tolerance, the later face is reported, so rays, the viewer and findSurfacePieces() see one piece.
  • The subdomain queries inherit it; the region functions do not change (both are zero at the contact).
  • The tolerance: exact ties happen only for analytic shapes on exact coordinates; meshes from different tools are flush only to about 1e-6 of their size. A default of a small multiple of the march tolerance at the object's scale (as the section cut's nudge uses), and a TapeOptions setting for parts that are only roughly flush.
  • Cost: comparisons on near-ties only; no extra evaluations elsewhere; device-safe.
  • Tests: abutting cubes from both sides name one surface; a rotated pair flush only to rounding, within the tolerance; a ray across the contact reports one crossing, on the later face.

Not covered: small gaps. Parts slightly apart leave a thin sliver of the background (region 0) between them. That is geometry, not a tie, and closing it would change the region functions. A check instead: checkLabels() or a sibling reports regions that nearly touch ("regions 1 and 2 nearly touch, gap 3e-6"), so the input can be fixed or an overlap built in.

Roadmap: #232.


Generated by Claude Code

Activity

  1. added
    enhancement
    csg
    on Nov 10, 2026
  2. rmrsk commented on Nov 27, 2026

    @rmrsk
    OwnerAuthor

    API proposal for approval (#325)

    Agreed in the design discussion (2026-11-27): the tolerance lives in TapeOptions only; the gap is closed in the region functions as well as in the queries; the near-gap check is in the same pull request. Branch: abutting-interfaces, off main.

    The rule

    Let A be a region and B the smallest of the solids of the regions after it (as in the region function today, without the masks the two share), and ε the tolerance as a length. Where S_A + S_B < ε, the two are one interface: B's face.

    • The region functions. A's solid is grown where it nearly meets a later one before it is clipped:
      S_A' = min(S_A, (S_A + S_B - ε) / 2), f_A = max(S_A', -S_B).
      Overlapping or flush operands are unchanged. A gap narrower than ε is filled by the earlier region up to the later one's face: both functions are zero on B's face, f_A = -f_B on B's side, and f_A stays 1-Lipschitz (a distance bound, so marching stays safe). region(), the background, the whole object and the subdomain functions use the grown solids.
    • The surface queries. closestSurface(p, region) names B's face where S_A + S_B < ε, from either side. The neighbor region and material across it are the other region's, from both sides; today, from the later region's side of flush faces, the neighbor comes out as the background.
    • Rays. firstRegionChange() reports one crossing at the interface, on B's face, in either direction; a ray traces the ungrown solids, so the crossing stays exact where the shapes have ray forms.
    • Geometry. The surface moves by at most the gap. A face meeting another at an angle gets a fillet about ε wide along the contact; a tangent or nearly tangent contact between two regions (two spheres, a cylinder on a plane) is welded by a neck of radius about sqrt(R ε). Only contacts between different regions are welded.

    The tolerance

    struct TapeOptions
    {
      // ...
    
      /**
       * @brief How close two regions must come to share one interface, as a fraction of the diagonal of the object's
       * bounding box. Where a region's solid and a later region's solid overlap, are flush, or leave a gap narrower than
       * this, the gap is closed (the earlier region fills it up to the later region's face) and the later region's face is
       * the interface between them, seen from either side: in the region functions, region(), closestSurface() and
       * firstRegionChange(). 0 welds only exact contacts. Default 1e-5.
       */
      double m_interfaceTolerance = 1e-5;
    };
    
    // Tape, host and device: the tolerance as a length, fixed when the tape is built.
    template <class T, class... UserPrimitives>
    class Tape
    {
      // ...
      EBGEOMETRY_HOST_DEVICE T getInterfaceTolerance() const noexcept;
    };

    Why 1e-5 of the diagonal: above float rounding of distances (a few 1e-7 of the coordinates) and the flushness of meshes exported by different tools (about 1e-6); below what an embedded-boundary grid resolves (rarely finer than 1e-4 of the domain); below the default march tolerance (1e-4), so the weld never decides more than a marched surface resolves.

    The near-gap check

    Host only, beside findSurfacePieces() (EBGeometry_SurfacePieces.hpp):

    /**
     * @brief Two regions a thin layer of background separates.
     */
    template <class T>
    struct NearGap
    {
      uint32_t m_region = 0;  // the two regions, m_region <= m_other (equal: one region coming close to itself)
      uint32_t m_other  = 0;
      T        m_gap    = 0;  // the narrowest gap a ray found (measured along an axis: at least the true gap)
      Vec3T<T> m_point;       // where it was found
    };
    
    /**
     * @brief The pairs of regions a thin layer of background separates: on the grid of axis rays findSurfacePieces()
     * casts, a stretch of region 0 shorter than a_width (a fraction of the box diagonal) between two regions. Each pair
     * once, with its narrowest gap. Gaps narrower than the tape's interface tolerance are closed, so never reported.
     * @details Host only. The cost is 3 a_resolution^2 rays, as for findSurfacePieces().
     */
    template <class TapeType>
    [[nodiscard]] EBGEOMETRY_HOST std::vector<NearGap<ShapePrecision<TapeType>>>
    findNearGaps(const TapeType& a_tape, double a_width = 1e-3, uint32_t a_resolution = 64);

    ebgstudio prints what it finds when a model loads, for example regions 1 and 2 nearly touch: gap 3.1e-6 at (0.2, 0, 0.5).

    Usage

    TapeOptions options;
    options.m_interfaceTolerance = 1e-4;  // parts from CAD that are only roughly flush
    
    const Tape<double> tape(pool, expr, options);
    
    for (const NearGap<double>& gap : findNearGaps(tape)) {
      std::printf("regions %u and %u nearly touch: gap %g\n", gap.m_region, gap.m_other, gap.m_gap);
    }

    Tests

    Abutting cubes name one surface, with the right neighbor material, from both sides; a rotated pair flush only to rounding is welded; a gap of 1e-6 of the diagonal is closed and one of 1e-4 is reported by findNearGaps() and not closed; a ray across the contact reports one crossing in each direction; two nearly touching spheres in different regions are welded by a neck; tolerance 0 keeps today's behavior for a gap. Each device-callable query also as a [gpu] case.

    Checks

    The region functions and render kernels change: ptxas registers, stack and spills of the region kernels against the base, and a receipt with host and CUDA sections.

  3. added
    gpu
    on Nov 27, 2026
  4. added
    ray tracing
    performance
    documentation
    examples
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    csgdocumentationenhancementexamplesgpuperformanceray tracing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions