import numpy as np import open3d as o3d import matplotlib.pyplot as plt from scipy.optimize import leastsq from Scripts import edges # Helper functions for circle fitting def calc_radius(xc, yc, x, y): return np.sqrt((x - xc) ** 2 + (y - yc) ** 2) def cost_function(params, x, y): xc, yc, r = params return calc_radius(xc, yc, x, y) - r def initial_guess(x, y): xc = np.mean(x) yc = np.mean(y) r = np.mean(np.sqrt((x - xc) ** 2 + (y - yc) ** 2)) return [xc, yc, r] def slope(pcd, b_vertical=None, y_divisor=0.22, delta_y=0.5, crc_l=58.67): """ Performs sliding, filtering and circle fitting operations on point cloud. Parameters: - points: numpy.ndarray, input point cloud (Nx3 dimensions). - y_divisor: float, division factor for filtering on Y axis. - delta_y: float, filtering width for X axis. - crc_l: float, filtering length on Y axis. Returns: - yc, zc: float, center coordinates of the fitted circle. - r_outer: float, radius of the fitted circle. """ points = np.asarray(pcd).copy() l79_73 = 0 # Flip Y axis points[:, 1] = -points[:, 1] # Shift points by subtracting minimum values min_vals = np.min(points, axis=0) points -= min_vals # Calculate min and max values x_min, x_max = np.min(points[:, 0]), np.max(points[:, 0]) y_min, y_max = np.min(points[:, 1]), np.max(points[:, 1]) z_min, z_max = np.min(points[:, 2]), np.max(points[:, 2]) # Determine Y and Z axis filtering boundaries y_min_filter = y_min + (y_divisor * (y_max - y_min)) - 2 y_max_filter = y_min_filter + crc_l x_center = (x_min + x_max) / 2 z_min_filter = z_min + 0.05 * (z_max - z_min) z_max_filter = z_min_filter + 100 # Filtering operation filtered_points = points[ (points[:, 0] > x_center - delta_y) & (points[:, 0] < x_center + delta_y) & (points[:, 1] > y_min_filter) & (points[:, 1] < y_max_filter) ] # Edge processing (2D) projected_points_2d = edges.process_and_visualize(filtered_points[:, [1, 2]]) # projected_points_2d = filtered_points[:, [1, 2]] # 2D coordinates for circle fitting scale = edges.get_scale() y_2d, z_2d = projected_points_2d[:, 1] / scale, projected_points_2d[:, 0] / scale # Initial guess and fitting operation guess = initial_guess(y_2d, z_2d) result, _ = leastsq(cost_function, guess, args=(y_2d, z_2d)) yc, zc, r_outer = result # print(f"Center of selected circle: ({yc:.2f}, {zc:.2f}), Radius: {r_outer:.2f}") if b_vertical: b_vertical = -b_vertical b_vertical = b_vertical - min_vals[1] l79_73 = yc - b_vertical # print("l_79_73",l79_73) # Generate circle points theta = np.linspace(0, 2 * np.pi, 100) circle_y = yc + r_outer * np.cos(theta) circle_z = zc + r_outer * np.sin(theta) # Visualization plt.figure(figsize=(8, 8)) plt.scatter(points[:, 1], points[:, 2], color="green", label="Points") plt.scatter(y_2d, z_2d, color="blue", label="Points") plt.plot(circle_y, circle_z, color="red", label=f"Fitted Circle (r={r_outer:.2f})") plt.title("Point Cloud and Circle Fitting in Y-Z Plane") plt.xlabel("Y") plt.ylabel("Z") plt.axis("equal") plt.legend() plt.grid(True) # 3D visualization with Open3D # filtered_pcd = o3d.geometry.PointCloud() # filtered_pcd.points = o3d.utility.Vector3dVector(filtered_points) # filtered_pcd.paint_uniform_color([0, 0, 1]) # Blue color # filtered_pcd2 = o3d.geometry.PointCloud() # filtered_pcd2.points = o3d.utility.Vector3dVector(points) # filtered_pcd2.paint_uniform_color([1, 0, 0]) # Red color # o3d.io.write_point_cloud("filtered_points.ply", filtered_pcd) # o3d.io.write_point_cloud("filtered2.ply", filtered_pcd2) return yc, zc, r_outer, l79_73