import open3d as o3d import numpy as np import matplotlib.pyplot as plt from Scripts import edges import logging from numpy import isfinite logger = logging.getLogger(__name__) def get_40(points): x_min = np.min(points[:, 0]) # Minimum value on X axis x_max = np.max(points[:, 0]) # Maximum value on X axis x_width = x_max - x_min # Width (difference) return x_width def save_filtered_point_cloud(points_array, file_path): if points_array.size > 0: pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points_array) o3d.io.write_point_cloud(file_path, pcd) else: print(f"'{file_path}' contains empty data, not saved.") def arm_horn_lengths(points, b_vertical=None): if b_vertical is None: raise ValueError("b_vertical value cannot be None.") # Division based on X center x_center = (np.max(points[:, 0]) + np.min(points[:, 0])) / 2 delta_x = 1 # Filtered points filtered_points = points[ (points[:, 0] > x_center) & (points[:, 0] < x_center + delta_x) ] # Empty data check if filtered_points.size == 0: logger.error("Warning: Filtered point cloud is empty!") return 0.0 # Visualization plt.figure(figsize=(12, 8)) # Original point cloud (gray, small) plt.scatter( points[:, 0], points[:, 1], c="lightgray", s=1, alpha=0.5, label="Original" ) # Filtered point clouds (larger and colorful) if filtered_points.size > 0: plt.scatter( filtered_points[:, 0], filtered_points[:, 1], c="red", s=3, alpha=0.8, label="Filter 1", ) plt.xlabel("X") plt.ylabel("Y") plt.title("Point Cloud Filtering Results (XY View)") plt.legend() plt.grid(True, alpha=0.3) plt.axis("equal") # Equal scaling # Save as PNG # plt.savefig('filtered_point_clouds_xy.png', dpi=300, bbox_inches='tight') # plt.show() l_248 = b_vertical - np.min(filtered_points[:, 1]) return l_248 def horn_diff( points, y_offset_low=60, y_offset_high=100, z_threshold=8, margin_fraction=0.05 ): """ Analyzes the left and right regions of the point cloud in the X-Y plane, determines edge points and calculates new left and right values using the average of points close to the edge line, then visualizes the differences. Parameters: - points: numpy.ndarray, array containing 3D points (Nx3). - y_offset_low: float, offset for lower limit on Y axis. - y_offset_high: float, offset for upper limit on Y axis. - z_threshold: float, threshold for selecting upper points on Z axis. - margin_fraction: float, fraction of the group's x range to use in edge point selection. Returns: - x_difference: float, difference between new left and right. - difference: float, difference between median and calculated center. """ import numpy as np import matplotlib.pyplot as plt # Median x value median = np.median(points[:, 0]) y_min = np.min(points[:, 1]) + y_offset_low y_max_val = np.min(points[:, 1]) + y_offset_high # Filter left and right regions left = points[ (points[:, 0] < median) & (points[:, 1] > y_min) & (points[:, 1] < y_max_val) ] right = points[ (points[:, 0] > median) & (points[:, 1] > y_min) & (points[:, 1] < y_max_val) ] # Z axis filter: If sufficient data exists left = ( left[left[:, 2] > (np.max(left[:, 2]) - z_threshold)] if left.size > 0 else np.empty((0, 3)) ) right = ( right[right[:, 2] > (np.max(right[:, 2]) - z_threshold)] if right.size > 0 else np.empty((0, 3)) ) # Edge operations: Call function from edges module (assumption: edges module exists) if left.size > 0: processed_left = edges.process_and_visualize(left[:, [0, 1]]) if isinstance(processed_left, tuple): processed_left = processed_left[0] processed_left = np.array(processed_left) else: processed_left = np.empty((0, 2)) if right.size > 0: processed_right = edges.process_and_visualize(right[:, [0, 1]]) if isinstance(processed_right, tuple): processed_right = processed_right[0] processed_right = np.array(processed_right) else: processed_right = np.empty((0, 2)) # Get scale information scale = edges.get_scale() # Convert processed data to two-dimensional array. # Note: column order; first column x value, second column y value. if processed_left.size > 0: left_arr = np.column_stack( (processed_left[:, 1] / scale, processed_left[:, 0] / scale) ) else: left_arr = np.empty((0, 2)) if processed_right.size > 0: right_arr = np.column_stack( (processed_right[:, 1] / scale, processed_right[:, 0] / scale) ) else: right_arr = np.empty((0, 2)) # Average of points close to the highest x value (edge) in left_arr if left_arr.size > 0: max_left_val = np.max(left_arr[:, 0]) left_range = max_left_val - np.min(left_arr[:, 0]) left_margin = left_range * margin_fraction left_edge_points = left_arr[left_arr[:, 0] >= max_left_val - left_margin] new_left = np.mean(left_edge_points[:, 0]) else: new_left = None # Average of points close to the lowest x value (edge) in right_arr if right_arr.size > 0: min_right_val = np.min(right_arr[:, 0]) right_range = np.max(right_arr[:, 0]) - min_right_val right_margin = right_range * margin_fraction right_edge_points = right_arr[right_arr[:, 0] <= min_right_val + right_margin] new_right = np.mean(right_edge_points[:, 0]) else: new_right = None # Calculations and visualization if new_left is not None and new_right is not None: center = (new_left + new_right) / 2 difference = np.abs(median - center) # print("feature14", difference) x_difference = new_right - new_left plt.figure(figsize=(8, 8)) plt.scatter( points[:, 0], points[:, 1], s=1, color="blue", alpha=0.5, label="Object" ) plt.scatter(left_arr[:, 0], left_arr[:, 1], s=10, color="green", label="Left") plt.scatter(right_arr[:, 0], right_arr[:, 1], s=10, color="red", label="Right") # Lines showing new edge averages plt.axvline(new_left, color="green", linestyle="--", label="New Left") plt.axvline(new_right, color="red", linestyle="--", label="New Right") plt.title("Horn Differences in X-Y Plane") plt.xlabel("X") plt.ylabel("Y") plt.axis("equal") plt.legend() plt.grid(True) plt.show() return x_difference, difference else: # print("Insufficient points for left or right region.") return 0, 0 def filter_and_visualize_projection_with_ply(points): # Select the middle region on X axis median = np.median(points[:, 0]) # Filter between max Y value and max Y - 40, between max Z - 15 on Z axis y_max = np.max(points[:, 1]) z_max = np.max(points[:, 2]) # Filtering operation projected_points_2d = points[ (points[:, 0] < median + 1) & (points[:, 0] > median - 1) & (points[:, 1] < y_max) & (points[:, 1] > y_max - 50) & (points[:, 2] > z_max - 13) ] l_81_5 = np.min(projected_points_2d[:, 1]) - np.min(points[:, 1]) l_7_1 = np.max(projected_points_2d[:, 1]) - np.min(projected_points_2d[:, 1]) # Visualization plt.figure(figsize=(8, 8)) plt.scatter( points[:, 2], points[:, 1], color="red", s=1, label="All Points (Y-Z Proj.)" ) plt.scatter( projected_points_2d[:, 2], projected_points_2d[:, 1], color="blue", s=1, label="Filtered Region (Y-Z Proj.)", ) plt.title("Filtered Region and All Points in Y-Z Plane") plt.xlabel("Y") plt.ylabel("Z") plt.axis("equal") plt.legend() plt.grid(True) return l_81_5, l_7_1