@@ -75,11 +75,27 @@ constexpr double THE_CELL_EXPAND_RATIO = 0.1;
7575// ! Ratio of distance for scaling tolerance in cell search.
7676constexpr double THE_DISTANCE_SCALE_RATIO = 0.1 ;
7777
78+ // ==================================================================================================
79+ // ! @name Early Termination and Candidate Processing Constants
80+ // ! Constants controlling candidate filtering, early termination, and iteration limits.
81+ // ==================================================================================================
82+
7883// ! Threshold for skipping max candidates that are clearly worse.
84+ // ! For Max mode: skip if estDist < bestDist * threshold (i.e., 90% of best).
7985constexpr double THE_MAX_SKIP_THRESHOLD = 0.9 ;
8086
81- // ! Threshold for refined distance comparison (slightly less than 1.0).
82- constexpr double THE_REFINED_DIST_THRESHOLD = 0.99 ;
87+ // ! Threshold margin for Min mode early termination.
88+ // ! For Min mode: skip if estDist > bestDist * (1 + margin).
89+ // ! Value of 0.2 means skip candidates 20% worse than current best.
90+ constexpr double THE_MIN_SKIP_MARGIN = 0.2 ;
91+
92+ // ! Maximum number of candidates to process before stopping.
93+ // ! Limits Newton iterations per grid scan for performance.
94+ constexpr int THE_MAX_CANDIDATES_TO_PROCESS = 50 ;
95+
96+ // ! Threshold for refined distance comparison.
97+ // ! Grid fallback accepted if refinedDist < currentMin * threshold.
98+ constexpr double THE_REFINED_DIST_THRESHOLD = 1.0 ;
8399
84100// ! Relaxation factor for gradient-based zero detection.
85101// ! Multiplied with tolerance for more robust extremum detection.
@@ -89,24 +105,46 @@ constexpr double THE_GRADIENT_TOL_FACTOR = 100.0;
89105// ! Multiplier for Newton retry tolerance when initial Newton fails.
90106constexpr double THE_NEWTON_RETRY_TOL_FACTOR = 10.0 ;
91107
92- // ! High precision tolerance target (5e-8, better than Precision::Confusion ~1e-7).
93- // ! Used for multi-start Newton and two-phase refinement.
94- constexpr double THE_HIGH_PRECISION_TOL = 5.0e-8 ;
108+ // ==================================================================================================
109+ // ! @name Cache and Coherent Scanning Constants
110+ // ! Constants for spatial coherence optimization and trajectory prediction.
111+ // ==================================================================================================
95112
96113// ! Squared threshold for spatial coherence optimization.
97114// ! Query points within this squared distance use cached solution.
115+ // ! Value of 100.0 corresponds to distance of 10.0 units.
98116constexpr double THE_COHERENCE_THRESHOLD_SQ = 100.0 ;
99117
100118// ! Minimum ratio for trajectory prediction (step magnitude ratio).
119+ // ! Ratios below this indicate irregular step sizes.
101120constexpr double THE_TRAJECTORY_MIN_RATIO = 0.5 ;
102121
103122// ! Maximum ratio for trajectory prediction (step magnitude ratio).
123+ // ! Ratios above this indicate acceleration/deceleration too large.
104124constexpr double THE_TRAJECTORY_MAX_RATIO = 2.0 ;
105125
106126// ! Minimum cosine for trajectory prediction (direction alignment).
107127// ! Values above this indicate roughly same direction (< ~45 degrees).
108128constexpr double THE_TRAJECTORY_MIN_COS = 0.7 ;
109129
130+ // ! Minimum cosine for quadratic extrapolation (high coherence).
131+ // ! Values above this indicate very smooth trajectory (< ~25 degrees).
132+ constexpr double THE_TRAJECTORY_QUADRATIC_COS = 0.9 ;
133+
134+ // ! Cache size for coherent scanning (number of solutions stored).
135+ // ! Increased from 3 to 5 to enable quadratic extrapolation.
136+ constexpr int THE_CACHE_SIZE = 5 ;
137+
138+ // ! Sanity factor for cache result validation.
139+ // ! If cache-based Newton result has squared distance > (theTol^2 * factor),
140+ // ! it's rejected as likely converging to wrong local minimum.
141+ // ! Value of 1e6 allows reasonable tolerance range while catching gross errors.
142+ constexpr double THE_CACHE_SANITY_FACTOR = 1.0e6 ;
143+
144+ // ! Velocity adjustment limit for trajectory prediction.
145+ // ! Limits acceleration factor to prevent overshoot.
146+ constexpr double THE_VELOCITY_ADJUST_LIMIT = 1.5 ;
147+
110148// ! Maximum number of Newton iterations for grid optimization.
111149constexpr int THE_MAX_GOLDEN_ITERATIONS = 50 ;
112150
0 commit comments