Browse code

add quick volume computation for dim == 2

Jorn Hoofwijk authored on 23/07/2018 14:12:44
Showing 1 changed files
... ...
@@ -202,8 +202,16 @@ def simplex_volume_in_embedding(vertices) -> float:
202 202
     # Implements http://mathworld.wolfram.com/Cayley-MengerDeterminant.html
203 203
     # Modified from https://codereview.stackexchange.com/questions/77593/calculating-the-volume-of-a-tetrahedron
204 204
 
205
-    # β_ij = |v_i - v_k|²
205
+    
206 206
     vertices = np.array(vertices, dtype=float)
207
+    dim = len(vertices[0])
208
+    if dim == 2:
209
+        # Heron's formula
210
+        a, b, c = scipy.spatial.distance.pdist(vertices, metric='euclidean')
211
+        s = 0.5 * (a + b + c)
212
+        return math.sqrt(s*(s-a)*(s-b)*(s-c))
213
+
214
+    # β_ij = |v_i - v_k|²
207 215
     sq_dists = scipy.spatial.distance.pdist(vertices, metric='sqeuclidean')
208 216
 
209 217
     # Add border while compressed