From 19c1fd84d1fd75e9bfd55feb670fda87a2e02086 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Fri, 08 Feb 2019 08:32:29 +0000
Subject: [PATCH] Merge branch 'master' of ssh://git.penic.eu:29418/trisurf-ng

---
 src/cluster.c |   33 ++++++++++++++++++++++-----------
 1 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/cluster.c b/src/cluster.c
index 11e4859..bb431f2 100644
--- a/src/cluster.c
+++ b/src/cluster.c
@@ -39,21 +39,23 @@
 	
 	for(i=0;i<cstlist->n;i++){
 		if(cstlist->cluster[i]==NULL){
+			if(i>=n) break;
+			//printf("Have to do compacting n=%d\n ",n);
 			do{
 				n--;
 			} while(cstlist->cluster[n]==NULL && n>i);
-			if(i<=n) break;
 			cstlist->cluster[i]=cstlist->cluster[n];
 			cstlist->cluster[n]=NULL;
+			//printf("After compacting n=%d\n",n);
 		}
 	}
 	cstlist->cluster=(ts_cluster **)realloc(cstlist->cluster,n*sizeof(ts_cluster *));
-
+	cstlist->n=n;
 	return TS_SUCCESS;
 }
 
 
-ts_bool cluster_join(ts_cluster *cluster1, ts_cluster *cluster2){
+ts_bool cluster_join(ts_cluster_list *cstlist, ts_cluster *cluster1, ts_cluster *cluster2){
 	ts_cluster *master_cluster,*slave_cluster;
 	ts_uint i;
 	if(cluster1->idx<cluster2->idx){
@@ -66,8 +68,13 @@
 	for(i=0;i<slave_cluster->nvtx;i++){
 		cluster_add_vertex(master_cluster,slave_cluster->vtx[i]);
 	}
-	cluster_free(slave_cluster);
-	slave_cluster=NULL;
+	//find cluster in the list and free the location of the cluster
+	for(i=0;i<cstlist->n;i++){
+		if(cstlist->cluster[i]==slave_cluster){
+			cluster_free(slave_cluster);
+			cstlist->cluster[i]=NULL;
+		}
+	}
 	return TS_SUCCESS;
 }
 
@@ -95,8 +102,12 @@
 
 /* This is a stub function. User should check whether the vertex is clustering or not. */
 ts_bool is_clusterable(ts_vertex *vtx){
-
-return 1;
+	if(fabs(vtx->c)>1e-6){
+		return 1;
+	}
+	else {
+		return 0;
+	}
 }
 
 
@@ -109,13 +120,13 @@
 	return NULL;
 }
 
-ts_bool cluster_vertex_neighbor_check(ts_vertex *vtx){
+ts_bool cluster_vertex_neighbor_check(ts_cluster_list *cstlist, ts_vertex *vtx){
 
 	int j;
 	for(j=0;j<vtx->neigh_no;j++){
 		if(vtx->neigh[j]->cluster!=NULL){
 			if(vtx->neigh[j]->cluster!=vtx->cluster){
-				cluster_join(vtx->cluster, vtx->neigh[j]->cluster);
+				cluster_join(cstlist, vtx->cluster, vtx->neigh[j]->cluster);
 			}
 		}
 	}
@@ -132,7 +143,7 @@
 	//for each vertex
 		vtx=vesicle->vlist->vtx[i];
 		if(is_clusterable(vtx)){
-			if(vtx->cluster!=NULL){
+			if(vtx->cluster==NULL){
 				//find first neigbor with cluster index
 				cst=cluster_vertex_neighbor(vtx);
 				if(cst==NULL){
@@ -142,7 +153,7 @@
 				} else {
 					//we are added to the first cluster found
 					cluster_add_vertex(cst,vtx);
-					cluster_vertex_neighbor_check(vtx);
+					cluster_vertex_neighbor_check(cstlist, vtx);
 					cluster_list_compact(cstlist);
 				}
 

--
Gitblit v1.9.3