- Improved sketching

This commit is contained in:
bklronin
2025-11-16 17:48:05 +01:00
parent 11d053fda4
commit d6044e551a
4 changed files with 1044 additions and 197 deletions

View File

@@ -282,21 +282,57 @@ class VTKWidget(QtWidgets.QWidget):
def render_from_points_direct_with_faces(self, vertices, faces, color=(0.1, 0.2, 0.8), line_width=2, point_size=5):
"""Sketch Widget has inverted Y axiis therefore we invert y via scale here until fix"""
# Handle empty vertices or faces
if len(vertices) == 0 or len(faces) == 0:
print("Warning: No vertices or faces to render")
return
points = vtk.vtkPoints()
# Use SetData with numpy array
vtk_array = numpy_to_vtk(vertices, deep=True)
points.SetData(vtk_array)
# Validate vertices shape
if vertices.ndim != 2 or vertices.shape[1] != 3:
print(f"Warning: Invalid vertex shape {vertices.shape}. Expected Nx3.")
return
# Validate faces shape
if faces.ndim != 2 or faces.shape[1] != 3:
print(f"Warning: Invalid face shape {faces.shape}. Expected Nx3.")
return
# Use SetData with numpy array - ensure vertices are float32
try:
vertices_float = np.asarray(vertices, dtype=np.float32)
vtk_array = numpy_to_vtk(vertices_float, deep=True)
points.SetData(vtk_array)
except Exception as e:
print(f"Error converting vertices to VTK array: {e}")
# Fallback: manually insert points
for vertex in vertices:
points.InsertNextPoint(vertex[0], vertex[1], vertex[2])
# Create a vtkCellArray to store the triangles
triangles = vtk.vtkCellArray()
for face in faces:
num_vertices = len(vertices)
for i, face in enumerate(faces):
# Validate face indices
if (face[0] >= num_vertices or face[0] < 0 or
face[1] >= num_vertices or face[1] < 0 or
face[2] >= num_vertices or face[2] < 0):
print(f"Warning: Invalid face indices {face} at index {i}. Skipping face.")
continue
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0, face[0])
triangle.GetPointIds().SetId(1, face[1])
triangle.GetPointIds().SetId(2, face[2])
triangle.GetPointIds().SetId(0, int(face[0]))
triangle.GetPointIds().SetId(1, int(face[1]))
triangle.GetPointIds().SetId(2, int(face[2]))
triangles.InsertNextCell(triangle)
# Check if we have any valid triangles
if triangles.GetNumberOfCells() == 0:
print("Warning: No valid triangles to render")
return
# Create a polydata object
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
@@ -309,7 +345,17 @@ class VTKWidget(QtWidgets.QWidget):
normalGenerator.ComputeCellNormalsOn()
normalGenerator.Update()
self.cell_normals = vtk_to_numpy(normalGenerator.GetOutput().GetCellData().GetNormals())
# Safely get cell normals, with fallback if they're not available
cell_normals = normalGenerator.GetOutput().GetCellData().GetNormals()
if cell_normals:
try:
self.cell_normals = vtk_to_numpy(cell_normals)
except Exception as e:
print(f"Warning: Could not convert cell normals to numpy array: {e}")
self.cell_normals = None
else:
print("Warning: No cell normals available")
self.cell_normals = None
# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()