/* SCCS - @(#)NslStoringVariables.java 1.7 - 09/01/99 - 00:15:47 */ //-------------------------------------- // $Log: NslStoringVariables.java,v $ // Revision 1.1 1998/02/02 23:47:11 erhan // NslStoringVariables // // Revision 1.4 1997/05/09 22:30:23 danjie // add some comments and Log // //-------------------------------------- /* *Copyright(c)1997 USC Brain Project. email nsl@java.usc.edu */ package nslj.src.display; import java.lang.*; import java.awt.*; import java.util.*; import java.io.*; import nslj.src.lang.*; import nslj.src.system.*; import java.lang.Math; public class NslStoringVariables extends NslCanvas { private Vector2 priv=new Vector2(); public void NslStoringVariables() { end = new Vector2(); indexControlPoints = 0; } public Vector2 V2SubII(Vector2 a, Vector2 b) { Vector2 c=new Vector2(); c.xset( a.xval() - b.xval() ); c.yset( a.yval() - b.yval() ); return(c); } /* * ComputeLeftTangent, ComputeRightTangent, ComputeCenterTangent : *Approximate unit tangents at endpoints and "center" of digitized curve */ public Vector2 ComputeLeftTangent(Vector2 [] d, int end) /* d; Digitized points*/ /* end; Index to "left" end of region */ { Vector2 tHat1; tHat1 = V2SubII(d[end+1], d[end]); //tHat1 = V2Normalize(tHat1); //System.out.println("end "+end+" d[end+1] "+d[end+1].xval()+" "+d[end+1].yval()+" d[end] "+d[end].xval()+" "+d[end].yval()+" IN LEFT TANGENT:"+tHat1.xval()+" "+tHat1.yval() ); tHat1.V2Normalize(); //--System.out.println("IN LEFT TANGENT:"+tHat1.xval()+" "+tHat1.yval() ); return tHat1; } public Vector2 ComputeRightTangent(Vector2 [] d, int end) /* d; Digitized points */ /* end; Index to "right" end of region */ { Vector2 tHat2; tHat2 = V2SubII(d[end-1], d[end]); //tHat2 = V2Normalize(tHat2); tHat2.V2Normalize(); //--System.out.println("IN RIGHT TANGENT:"+tHat2.xval()+" "+tHat2.yval() ); return tHat2; } public Vector2 ComputeCenterTangent(Vector2 [] d, int center) /* d; Digitized points */ /*center; Index to point inside region */ { Vector2 V1, V2, tHatCenter; V1 = V2SubII(d[center-1], d[center]); V2 = V2SubII(d[center], d[center+1]); tHatCenter = new Vector2(); tHatCenter.xset ( (V1.xval() + V2.xval() )/2.0); tHatCenter.yset ( (V1.yval() + V2.yval() )/2.0); tHatCenter.V2Normalize(); return tHatCenter; } /* * ChordLengthParameterize : * Assign parameter values to digitized points * using relative distances between points. */ public double [] ChordLengthParameterize(Vector2 [] d, int first, int last) /* *d; Array of digitized points */ /* first, last; Indices defining region */ { int i; double [] u; /* Parameterization */ //--System.out.println("ENTERING ChordLengthParametrize"); u = new double [last-first+1]; /* u = (double *)malloc((unsigned)(last-first+1) * sizeof(double)); */ u[0] = 0.0; for (i = first+1; i <= last; i++) { u[i-first] = u[i-first-1] + V2DistanceBetween2Points(d[i], d[i-1]); } for (i = first + 1; i <= last; i++) { u[i-first] = u[i-first] / u[last-first]; //--System.out.print(" "+u[i-first]); } //--System.out.println("\nEXITING ChordLengthParametrize"); return(u); } /* * ComputeMaxError : * Find the maximum squared distance of digitized points * to fitted curve. */ public double ComputeMaxError(Vector2 [] d, int first, int last, Vector2 [] bezCurve, double [] u, int [] splitPoint) /* d; Array of digitized points */ /* first, last; Indices defining region */ /* BezierCurve bezCurve Fitted Bezier curve */ /* u; Parameterization of points */ /* splitPoint; Point of maximum error */ { int i; double maxDist; /* Maximum error */ double dist; /* Current error */ Vector2 P; /* Point on curve */ Vector2 v; /* Vector from point to curve */ //System.out.println("ENTERING MAX ERROR"+splitPoint[0]); splitPoint[0] = (last - first + 1)/2; /* *splitPoint = (last - first + 1)/2; */ maxDist = 0.0; for (i = first + 1; i < last; i++) { P = BezierII(3, bezCurve, u[i-first]); v = V2SubII(P, d[i]); dist = v.V2SquaredLength(); /* dist = V2SquaredLength(&v); */ if (dist >= maxDist) { maxDist = dist; /* *splitPoint = i; */ splitPoint[0] = i; } } //System.out.println("EXITING MAX ERROR"+splitPoint[0]); //--System.out.println("splitPoint "+splitPoint[0]+" and maxDist "+maxDist); return (maxDist); } /* * Reparameterize: * Given set of points and their parameterization, try to find * a better parameterization. * */ public double [] Reparameterize(Vector2 [] d, int first, int last, double [] u, Vector2 [] bezCurve) /* d; Array of digitized points */ /* first, last; Indices defining region */ /* u; Current parameter values */ /* bezCurve; Current fitted curve */ { int nPts = last-first+1; int i; double [] uPrime = new double [nPts]; /* New parameter values */ /* uPrime = (double *)malloc(nPts * sizeof(double)); */ for (i = first; i <= last; i++) { uPrime[i-first] = NewtonRaphsonRootFind(bezCurve, d[i], u[i-first]); //-- System.out.print(" "+uPrime[i-first]); } //-- System.out.println(); return (uPrime); } /* * NewtonRaphsonRootFind : * Use Newton-Raphson iteration to find better root. */ public double NewtonRaphsonRootFind(Vector2 [] Q, Vector2 P, double u) /* Q; Current fitted curve */ /* P; Digitized point */ /* u; Parameter value for "P" */ { double numerator, denominator; Vector2 [] Q1 = new Vector2[3]; Vector2 [] Q2= new Vector2[2]; /* Q' and Q'' */ Vector2 Q_u, Q1_u, Q2_u; /*u evaluated at Q, Q', & Q'' */ double uPrime; /* Improved u */ int i; /* Compute Q(u) */ Q_u = BezierII(3, Q, u); /* Generate control vertices for Q' */ for (i = 0; i <= 2; i++) { double x1 = Q[i+1].xval() - Q[i].xval(); double y1 = Q[i+1].yval() - Q[i].yval(); Q1[i] = new Vector2(); Q1[i].xset (x1 * 3.0); Q1[i].yset (y1 * 3.0); } /* Generate control vertices for Q'' */ for (i = 0; i <= 1; i++) { double x1 = Q[i+1].xval() - Q[i].xval(); double y1 = Q[i+1].yval() - Q[i].yval(); Q2[i] = new Vector2(); Q2[i].xset ( x1 * 2.0); Q2[i].yset ( y1 * 2.0); } /* Compute Q'(u) and Q''(u) */ Q1_u = BezierII(2, Q1, u); Q2_u = BezierII(1, Q2, u); /* Compute f(u)/f'(u) */ numerator = (Q_u.xval() - P.xval()) * (Q1_u.xval()) + (Q_u.yval() - P.yval()) * (Q1_u.yval()); denominator = (Q1_u.xval()) * (Q1_u.xval()) + (Q1_u.yval()) * (Q1_u.yval()) + (Q_u.xval() - P.xval()) * (Q2_u.xval()) + (Q_u.yval() - P.yval()) * (Q2_u.yval()); /* u = u - f(u)/f'(u) */ uPrime = u - (numerator/denominator); return (uPrime); } /* * FitCurve : * Fit a Bezier curve to a set of digitized points */ void FitCurve(Vector2 []d, int nPts, double error, PrintWriter pw, DataOutputStream fs) /* *d; Array of digitized points */ /* nPts; Number of digitized points */ /* error; User-defined error squared */ /* FILE *fp; */ { Vector2 tHat1, tHat2; /* Unit tangent vectors at endpoints */ int ii, jj; //-- System.out.println("FITTING A CURVE!!!\n\n\n\n"); //-- for (ii=0;ii= MaxCtrlPts) { /* NslOutOfBoundsBezier ap = new NslOutOfBoundsBezier(); return; */ } if ((curve[n].xval() == end.xval()) && (curve[n].yval() == end.yval()) ) { // System.out.println("CtrlPt["+n+"] = ("+curve[n].xval()+","+curve[n].yval()); pw.println(curve[n].xval()+" "+curve[n].yval() ); // if (ControlPoints[ indexControlPoints ] == 0) ControlPoints[ indexControlPoints ] = new Vector2(); System.out.println("\n\n\nTotal_time "+total_time+" Time Interval "+time_interval +" xset "+(total_time - time_interval +1 + curve[i].xval() ) ); //ControlPoints[ indexControlPoints ].xset ( (total_time - (time_interval-1)) + curve[i].xval() ); ControlPoints[ indexControlPoints ].xset ( curve[i].xval() ); ControlPoints[ indexControlPoints ].yset ( curve[i].yval() ); } } /* * Bezier (fitting curves): * Example of how to use the curve-fitting code. Given an array * of points and a tolerance (squared error between points and * fitted curve), the algorithm will generate a piecewise * cubic Bezier representation that approximates the points. * When a cubic is generated, the routine "DrawBezierCurve" * is called, which outputs the Bezier curve just created * (arguments are the degree and the control points, respectively). * */ public int times(int ti) { int num = -1; for (int ii=0;ii< ti; ii++) num = -1* num; return(num); } public void Bezier(NslVariable var, int drawing_time) { //Vector2 d[] = new Vector2 [ MaxCtrlPts ]; Vector2 d[] = new Vector2 [ drawing_time +1]; end = new Vector2(); time_interval = var.time_interval; total_time = var.total_time; System.out.println("Time_intervale "+ time_interval +" total_time "+total_time); //System.out.println("XX "+ var.x_dimension +" YY "+var.y_dimension); for (int ii= 0; ii<= var.info.getDimension(0)-1; ii++) for (int jj= 0; jj<= var.info.getDimension(1)-1; jj++) { for (int kk=0;kk<= drawing_time ;kk++){ d[kk]= new Vector2(); //d[kk].xset( (total_time - (time_interval -1)) + kk ); d[kk].xset( (total_time - (time_interval )) + kk ); d[kk].yset( var.data[ii][jj][kk] ); /* if (kk==( MaxCtrlPts -1)) { end.xset( d[kk].xval() ); end.yset( d[kk].yval() ); } */ } ControlPoints = new Vector2 [ drawing_time ]; indexControlPoints = 0; Integer int1 = new Integer( ii ); Integer int2 = new Integer( jj ); Integer int3 = new Integer( var.total_time - 1 ); try { DataOutputStream fs = new DataOutputStream(new FileOutputStream("data/"+var.info.nslGetName()+"_bezier_byte"+int1.toString()+"_"+int2.toString()+"_"+int3.toString())) ; PrintWriter pw = new PrintWriter ( new FileOutputStream("data/"+var.info.nslGetName()+"_bezier_"+int1.toString()+"_"+int2.toString()+"_"+int3.toString()), true ); double error = 0.1; /* Squared error */ int pts, i, j,k ; float x,y,z; pts= drawing_time ; // System.out.print("Fitting curves at time "+drawing_time+"\n\n\n"); FitCurve(d,pts, error, pw, fs); /* Fit the Bezier curves */ //fs.writeDouble((double)(drawing_time-1)); System.out.print("Writing the last element at "+(total_time-1)+"\n\n\n"); System.out.print("Writing the value "+(total_time-1)+"\n\n\n"); pw.println((total_time-1)+" "+var.data[ii][jj][drawing_time-1] ); fs.writeDouble((double)(total_time-1)); fs.writeDouble((double)(var.data[ii][jj][drawing_time-1])); fs.close(); /* write the number of control points */ fs = new DataOutputStream(new FileOutputStream("data/"+var.info.nslGetName()+"_bezierContrlPoints_"+int1.toString()+"_"+int2.toString())) ; fs.writeInt((indexControlPoints+1)); fs.close(); } catch(IOException e) { } } } /* StoringHistory (stores all the values of the data) */ public void StoringHistory(NslVariable var, int drawing_time) { total_time = var.total_time; time_interval = var.time_interval; for (int ii= 0; ii<= var.info.getDimension(0) - 1; ii++) for (int jj= 0; jj<= var.info.getDimension(1) - 1; jj++) { Integer int1 = new Integer( ii ); Integer int2 = new Integer( jj ); Integer int3 = new Integer( var.total_time - 1 ); try { DataOutputStream fs = new DataOutputStream(new FileOutputStream("data/"+var.info.nslGetName()+"_bytes_totalhistory_"+int1.toString()+"_"+int2.toString() )); fs.writeInt(drawing_time); fs.writeInt(total_time); PrintWriter pw = new PrintWriter ( new FileOutputStream("data/"+var.info.nslGetName()+"_totalhistory_"+int1.toString()+"_"+int2.toString() ), true ); fs.close(); pw.close(); fs = new DataOutputStream(new FileOutputStream("data/"+var.info.nslGetName()+"_bytes_totalhistory_"+int1.toString()+"_"+int2.toString()+"_"+int3.toString() )); pw = new PrintWriter ( new FileOutputStream("data/"+var.info.nslGetName()+"_totalhistory_"+int1.toString()+"_"+int2.toString()+"_"+int3.toString() ), true ); //ps.println(drawing_time+" "+total_time); pw.println(drawing_time); System.out.println("Storing: DRAWING TIME "+drawing_time+" TOTAL TIME "+total_time); fs.writeInt(drawing_time); // number of pts to be stored //fs.writeInt(total_time); // total history that should be expected for (int kk=0;kk<= drawing_time ;kk++){ pw.println(((total_time-time_interval)+kk)+" "+var.data[ii][jj][kk] ); fs.writeDouble((double)((total_time - time_interval) + kk)); // fs.writeDouble((double)kk); fs.writeDouble( (double)(var.data[ii][jj][kk]) ); //System.out.println(" Total time "+total_time+" Time interval "+time_interval+" "+ ((total_time-time_interval)+kk)+" "+ var.data[ii][jj][kk]); } fs.close(); } catch(IOException e) { } } } private float y_max=100, y_min=0; private int data_x_size, data_y_size, x_dimension, y_dimension; private int last_data_pos, draw_time; private float [][][] data; private float []xdata; private float []ydata; private float [][][] tempox; private float [][][] tempoy; private Color boxColor=Color.black; private int VCount; private Vector2 end; public static int dummyv; public static float []xy; public static Vector Xvariable, Yvariable; public static NslFrame frame; public static String xname; public static String yname; public Vector2 [] ControlPoints; public int indexControlPoints; public int MaxCtrlPts = 150 ; // for testing purposes public int total_time = -1; // for testing purposes public int time_interval = -1 ; // for testing purposes }