-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmesh.py
executable file
·62 lines (52 loc) · 1.74 KB
/
mesh.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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 14 15:43:44 2010.
@author: - B. Burle & A. Gramfort
"""
import numpy as np
import openmeeg as om
class Mesh():
"""
A class allowing to open meshes.
A class allowing to open meshes data (vertices and triangles), and plot.
their respective surfaces.
Example:
--------
head = Mesh("MyHeadFile.tri")
To plot the surface of the corresponding object, call the function
'self.plot(**kwarg)'. The kwarg options are the one from
mayavi.mlab.triangular_mesh
"""
def __init__(self, fname=None):
"""Give a filename."""
self.points = []
self.faces = []
if fname is not None:
self.load(fname)
def load(self, fname):
"""Load a filename."""
m = om.Mesh(fname)
self.points = np.zeros((m.nb_vertices(), 3), dtype=float)
self.faces = np.zeros((m.nb_triangles(), 3), dtype=int)
vit = m.vertex_begin()
iit = 0
while vit != m.vertex_end():
self.points[iit, :] = [vit.value()(0),
vit.value()(1),
vit.value()(2)]
iit += 1
vit.incr()
tit = m.begin()
iit = 0
while tit != m.end():
self.faces[iit, :] = [tit.value().s1().getindex(),
tit.value().s2().getindex(),
tit.value().s3().getindex()]
iit += 1
tit.incr()
def plot(self, **kwarg):
"""Plot mesh with Mayavi."""
from mayavi import mlab
f = mlab.triangular_mesh(self.points[:, 0], self.points[:, 1],
self.points[:, 2], self.faces, **kwarg)
return f