harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-4720harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-4720PSEUDOCODE TASK
Algorithm: bounding_sphere(points, method='welzl') Input: points (n x 3 array-like, n >= 1), method in {'welzl','ritter'} Output: center (3-vector), radius (non-negative scalar)
// Normalize input P ← convert_to_numpy_array(points) // shape (n,3) n ← number_of_rows(P)
IF n == 1 THEN RETURN (P[0], 0)
// Helper: distance between two 3D points FUNCTION dist(a, b): RETURN EuclideanNorm(a − b)
// Helper: sphere through boundary set R (|R| ≤ 4) FUNCTION ball_from(R) m ← length(R) IF m == 0 THEN RETURN (arbitrary_center (e.g., [0,0,0]), -1) ELSE IF m == 1 THEN RETURN (R[0], 0) ELSE IF m == 2 THEN c ← (R[0] + R[1]) / 2 r ← dist(R[0], R[1]) / 2 RETURN (c, r) ELSE IF m == 3 THEN c ← circumcenter_3d(R[0], R[1], R[2]) IF c exists THEN r ← dist(c, R[0]) RETURN (c, r) ELSE // Collinear: use diameter of farthest pair (a,b) ← pair_of_farthest_points(R[0], R[1], R[2]) RETURN ball_from([a, b]) ELSE IF m == 4 THEN s ← sphere_through_4_points(R[0], R[1], R[2], R[3]) IF s is valid THEN RETURN s ELSE // Degenerate: try all smaller subsets best ← (None, +∞) FOR each subset S in combinations(R, 3) DO t ← ball_from(S) IF all_points_in_ball(R, t) AND t.radius < best.radius THEN best ← t IF best.center is not None THEN RETURN best FOR each pair (p, q) in combinations(R, 2) DO t ← ball_from([p, q]) IF all_points_in_ball(R, t) AND t.radius < best.radius THEN best ← t RETURN best ENDIF ENDIF END FUNCTION
// Helper: sphere from four points by solving linear system FUNCTION sphere_through_4_points(p1, p2, p3, p4) A ← [2*(p2 − p1); 2*(p3 − p1); 2*(p4 − p1)] // 3x3 rows b ← [|p2|^2 − |p1|^2, |p3|^2 − |p1|^2, |p4|^2 − |p1|^2] IF determinant(A) near 0 THEN RETURN invalid C ← solve_linear_system(A, b) // center r ← dist(C, p1) RETURN (C, r) IF all dist(C, pi) ≤ r + tol for i=1..4 END FUNCTION
// Helper: circumcenter of triangle in 3D FUNCTION circumcenter_3d(p1, p2, p3) n ← cross(p2 − p1, p3 − p1) IF norm(n) near 0 THEN RETURN null // collinear A ← [2*(p2 − p1); 2*(p3 − p1); n] // 3x3 b ← [|p2|^2 − |p1|^2, |p3|^2 − |p1|^2, dot(n, p1)] C ← solve_linear_system(A, b) RETURN C END FUNCTION
// Helper: compute pair of farthest points from a set (for Ritter) FUNCTION pair_of_farthest_points(a, b, c) // returns the two farthest among (a,b,c) IF dist(a,b) ≥ dist(a,c) AND dist(a,b) ≥ dist(b,c) THEN RETURN (a,b) ELSE IF dist(a,c) ≥ dist(a,b) AND dist(a,c) ≥ dist(b,c) THEN RETURN (a,c) ELSE RETURN (b,c) END FUNCTION
// Helper: check if point p lies inside or on sphere (center, radius) FUNCTION point_in_sphere(p, sphere) (c, r) ← sphere IF r < 0 THEN RETURN FALSE RETURN dist(p, c) ≤ r + tol END FUNCTION
// Helper: all points of S lie inside or on sphere t FUNCTION all_points_in_ball(S, t) FOR each q in S DO IF NOT point_in_sphere(q, t) THEN RETURN FALSE RETURN TRUE END FUNCTION
// Welzl’s algorithm (exact MEC in 3D) IF method == 'welzl' THEN P ← random_permutation(P) // Recursive function FUNCTION welzl(n, R) IF n == 0 OR length(R) == 4 THEN RETURN ball_from(R) p ← P[n−1] D ← welzl(n−1, R) IF point_in_sphere(p, D) THEN RETURN D ELSE R′ ← R ∪ {p} RETURN welzl(n−1, R′) ENDIF END FUNCTION (C, R_final) ← welzl(n, []) RETURN (C, R_final) END IF
// Ritter’s algorithm (approximate, fast bounding sphere) IF method == 'ritter' THEN // Step 1: find farthest pair a, b ← pair_of_farthest_points_among(P) C ← (a + b) / 2 R ← dist(a, b) / 2 // Step 2: refine with outliers FOR each p in P DO d ← dist(p, C) IF d > R + tol THEN C ← (C + p) / 2 R ← dist(p, C) ENDIF RETURN (C, R) END IF
RAISE error: unsupported method
END FUNCTION
Notes