Skip to content

Commit b1a65dc

Browse files
committed
Added curvature
1 parent 6ad622f commit b1a65dc

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

exercise-02/i2k_mesh_vesicles/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .cut_and_close import cut_and_close
1616
from .closest_nuclei import spot_to_closest_nucleus
1717
from .filter_by_volume import find_objects_by_volume
18+
from .process_curvature import process_curvature
1819

1920
### > Functions call have to be done wrapped in an operator.
2021

@@ -92,6 +93,17 @@ def execute(self, context):
9293
return {'FINISHED'}
9394

9495

96+
class OBJECT_OT_nuclei_curvature(bpy.types.Operator):
97+
bl_idname = "object.nuclei_curvature"
98+
bl_label = "Nuclei curvature"
99+
bl_description = "Process the local vertex curvature of the nuclei"
100+
101+
def execute(self, context):
102+
process_curvature()
103+
self.report({'INFO'}, "Produced vertex attribute")
104+
return {'FINISHED'}
105+
106+
95107
# We make our panel (looking like a tab) in the viewer's side panel
96108
# (the one that you can open with N)
97109
class VIEW3D_PT_vesicles_tools_panel(bpy.types.Panel):
@@ -116,6 +128,7 @@ def draw(self, context):
116128

117129
layout.operator("object.spots_as_empties", text="Spots as empties")
118130
layout.operator("object.spots_ownership", text="Spots ownership")
131+
layout.operator("object.nuclei_curvature", text="Nuclei curvature")
119132

120133

121134
# In Blender, you need to register your classes if you want them to be loaded in the pool of operators.
@@ -135,6 +148,7 @@ def unregister_props():
135148
OBJECT_OT_select_by_volume,
136149
OBJECT_OT_spots_as_empties,
137150
OBJECT_OT_spots_ownership,
151+
OBJECT_OT_nuclei_curvature,
138152
VIEW3D_PT_vesicles_tools_panel
139153
)
140154

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import bpy
2+
import bmesh
3+
import mathutils
4+
5+
def _process_curvature(obj, attribute_name):
6+
mesh = obj.data
7+
bm = bmesh.new()
8+
bm.from_mesh(mesh)
9+
10+
if bm.verts.layers.float.get(attribute_name):
11+
curvature_layer = bm.verts.layers.float[attribute_name]
12+
else:
13+
curvature_layer = bm.verts.layers.float.new(attribute_name)
14+
15+
for vert in bm.verts:
16+
if len(vert.link_edges) == 0:
17+
continue
18+
angle_sum = 0.0
19+
for edge in vert.link_edges:
20+
faces = edge.link_faces
21+
if len(faces) == 2:
22+
normal1 = faces[0].normal
23+
normal2 = faces[1].normal
24+
angle = normal1.angle(normal2)
25+
angle_sum += angle
26+
27+
avg_curvature = angle_sum / len(vert.link_edges)
28+
vert[curvature_layer] = avg_curvature
29+
30+
bm.to_mesh(mesh)
31+
bm.free()
32+
mesh.update()
33+
34+
def process_curvature():
35+
attribute_name = "vertex_curvature"
36+
bpy.ops.object.mode_set(mode='OBJECT')
37+
for obj in bpy.data.collections['Nuclei'].objects:
38+
if obj.type != 'MESH':
39+
continue
40+
_process_curvature(obj, attribute_name)
41+
42+
43+
if __name__ == "__main__":
44+
process_curvature()

exercise-03/astrocytes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def makeSourceName(name):
2323
return os.path.basename(name).split('.')[0]
2424

2525

26-
class AstrocytesContactState(object):
26+
class AstrocytesContact(object):
2727

2828
def __init__(self):
2929
self.meshes = None
@@ -416,7 +416,7 @@ def show_in_napari(acs):
416416

417417

418418
def run_workflow():
419-
acs = AstrocytesContactState()
419+
acs = AstrocytesContact()
420420
target_folder = "/home/benedetti/Downloads/I2K/data/astrocytes"
421421
target_path = os.path.join(target_folder, "contact-surface.ply")
422422
acs.open_mesh(target_path)
@@ -441,7 +441,7 @@ def run_workflow():
441441

442442

443443
def dump():
444-
acs = AstrocytesContactState()
444+
acs = AstrocytesContact()
445445
target_folder = "/home/benedetti/Downloads/I2K/data/"
446446
target_path = os.path.join(target_folder, "test-holes.ply")
447447
acs.open_mesh(target_path)
@@ -455,4 +455,4 @@ def dump():
455455

456456
if __name__ == "__main__":
457457
run_workflow()
458-
# show_in_napari(acs)
458+
# show_in_napari(acs)

0 commit comments

Comments
 (0)