|
2 | 2 | import compas_libigl as igl |
3 | 3 | from compas.colors import Color |
4 | 4 | from compas.datastructures import Mesh |
5 | | -from compas.geometry import Line |
6 | | -from compas.geometry import Point |
7 | | -from compas.geometry import Vector |
| 5 | +from compas.geometry import Line, Point, Vector, Scale, Rotation |
8 | 6 | from compas_viewer import Viewer |
9 | 7 |
|
10 | 8 | # ============================================================================== |
11 | 9 | # Input geometry |
12 | 10 | # ============================================================================== |
13 | 11 |
|
14 | 12 | mesh = Mesh.from_obj(compas.get("tubemesh.obj")) |
15 | | - |
16 | 13 | trimesh = mesh.copy() |
17 | 14 | trimesh.quads_to_triangles() |
18 | 15 |
|
19 | 16 | # ============================================================================== |
20 | | -# curvature |
| 17 | +# Curvature |
21 | 18 | # ============================================================================== |
22 | 19 |
|
23 | | -curvature = igl.trimesh_gaussian_curvature(trimesh.to_vertices_and_faces()) |
| 20 | +vertices, faces = trimesh.to_vertices_and_faces() |
| 21 | +gaussian_curvature = igl.trimesh_gaussian_curvature((vertices, faces)) |
| 22 | +PD1, PD2, PV1, PV2 = igl.trimesh_principal_curvature((vertices, faces)) |
| 23 | + |
| 24 | +# Normalize curvature values for better visualization |
| 25 | +max_gaussian = max(abs(k) for k in gaussian_curvature) |
| 26 | +max_principal = max(max(abs(k) for k in PV1), max(abs(k) for k in PV2)) |
| 27 | + |
| 28 | +# Scale factors for visualization |
| 29 | +gaussian_scale = 0.5 / max_gaussian if max_gaussian > 0 else 0.5 |
| 30 | +principal_scale = 0.3 / max_principal if max_principal > 0 else 0.3 |
24 | 31 |
|
25 | 32 | # ============================================================================== |
26 | | -# Visualisation |
| 33 | +# Visualization |
27 | 34 | # ============================================================================== |
28 | 35 |
|
29 | 36 | viewer = Viewer(width=1600, height=900) |
30 | | -# viewer.view.camera.position = [1, -6, 2] |
31 | | -# viewer.view.camera.look_at([1, 1, 1]) |
32 | 37 |
|
33 | | -viewer.scene.add(mesh, opacity=0.7, show_points=False) |
| 38 | +# Add base mesh |
| 39 | +viewer.scene.add( |
| 40 | + mesh, |
| 41 | + opacity=0.7, |
| 42 | + show_points=False |
| 43 | +) |
34 | 44 |
|
| 45 | +# Visualize curvature at each vertex |
35 | 46 | for vertex in mesh.vertices(): |
36 | 47 | if mesh.is_vertex_on_boundary(vertex): |
37 | 48 | continue |
38 | | - |
| 49 | + |
| 50 | + # Get vertex position and normal |
39 | 51 | point = Point(*mesh.vertex_coordinates(vertex)) |
40 | 52 | normal = Vector(*mesh.vertex_normal(vertex)) |
41 | | - c = curvature[vertex] |
42 | | - normal.scale(10 * c) |
43 | | - |
| 53 | + |
| 54 | + # Gaussian curvature visualization (vertical lines) |
| 55 | + k = gaussian_curvature[vertex] |
| 56 | + scaled_normal = normal.scaled(gaussian_scale * k) |
44 | 57 | viewer.scene.add( |
45 | | - Line(point, point + normal), |
46 | | - linecolor=(Color.red() if c > 0 else Color.blue()), |
47 | | - linewidth=2, |
| 58 | + Line(point, point + scaled_normal*100), |
| 59 | + linecolor=Color.blue(), |
| 60 | + linewidth=3 |
48 | 61 | ) |
| 62 | + |
| 63 | + # Principal curvature visualization (cross lines) |
| 64 | + pd1 = Vector(*PD1[vertex]).scaled(2*principal_scale * abs(PV1[vertex])) |
| 65 | + pd2 = Vector(*PD2[vertex]).scaled(2*principal_scale * abs(PV2[vertex])) |
| 66 | + |
| 67 | + # Create lines for principal directions |
| 68 | + viewer.scene.add( |
| 69 | + Line(point - pd1, point + pd1), |
| 70 | + linecolor=Color.black(), |
| 71 | + linewidth=4 |
| 72 | + ) |
| 73 | + viewer.scene.add( |
| 74 | + Line(point - pd2, point + pd2), |
| 75 | + linecolor=Color.black(), |
| 76 | + linewidth=2 |
| 77 | + ) |
| 78 | + |
49 | 79 |
|
50 | 80 | viewer.show() |
0 commit comments