-
Notifications
You must be signed in to change notification settings - Fork 12
/
cellblender_meshalyzer.py
482 lines (371 loc) · 15.8 KB
/
cellblender_meshalyzer.py
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
"""
This file contains the classes for CellBlender's Meshalyzer.
"""
import cellblender
# blender imports
import bpy
from bpy.props import BoolProperty, CollectionProperty, EnumProperty, \
FloatProperty, FloatVectorProperty, IntProperty, \
IntVectorProperty, PointerProperty, StringProperty
#from bpy.app.handlers import persistent
#import math
#import mathutils
# python imports
import re
import mathutils
# CellBlender imports
import cellblender
from . import parameter_system
from . import cellblender_release
from . import cellblender_utils
# Meshalyzer Operators:
class MCELL_OT_meshalyzer(bpy.types.Operator):
bl_idname = "mcell.meshalyzer"
bl_label = "Analyze Geometric Properties of Mesh"
bl_description = "Analyze Geometric Properties of Mesh"
bl_options = {'REGISTER', 'UNDO'}
def count_components(self,context):
bpy.ops.object.mode_set(mode='OBJECT')
obj = context.active_object
mesh = obj.data
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.reveal()
bpy.ops.mesh.select_mode(type='VERT')
bpy.ops.mesh.select_all(action='DESELECT')
# Count total vertices and number of vertices contiguous with vertex 0
bpy.ops.object.mode_set(mode='OBJECT')
mesh.vertices[0].select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_linked()
n_v_tot = len(mesh.vertices)
n_v_sel = mesh.total_vert_sel
bpy.ops.object.mode_set(mode='OBJECT')
# Loop over disjoint components
n_components = 1
while (n_v_sel < n_v_tot):
n_components += 1
# make list of selected indices
vl1 = [v.index for v in mesh.vertices if v.select == True]
# make list of indices of remaining component(s)
vl2 = [v.index for v in mesh.vertices if v.select == False]
# Grow selection with vertices contiguous with first vertex of remainder
mesh.vertices[vl2[0]].select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_linked()
# Count number of vertices now selected and loop again if necessary
n_v_sel = mesh.total_vert_sel
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type='FACE')
bpy.ops.object.mode_set(mode='OBJECT')
return n_components
def execute(self, context):
mcell = context.scene.mcell
objs = context.selected_objects
mcell.meshalyzer.object_name = ""
mcell.meshalyzer.vertices = 0
mcell.meshalyzer.edges = 0
mcell.meshalyzer.faces = 0
mcell.meshalyzer.watertight = ""
mcell.meshalyzer.manifold = ""
mcell.meshalyzer.normal_status = ""
mcell.meshalyzer.components = 0
mcell.meshalyzer.genus_string = ""
mcell.meshalyzer.area = 0
mcell.meshalyzer.volume = 0
mcell.meshalyzer.sav_ratio = 0
if (len(objs) != 1):
mcell.meshalyzer.status = "Please Select One Mesh Object"
return {'FINISHED'}
obj = objs[0]
mcell.meshalyzer.object_name = obj.name
if not (obj.type == 'MESH'):
mcell.meshalyzer.status = "Selected Object Not a Mesh"
return {'FINISHED'}
t_mat = obj.matrix_world
mesh = obj.data
mcell.meshalyzer.vertices = len(mesh.vertices)
mcell.meshalyzer.edges = len(mesh.edges)
mcell.meshalyzer.faces = len(mesh.polygons)
mcell.meshalyzer.components = self.count_components(context)
mcell.meshalyzer.genus = mcell.meshalyzer.components - ( (mcell.meshalyzer.vertices + mcell.meshalyzer.faces - mcell.meshalyzer.edges) / 2 )
mcell.meshalyzer.genus_string = "Genus = %d" % (mcell.meshalyzer.genus)
area = 0
for f in mesh.polygons:
if not (len(f.vertices) == 3):
mcell.meshalyzer.status = "***** Mesh Not Triangulated *****"
mcell.meshalyzer.watertight = "Mesh Not Triangulated"
return {'FINISHED'}
tv0 = mesh.vertices[f.vertices[0]].co @ t_mat
tv1 = mesh.vertices[f.vertices[1]].co @ t_mat
tv2 = mesh.vertices[f.vertices[2]].co @ t_mat
area = area + mathutils.geometry.area_tri(tv0, tv1, tv2)
mcell.meshalyzer.area = area
(edge_faces, edge_face_count) = make_efdict(mesh)
is_closed = check_closed(edge_face_count)
is_manifold = check_manifold(edge_face_count)
is_orientable = check_orientable(mesh, edge_faces, edge_face_count)
if is_orientable:
mcell.meshalyzer.normal_status = "Consistent Normals"
else:
mcell.meshalyzer.normal_status = "Inconsistent Normals"
if is_closed:
mcell.meshalyzer.watertight = "Watertight Mesh"
else:
mcell.meshalyzer.watertight = "Non-watertight Mesh"
if is_manifold:
mcell.meshalyzer.manifold = "Manifold Mesh"
else:
mcell.meshalyzer.manifold = "Non-manifold Mesh"
volume = 0
if is_orientable and is_manifold and is_closed:
volume = mesh_vol(mesh, t_mat)
if volume >= 0:
mcell.meshalyzer.normal_status = "Outward Facing Normals"
else:
mcell.meshalyzer.normal_status = "Inward Facing Normals"
mcell.meshalyzer.volume = volume
if (not volume == 0.0):
mcell.meshalyzer.sav_ratio = area/volume
mcell.meshalyzer.status = ""
return {'FINISHED'}
class MCELL_OT_gen_meshalyzer_report(bpy.types.Operator):
bl_idname = "mcell.gen_meshalyzer_report"
bl_label = "Analyze Geometric Properties of Multiple Meshes"
bl_description = "Generate Analysis Report of Geometric Properties of Multiple Meshes"
bl_options = {'REGISTER', 'UNDO'}
def execute(self,context):
mcell = context.scene.mcell
objs = context.selected_objects
mcell.meshalyzer.object_name = ''
mcell.meshalyzer.vertices = 0
mcell.meshalyzer.edges = 0
mcell.meshalyzer.faces = 0
mcell.meshalyzer.watertight = ''
mcell.meshalyzer.manifold = ''
mcell.meshalyzer.normal_status = ''
mcell.meshalyzer.area = 0
mcell.meshalyzer.volume = 0
mcell.meshalyzer.sav_ratio = 0
if (len(objs) == 0):
mcell.meshalyzer.status = 'Please Select One or More Mesh Objects'
return {'FINISHED'}
bpy.ops.text.new()
report = bpy.data.texts['Text']
report.name = 'mesh_analysis.txt'
report.write("# Object Surface Area Volume\n")
for obj in objs:
mcell.meshalyzer.object_name = obj.name
if not (obj.type == 'MESH'):
mcell.meshalyzer.status = 'Selected Object Not a Mesh'
return {'FINISHED'}
t_mat = obj.matrix_world
mesh=obj.data
mcell.meshalyzer.vertices = len(mesh.vertices)
mcell.meshalyzer.edges = len(mesh.edges)
mcell.meshalyzer.faces = len(mesh.polygons)
area = 0
for f in mesh.polygons:
if not (len(f.vertices) == 3):
mcell.meshalyzer.status = '***** Mesh Not Triangulated *****'
mcell.meshalyzer.watertight = 'Mesh Not Triangulated'
return {'FINISHED'}
tv0 = mesh.vertices[f.vertices[0]].co @ t_mat
tv1 = mesh.vertices[f.vertices[1]].co @ t_mat
tv2 = mesh.vertices[f.vertices[2]].co @ t_mat
area = area + mathutils.geometry.area_tri(tv0,tv1,tv2)
mcell.meshalyzer.area = area
(edge_faces, edge_face_count) = make_efdict(mesh)
is_closed = check_closed(edge_face_count)
is_manifold = check_manifold(edge_face_count)
is_orientable = check_orientable(mesh,edge_faces,edge_face_count)
if is_orientable:
mcell.meshalyzer.normal_status = 'Consistent Normals'
else:
mcell.meshalyzer.normal_status = 'Inconsistent Normals'
if is_closed:
mcell.meshalyzer.watertight = 'Watertight Mesh'
else:
mcell.meshalyzer.watertight = 'Non-watertight Mesh'
if is_manifold:
mcell.meshalyzer.manifold = 'Manifold Mesh'
else:
mcell.meshalyzer.manifold = 'Non-manifold Mesh'
volume = 0
if is_orientable and is_manifold and is_closed:
volume = mesh_vol(mesh,t_mat)
if volume >= 0:
mcell.meshalyzer.normal_status = 'Outward Facing Normals'
else:
mcell.meshalyzer.normal_status = 'Inward Facing Normals'
mcell.meshalyzer.volume = volume
if (not volume == 0.0):
mcell.meshalyzer.sav_ratio = area/volume
report.write("%s %.9g %.9g\n" % (obj.name, mcell.meshalyzer.area, mcell.meshalyzer.volume))
mcell.meshalyzer.status = ''
return {'FINISHED'}
# Meshalyzer support functions
def mesh_vol(mesh, t_mat):
"""Compute volume of triangulated, orientable, watertight, manifold mesh
volume > 0 means outward facing normals
volume < 0 means inward facing normals
"""
volume = 0.0
for f in mesh.polygons:
tv0 = mesh.vertices[f.vertices[0]].co @ t_mat
tv1 = mesh.vertices[f.vertices[1]].co @ t_mat
tv2 = mesh.vertices[f.vertices[2]].co @ t_mat
x0 = tv0.x
y0 = tv0.y
z0 = tv0.z
x1 = tv1.x
y1 = tv1.y
z1 = tv1.z
x2 = tv2.x
y2 = tv2.y
z2 = tv2.z
det = x0*(y1*z2-y2*z1)+x1*(y2*z0-y0*z2)+x2*(y0*z1-y1*z0)
volume = volume + det
volume = volume/6.0
return(volume)
def make_efdict(mesh):
edge_faces = {}
edge_face_count = {}
for f in mesh.polygons:
for ek in f.edge_keys:
if ek in edge_faces:
edge_faces[ek] ^= f.index
edge_face_count[ek] = edge_face_count[ek] + 1
else:
edge_faces[ek] = f.index
edge_face_count[ek] = 1
return(edge_faces, edge_face_count)
def check_manifold(edge_face_count):
""" Make sure the object is manifold """
for ek in edge_face_count.keys():
if edge_face_count[ek] != 2:
return (0)
return(1)
def check_closed(edge_face_count):
""" Make sure the object is closed (no leaks). """
for ek in edge_face_count.keys():
if not edge_face_count[ek] == 2:
return (0)
return(1)
def check_orientable(mesh, edge_faces, edge_face_count):
ev_order = [[0, 1], [1, 2], [2, 0]]
edge_checked = {}
for f in mesh.polygons:
for i in range(0, len(f.vertices)):
ek = f.edge_keys[i]
if not ek in edge_checked:
edge_checked[ek] = 1
if edge_face_count[ek] == 2:
nfi = f.index ^ edge_faces[ek]
nf = mesh.polygons[nfi]
for j in range(0, len(nf.vertices)):
if ek == nf.edge_keys[j]:
if f.vertices[ev_order[i][0]] != nf.vertices[
ev_order[j][1]]:
return (0)
break
return (1)
# Meshalyzer Panel Classes
class MCELL_PT_meshalyzer(bpy.types.Panel):
bl_label = "CellBlender - Mesh Analysis"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "CellBlender"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scn = context.scene
mcell = context.scene.mcell
if not mcell.initialized:
mcell.draw_uninitialized ( self.layout )
else:
row = layout.row()
row.operator("mcell.meshalyzer", text="Analyze Mesh",
icon='MESH_ICOSPHERE')
row = layout.row()
row.operator("mcell.gen_meshalyzer_report",
text="Generate Analysis Report",icon="MESH_ICOSPHERE")
if (mcell.meshalyzer.status != ""):
row = layout.row()
row.label(text=mcell.meshalyzer.status, icon='ERROR')
row = layout.row()
row.label(text="Object Name: %s" % (mcell.meshalyzer.object_name))
row = layout.row()
row.label(text="Vertices: %d" % (mcell.meshalyzer.vertices))
row = layout.row()
row.label(text="Edges: %d" % (mcell.meshalyzer.edges))
row = layout.row()
row.label(text="Faces: %d" % (mcell.meshalyzer.faces))
row = layout.row()
row.label(text="Surface Area: %.5g" % (mcell.meshalyzer.area))
row = layout.row()
if (mcell.meshalyzer.watertight == 'Watertight Mesh'):
row.label(text="Volume: %.5g" % (mcell.meshalyzer.volume))
else:
row.label(text="Volume: approx: %.5g" % (mcell.meshalyzer.volume))
row = layout.row()
row.label(text="SA/V Ratio: %.5g" % (mcell.meshalyzer.sav_ratio))
row = layout.row()
row.label(text="Mesh Topology:")
row = layout.row()
row.label(text=" %s" % (mcell.meshalyzer.watertight))
row = layout.row()
row.label(text=" %s" % (mcell.meshalyzer.manifold))
row = layout.row()
row.label(text=" %s" % (mcell.meshalyzer.normal_status))
row = layout.row()
row.label(text=" Components = %d" % (mcell.meshalyzer.components))
row = layout.row()
row.label(text=" %s" % (mcell.meshalyzer.genus_string))
# Meshalyzer Property Groups
class MCellMeshalyzerPropertyGroup(bpy.types.PropertyGroup):
object_name: StringProperty(name="Object Name")
vertices: IntProperty(name="Vertices", default=0)
edges: IntProperty(name="Edges", default=0)
faces: IntProperty(name="Faces", default=0)
watertight: StringProperty(name="Watertight")
manifold: StringProperty(name="Manifold")
normal_status: StringProperty(name="Surface Normals")
components: IntProperty(name="Components", default=0)
genus: IntProperty(name="Genus", default=0)
genus_string: StringProperty(name="", default="")
area: FloatProperty(name="Area", default=0)
volume: FloatProperty(name="Volume", default=0)
sav_ratio: FloatProperty(name="SA/V Ratio", default=0)
status: StringProperty(name="Status")
def remove_properties ( self, context ):
print ( "Removing all Meshalyzer Properties... no collections to remove." )
classes = (
MCELL_OT_meshalyzer,
MCELL_OT_gen_meshalyzer_report,
MCELL_PT_meshalyzer,
MCellMeshalyzerPropertyGroup,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)