Rectangle Fitting Rules
Rectangle Fitting Rules
Purpose
This note defines the rectangle-fitting rules used when HAPI converts surveyed coordinates into a mathematically regular enclosing rectangle.
Source Priority
- User-confirmed project correction
- Rectangle-fitting Algorithms.md
- HAPI User Manual.pdf
- Point Code and Style Code.md
Confirmed Manual Correction
The HAPI User Manual describes Shrink to Fit as selecting the smallest-area rectangle. This has been superseded by the user-confirmed project rule:
Shrink to Fit Mode uses Minimum Perimeter, not Minimum Area.
Foundation and implementation notes should therefore use:
Score = Width + Height
The candidate rectangle with the lowest score is selected.
Objective
Transform a list of surveyed 3D coordinates into exactly four mathematically regular corner points representing an enclosing rectangle.
List<Point3d> → 4 ordered rectangle corner points
Algorithm 1: Best-fit
Behavioural Summary
Best-fit solves the common three-point survey problem. It identifies the surveyed vertex closest to a true 90-degree corner and aligns the local coordinate system to that corner.
Execution Logic
- Project input 3D points onto the XY plane for angle analysis.
- Generate directional vectors between sequential points.
- Calculate internal angles using vector dot products.
- Score each angle by its deviation from 90 degrees.
- Select the anchor vertex with the smallest deviation.
- Build a local coordinate system aligned with the winning vector.
- Transform the original 3D points into local coordinates.
- Generate an axis-aligned bounding box in local space.
- Extract four local corners.
- Transform the corners back into world coordinates.
- Apply Universal Orientation Stabilisation.
Recommended Use
Best-fit is preferred for standard rectangular objects where the surveyed points are expected to reflect a true rectangular feature.
Algorithm 2: Shrink to Fit
Behavioural Summary
Shrink to Fit tests candidate orientations and selects the most compact box-like boundary by Minimum Perimeter.
This penalises long, thin candidate rectangles and is more suitable for noisy or irregular point groups.
Execution Logic
- Project input 3D points onto the XY plane.
- Generate candidate axes from sequential point segments.
- For each candidate axis, build a local coordinate system.
- Transform the point cloud into each candidate coordinate system.
- Calculate the axis-aligned bounding box for each candidate.
- Calculate the perimeter score:
Width = Max.X - Min.X
Height = Max.Y - Min.Y
Score = Width + Height
- Select the candidate with the lowest score.
- Recalculate the final AABB in the winning coordinate system.
- Extract four local corners.
- Transform the corners back to world coordinates.
- Apply Universal Orientation Stabilisation.
Recommended Use
Shrink to Fit is preferred for irregular point groupings, noisy datasets, or features that are not perfectly rectangular.
Universal Orientation Stabilisation
After either rectangle-fitting algorithm, the final corner sequence must be stabilised.
Rule
The starting position should be based on the left-most midpoint of the rectangle's short edge. This prevents downstream text, symbols, or blocks from appearing upside down or right-to-left.
Execution Logic
- Calculate edge lengths for the four rectangle edges.
- Detect whether the shape is effectively square:
IsSquare = (MaxLength - MinLength) < 0.001
- Calculate all edge midpoints.
- If square, keep all midpoints as candidates.
- If not square, keep only the midpoints of the two shortest edges.
- Sort candidate midpoints using:
SortScore = (XCoordinate * 100000) + YCoordinate
- Select the lowest score.
- Reorder the rectangle points to begin on the edge associated with the winning midpoint, proceeding counter-clockwise.
Preferred Function Names
| Concept | Planned Function Name |
|---|---|
| Best-fit rectangle | CalculateBestFitRectangle |
| Shrink-to-fit rectangle | CalculateShrinkToFitRectangle |
| Perimeter scoring | CalculatePerimeterScore |
| Orientation stabilisation | ApplyLeftFirstOrientation |
| Rectangle corner extraction | ExtractRectangleCorners |
Related Notes
Open Questions
- Confirm whether Z values in the returned rectangle corners should preserve minimum Z, average Z, or source-point-specific Z according to the intended ECM downstream workflow.