From bf16ce747403404a1ecb3d7995700fa2f8e7480a Mon Sep 17 00:00:00 2001 From: Maxime Van Der Valk Date: Fri, 30 Jan 2026 09:09:10 +0100 Subject: [PATCH 1/2] new examples plus correction lighting effects plot --- examples/example_curvature.py | 20 +++++++--- examples/example_distortion.py | 62 +++++++++++++++++++++++++++--- examples/example_topology.py | 14 ++++--- examples/example_vertex_voronoi.py | 4 +- slam/plot.py | 29 +++++++------- 5 files changed, 96 insertions(+), 33 deletions(-) diff --git a/examples/example_curvature.py b/examples/example_curvature.py index 6a235af1..0b090e37 100644 --- a/examples/example_curvature.py +++ b/examples/example_curvature.py @@ -146,9 +146,11 @@ ) angular_error_1 = 180 * angular_error_1 / np.pi + ############################################################################# -# VISUALIZATION USING EXTERNAL TOOLS +# VISUALIZATION USING INTERNAL TOOLS ############################################################################# + import slam.plot as splt vertices = mesh.vertices @@ -161,10 +163,12 @@ [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)]]) vertices_translate = np.dot(rot_x, vertices_translate.T).T -rot_z = np.array([[np.cos(theta), -np.sin(theta),0], - [np.sin(theta), np.cos(theta),0], - [0, 0, 1],]) +rot_z = np.array([[np.cos(theta), -np.sin(theta), 0], + [np.sin(theta), np.cos(theta), 0], + [0, 0, 1], ]) vertices_translate = np.dot(rot_z, vertices_translate.T).T + +# Plot Mean Curvature display_settings = {} display_settings['colorbar_label'] = 'Mean Curvature' mesh_data = {} @@ -180,6 +184,7 @@ display_settings=display_settings) Fig.show() +# Plot Gaussian Curvature mesh_data['title'] = 'example_mesh.gii Gaussian Curvature' intensity_data['values'] = gaussian_curv display_settings['colorbar_label'] = 'Gaussian Curvature' @@ -189,6 +194,7 @@ display_settings=display_settings) Fig.show() +# Plot Shape Index mesh_data['title'] = 'example_mesh.gii Shape Index' intensity_data['values'] = shapeIndex display_settings['colorbar_label'] = 'Shape Index' @@ -198,6 +204,7 @@ display_settings=display_settings) Fig.show() +# Plot Curvedness mesh_data['title'] = 'example_mesh.gii Curvedness' intensity_data['values'] = curvedness display_settings['colorbar_label'] = 'Curvedness' @@ -207,6 +214,7 @@ display_settings=display_settings) Fig.show() +# Plot Quadric K Mean Absolute Change mesh_data['vertices'] = quadric.vertices mesh_data['faces'] = quadric.faces mesh_data['title'] = 'Quadric K Mean Absolute Change' @@ -218,6 +226,7 @@ display_settings=display_settings) Fig.show() +# Plot Quadric Angular Error 0 mesh_data['title'] = 'Quadric Angular Error 0' intensity_data['values'] = angular_error_0 display_settings['colorbar_label'] = 'Angular Error 0' @@ -227,7 +236,8 @@ display_settings=display_settings) Fig.show() -mesh_data['title'] = ('Quadric Angular Error 1') +# Plot Quadric Angular Error 1 +mesh_data['title'] = 'Quadric Angular Error 1' intensity_data['values'] = angular_error_1 display_settings['colorbar_label'] = 'Angular Error 1' Fig = splt.mes3d_projection( diff --git a/examples/example_distortion.py b/examples/example_distortion.py index 02077f4d..3ee4fdc4 100644 --- a/examples/example_distortion.py +++ b/examples/example_distortion.py @@ -52,13 +52,65 @@ edge_diff = sdst.edge_length_difference(mesh, mesh_s) edge_diff + ############################################################################# -# VISUALIZATION USING EXTERNAL TOOLS +# VISUALIZATION USING INTERNAL TOOLS ############################################################################# + +import slam.plot as splt + # # Visualization of the original mesh -# visb_sc = splt.visbrain_plot(mesh=mesh, caption="original mesh") -# visb_sc.preview() +vertices = mesh.vertices +# center the vertices +vertices = vertices - np.mean(vertices, axis=0) +vertices_translate = np.copy(vertices) +# rotate the vertices +theta = np.pi / 2 +rot_x = np.array([[1, 0, 0], + [0, np.cos(theta), -np.sin(theta)], + [0, np.sin(theta), np.cos(theta)]]) +vertices_translate = np.dot(rot_x, vertices_translate.T).T +rot_z = np.array([[np.cos(theta), -np.sin(theta), 0], + [np.sin(theta), np.cos(theta), 0], + [0, 0, 1],]) +vertices_translate = np.dot(rot_z, vertices_translate.T).T +display_settings = {} +mesh_data = {} +mesh_data['vertices'] = vertices_translate +mesh_data['faces'] = mesh.faces +mesh_data['title'] = 'example_mesh.gii Original Mesh' +intensity_data = None +Fig = splt.mes3d_projection( + mesh_data=mesh_data, + intensity_data=intensity_data, + display_settings=display_settings) +Fig.show() + # ############################################################################ # # Visualization of the smoothed mesh -# visb_sc = splt.visbrain_plot(mesh=mesh_s, caption="smoothed mesh") -# visb_sc.preview() + +vertices = mesh_s.vertices +# center the vertices +vertices = vertices - np.mean(vertices, axis=0) +vertices_translate = np.copy(vertices) +# rotate the vertices +theta = np.pi / 2 +rot_x = np.array([[1, 0, 0], + [0, np.cos(theta), -np.sin(theta)], + [0, np.sin(theta), np.cos(theta)]]) +vertices_translate = np.dot(rot_x, vertices_translate.T).T +rot_z = np.array([[np.cos(theta), -np.sin(theta), 0], + [np.sin(theta), np.cos(theta), 0], + [0, 0, 1],]) +vertices_translate = np.dot(rot_z, vertices_translate.T).T +display_settings = {} +mesh_data = {} +mesh_data['vertices'] = vertices_translate +mesh_data['faces'] = mesh_s.faces +mesh_data['title'] = 'example_mesh.gii Smoothed Mesh' +intensity_data = None +Fig = splt.mes3d_projection( + mesh_data=mesh_data, + intensity_data=intensity_data, + display_settings=display_settings) +Fig.show() diff --git a/examples/example_topology.py b/examples/example_topology.py index 1f3052cb..7beaaf2f 100644 --- a/examples/example_topology.py +++ b/examples/example_topology.py @@ -92,6 +92,14 @@ print(mesh.is_watertight) print(mesh_closed.is_watertight) + +print('close mesh') +broken_vertices_mesh_closed = stop.broken_vertices(mesh_closed) +print(np.count_nonzero(broken_vertices_mesh_closed)) +print('open mesh') +broken_vertices_open_mesh = stop.broken_vertices(open_mesh) +print(np.count_nonzero(broken_vertices_open_mesh)) + ############################################################################# # VISUALIZATION USING EXTERNAL TOOLS ############################################################################# @@ -222,9 +230,3 @@ # visb_sc5 = splt.visbrain_plot(mesh=mesh_closed, caption="closed mesh") # visb_sc5.preview() -print('close mesh') -broken_vertices = stop.broken_vertices(mesh_closed) -print(np.count_nonzero(broken_vertices)) -print('open mesh') -broken_vertices = stop.broken_vertices(open_mesh) -print(np.count_nonzero(broken_vertices)) diff --git a/examples/example_vertex_voronoi.py b/examples/example_vertex_voronoi.py index 47377b51..8e843236 100644 --- a/examples/example_vertex_voronoi.py +++ b/examples/example_vertex_voronoi.py @@ -36,9 +36,11 @@ print(vert_vor.shape) print(np.sum(vert_vor) - mesh.area) + ############################################################################# -# VISUALIZATION USING EXTERNAL TOOLS +# VISUALIZATION USING INTERNAL TOOLS ############################################################################# + import slam.plot as splt ############################################################################### # Visualization diff --git a/slam/plot.py b/slam/plot.py index 8c28503c..a1686c9f 100755 --- a/slam/plot.py +++ b/slam/plot.py @@ -73,14 +73,15 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): faces = mesh_data["faces"] title = mesh_data.get("title", "") - lighting = { - "ambient": 0.7, - "diffuse": 0.8, - "specular": 0.5, - "roughness": 0.2, - "fresnel": 0.1, - } - + lighting_effects = ( + dict(ambient=0.4, + diffuse=0.5, + roughness=0.9, + specular=0.6, + fresnel=0.2)) + lightposition = dict( + x=-10, y=10, z=10 + ) mesh_kwargs = { "x": vertices[:, 0], "y": vertices[:, 1], @@ -90,7 +91,8 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): "k": faces[:, 2], "color": "ghostwhite", "flatshading": False, - "lighting": lighting, + "lighting": lighting_effects, + "lightposition": lightposition, } if intensity_data is not None: @@ -107,13 +109,8 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): "tickfont": {"size": 16}, }, "flatshading": True, - "lighting": { - "ambient": 1, - "diffuse": 0, - "specular": 0, - "roughness": 1, - "fresnel": 0, - }, + "lighting": lighting_effects, + "lightposition": lightposition, "colorbar_tickvals": display_settings.get("tickvals", None), "colorbar_ticktext": display_settings.get("ticktext", None), }) From 2d5b137e223175d281b35ced1defee62514d0024 Mon Sep 17 00:00:00 2001 From: Maxime Van Der Valk Date: Fri, 30 Jan 2026 09:30:31 +0100 Subject: [PATCH 2/2] new examples --- examples/example_texture.py | 2 +- examples/example_topology.py | 70 +++++++++++++++++++++++++++++++++--- examples/test.gii | 9 ----- slam/plot.py | 31 ++++++++-------- 4 files changed, 83 insertions(+), 29 deletions(-) delete mode 100644 examples/test.gii diff --git a/examples/example_texture.py b/examples/example_texture.py index 4d077007..34d8ed3b 100755 --- a/examples/example_texture.py +++ b/examples/example_texture.py @@ -80,6 +80,6 @@ intensity_data, display_settings, ) - +fig.show() #os.makedirs(PATH, exist_ok=True) #fig.write_image(SAVE_DIR, width=1600, height=900) diff --git a/examples/example_topology.py b/examples/example_topology.py index 7beaaf2f..37d48691 100644 --- a/examples/example_topology.py +++ b/examples/example_topology.py @@ -101,12 +101,72 @@ print(np.count_nonzero(broken_vertices_open_mesh)) ############################################################################# -# VISUALIZATION USING EXTERNAL TOOLS +# VISUALIZATION USING INTERNAL TOOLS ############################################################################# -# import slam.plot as splt -# from vispy.scene import Line -# from visbrain.objects import VispyObj, SourceObj -# + +import slam.plot as splt + +vertices = mesh_closed.vertices +# center the vertices +vertices = vertices - np.mean(vertices, axis=0) +vertices_translate = np.copy(vertices) +# rotate the vertices +theta = np.pi / 2 +rot_x = np.array([[1, 0, 0], + [0, np.cos(theta), -np.sin(theta)], + [0, np.sin(theta), np.cos(theta)]]) +vertices_translate = np.dot(rot_x, vertices_translate.T).T +rot_z = np.array([[np.cos(theta), -np.sin(theta), 0], + [np.sin(theta), np.cos(theta), 0], + [0, 0, 1], ]) +vertices_translate = np.dot(rot_z, vertices_translate.T).T + +# Plot Mean Curvature +display_settings = {} +display_settings['colorbar_label'] = 'Broken Vertices' +mesh_data = {} +mesh_data['vertices'] = vertices_translate +mesh_data['faces'] = mesh_closed.faces +mesh_data['title'] = 'Mesh Close' +intensity_data = {} +intensity_data['values'] = broken_vertices_mesh_closed +intensity_data["mode"] = "vertex" +Fig = splt.mes3d_projection( + mesh_data=mesh_data, + intensity_data=intensity_data, + display_settings=display_settings) +Fig.show() + + + +vertices = open_mesh.vertices +# center the vertices +vertices = vertices - np.mean(vertices, axis=0) +vertices_translate = np.copy(vertices) +# rotate the vertices +theta = np.pi / 2 +rot_x = np.array([[1, 0, 0], + [0, np.cos(theta), -np.sin(theta)], + [0, np.sin(theta), np.cos(theta)]]) +vertices_translate = np.dot(rot_x, vertices_translate.T).T +rot_z = np.array([[np.cos(theta), -np.sin(theta), 0], + [np.sin(theta), np.cos(theta), 0], + [0, 0, 1], ]) +vertices_translate = np.dot(rot_z, vertices_translate.T).T +display_settings = {} +display_settings['colorbar_label'] = 'Broken Vertices' +mesh_data = {} +mesh_data['vertices'] = vertices_translate +mesh_data['faces'] = open_mesh.faces +mesh_data['title'] = 'Mesh Close' +intensity_data = {} +intensity_data['values'] = broken_vertices_open_mesh +intensity_data["mode"] = "vertex" +Fig = splt.mes3d_projection( + mesh_data=mesh_data, + intensity_data=intensity_data, + display_settings=display_settings) +Fig.show() # ############################################################################### # # show the result # # WARNING : BrainObj should be added first before diff --git a/examples/test.gii b/examples/test.gii deleted file mode 100644 index 841a02eb..00000000 --- a/examples/test.gii +++ /dev/null @@ -1,9 +0,0 @@ - - -NIFTI_XFORM_UNKNOWNNIFTI_XFORM_UNKNOWN 1.000000 0.000000 0.000000 0.000000 - 0.000000 1.000000 0.000000 0.000000 - 0.000000 0.000000 1.000000 0.000000 - 0.000000 0.000000 0.000000 1.000000eJxjYEAAAAAMAAE=NIFTI_XFORM_UNKNOWNNIFTI_XFORM_UNKNOWN 1.000000 0.000000 0.000000 0.000000 - 0.000000 1.000000 0.000000 0.000000 - 0.000000 0.000000 1.000000 0.000000 - 0.000000 0.000000 0.000000 1.000000eJxjYEAAAAAMAAE= \ No newline at end of file diff --git a/slam/plot.py b/slam/plot.py index a1686c9f..1c7ccd0c 100755 --- a/slam/plot.py +++ b/slam/plot.py @@ -73,15 +73,14 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): faces = mesh_data["faces"] title = mesh_data.get("title", "") - lighting_effects = ( - dict(ambient=0.4, - diffuse=0.5, - roughness=0.9, - specular=0.6, - fresnel=0.2)) - lightposition = dict( - x=-10, y=10, z=10 - ) + lighting = { + "ambient": 0.7, + "diffuse": 0.8, + "specular": 0.5, + "roughness": 0.2, + "fresnel": 0.1, + } + mesh_kwargs = { "x": vertices[:, 0], "y": vertices[:, 1], @@ -91,8 +90,7 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): "k": faces[:, 2], "color": "ghostwhite", "flatshading": False, - "lighting": lighting_effects, - "lightposition": lightposition, + "lighting": lighting, } if intensity_data is not None: @@ -109,8 +107,13 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): "tickfont": {"size": 16}, }, "flatshading": True, - "lighting": lighting_effects, - "lightposition": lightposition, + "lighting": { + "ambient": 1, + "diffuse": 0, + "specular": 0, + "roughness": 1, + "fresnel": 0, + }, "colorbar_tickvals": display_settings.get("tickvals", None), "colorbar_ticktext": display_settings.get("ticktext", None), }) @@ -140,4 +143,4 @@ def mes3d_projection(mesh_data, intensity_data=None, display_settings=None): }, ) - return fig + return fig \ No newline at end of file