GrabBag/AppAlgo/holeDetection/include/HoleDetectionParams.h
2026-03-11 23:40:06 +08:00

187 lines
7.3 KiB
C

#ifndef HOLE_DETECTION_PARAMS_H
#define HOLE_DETECTION_PARAMS_H
#include <cmath>
#include "../include/VZNL_Types.h" // Use types from VZNL_Types.h
/**
* @brief Sorting mode for detected holes
*/
enum ESortMode {
keSortMode_None = 0, // No sorting
keSortMode_ByRadius = 1, // Sort by radius (largest first)
keSortMode_ByDepth = 2, // Sort by depth (deepest first)
keSortMode_ByQuality = 3 // Sort by quality score (highest first)
};
/**
* @brief Detection parameters for hole detection algorithm
*/
struct SHoleDetectionParams {
// Pit detection parameters
int neighborCount; // Adjacent points for line connection (default: 3)
float angleThresholdPos; // Positive angle threshold in degrees (default: 70.0)
float angleThresholdNeg; // Negative angle threshold in degrees (default: -70.0)
float minPitDepth; // Minimum pit depth in mm (default: 5.0)
// Radial scanning parameters
float angleStep; // Angular step for radial scan in degrees (default: 1.0)
float maxScanRadius; // Maximum scan radius in mm (default: 100.0)
// Clustering parameters (DBSCAN)
float clusterEps; // DBSCAN epsilon in mm (default: 10.0)
int clusterMinPoints; // DBSCAN min points (default: 5)
// Ellipse fitting parameters
float minRadius; // Minimum hole radius in mm (default: 5.0)
float maxRadius; // Maximum hole radius in mm (default: 50.0)
// Plane fitting parameters
int expansionSize1; // First ring expansion in mm (default: 10.0)
int expansionSize2; // Second ring expansion in mm (default: 20.0)
// Validation parameters
float validZThreshold; // Valid Z-value threshold (default: 1e-4)
// V-type detection parameters
int minVTransitionPoints; // Minimum valid transition points between V-shape endpoints (default: 3)
// Corner-based angle detection parameters (similar to cornerMethod)
float cornerScale; // Search distance for forward/backward points in mm (default: 5.0)
float cornerAngleThreshold; // Minimum corner angle change in degrees (default: 15.0)
float jumpCornerTh_1; // Small angle threshold for jump detection (default: 10.0)
float jumpCornerTh_2; // Large angle threshold for jump detection (default: 30.0)
float minEndingGap; // Y-direction distance threshold for jump pairing in mm (default: 5.0)
float minEndingGap_z; // Z-direction height threshold for jump validation in mm (default: 1.0)
// Constructor with defaults
SHoleDetectionParams()
: neighborCount(3)
, angleThresholdPos(10.0f)
, angleThresholdNeg(-10.0f)
, minPitDepth(1.0f)
, angleStep(1.0f)
, maxScanRadius(100.0f)
, clusterEps(5.0f)
, clusterMinPoints(5)
, minRadius(5.0f)
, maxRadius(50.0f)
, expansionSize1(5)
, expansionSize2(10)
, validZThreshold(1e-4f)
, minVTransitionPoints(1)
, cornerScale(2.0f)
, cornerAngleThreshold(45.0f)
, jumpCornerTh_1(10.0f)
, jumpCornerTh_2(30.0f)
, minEndingGap(5.0f)
, minEndingGap_z(1.0f)
{}
};
/**
* @brief Filter parameters for hole filtering
*/
struct SHoleFilterParams {
// Size range filtering
float minHoleRadius; // Minimum hole radius in mm (default: 5.0)
float maxHoleRadius; // Maximum hole radius in mm (default: 50.0)
// Quality threshold filtering
float maxEccentricity; // Maximum eccentricity (default: 0.5, standard e=sqrt(1-(b/a)^2))
// Shape filtering (pre-fitting)
float maxCornerRatio; // Maximum corner ratio for rectangularity (default: 0.15)
// Higher = more rectangular. Set to 1.0 to disable.
float minAngularCoverage; // Minimum angular coverage in degrees (default: 300.0)
// Used to filter non-closed boundaries. Set to 0 to disable.
float maxRadiusFitRatio; // Maximum radiusVariance/radius ratio (default: 0.3)
// Measures how well points fit the circle. Set to 1.0 to disable.
float minQualityScore; // Minimum overall quality score (default: 0.3)
// Weighted combination of shape metrics. Set to 0 to disable.
// Planarity filtering (pre-projection)
float maxPlaneResidual; // Maximum point-to-plane residual in mm (default: 10.0)
// Rejects non-planar clusters (e.g. cliffs, step edges).
float maxAngularGap; // Maximum angular gap in degrees (default: 90.0)
// Rejects L-shaped or non-closed boundaries.
float minInlierRatio; // Minimum inlier ratio for ellipse fit (default: 0.7)
// Fraction of points within tolerance of fitted ellipse.
// Constructor with defaults
SHoleFilterParams()
: minHoleRadius(1.0f)
, maxHoleRadius(10.0f)
, maxEccentricity(0.99995f)
, maxCornerRatio(0.15f)
, minAngularCoverage(10.f)
, maxRadiusFitRatio(0.3f)
, minQualityScore(0.3f)
, maxPlaneResidual(10.0f)
, maxAngularGap(90.0f)
, minInlierRatio(0.3f)
{}
};
/**
* @brief Single hole detection result
*
* Note: SVzNL3DPointF and SVzNL2DPointF are defined in VZNL_Types.h
*/
struct SHoleResult {
SVzNL3DPointF center; // Hole center point (x, y, z)
SVzNL3DPointF normal; // Plane normal vector (unit length)
float radius; // Hole radius in mm
float depth; // Pit depth in mm
float eccentricity; // Eccentricity (0 = perfect circle)
float radiusVariance; // Radius variance in mm
float angularSpan; // Angular coverage in degrees
float qualityScore; // Quality score (0-1, higher = better)
SHoleResult()
: center()
, normal()
, radius(0.0f)
, depth(0.0f)
, eccentricity(0.0f)
, radiusVariance(0.0f)
, angularSpan(0.0f)
, qualityScore(0.0f)
{}
};
/**
* @brief Multiple hole detection result
*
* @note Memory management: The holes array is NOT automatically freed.
* Caller must call FreeMultiHoleResult() or manually delete[] holes.
*/
struct SMultiHoleResult {
int holeCount; // Number of detected holes
SHoleResult* holes; // Array of hole results (caller must free)
int totalCandidates; // Total candidate holes before filtering
int filteredCount; // Number of holes filtered out
SMultiHoleResult()
: holeCount(0)
, holes(nullptr)
, totalCandidates(0)
, filteredCount(0)
{}
};
/**
* @brief Free memory allocated by DetectMultipleHoles
*
* @param [in,out] result Result structure to free
*/
inline void FreeMultiHoleResult(SMultiHoleResult* result) {
if (result != nullptr && result->holes != nullptr) {
delete[] result->holes;
result->holes = nullptr;
result->holeCount = 0;
}
}
#endif // HOLE_DETECTION_PARAMS_H