File size: 8,196 Bytes
f8efe2e
 
 
 
38809fd
f8efe2e
 
 
 
38809fd
f8efe2e
38809fd
 
 
f8efe2e
 
38809fd
f8efe2e
 
 
 
 
 
38809fd
 
f8efe2e
 
 
38809fd
f8efe2e
38809fd
f8efe2e
 
 
38809fd
f8efe2e
38809fd
f8efe2e
38809fd
 
f8efe2e
38809fd
f8efe2e
 
38809fd
f8efe2e
38809fd
 
 
 
 
 
 
f8efe2e
38809fd
 
 
 
 
 
 
 
 
 
 
 
f8efe2e
 
38809fd
 
 
f8efe2e
 
 
 
 
 
38809fd
 
 
 
f8efe2e
38809fd
 
 
f8efe2e
 
38809fd
 
 
 
 
f8efe2e
 
38809fd
 
f8efe2e
 
 
 
38809fd
f8efe2e
 
 
 
38809fd
 
 
 
 
 
 
f8efe2e
38809fd
 
 
 
 
 
 
 
 
 
 
 
 
f8efe2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38809fd
f8efe2e
 
38809fd
 
f8efe2e
38809fd
 
 
f8efe2e
 
 
38809fd
 
 
f8efe2e
 
 
38809fd
f8efe2e
 
 
 
 
 
 
 
 
38809fd
f8efe2e
 
 
 
 
 
 
 
 
38809fd
f8efe2e
 
 
 
 
 
 
38809fd
 
 
 
 
 
 
 
 
f8efe2e
 
38809fd
f8efe2e
 
 
 
 
 
38809fd
f8efe2e
 
38809fd
f8efe2e
38809fd
f8efe2e
 
38809fd
f8efe2e
 
 
38809fd
f8efe2e
38809fd
 
 
 
 
f8efe2e
38809fd
 
 
 
f8efe2e
38809fd
 
 
 
 
 
 
 
 
 
 
f8efe2e
 
38809fd
f8efe2e
38809fd
f8efe2e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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