10
10
data_path = pjoin (subj_dir , subject_id )
11
11
12
12
13
+ def _slow_compute_normals (rr , tris ):
14
+ """Efficiently compute vertex normals for triangulated surface"""
15
+ # first, compute triangle normals
16
+ r1 = rr [tris [:, 0 ], :]
17
+ r2 = rr [tris [:, 1 ], :]
18
+ r3 = rr [tris [:, 2 ], :]
19
+ tri_nn = np .cross ((r2 - r1 ), (r3 - r1 ))
20
+
21
+ # Triangle normals and areas
22
+ size = np .sqrt (np .sum (tri_nn * tri_nn , axis = 1 ))
23
+ zidx = np .where (size == 0 )[0 ]
24
+ size [zidx ] = 1.0 # prevent ugly divide-by-zero
25
+ tri_nn /= size [:, np .newaxis ]
26
+
27
+ # accumulate the normals
28
+ nn = np .zeros ((len (rr ), 3 ))
29
+ for p , verts in enumerate (tris ):
30
+ nn [verts ] += tri_nn [p , :]
31
+ size = np .sqrt (np .sum (nn * nn , axis = 1 ))
32
+ size [size == 0 ] = 1.0 # prevent ugly divide-by-zero
33
+ nn /= size [:, np .newaxis ]
34
+ return nn
35
+
36
+
13
37
@utils .requires_fsaverage
14
38
def test_surface ():
15
39
"""Test IO for Surface class"""
16
40
for subjects_dir in [None , subj_dir ]:
17
41
surface = utils .Surface ('fsaverage' , 'lh' , 'inflated' ,
18
- subjects_dir = subjects_dir )
42
+ subjects_dir = subjects_dir )
19
43
surface .load_geometry ()
20
44
surface .load_label ('BA1' )
21
45
surface .load_curvature ()
@@ -25,3 +49,7 @@ def test_surface():
25
49
surface .apply_xfm (xfm )
26
50
x_ = surface .x
27
51
assert_array_almost_equal (x + 2 , x_ )
52
+
53
+ # normals
54
+ nn = _slow_compute_normals (surface .coords , surface .faces )
55
+ assert_array_almost_equal (nn , surface .nn )
0 commit comments