From 719c9febac2eaff9483fda487b57684afbb59bb2 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Wed, 05 Mar 2014 16:47:50 +0000
Subject: [PATCH] dont know

---
 src/bond.c |  101 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/src/bond.c b/src/bond.c
index 492ad24..7e98110 100644
--- a/src/bond.c
+++ b/src/bond.c
@@ -1,24 +1,107 @@
 #include<stdlib.h>
 #include "general.h"
-#include<stdio.h>
+#include "vertex.h"
 
-ts_bool init_bond_list(ts_bond_list *blist){
+ts_bond_list *init_bond_list(){
+    ts_bond_list *blist=(ts_bond_list *)malloc(sizeof(ts_bond_list));
 	blist->n=0;
 	blist->bond=NULL;
-	return TS_SUCCESS;
+	return blist;
 }
 
-ts_bool bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
+ts_bond  *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
+    ts_uint i;
+    /* no vertices must be null! */
+    if(vtx1==NULL || vtx2==NULL) return NULL;
+    /* Verify if the bond already exists... Don't do multiple bonds */
+    for(i=0;i<blist->n;i++){
+       if((blist->bond[i]->vtx1==vtx1 || blist->bond[i]->vtx1==vtx2)
+        &&(blist->bond[i]->vtx2==vtx1 || blist->bond[i]->vtx2==vtx2))
+        return blist->bond[i];
+    }
 	blist->n++;
-	blist->bond=realloc(blist->bond,blist->n*sizeof(ts_bond));
-	if(blist->bond==NULL) fatal("Cannot reallocate memory for additional *ts_bond.",5);
-	//NOW insert vertices!	
-	blist->bond[blist->n - 1].vtx1=vtx1;	
-	blist->bond[blist->n - 1].vtx2=vtx2;	
+	blist->bond=(ts_bond **)realloc(blist->bond,blist->n*sizeof(ts_bond *));
+	if(blist->bond==NULL) fatal("Cannot reallocate memory for additional **ts_bond.",100);
+    blist->bond[blist->n-1]=(ts_bond *)malloc(sizeof(ts_bond));
+    if(blist->bond[blist->n-1]==NULL) fatal("Cannot allocate memory for additional *ts_bond.",100);
+    
+	//NOW insert vertices into data!	
+	blist->bond[blist->n - 1]->vtx1=vtx1;	
+	blist->bond[blist->n - 1]->vtx2=vtx2;
+	blist->bond[blist->n - 1]->tainted=0;
+    //Should we calculate bond length NOW?
+
+
+	
+	
+	return blist->bond[blist->n-1];
+}
+
+
+/* Two triangles are assigned to the bond. Bond must already be initialized. */
+ts_bool bond_assign_triangles(ts_bond *bond){
+	ts_vertex *vtx1=bond->vtx1, *vtx2=bond->vtx2;
+	ts_uint i,j,k;
+	ts_uint i1=0,i2=0,i3=0,im=0;
+	ts_triangle *ttria;
+	for(i=0,k=0;i<vtx1->tristar_no;i++){
+		for(j=0;j<3;j++){
+			if(vtx1->tristar[i]->vertex[j]==vtx2){
+				//triangle found;
+				bond->tria[k]=vtx1->tristar[i];
+				k++;
+			}
+		}
+	}
+	if(k<2) return TS_FAIL;
+
+	//We will sort the triangles so, they are oriented as km and kp for bondflip
+	for(i=0;i<3;i++){
+		if(bond->tria[0]->vertex[i]==vtx1){
+			i1=i;
+		}
+		else if(bond->tria[0]->vertex[i]==vtx2){
+			i2=i;
+		}
+		else i3=i;
+	}
+
+	if(i1>i2 || (i1==0 && i2==2)){
+		//pozitivna orientacija!
+		ttria=bond->tria[0];
+		bond->tria[0]=bond->tria[1];
+		bond->tria[1]=ttria;	
+		bond->adjvtx[1]=bond->tria[0]->vertex[i3];
+		im=0;
+	}
+	else{
+		bond->adjvtx[0]=bond->tria[0]->vertex[i3];
+		im=1;
+	}
+	
+		for(i=0;i<3;i++){
+		if(bond->tria[1]->vertex[i]==vtx1){
+			i1=i;
+		}
+		else if(bond->tria[1]->vertex[i]==vtx2){
+			i2=i;
+		}
+		else i3=i;
+	}
+		bond->adjvtx[im]=bond->tria[1]->vertex[i3];
+#ifdef DEBUG
+	if(bond->adjvtx[0]==NULL) fatal("AdjVTX[0] is NULL",1);
+	if(bond->adjvtx[1]==NULL) fatal("AdjVTX[1] is NULL",1);
+#endif
 	return TS_SUCCESS;
 }
 
 ts_bool bond_list_free(ts_bond_list *blist){
+    ts_uint i;
+    for(i=0;i<blist->n;i++){
+    free(blist->bond[i]);
+    }
     free(blist->bond);
+    free(blist);
     return TS_SUCCESS;
 }

--
Gitblit v1.9.3