Pattern Matching#

Pattern matching enables finding occurrences of reference shapes within larger assemblies using graph-based subgraph isomorphism.

Pattern Matcher Module#

The Pattern Matcher module provides tools for finding geometric patterns within assembly structures. It uses graph-based subgraph isomorphism to identify occurrences of a reference shape (pattern) within a larger assembly.


Overview#

Pattern matching is essential for:

  • Component identification: Find all instances of a specific part (screws, bolts, bearings)

  • Quality assurance: Verify that required components are present in assemblies

  • Design analysis: Identify repeating structural elements

  • Cost estimation: Count occurrences of specific parts

The module combines two key technologies:

  1. Attributed Adjacency Graphs (AAG) for geometric representation

  2. VF2 subgraph isomorphism algorithm for pattern matching


Class: PatternMatcher#

Description#

The PatternMatcher class searches for specific subgraph patterns within a GraphAssembly structure, focusing on solid geometries and their face relationships.

Constructor#

from volmdlr_tools.features import PatternMatcher

matcher = PatternMatcher(
    graph_assembly: GraphAssembly,
    angular_tol: float = 1e-6,
    min_similarity: float = 1.0,
    min_face_signature_similarity: float = 1.0,
    name: str = ""
)

Parameters:

  • graph_assembly: The assembly graph to search within

  • angular_tol: Angular tolerance in radians for edge matching

  • min_similarity: Minimum similarity threshold (1.0 = exact match)

  • min_face_signature_similarity: Minimum face signature similarity

  • name: Instance name


Methods#

find_pattern_matches(pattern_graph) -> list[Match]#

Find all occurrences of a pattern graph within the assembly.

matches = matcher.find_pattern_matches(pattern_graph=aag_pattern)

Parameters:

  • pattern_graph: An AttributedAdjacencyGraph representing the pattern to search for

Returns:

  • List of Match objects, each containing the faces that match the pattern

find_matches_from_multiple_patterns(patterns) -> list[list[int]]#

Search for multiple different patterns in a single pass.

all_matches = matcher.find_matches_from_multiple_patterns(patterns=[pattern1, pattern2])

Class: Match#

A match result containing the matched faces and their bounding geometry.

Attributes#

Attribute

Type

Description

faces

list[Face]

The matched face objects

match_shell

Shell

Shell created from matched faces

enclosing_box

Solid

Oriented bounding box enclosing the match

frame

Frame

Coordinate frame of the bounding box

color

Color for visualization

Methods#

# Get visualization primitives
primitives = match.volmdlr_primitives()

Matching Modes#

Exact Matching (default)#

When min_similarity=1.0, the matcher uses exact subgraph isomorphism:

  • Node matching: Target node degree >= pattern node degree

  • Edge matching: Angles must match within angular_tol

  • Face signatures must match within min_face_signature_similarity

matcher = PatternMatcher(
    graph_assembly=assembly_graph,
    min_similarity=1.0,  # Exact matching
    angular_tol=1e-6
)

Approximate Matching#

For finding similar but not identical patterns:

matcher = PatternMatcher(
    graph_assembly=assembly_graph,
    min_similarity=0.8,  # Allow 80% similarity
    min_face_signature_similarity=0.9
)

Approximate matching considers:

  • Node count similarity

  • Surface area similarity

  • Bounding box volume ratios


Complete Example#

from volmdlr_tools.features import PatternMatcher
from volmdlr_tools.graph.faces import AttributedAdjacencyGraph
from volmdlr_tools.graph.assembly import GraphAssembly
import volmdlr
from volmdlr import model, shapes

# Load assembly and pattern
folder = "data/step/"
volume_model = model.VolumeModel.from_step(folder + "HAWT Gear Assembly.STEP")
pattern_model = model.VolumeModel.from_step(folder + "hex-screw-pattern1.step")

# Create AAG for the pattern
aag_pattern = AttributedAdjacencyGraph(pattern_model.primitives[0])

# Create graph assembly and pattern matcher
graph_assembly = GraphAssembly.from_volume_model(volume_model)
pattern_matcher = PatternMatcher(graph_assembly=graph_assembly)

# Find all matches
matches = pattern_matcher.find_pattern_matches(pattern_graph=aag_pattern)

print(f"Found {len(matches)} pattern matches")

# Visualize results
prims = []
for match in matches:
    color = volmdlr.utils.common_operations.random_color()
    match.color = color
    prims.extend(match.volmdlr_primitives())

model.VolumeModel([volume_model, *prims]).babylonjs()

Visual Examples#

Assembly to Search Within#

The gearbox assembly — download HAWT Gear Assembly.STEP:

gearbox.png

Found Patterns#

All identified hex screws highlighted:

gearbox-with-screw-patterns.png

gearbox-with-screw-patterns2.png


How It Works#

1. Pattern Preparation#

Before matching, the pattern graph is prepared:

from volmdlr_tools.features.pattern_matcher import prepare_graph

prepare_graph(pattern_graph, tolerance=1.0, n_bins=150, n_points=10000)

This calculates:

  • Face angles between adjacent faces

  • Node degrees in the graph

  • Face-related attributes (signatures, areas)

2. Graph Traversal#

The matcher iterates through all Shell nodes in the assembly:

for solid_node in graph_assembly.get_nodes(
    lambda node: graph_assembly[node]["class"] == "Shell"
):
    aag = graph_assembly.get_aag(solid_node)
    # ... perform matching

3. Subgraph Isomorphism#

The VF2 algorithm finds subgraph mappings where:

  • Node attributes match (degree, face signatures)

  • Edge attributes match (angles between faces)

4. Overlap Prevention#

Matches are filtered to avoid overlapping:

  • Visited nodes are tracked

  • Only non-overlapping matches are returned


Performance Considerations#

  • Large assemblies: Pattern matching complexity grows with assembly size

  • Pattern size: Larger patterns reduce false positives but increase matching time

  • Tolerance: Tighter tolerances reduce false positives but may miss valid matches

  • Face signatures: Pre-computing signatures improves matching accuracy


Use Cases#

Finding Fasteners#

# Load fastener patterns
bolt_pattern = AttributedAdjacencyGraph(bolt_shape)
nut_pattern = AttributedAdjacencyGraph(nut_shape)
washer_pattern = AttributedAdjacencyGraph(washer_shape)

# Find all fasteners
all_matches = matcher.find_matches_from_multiple_patterns(
    [bolt_pattern, nut_pattern, washer_pattern]
)

Quality Verification#

# Verify expected component count
matches = matcher.find_pattern_matches(critical_component_pattern)
expected_count = 8

if len(matches) != expected_count:
    print(f"Warning: Expected {expected_count} components, found {len(matches)}")

Assembly BOM Generation#

# Build component count for Bill of Materials
component_patterns = {
    "M6 Bolt": bolt_m6_pattern,
    "M6 Nut": nut_m6_pattern,
    "Bearing 6205": bearing_pattern,
}

bom = {}
for name, pattern in component_patterns.items():
    matches = matcher.find_pattern_matches(pattern)
    bom[name] = len(matches)

Copies of One Solid#

The pattern matcher looks for a reference shape you give it. To find which solids of an assembly are one solid placed several times, with no reference, use congruent_groups from volmdlr_tools.shapes.solid_groups. It compares the solids among themselves and returns the groups of two or more that are the same solid moved, or mirrored, each as a CongruentGroup:

  • members: the indices of the solids in the list you passed, the first being the one the others are copies of;

  • motions: for each member, the Motion that carries the first member onto it (the first carries it onto itself).

A solid alone in its kind is not returned. A Motion maps a point p to matrix @ p + translation. Its kind names it in the simplest terms that say it all: "identity", "translation", "rotation" about an axis, "reflection" across a plane, or "general" for anything else, such as a screw motion. mirrored tells a mirror image apart; rotation_axis() gives a point of the axis, its direction and the angle in degrees, and reflection_plane() a point and the normal of the mirror plane (each None when the motion is not of that kind). inverse() and then() combine motions.

A solid with symmetries of its own reaches its copy by several motions. The group is then described by as few kinds of motion as it takes, so copies that are all mirror images are described as mirrorings.

copy_series(motions) reads the copies of one original as a series, when there are at least three of them, all shifts or all turns about one axis, at equal steps with the first copy one step from the original. Pass the motions without the original’s own. It returns a TranslationSeries (the step vector) or a RotationSeries (a point and axis of the turn, and the step angle in degrees), with order, the copies in the order they follow one another; None otherwise.

from volmdlr import shapes
from volmdlr.model import VolumeModel

from volmdlr_tools.shapes.solid_groups import congruent_groups, copy_series


def solids_of(shape):
    """Return every solid of a shape, going down into its sub-assemblies."""
    if isinstance(shape, shapes.Solid):
        return [shape]
    return [solid for child in shape.primitives for solid in solids_of(child)]


volume_model = VolumeModel.from_step("data/step/Gearbox Assembly.STEP")
solids = solids_of(volume_model.primitives[0])
groups = congruent_groups(solids)

print(len(solids), "solids,", [len(group.members) for group in groups])
# 50 solids, [4, 7, 4, 2]
group = groups[2]
print(group.members, [motion.kind for motion in group.motions])
# (21, 22, 23, 24) ['identity', 'rotation', 'rotation', 'rotation']

point, axis, angle = group.motions[1].rotation_axis()
print(axis, round(angle, 6))
# Vector3D(0.0, 0.0, 1.0) 45.0

series = copy_series(group.motions[1:])
print(series.axis, round(series.step, 6), series.order)
# Vector3D(0.0, 0.0, 1.0) 45.0 (0, 1, 2)
The congruent groups of the gearbox assembly

The gearbox with its housing, its cover and the rings of one ball bearing hidden. Each congruent group has its own colour: the four compound gears in blue, the seven slide bearings in orange, and the two groups of bearing balls in purple and magenta. The solids in no group are grey.

See Also#