@@ -63,6 +63,13 @@ class P2PDistance(BaseInterface):
63
63
input_spec = P2PDistanceInputSpec
64
64
output_spec = P2PDistanceOutputSpec
65
65
66
+ def _triangle_area (A , B , C ):
67
+ ABxAC = euclidean (A ,B ) * euclidean (A ,C )
68
+ prod = np .dot (np .array (B )- np .array (A ),np .array (C )- np .array (A ))
69
+ angle = np .arccos ( prod / ABxAC )
70
+ area = 0.5 * ABxAC * np .sin ( angle )
71
+ return area
72
+
66
73
def _run_interface (self , runtime ):
67
74
r1 = tvtk .PolyDataReader ( file_name = self .inputs .surface1 )
68
75
r2 = tvtk .PolyDataReader ( file_name = self .inputs .surface2 )
@@ -73,16 +80,27 @@ def _run_interface(self, runtime):
73
80
74
81
d = 0.0
75
82
totalWeight = 0.0
76
- for p1 ,p2 in zip ( vtk1 .points , vtk2 .points ):
83
+
84
+ points = vtk1 .points
85
+ faces = vtk1 .polys .to_array ().reshape (- 1 ,4 ).astype (int )[:,1 :]
86
+
87
+ for p1 ,p2 in zip ( points , vtk2 .points ):
77
88
weight = 1.0
78
89
if (self .inputs .method == 'surface' ):
79
90
#compute surfaces, set in weight
80
- print "Error, not implemented"
91
+ weight = 0.0
92
+ point_faces = faces [ (faces [:,:]== 0 ).any (axis = 1 ) ]
93
+
94
+ for idset in point_faces :
95
+ p1 = points [ int (idset [0 ]) ]
96
+ p2 = points [ int (idset [1 ]) ]
97
+ p3 = points [ int (idset [2 ]) ]
98
+ weight = weight + self ._triangle_area (p1 , p2 , p3 )
81
99
82
- d += euclidean ( p1 , p2 )
100
+ d += weight * euclidean ( p1 , p2 )
83
101
totalWeight = totalWeight + weight
84
102
85
- self ._distance = d / weight
103
+ self ._distance = d / totalWeight
86
104
return runtime
87
105
88
106
def _list_outputs (self ):
0 commit comments