forked from KrisYu/SharpEdge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_plot_viewer.py
More file actions
727 lines (591 loc) · 25.2 KB
/
Copy pathutility_plot_viewer.py
File metadata and controls
727 lines (591 loc) · 25.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def plot_sketch_data(V, P):
"""
Plot a list of 3D polylines.
Args:
V (ndarray): nx3 array of vertex coordinates where n is number of vertices
P (list): List of arrays containing vertex indices for each polyline
Each array in P contains indices that reference vertices in V
"""
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
V = np.asarray(V)
# ax.scatter(V[:, 0], V[:, 1], V[:, 2], s = 6, color = 'k')
# for i, (x, y, z) in enumerate(V):
# ax.text(x + 0.01, y + 0.01, z + 0.01, str(i), fontsize=8)
for index, polyline in enumerate(P):
points = [V[index] for index in polyline]
pts = np.asarray( points )
xs = pts[:, 0]
ys = pts[:, 1]
zs = pts[:, 2]
ax.plot(xs, ys, zs)
ax.scatter(xs, ys, zs, s = 5)
# Calculate midpoint for edge label
midx = np.mean(xs)
midy = np.mean(ys)
midz = np.mean(zs)
# ax.text(midx, midy, midz, str(index))
plt.axis('off')
plt.axis('equal')
plt.show()
def plot_edge_info(V, E):
'''
Given:
- V: nx3 array of vertex coordinates
- E: mx2 array of edge vertex indices (no duplicates)
'''
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
V = np.asarray(V)
for edge_index, edge in enumerate(E):
e0, e1 = edge
pts = np.asarray([V[e0], V[e1]])
xs = pts[:, 0]
ys = pts[:, 1]
zs = pts[:, 2]
# Calculate midpoint for edge label
midx = (xs[0] + xs[1]) / 2
midy = (ys[0] + ys[1]) / 2
midz = (zs[0] + zs[1]) / 2
ax.plot(xs, ys, zs)
ax.scatter(xs, ys, zs, s=5)
ax.text(midx, midy, midz, str(edge_index))
plt.axis('off')
plt.axis('equal')
plt.show()
def plot_polylines( polylines ):
'''
just purely plot polylines.
'''
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
for index, polyline in enumerate(polylines):
pts = np.asarray( polyline )
xs = pts[:, 0]
ys = pts[:, 1]
zs = pts[:, 2]
ax.plot(xs, ys, zs)
ax.scatter(xs, ys, zs, s = 5)
plt.axis('off')
plt.axis('equal')
plt.show()
def plot_convex_hull_with_normals(points, faces, normals, scale=0.08):
"""
Plots the convex hull and the normal vectors at each point.
Args:
- points: np.array of shape (n_points, 3), the 3D coordinates of the convex hull points.
- faces: np.array of shape (n_faces, 3), the indices of the points that form the triangular faces.
- normals: np.array of shape (n_points, 3), the normal vectors at each point.
- scale: float, scale factor for the length of the normal vectors.
"""
# Create a 3D plot
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot the triangular faces of the convex hull
poly3d = [[points[face] for face in triangle] for triangle in faces]
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='cyan', alpha=.1, edgecolors=(0, 0, 0, 0.1)))
# Plot the points of the convex hull
ax.scatter(points[:, 0], points[:, 1], points[:, 2], s=5, alpha = 0.1, color="blue")
# Plot the normal vectors at each point
for i in range(len(points)):
point = points[i]
normal = normals[i]
ax.quiver(point[0], point[1], point[2], normal[0], normal[1], normal[2],
length=scale, color='g')
plt.axis('off')
plt.tight_layout()
plt.show()
def plot_edge_frames(V, E, P, Us, Vs, scale=0.08):
"""
Plot polylines with different colors and their frame vectors.
Args:
V: (n,3) array of vertex coordinates
E: (m,2) array of edge vertex pairs
P: a list of arrays containing vertex indices for each polyline
Us, Vs: lists of frame vectors for each edge (guaranteed to exist)
scale: scaling factor for frame vectors (default: 0.03)
"""
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot vertices
ax.scatter(V[:, 0], V[:, 1], V[:, 2],
c='blue', s=3, alpha=0.6, marker='o', label='Vertices')
# Generate colors for each polyline
num_polylines = len(P)
colors = plt.cm.rainbow(np.linspace(0, 1, num_polylines))
# Map from edge index to polyline color
edge_to_color = {}
# First identify which edge belongs to which polyline
for poly_idx, polyline in enumerate(P):
# Extract edges from polyline
for i in range(len(polyline) - 1):
v1, v2 = polyline[i], polyline[i+1]
# Find this edge in the edge list
for e_idx, (e1, e2) in enumerate(E):
if (e1 == v1 and e2 == v2) or (e1 == v2 and e2 == v1):
edge_to_color[e_idx] = colors[poly_idx]
# Plot edges and frames with polyline colors
for e_idx, (e, u, v) in enumerate(zip(E, Us, Vs)):
start = V[e[0]]
end = V[e[1]]
# Get color for this edge
color = edge_to_color.get(e_idx, 'gray') # Default to gray if not in a polyline
# Plot edge
ax.plot([start[0], end[0]],
[start[1], end[1]],
[start[2], end[2]],
color=color, linewidth=2)
# Plot frame vectors at edge midpoint
mid = (start + end) / 2
ax.quiver(mid[0], mid[1], mid[2],
u[0], u[1], u[2],
color='blue', length=scale, normalize=True)
ax.quiver(mid[0], mid[1], mid[2],
v[0], v[1], v[2],
color='green', length=scale, normalize=True)
plt.axis('off')
plt.axis('equal')
ax.set_title('U V Frame')
plt.show()
def plot_normal_data(V, E, N, scale=0.08):
"""
Plot edges and their normal vectors:
- Black edges with red normal vectors
Args:
V: (n,3) array of vertex coordinates
E: (m,2) array of edge vertex pairs
N: (m,3) array of normal vectors for edges
scale: scaling factor for normal vectors (default: 0.03)
"""
# Create figure
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot vertices
ax.scatter(V[:, 0], V[:, 1], V[:, 2],
c='blue', # Color of points
s=3, # Size of points
alpha=0.6, # Transparency
marker='o', # Point style
label='Vertices') # Label for legend
# Plot edges and normals
for i, e in enumerate(E):
start = V[e[0]]
end = V[e[1]]
# Plot edge
ax.plot([start[0], end[0]],
[start[1], end[1]],
[start[2], end[2]],
color='black', linewidth=2)
# Calculate midpoint and plot normal vector
mid = (start + end) / 2
ax.quiver(mid[0], mid[1], mid[2],
N[i,0], N[i,1], N[i,2],
color='green', length=scale, normalize=True,
arrow_length_ratio=0.2)
# ax.text(mid[0], mid[1], mid[2], i)
# Make axes equal and set labels
plt.axis('off')
plt.axis('equal')
plt.show()
def plot_polyline_best_constraints(V, E, P, polyline_normal, scale=0.08, str=None, filename=None):
"""
Args:
V: (n,3) array of vertex coordinates
E: (m,2) array of edge vertex pairs
P: list of lists, where each inner list contains vertex indices for a polyline with its color
polyline_normal: dictionary, key - polyline_index, value : (edge_pos_in_polyline, best_normal_vector)
scale: scaling factor for normal vectors (default: 0.03)
str: optional string label for the plot (default: None)
filename: if provided, save plot to this filename (default: None)
"""
# Create figure
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot polylines
for polyline_idx, polyline in enumerate(P):
polyline_points = np.array([V[index] for index in polyline])
ax.plot(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2])
# Plot normal vectors for constrained edges
for polyline_idx, (edge_pos, normal) in polyline_normal.items():
polyline = P[polyline_idx]
# Get the edge vertices
start = V[polyline[edge_pos]]
end = V[polyline[edge_pos + 1]]
# Calculate midpoint of edge
mid = (start + end) / 2
# Plot normal vector
ax.quiver(mid[0], mid[1], mid[2],
normal[0], normal[1], normal[2],
color='green', length=scale, normalize=True,
arrow_length_ratio=0.2)
# Make axes equal and set labels
plt.axis('off')
plt.axis('equal')
# Add title if str is provided
if str is not None:
ax.set_title(str)
# Save to file if filename is provided
if filename:
plt.savefig(filename,
dpi=300, # High resolution
bbox_inches='tight',# Trim white space
pad_inches=0.1) # Small padding
plt.close() # Close the figure to free memory
else:
plt.show()
def plot_polyline_normals(V, E, P, polyline_normals, scale=0.08, str=None, filename=None):
"""
Args:
V: (n,3) array of vertex coordinates
E: (m,2) array of edge vertex pairs
P: list of lists, where each inner list contains vertex indices for a polyline with its color
polyline_normals: dictionary, key - polyline_index, value: a list of normals corresponding to each segment of polyline
scale: scaling factor for normal vectors (default: 0.03)
str: optional string label for the plot (default: None)
filename: if provided, save plot to this filename (default: None)
"""
# Create figure
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot polylines
for polyline_idx, polyline in enumerate(P):
polyline_points = np.array([V[index] for index in polyline])
ax.plot(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2])
# Plot normal vectors for each segment of the polyline
if polyline_idx in polyline_normals:
normals = polyline_normals[polyline_idx]
# For each segment in the polyline
for i in range(len(polyline) - 1):
# Get segment endpoints
start = V[polyline[i]]
end = V[polyline[i + 1]]
# Calculate midpoint of segment
mid = (start + end) / 2
# Get corresponding normal vector
normal = normals[i]
# Plot normal vector
ax.quiver(mid[0], mid[1], mid[2],
normal[0], normal[1], normal[2],
color='green', length=scale, normalize=True,
arrow_length_ratio=0.2)
# Make axes equal and set labels
plt.axis('off')
plt.axis('equal')
# Add title if str is provided
if str is not None:
ax.set_title(str)
# Save to file if filename is provided
if filename:
plt.savefig(filename,
dpi=300, # High resolution
bbox_inches='tight',# Trim white space
pad_inches=0.1) # Small padding
plt.close() # Close the figure to free memory
else:
plt.show()
def plot_edge_constraints(V, E, P, constraints, unconstrained_polylines_indices=None, scale=0.08, str=None, filename=None, block=True):
"""
Plot 3D visualization of polylines with edge normal constraints.
Args:
V: (n,3) array of vertex coordinates
E: (m,2) array of edge vertex pairs
P: list of lists, where each inner list contains vertex indices for a polyline
constraints: Either:
1. [(index, normal)] list of tuples of index and normal constraint, or
2. {index: normal} dictionary mapping edge indices to normal vectors
unconstrained_polylines_indices : a set/list of polyline indices
scale: scaling factor for normal vectors (default: 0.03)
str: optional title string for the plot (default: None)
filename: if provided, save plot to this filename (default: None)
Plot 3D visualization of polylines with edge normal constraints.
Added 'block' parameter to control whether plot blocks execution.
"""
# Store the current interactive state
was_interactive = plt.isinteractive()
# Set interactive mode according to blocking preference
if block:
plt.ioff() # Turn off interactive mode for blocking display
else:
plt.ion() # Turn on interactive mode for non-blocking display
# Create figure or reuse existing one
if hasattr(plot_edge_constraints, 'fig') and plt.fignum_exists(plot_edge_constraints.fig.number):
# Clear existing figure
plt.figure(plot_edge_constraints.fig.number)
plt.clf()
fig = plot_edge_constraints.fig
ax = fig.add_subplot(111, projection='3d')
else:
# Create new figure
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
plot_edge_constraints.fig = fig # Store figure for reuse
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Rest of your plotting code remains the same...
# [Your existing plotting code here]
# Plot polylines
if P is not None:
for index, polyline in enumerate(P):
polyline_points = np.array([V[idx] for idx in polyline])
# Default style for constrained polylines
style = {}
scatter_style = {'s': 5}
# Check if we need to use the unconstrained style
if unconstrained_polylines_indices is not None and index in unconstrained_polylines_indices:
style = {'linestyle': '--', 'color': 'r'}
scatter_style['color'] = 'r'
# Plot the polyline and points
ax.plot(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2], **style)
ax.scatter(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2], **scatter_style)
elif P is None:
V = np.asarray(V)
for edge_index, edge in enumerate(E):
e0, e1 = edge
pts = np.asarray([V[e0], V[e1]])
xs = pts[:, 0]
ys = pts[:, 1]
zs = pts[:, 2]
ax.plot(xs, ys, zs)
ax.scatter(xs, ys, zs, s=5)
# Convert constraints to list of (edge_idx, normal) pairs if it's a dictionary
if isinstance(constraints, dict):
constraint_pairs = list(constraints.items())
else:
constraint_pairs = constraints
# Plot normal vectors for constrained edges
for edge_idx, normal in constraint_pairs:
e = E[edge_idx]
e0, e1 = e
start = V[e0]
end = V[e1]
# Calculate midpoint of edge
mid = (start + end) / 2
color = 'green'
if unconstrained_polylines_indices is not None:
for index in unconstrained_polylines_indices:
polyline_vertex_indices = P[index]
if e0 in polyline_vertex_indices and e1 in polyline_vertex_indices:
color = 'red'
break
# Plot normal vector
ax.quiver(mid[0], mid[1], mid[2],
normal[0], normal[1], normal[2],
color=color , length=scale, normalize=True,
arrow_length_ratio=0.2)
# ax.text(mid[0], mid[1], mid[2], edge_idx)
# Make axes equal and set labels
plt.axis('off')
plt.axis('equal')
# Add title if str is provided
if str is not None:
ax.set_title(str)
# Save to file if filename is provided
if filename:
plt.savefig(filename, dpi=300, bbox_inches='tight', pad_inches=0.1)
plt.close()
else:
# Draw the plot
fig.canvas.draw()
if block:
# Use the correct blocking behavior
plt.show() # Default is blocking when interactive mode is off
else:
# For non-blocking, explicitly set block=False and add a pause
plt.show(block=False)
plt.pause(0.001) # Small pause to ensure the plot displays
# Restore previous interactive state
if was_interactive:
plt.ion()
else:
plt.ioff()
return fig, ax
def plot_edge_constraints_two_normals(V, E, P, constraints, unconstrained_polylines_indices=None, scale=0.08, str=None, filename=None, block=False):
"""
Plot 3D visualization of polylines with edge normal constraints.
Handles two normals per edge using tuple keys (edge_idx, which_normal).
"""
# Setup figure (same as before)
plt.ion()
if hasattr(plot_edge_constraints_two_normals, 'fig') and plt.fignum_exists(plot_edge_constraints_two_normals.fig.number):
plt.figure(plot_edge_constraints_two_normals.fig.number)
plt.clf()
fig = plot_edge_constraints_two_normals.fig
ax = fig.add_subplot(111, projection='3d')
else:
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
plot_edge_constraints_two_normals.fig = fig
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Plot polylines (same as before)
for index, polyline in enumerate(P):
polyline_points = np.array([V[idx] for idx in polyline])
style = {}
scatter_style = {'s': 5}
if unconstrained_polylines_indices is not None and index in unconstrained_polylines_indices:
style = {'linestyle': '--', 'color': 'r'}
scatter_style['color'] = 'r'
ax.plot(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2], **style)
ax.scatter(polyline_points[:,0], polyline_points[:,1], polyline_points[:,2], **scatter_style)
# Convert constraints to list format if it's a dictionary
if isinstance(constraints, dict):
constraint_pairs = list(constraints.items())
else:
constraint_pairs = constraints
# Plot normal vectors
for constraint_item in constraint_pairs:
# Parse the edge info - could be different formats
if isinstance(constraint_item[0], tuple):
# Format: ((edge_idx, which_edge), normal)
edge_key, normal = constraint_item
edge_idx, which_edge = edge_key
else:
# Handle other possible formats if needed
edge_idx, normal = constraint_item
which_edge = 0
# Get the edge vertices
if edge_idx < len(E): # Make sure edge_idx is valid
e = E[edge_idx]
e0, e1 = e
start = V[e0]
end = V[e1]
# Calculate midpoint with offset
mid = (start + end) / 2
# Add a small offset to separate the two normal vectors visually
offset = 0.01 * (which_edge - 0.5)
edge_dir = end - start
edge_dir_norm = edge_dir / np.linalg.norm(edge_dir)
# Offset perpendicular to both edge and normal
offset_dir = np.cross(edge_dir_norm, normal)
if np.linalg.norm(offset_dir) > 1e-6: # Check if not zero
offset_dir = offset_dir / np.linalg.norm(offset_dir)
mid = mid + offset * offset_dir
# Choose color based on which edge (0 or 1)
color = 'green' if which_edge == 0 else 'red'
# Plot the normal vector
ax.quiver(mid[0], mid[1], mid[2],
normal[0], normal[1], normal[2],
color=color, length=scale, normalize=True,
arrow_length_ratio=0.2)
# Finalize plot (same as before)
plt.axis('off')
plt.axis('equal')
if str is not None:
ax.set_title(str)
if filename:
plt.savefig(filename, dpi=300, bbox_inches='tight', pad_inches=0.1)
# Only close if we're not going to show it
if not block:
plt.close()
# Always handle display logic (whether we saved or not)
fig.canvas.draw()
fig.canvas.flush_events()
if block:
plt.ioff() # Turn off interactive mode when blocking
plt.show(block=True)
else:
plt.ion() # Turn on interactive mode for non-blocking
plt.show(block=False)
plt.pause(0.1) # Increased pause time for better visibility
return fig, ax
def plot_constraints_around_vertex(vertex_idx, V, E, P, plotting_normals, unconstrained_polylines_indices=None, scale=0.08, str_title=None, filename=None, block=False):
"""
Plot 3D visualization of normals only around a specific vertex.
Shows the vertex, its incident edges, and any normals on those edges.
plotting_normals: dict with keys (edge_idx, which_normal) and values as normal vectors
"""
# Setup figure
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y', elev=30, azim=45)
ax.set_aspect('equal')
# Find all edges incident to the vertex
incident_edges = []
for ei, (v1, v2) in enumerate(E):
if v1 == vertex_idx or v2 == vertex_idx:
incident_edges.append(ei)
# Plot all edges in very light gray first
for ei, (v1, v2) in enumerate(E):
edge_pts = np.array([V[v1], V[v2]])
ax.plot(edge_pts[:, 0], edge_pts[:, 1], edge_pts[:, 2],
color='gray', alpha=0.3, linewidth=1)
# Plot all vertices in light gray
ax.scatter(V[:,0], V[:,1], V[:,2], color='lightgray', s=20, alpha=0.5)
# Highlight the central vertex
vertex_pos = V[vertex_idx]
ax.scatter(*vertex_pos, color='red', s=100, label=f'Central vertex {vertex_idx}')
# Highlight incident edges with colors
colors = plt.cm.tab10(np.linspace(0, 1, len(incident_edges)))
for i, ei in enumerate(incident_edges):
v1, v2 = E[ei]
other_idx = v2 if v1 == vertex_idx else v1
edge_pts = np.array([vertex_pos, V[other_idx]])
# Plot the incident edge with color
ax.plot(edge_pts[:, 0], edge_pts[:, 1], edge_pts[:, 2],
color=colors[i], linewidth=3)
# Highlight the connected vertex
ax.scatter(*V[other_idx], color=colors[i], s=60)
# Label the edge
# mid = (vertex_pos + V[other_idx]) / 2
# ax.text(*mid, f'{ei}', fontsize=10, color='black',
# bbox=dict(boxstyle='round,pad=0.2', facecolor='white', alpha=0.8))
# Plot normal vectors only for incident edges
normal_count = 0
for edge_key, normal in plotting_normals.items():
edge_idx, which_normal = edge_key
# Only plot if this edge is incident to our vertex
if edge_idx in incident_edges:
normal_count += 1
e = E[edge_idx]
e0, e1 = e
start = V[e0]
end = V[e1]
# Calculate midpoint with offset
mid = (start + end) / 2
# Add a small offset to separate the two normal vectors visually
offset = 0.01 * (which_normal - 0.5)
edge_dir = end - start
edge_dir_norm = edge_dir / np.linalg.norm(edge_dir)
# Offset perpendicular to both edge and normal
offset_dir = np.cross(edge_dir_norm, normal)
if np.linalg.norm(offset_dir) > 1e-6:
offset_dir = offset_dir / np.linalg.norm(offset_dir)
mid = mid + offset * offset_dir
# Choose color based on which normal (0 or 1)
color = 'green' if which_normal == 0 else 'red'
# Plot the normal vector
ax.quiver(mid[0], mid[1], mid[2],
normal[0], normal[1], normal[2],
color=color, length=scale, normalize=True,
arrow_length_ratio=0.2)
# Set title if provided
if str_title is not None:
ax.set_title(str_title)
plt.axis('off')
plt.axis('equal')
if filename:
plt.savefig(filename, dpi=300, bbox_inches='tight', pad_inches=0.1)
if not block:
plt.close()
plt.show()
return fig, ax