import numpy as np import vtk import matplotlib.pyplot as plt import matplotlib…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import numpy as np
import vtk
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
def create_vtk_polygon(polygon):
"""Create a real vtkPolygon from a 2D list of (x, y) coordinates."""
vtk_points = vtk.vtkPoints()
for x, y in polygon:
vtk_points.InsertNextPoint(x, y, 0)
vtk_polygon = vtk.vtkPolygon()
vtk_polygon.GetPoints().DeepCopy(vtk_points)
vtk_polygon.GetPointIds().SetNumberOfIds(len(polygon))
for i in range(len(polygon)):
vtk_polygon.GetPointIds().SetId(i, i)
return vtk_polygon
def robust_vtk_intersections(polygon, y_line):
vtk_poly = create_vtk_polygon(polygon)
n = len(polygon)
intersections = []
tol = 1e-8
for i in range(n):
x1, y1, _ = vtk_poly.GetPoints().GetPoint(i)
x2, y2, _ = vtk_poly.GetPoints().GetPoint((i+1)%n)
p0 = (x1 - 1e4, y_line, 0.0)
p1 = (x2 + 1e4, y_line, 0.0)
edge_p0 = (x1, y1, 0.0)
edge_p1 = (x2, y2, 0.0)
line = vtk.vtkLine()
t = [0.0]
x = [0.0, 0.0, 0.0]
pcoords = [0.0, 0.0, 0.0]
subId = [0]
result = line.IntersectWithLine(edge_p0, edge_p1, tol, t, x, pcoords, subId)
if result == 1:
if 0 <= t[0] <= 1:
intersections.append(x[0])
if y1 == y_line:
y_prev = polygon[i-1][1]
y_next = y2
if (y_prev - y_line) * (y_next - y_line) < 0:
intersections.append(x1)
return sorted(set(intersections))
def all_intersections(polygon, y_line):
"""Naive version: includes edge/vertex tangents (not for real infill!)"""
xs = []
n = len(polygon)
for i in range(n):
p1 = polygon[i]
p2 = polygon[(i+1)%n]
y1, y2 = p1[1], p2[1]
x1, x2 = p1[0], p2[0]
if (y1 - y_line) * (y2 - y_line) <= 0:
if y1 == y2: continue # ignore horizontal edge on line
x = x1 + (x2 - x1) * (y_line - y1) / (y2 - y1)
xs.append(x)
return sorted(xs)
def plot_vtk_vs_naive(polygons, y_line):
fig, axes = plt.subplots(1, 2, figsize=(18, 7))
for ax, method, title in zip(
axes,
[all_intersections, robust_vtk_intersections],
["Naive (all_intersections)", "Robust (Real vtkPolygon & vtkLine)"]
):
colors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6']
for idx, poly in enumerate(polygons):
poly_np = np.array(poly)
ax.plot(*np.append(poly_np, [poly_np[0]], axis=0).T, '-', color=colors[idx], lw=2)
xs = method(poly, y_line)
ys = [y_line]*len(xs)
ax.plot(xs, ys, 'ro', markersize=8)
ax.axhline(y_line, color='k', ls='--')
ax.set_title(title)
ax.set_aspect('equal')
ax.grid(True)
plt.show()
polygons = [
[(2,1), (4,1), (4,4), (2,4)],
[(6,1), (9,1), (7.5,3), (9,5), (6,5)],
[(11,1), (13,1), (13,4), (12.5,5), (11,4)],
[(2,7), (3,8), (4,8), (11,8), (10,2), (10,6), (5,6), (5,4)],
[(15,4), (15,8), (25,8), (25,4), (23,7), (21,4), (19,7)],
[(17,1), (18,5), (19,2), (23,4), (23,1)],
[(1,0), (1,9), (26,9), (26,0)],
]
y_line = 4
plot_vtk_vs_naive(polygons, y_line)
result = line.IntersectWithLine(edge_p0, edge_p1, tol, t, x, pcoords, subId)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: IntersectWithLine argument 4: must be real number, not list
The error occurs because the IntersectWithLine method in VTK expects a single float value for the t parameter, but you're passing a list [0.0]. The same issue applies to the x, pcoords, and subId parameters.
Here's the corrected version of the robust_vtk_intersections function:
def robust_vtk_intersections(polygon, y_line):
vtk_poly = create_vtk_polygon(polygon)
n = len(polygon)
intersections = []
tol = 1e-8
for i in range(n):
x1, y1, _ = vtk_poly.GetPoints().GetPoint(i)
x2, y2, _ = vtk_poly.GetPoints().GetPoint((i+1)%n)
p0 = (x1 - 1e4, y_line, 0.0)
p1 = (x2 + 1e4, y_line, 0.0)
edge_p0 = (x1, y1, 0.0)
edge_p1 = (x2, y2, 0.0)
line = vtk.vtkLine()
# Initialize single float values instead of lists
t = 0.0
x = [0.0, 0.0, 0.0] # This can remain a list as it's used to return coordinates
pcoords = 0.0
subId = 0
result = line.IntersectWithLine(edge_p0, edge_p1, tol, t, x, pcoords, subId)
if result == 1:
# t is the parametric distance on the edge
if 0 <= t <= 1:
intersections.append(x[0])
# Handle cases where a vertex is exactly on the line
for i in range(n):
x1, y1 = polygon[i]
if y1 == y_line:
y_prev = polygon[i-1][1]
y_next = polygon[(i+1)%n][1]
if (y_prev - y_line) * (y_next - y_line) < 0:
intersections.append(x1)
return sorted(set(intersections))
Key changes made:
t = [0.0] to t = 0.0pcoords = [0.0, 0.0, 0.0] to pcoords = 0.0subId = [0] to subId = 0x as a list since it's used to return the intersection coordinatesThe function should now work correctly with VTK's IntersectWithLine method. The method will populate the x array with the intersection coordinates if there is an intersection, and return 1 if there was an intersection.
The rest of your code should work as expected with this corrected function. The plotting comparison between the naive and robust methods should now execute without the TypeError.