Skip to content

ShapeUpgrade_UnifySameDomain with UnifyFaces=True causes severe performance degradation #1477

Description

@ChaitanyaYeole02

Problem Summary

When using ShapeUpgrade_UnifySameDomain with UnifyFaces=True, I experience:
First call in a Python session takes 75-90 seconds (lazy initialization)

Environment

  • OpenCascade Version: 7.8.1 (via pythonocc-core)
  • Python Version: 3.12
  • OS: macOS (darwin 25.2.0)

Minimal Reproduction

Original Code

from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.ShapeUpgrade import ShapeUpgrade_UnifySameDomain

# Read STEP file
reader = STEPControl_Reader()
reader.ReadFile("file_with_multiple_threads.step")  # 465 faces
reader.TransferRoots()
shape = reader.OneShape()

# Unify faces (SLOW!)
unify = ShapeUpgrade_UnifySameDomain(
    shape,
    True,   # UnifyEdges
    True,   # UnifyFaces - THIS IS THE BOTTLENECK
    False   # ConcatBSplines
)
unify.Build()
unified_shape = unify.Shape()

Fast Alternative

# Just read the file, no unification
reader = STEPControl_Reader()
reader.ReadFile("file_with_multiple_threads.step")
reader.TransferRoots()
shape = reader.OneShape()

# Result: 465 faces (no reduction)
# Problem: Circular planes split into hemispheres, cylinders split

Current Workaround

I observed that a warm-up strategy to mitigate the cold start:

# Warm up with small file
def warmup_cad_reader(warmup_file: str = "small_file.stp"):
    """Process small file to initialize OpenCascade caches"""
    reader = STEPControl_Reader()
    reader.ReadFile(warmup_file)
    reader.TransferRoots()
    shape = reader.OneShape()
    
    unify = ShapeUpgrade_UnifySameDomain(shape, True, True, False)
    unify.Build()
    return True

Current Optimizations Applied

unify = ShapeUpgrade_UnifySameDomain(shape, True, True, False)
unify.SetLinearTolerance(5e-4)      # 0.5mm tolerance
unify.SetAngularTolerance(0.1)      # ~5.7 degrees
unify.SetSafeInputMode(False)       # Skip extra validation
unify.AllowInternalEdges(True)      # Handle complex topology
unify.Build()

Questions

  1. Cold Start Penalty:

    • Why does the first call to ShapeUpgrade_UnifySameDomain.Build() take 75-90 seconds?
    • Is there a way to pre-initialize these internal caches without processing a file?
  2. Performance Optimization:

    • Is there a way to make face unification faster while maintaining accuracy?
    • Can the O(n²) face comparison be optimized (spatial indexing, parallel processing)?
  3. Alternative Approaches:

    • Is there a different algorithm or class that can merge split faces more efficiently?

Would love to discuss and know more about this @tpaviot

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions