From 6fa45813555e77e80a4bcb07837fb645ccb7c551 Mon Sep 17 00:00:00 2001
From: Jean-Yves TINEVEZ <jean-yves.tinevez@pasteur.fr>
Date: Fri, 24 Jul 2020 15:26:32 +0200
Subject: [PATCH] Better version of find_contour.

One call to pdist per run. Speed-up ~ x8.
---
 src/@deproj/find_countour.m | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/@deproj/find_countour.m b/src/@deproj/find_countour.m
index a5cae48..47920b8 100644
--- a/src/@deproj/find_countour.m
+++ b/src/@deproj/find_countour.m
@@ -4,9 +4,12 @@ function P2 = find_countour( P )
     P2 = NaN( size( P ) );
     current = 1;
 
+    D = squareform( pdist ( P ) );
+    D( D == 0 ) = Inf;
+    
     % Start point
-    ps = P( 1, : );    
-    set_visited( 1 )
+    id = 1;
+    ps = P( id, : );    
     P2( current, : ) = ps;
     current = current + 1;
     
@@ -16,9 +19,10 @@ function P2 = find_countour( P )
 
         done = current == size( P, 1 );
 
-        id = find_next_point( ps );
+        prev_id = id;
+        id = find_next_point( id );
         ps = P( id, : );
-        set_visited( id )
+        set_visited( prev_id )
         P2( current, : ) = ps;
         current = current + 1;
         
@@ -26,11 +30,12 @@ function P2 = find_countour( P )
     
     
     function set_visited( id )
-        P( id, : ) = NaN;
+        D( id, : ) = Inf;
+        D( :, id ) = Inf;
     end
     
-    function id = find_next_point( p0 )        
-        [ ~, id ] = pdist2( P, p0, 'Euclidean', 'Smallest', 1 );
+    function id = find_next_point( id )                
+        [ ~, id ] = min( D( :, id ) );
     end
     
 end
-- 
GitLab