Show HN:多種形狀正規化演算法

Show HN:多種形狀正規化演算法

Hacker News·

Hacker News 的 Show HN 板塊介紹了一個名為 'shreg' 的 GitHub 儲存庫,其中包含多種用於清理和精煉線段和閉合輪廓等幾何數據的形狀正規化演算法的 Python 實現。

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

To see all available qualifiers, see our documentation.

Various shape regularization algorithms

Uh oh!

There was an error while loading. Please reload this page.

nickponline/shreg

Folders and files

Latest commit

History

Repository files navigation

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 up noisy or imprecise geometric data by aligning segments to common orientations and adjusting their positions to create cleaner, more regular shapes.

Features

Installation

Using pip

Using uv

From source

Quick Start

Segment Regularization

Regularize a set of line segments by aligning their angles and offsets:

Contour Regularization

Simplify a closed polygon by aligning edges to principal directions:

Snap Regularization

Close gaps between nearby endpoints to create watertight polygons:

Metric Regularization

Constrain segment dimensions - force equal lengths, quantize to grid units, or equalize spacing:

Examples

Segment Regularization

The algorithm optimizes segment orientations and positions to create cleaner line arrangements:

Image

Angle regularization aligns crossing lines to common orientations:

Image

Combined angle and offset regularization on a hexagon:

Image

This example from the CGAL documentation demonstrates sequential angle and offset regularization on 15 segments organized into three groups: outer boundary, top rhombus, and bottom rhombus.

Image

Contour Regularization

Simplify complex polygons while preserving their essential shape:

Image

Complex shapes are reduced to their essential vertices:

Image

Snap Regularization

Snap regularization connects nearby endpoints to create watertight geometry. This is essential for creating closed polygons suitable for 3D extrusion, mesh generation, or CAD operations.

The cluster method groups nearby endpoints and moves them to their centroid. This is the fastest approach and guarantees watertight results:

Image

Hard constraints use quadratic programming to find the optimal positions that exactly satisfy all snap constraints while minimizing total endpoint movement:

Image

Soft constraints add "spring" forces between endpoints that should connect. This is useful when data is noisy and you're not certain endpoints should be exactly coincident:

Image

T-junctions occur when an endpoint should snap onto another segment's interior (not its endpoints). Enable T-junction detection for proper connectivity:

Image

Snap regularization works on polygons of any complexity:

Image

Metric & Pattern Regularization

Metric regularization constrains the relative measurements of segments. This is useful for architectural drawings, CAD cleanup, and any domain where dimensions should follow regular patterns.

Forces segments with similar lengths to be exactly equal. Useful when objects (like windows or columns) should have identical dimensions:

Image

Snaps segment lengths to integer multiples of a base unit. Essential for architectural plans where walls must be multiples of a grid unit:

Image

Forces equal gaps between parallel lines. Perfect for regularizing staircases, window arrays, or any repeated elements:

Image

API Reference

See API.md for the complete API documentation.

Command Line Interface

Run the demo examples:

Or using Python module syntax:

Algorithm

Energy Minimization via Quadratic Programming

The regularization problem is formulated as an energy minimization problem. Given a set of segments, we seek small adjustments (rotations and translations) that minimize an energy function while respecting constraints on maximum deviations.

The energy function balances two objectives:

This leads to a quadratic program (QP) of the form:

where x contains the rotation and translation corrections for each segment, P encodes the fidelity cost, and the constraints enforce that angle/offset differences between nearby segments are minimized.

Segment Regularization Pipeline

Snap (Connectivity) Regularization

Snap regularization is formulated as a Constrained Quadratic Programming problem that minimizes endpoint movement while enforcing connectivity constraints.

Variables: For N segments, the state vector contains all 4N endpoint coordinates:

Objective (Fidelity): Minimize squared distance from original positions:

Methods:

Pipeline:

Metric & Pattern Regularization

Metric regularization constrains segment dimensions (length, distance). The challenge is that length calculation √(Δx² + Δy²) is non-linear, but QP solvers require linear constraints.

Linearization: We approximate length using the segment's unit direction vector d = (dₓ, dᵧ):

This is linear in the endpoint coordinates and can be directly inserted into the constraint matrix.

Constraint Formulations:

Iterative Refinement (SQP): Because the unit vectors are computed from the current geometry, results are approximate if segments rotate significantly. The algorithm uses Sequential Quadratic Programming:

Pipeline:

Contour Regularization

The contour regularization algorithm follows CGAL's approach for closed polygons:

Dependencies

Development

Install development dependencies:

Run tests:

Run tests with coverage:

References

About

Various shape regularization algorithms

Topics

Resources

Uh oh!

There was an error while loading. Please reload this page.

Stars

Watchers

Forks

Releases

Packages

  0

Languages

Footer

Footer navigation

Hacker News

相關文章

  1. 序列注意力機制:在不犧牲準確性的前提下,讓 AI 模型更精簡、更快速

    Google Research · 3 個月前

  2. Show HN:逆向工程 YouTube 的「最常重播」圖表

    3 個月前

  3. 關於形式方法與AI安全性的信念

    Lesswrong · 6 個月前

  4. NanoGPT Slowrun:透過無限算力實現 10 倍數據效率

    大約 1 個月前

  5. 使用 Sentence Transformers 訓練與微調多模態嵌入及重排序模型

    Huggingface · 10 天前

其他收藏 · 0