TL;DR

shreg is a pure-Python library that implements multiple shape-regularization algorithms for line segments and closed contours. It offers angle/offset alignment, snapping for watertight geometry, metric constraints, contour simplification, visualization, and a CLI with example demos.

What happened

A new Python package, shreg, provides implementations of several shape-regularization techniques intended to clean and regularize noisy geometric data. The library exposes functions for segment regularization (aligning angles and offsets via quadratic programming), snap regularization (connecting nearby endpoints to form watertight polygons), metric regularization (equal lengths, length quantization, equal spacing), and contour regularization (aligning polygon edges to principal directions). The package includes multiple snapping strategies (cluster, hard, soft) and can detect T-junctions so endpoints snap onto segment interiors. shreg bundles plotting utilities for before/after comparisons, a command-line interface for running demos, and example scripts showing common workflows. Installation options include pip, an 'uv pip' invocation, or editable install from the GitHub repository. Algorithmically, segment regularization is solved as a QP that balances fidelity to input geometry with regularity constraints, using neighbor detection and constraint graphs to build the problem.

Why it matters

  • Cleans noisy vector geometry so lines and polygons align to expected orientations and offsets, useful for CAD and architectural plans.
  • Creates watertight polygons and meshes by snapping nearby endpoints, which is important for downstream operations like extrusion and mesh generation.
  • Provides metric constraints (equal lengths, quantization, equal spacing) that help enforce design regularity in repetitive elements.
  • Offered as pure Python with plotting and a CLI, making it accessible for scripting and batch processing of geometric data.

Key facts

  • Library name: shreg; repository available via git clone https://github.com/nickp/shreg.git.
  • Installation: pip install shreg, uv pip install shreg, or pip install -e . from source.
  • Segment functions include solve_line_segments and helpers for creating segments.
  • Contour regularization available via regularize_contour with options such as axis-alignment and max offset.
  • Snap regularization supports methods: cluster (centroid grouping), hard (exact constraints), and soft (elastic/spring constraints); T-junction detection is optional.
  • Metric regularization routines support equal_length, length_quantization (base_unit), and equal_spacing with configurable tolerances.
  • A command-line interface runs demo examples with plotting; flags include –no-plot, –segments, and –contours.
  • The core segment regularization is formulated as a quadratic program (minimize a quadratic energy with linear constraints) built from neighbor detection (Delaunay triangulation) and solved with OSQP.
  • Built-in visualization utilities let users compare inputs and results.

What to watch next

  • Availability and handling of the QP solver OSQP as a runtime dependency (OSQP is used by the implementation and referenced in the algorithm description).
  • Scalability limits and performance characteristics on very large segment sets — not confirmed in the source.
  • Integration or export capabilities for CAD/BIM tools and common geometry formats (e.g., DXF/OBJ) — not confirmed in the source.

Quick glossary

  • Shape regularization: Techniques that adjust geometric primitives to reduce noise and enforce alignment, spacing, or metric patterns.
  • Quadratic programming (QP): An optimization problem where a quadratic objective function is minimized subject to linear constraints.
  • T-junction: A geometry configuration where an endpoint meets the interior of another segment rather than matching another endpoint.
  • Delaunay triangulation: A method of triangulating points such that no point lies inside the circumcircle of any triangle, often used for neighbor detection.
  • Quantization (length quantization): Snapping continuous measurements (like segment lengths) to multiples of a chosen base unit.

Reader FAQ

Is shreg pure Python or does it depend on compiled libraries?
The source describes shreg as pure Python with no dependencies required.

How do I run examples and demos?
A CLI is provided; running shreg executes demos and flags such as –no-plot, –segments, and –contours are supported.

Does shreg use a QP solver?
Yes — the segment-regularization algorithm is formulated as a quadratic program and the source references solving it with OSQP.

Is there built-in support for exporting to CAD formats or integrations with CAD/BIM tools?
not confirmed in the source

shreg – Shape Regularization A Python implementation of various shape regularization algorithms for regularizing line segments and closed contours. Shape regularization is a technique used in computational geometry to clean…

Sources

Related posts

By

Leave a Reply

Your email address will not be published. Required fields are marked *