From 298c6ee66791adc5948b9c9c7f03198437117f87 Mon Sep 17 00:00:00 2001
From: "N.N." <nimon@users.sourceforge.net>
Date: Fri, 10 Sep 2010 09:58:09 +0000
Subject: svn path=/trunk/externals/nusmuk/; revision=14035

---
 msd/src/msd.h | 110 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 58 insertions(+), 52 deletions(-)

(limited to 'msd/src')

diff --git a/msd/src/msd.h b/msd/src/msd.h
index b1912a9..4555987 100644
--- a/msd/src/msd.h
+++ b/msd/src/msd.h
@@ -173,19 +173,21 @@ public:
 	t_float longueur, long_min, long_max;
 	t_float distance_old;
 	t_float puissance;
-	t_int link_type; //0 : no, 1 : tangential, 2 : normal
+	t_int link_type; //0 : no, 1 : tangential, 2 : normal, 3 : table
 	t_float tdirection1[N], tdirection2[N];
 	const t_symbol *k_tabname, *d_tabname;
 	flext::buffer *k_tab, *d_tab;
 	t_float l_tab;
+	t_int buffer_tested;
 	
 	Link(t_int n,const t_symbol *id,Mass<N> *m1,Mass<N> *m2,t_float k1,t_float d1, t_int o=0, t_float tangent[N]=NULL,t_float pow=1, t_float lmin = 0,t_float lmax = 1e10,const t_symbol *ktab=NULL,const t_symbol *dtab=NULL, t_float ltab=1)
-		: nbr(n),Id(id)
-		, mass1(m1),mass2(m2)
-		, K1(k1),D1(d1),D2(0),link_type(o),puissance(pow)
-		, long_min(lmin),long_max(lmax)
-		, k_tabname(ktab), d_tabname(dtab), l_tab(ltab)
-		, k_tab(NULL),d_tab(NULL)
+	: nbr(n),Id(id)
+	, mass1(m1),mass2(m2)
+	, K1(k1),D1(d1),D2(0),link_type(o),puissance(pow)
+	, long_min(lmin),long_max(lmax)
+	, k_tabname(ktab), d_tabname(dtab), l_tab(ltab)
+	, k_tab(NULL),d_tab(NULL)
+	, buffer_tested(1)
 	{
 		for (int i=0; i<N; i++)	{
 			tdirection1[i] = 0;
@@ -195,12 +197,11 @@ public:
 			distance_old = longueur = Mass<N>::dist(*mass1,*mass2); // L[n-1]
 		else if (link_type == 1)	{			// TANGENTIAL LINK
 			t_float norme = 0;
-			for(int i = 0; i < N; ++i) norme += sqr(tangent[i]);
+			for(int i = 0; i < N; ++i) 
+				norme += sqr(tangent[i]);
 			norme = sqrt(norme);
-			//t_float norme = sqrt(sqr(xa)+sqr(ya)+sqr(za));
-			for(int i = 0; i < N; ++i)tdirection1[i] = tangent[i]/norme;
-			//tdirection1[1] = ya/norme;
-			//tdirection1[2] = za/norme;
+			for(int i = 0; i < N; ++i)
+				tdirection1[i] = tangent[i]/norme;
 			distance_old = 0;
 			for(int i = 0; i < N; ++i)	
 				distance_old += sqr((m1->pos[i]-m2->pos[i])*tdirection1[i]);
@@ -213,58 +214,66 @@ public:
 				if(k_tab)
 					delete k_tab;
 				k_tab = new flext::buffer(k_tabname);
-				if(!k_tab->Ok())
-   					post("warning: buffer is currently not valid!");  
+				if(!k_tab->Ok()) {
+   					post("error : buffer %s is currently not valid!", *k_tabname); 
+   					buffer_tested *= 0;
+				}
+   				buffer_tested *= buffer_test(k_tab);	
 			}
 			if (d_tabname) {
 				if(d_tab)
 					delete d_tab;
 				d_tab = new flext::buffer(d_tabname);
-				if(!d_tab->Ok())
-   					post("warning: buffer is currently not valid!");  
-			}
-		}
-		/*else if (link_type == 2)	{			// NORMAL LINK 2D
-			if (N >= 2)	{
-				const t_float norme = sqrt(sqr(xa)+sqr(ya));
-				tdirection1[0]=ya/norme;
-				tdirection1[1]=xa/norme;
-				distance_old = 0;
-				for(int i = 0; i < N; ++i)	
-					distance_old += sqr((m1->pos[i]-m2->pos[i])*tdirection1[i]);
-				if (N == 3)	{			// NORMAL LINK 3D
-					if (xa == 0 && ya==0 && za!= 0)	{	// Special case
-						tdirection1[0]=1;
-						tdirection1[1]=0;
-						tdirection1[2]=0;
-						tdirection2[0]=0;
-						tdirection2[1]=1;
-						tdirection2[2]=0;
-					}
-					else	{				// Normal case
-						const t_float norme2 = sqrt(sqr(xa*ya +za*xa)+sqr(xa*ya+za*ya)+sqr(sqr(xa)+sqr(ya)));
-						tdirection2[0] = (xa*za+xa*ya)/norme2;
-						tdirection2[1] = (xa*ya+za*ya)/norme2;
-						tdirection2[2] = (sqr(xa)+sqr(ya))/norme2;
-					}
-					distance_old = 0;
-					for(int i = 0; i < N; ++i)	
-						distance_old += sqr((m1->pos[i]-m2->pos[i])*(tdirection1[i]+tdirection2[i]));
+				if(!d_tab->Ok()) { 
+   					post("error : buffer %s is currently not valid!", *d_tabname);
+   					buffer_tested *= 0;
 				}
-				distance_old  = sqrt(distance_old);
-				longueur = distance_old;			
+   				buffer_tested *= buffer_test(d_tab);	
 			}
-		}*/	
+		}
 		mass1->links.insert(this);
 		mass2->links.insert(this);
 	}
 	
 	~Link()
 	{
+		if(k_tab) {
+			delete k_tab;
+			k_tab = NULL;
+			k_tabname = NULL; 
+		}
+		if(d_tab) {
+			delete d_tab;
+			d_tab = NULL;
+			d_tabname = NULL; 
+		}
 		mass1->links.erase(this);
 		mass2->links.erase(this);
 	}
 	
+	inline t_int buffer_test(flext::buffer *buf) {
+		if(!buf || !buf->Valid()) { 
+  			post("no valid buffer defined"); 
+  			// return zero length 
+  			return 0; 
+ 		}  
+ 		else { 
+  			if(buf->Update()) { 
+   			// buffer parameters have been updated 
+   				if(buf->Valid()) { 
+    				post("updated buffer reference"); 
+    				return 1; 
+   				} 
+   				else { 
+    				post("buffer has become invalid"); 
+    				return 0; 
+   				}
+   			}  
+  			else 
+   				return 1;
+ 		}	   
+ 	} 
+
 	inline t_float interp_buf(t_float indexf, flext::buffer *buf, t_float factor) {
 		t_float size_buf=buf->Frames();
 		t_float index_factor = indexf*(size_buf-1)/factor;
@@ -303,14 +312,11 @@ public:
 				distance = Mass<N>::dist(*m1,*m2); 
 					
 			if (distance < long_min || distance > long_max || distance == 0) {
-//			for(int i = 0; i < N; ++i) {
-	//			m1->force[i] -= D2 * m1->speed[i]; 	//  Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
-	//			m2->force[i] += D2 * m2->speed[i]; 	// Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
-	//		}
+			// pas de forces
 			}
 			else {	// Lmin < L < Lmax
 				// F[n] = k1 (L[n] - L[0])/L[n] + D1 (L[n] - L[n-1])/L[n]
-				if (link_type == 3) { // tabLink
+				if (link_type == 3 && buffer_tested) { // tabLink
 					t_float k_temp = distance-longueur;
 					if (k_tabname) {
 						k_temp = interp_buf(distance,k_tab,l_tab);
-- 
cgit v1.2.1