From 608cbe1dd70feed73f702459c52876bf62f276b7 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Mon, 06 Jul 2020 07:44:11 +0000
Subject: [PATCH] Triangles are in right order.

---
 src/energy.c |   64 ++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/src/energy.c b/src/energy.c
index 5fe0d5b..1f2bd1c 100644
--- a/src/energy.c
+++ b/src/energy.c
@@ -88,7 +88,7 @@
  * @returns TS_SUCCESS on successful calculation.
 */
 inline ts_bool energy_vertex(ts_vertex *vtx){
-    ts_uint jj, i, j, cnt=0;
+    ts_uint jj, i, j;
     ts_double edge_vector_x[7]={0,0,0,0,0,0,0};
     ts_double edge_vector_y[7]={0,0,0,0,0,0,0};
     ts_double edge_vector_z[7]={0,0,0,0,0,0,0};
@@ -101,8 +101,11 @@
     ts_double vertex_normal_x=0.0;
     ts_double vertex_normal_y=0.0;
     ts_double vertex_normal_z=0.0;
-    ts_triangle *triedge[2]={NULL,NULL};
+//    ts_triangle *triedge[2]={NULL,NULL};
 
+    ts_uint nei,neip,neim;
+    ts_vertex *it, *k, *kp,*km;
+    ts_triangle *lm=NULL, *lp=NULL;
     ts_double sumnorm;
 
     // Here edge vector is calculated
@@ -111,6 +114,51 @@
 	edge_vector_x[jj]=vtx->neigh[jj]->x-vtx->x;
 	edge_vector_y[jj]=vtx->neigh[jj]->y-vtx->y;
 	edge_vector_z[jj]=vtx->neigh[jj]->z-vtx->z;
+
+
+	it=vtx;
+	k=vtx->neigh[jj];
+	nei=0;
+    	for(i=0;i<it->neigh_no;i++){ // Finds the nn of it, that is k 
+        	if(it->neigh[i]==k){
+            	nei=i;
+            	break;
+        	}
+    	}
+    	neip=nei+1;  // I don't like it.. Smells like I must have it in correct order
+    	neim=nei-1;
+    	if(neip>=it->neigh_no) neip=0;
+    	if((ts_int)neim<0) neim=it->neigh_no-1; /* casting is essential... If not
+there the neim is never <0 !!! */
+  //  fprintf(stderr,"The numbers are: %u %u\n",neip, neim);
+    	km=it->neigh[neim];  // We located km and kp
+    	kp=it->neigh[neip];
+
+    	if(km==NULL || kp==NULL){
+        	fatal("In bondflip, cannot determine km and kp!",999);
+    	}
+
+   for(i=0;i<it->tristar_no;i++){
+        for(j=0;j<k->tristar_no;j++){
+            if((it->tristar[i] == k->tristar[j])){ //ce gre za skupen trikotnik
+                if((it->tristar[i]->vertex[0] == km || it->tristar[i]->vertex[1]
+== km || it->tristar[i]->vertex[2]== km )){
+                lm=it->tristar[i];
+         //       lmidx=i;
+                }
+                else
+                {
+                lp=it->tristar[i];
+         //       lpidx=i;
+                }
+
+            }
+        }
+    }
+if(lm==NULL || lp==NULL) fatal("ts_flip_bond: Cannot find triangles lm and lp!",999);
+
+
+/*
 	// We find lm and lp from k->tristar !
 	cnt=0;
     	for(i=0;i<vtx->tristar_no;i++){
@@ -122,18 +170,20 @@
         	}
     	}
 	if(cnt!=2) fatal("ts_energy_vertex: both triangles not found!", 133);
-	sumnorm=sqrt( pow((triedge[0]->xnorm + triedge[1]->xnorm),2) + pow((triedge[0]->ynorm + triedge[1]->ynorm), 2) + pow((triedge[0]->znorm + triedge[1]->znorm), 2));
+*/
 
-	edge_normal_x[jj]=(triedge[0]->xnorm+ triedge[1]->xnorm)/sumnorm;
-	edge_normal_y[jj]=(triedge[0]->ynorm+ triedge[1]->ynorm)/sumnorm;
-	edge_normal_z[jj]=(triedge[0]->znorm+ triedge[1]->znorm)/sumnorm;
+	sumnorm=sqrt( pow((lm->xnorm + lp->xnorm),2) + pow((lm->ynorm + lp->ynorm), 2) + pow((lm->znorm + lp->znorm), 2));
+
+	edge_normal_x[jj]=(lm->xnorm+ lp->xnorm)/sumnorm;
+	edge_normal_y[jj]=(lm->ynorm+ lp->ynorm)/sumnorm;
+	edge_normal_z[jj]=(lm->znorm+ lp->znorm)/sumnorm;
 
 
 	edge_binormal_x[jj]=(edge_normal_y[jj]*edge_vector_z[jj])-(edge_normal_z[jj]*edge_vector_y[jj]);
 	edge_binormal_y[jj]=-(edge_normal_x[jj]*edge_vector_z[jj])+(edge_normal_z[jj]*edge_vector_x[jj]);
 	edge_binormal_z[jj]=(edge_normal_x[jj]*edge_vector_y[jj])-(edge_normal_y[jj]*edge_vector_x[jj]);
 
-	printf("(%f %f %f); (%f %f %f); (%f %f %f), %d\n", edge_vector_x[jj], edge_vector_y[jj], edge_vector_z[jj], edge_normal_x[jj], edge_normal_y[jj], edge_normal_z[jj], edge_binormal_x[jj], edge_binormal_y[jj], edge_binormal_z[jj],triedge[0]->idx);
+	printf("(%f %f %f); (%f %f %f); (%f %f %f)\n", edge_vector_x[jj], edge_vector_y[jj], edge_vector_z[jj], edge_normal_x[jj], edge_normal_y[jj], edge_normal_z[jj], edge_binormal_x[jj], edge_binormal_y[jj], edge_binormal_z[jj]);
 
     }
 	for(i=0; i<vtx->tristar_no; i++){

--
Gitblit v1.9.3