@@ -34,10 +34,13 @@ class P2PDistanceInputSpec(BaseInterfaceInputSpec):
34
34
'"surface": edge distance is weighted by the '
35
35
'corresponding surface area' ))
36
36
37
+
37
38
class P2PDistanceOutputSpec (TraitedSpec ):
38
39
distance = traits .Float (desc = "computed distance" )
39
40
41
+
40
42
class P2PDistance (BaseInterface ):
43
+
41
44
"""Calculates a point-to-point (p2p) distance between two corresponding
42
45
VTK-readable meshes or contours.
43
46
@@ -57,41 +60,52 @@ class P2PDistance(BaseInterface):
57
60
output_spec = P2PDistanceOutputSpec
58
61
59
62
def _triangle_area (self , A , B , C ):
60
- ABxAC = euclidean (A ,B ) * euclidean (A ,C )
61
- prod = np .dot (np .array (B )- np .array (A ),np .array (C )- np .array (A ))
62
- angle = np .arccos ( prod / ABxAC )
63
- area = 0.5 * ABxAC * np .sin ( angle )
63
+ ABxAC = euclidean (A , B ) * euclidean (A , C )
64
+ prod = np .dot (np .array (B ) - np .array (A ), np .array (C ) - np .array (A ))
65
+ angle = np .arccos (prod / ABxAC )
66
+ area = 0.5 * ABxAC * np .sin (angle )
64
67
return area
65
68
66
69
def _run_interface (self , runtime ):
67
- from tvtk .api import tvtk
68
- r1 = tvtk .PolyDataReader ( file_name = self .inputs .surface1 )
69
- r2 = tvtk .PolyDataReader ( file_name = self .inputs .surface2 )
70
+ try :
71
+ from tvtk .api import tvtk
72
+ except ImportError :
73
+ raise ImportError ('Interface P2PDistance requires tvtk' )
74
+
75
+ try :
76
+ from enthought .etsconfig .api import ETSConfig
77
+ ETSConfig .toolkit = 'null'
78
+ except ImportError :
79
+ iflogger .warn (('ETS toolkit could not be imported' ))
80
+ pass
81
+
82
+ r1 = tvtk .PolyDataReader (file_name = self .inputs .surface1 )
83
+ r2 = tvtk .PolyDataReader (file_name = self .inputs .surface2 )
70
84
vtk1 = r1 .output
71
85
vtk2 = r2 .output
72
86
r1 .update ()
73
87
r2 .update ()
74
- assert ( len (vtk1 .points ) == len (vtk2 .points ) )
88
+ assert (len (vtk1 .points ) == len (vtk2 .points ))
75
89
d = 0.0
76
90
totalWeight = 0.0
77
91
78
92
points = vtk1 .points
79
- faces = vtk1 .polys .to_array ().reshape (- 1 ,4 ).astype (int )[:,1 :]
93
+ faces = vtk1 .polys .to_array ().reshape (- 1 , 4 ).astype (int )[:, 1 :]
80
94
81
- for p1 ,p2 in zip ( points , vtk2 .points ):
95
+ for p1 , p2 in zip (points , vtk2 .points ):
82
96
weight = 1.0
83
97
if (self .inputs .weighting == 'surface' ):
84
- #compute surfaces, set in weight
98
+ # compute surfaces, set in weight
85
99
weight = 0.0
86
- point_faces = faces [ (faces [:,:] == 0 ).any (axis = 1 ) ]
100
+ point_faces = faces [(faces [:, :] == 0 ).any (axis = 1 )]
87
101
88
102
for idset in point_faces :
89
- p1 = points [ int (idset [0 ]) ]
90
- p2 = points [ int (idset [1 ]) ]
91
- p3 = points [ int (idset [2 ]) ]
103
+ p1 = points [int (idset [0 ])]
104
+ p2 = points [int (idset [1 ])]
105
+ p3 = points [int (idset [2 ])]
92
106
weight = weight + self ._triangle_area (p1 , p2 , p3 )
93
107
94
- d += weight * euclidean ( p1 , p2 )
108
+ d += weight * euclidean (p1 , p2 )
95
109
totalWeight = totalWeight + weight
96
110
97
111
self ._distance = d / totalWeight
@@ -101,4 +115,3 @@ def _list_outputs(self):
101
115
outputs = self ._outputs ().get ()
102
116
outputs ['distance' ] = self ._distance
103
117
return outputs
104
-
0 commit comments