VisClient/org/hfbk/vis/visnode/VisRoute.java

Go to the documentation of this file.
00001 /*
00002  * Created on 25.04.2005
00003  *
00004  */
00005 package org.hfbk.vis.visnode;
00006 
00007 import java.io.BufferedReader;
00008 import java.io.File;
00009 import java.io.FileNotFoundException;
00010 import java.io.FileReader;
00011 import java.text.DateFormat;
00012 import java.text.SimpleDateFormat;
00013 import java.util.ArrayList;
00014 import java.util.Date;
00015 import java.util.regex.Matcher;
00016 import java.util.regex.Pattern;
00017 
00018 import org.dronus.gl.GLBoxRenderer;
00019 import org.dronus.graph.Node;
00020 import org.lwjgl.opengl.GL11;
00021 import org.lwjgl.util.vector.Vector3f;
00022 
00038 public class VisRoute extends VisNode{
00039         
00040         static float centerx, centery;
00041         static int count;
00042         
00043         int dl;
00044         
00045         public LatLon[] pts;
00046 
00047         float mx,my,baseRadius;
00048 
00049         long time;
00050         int index;
00051         
00052         
00053         
00054         static public class LatLon {
00055                 
00056                 final double JUMP=50; //distance to consider node a jump. 
00057                 
00058                 long time;
00059                 double  x,y;
00060                 boolean skip;
00061 
00062                 final static double LAT_TOLERANCE=0.0000001;
00063                 
00064                 boolean isSkip(LatLon last){
00065                         
00066                         boolean jump=false;
00067                         
00068                         if(last!=null)
00069                                 jump=Math.abs(last.x-x)+Math.abs(last.y-y)>JUMP;
00070                         
00071                         return skip || jump;
00072                 }
00073         
00074                 LatLon(double lat, double lon, boolean skip, long time) {
00075                         this.time=time;
00076                         this.skip=skip;
00077                         x=lonToX(lon);  y=latToY(lat);
00078                 }
00079 
00080                 /*LatLon(String latLon){
00081                         y=latToY(new Double(latLon.split(" ")[0]));
00082                         x=lonToX(new Double(latLon.split(" ")[1]));             
00083                 }*/
00084                 
00085                 private static double lonToX(double l) {
00086                         return (0.02912711127225e+5d*l+5.24287987005829e+5d);
00087                 }
00088 
00089                 private static double latToY(double l) {
00090                         return  
00091                          -0.000000000000755295599727381190d             *l*l*l*l*l*l*l*l*l
00092                          +0.000000000052641300469669939483d             *l*l*l*l*l*l*l*l
00093                          +0.000000000075316265389785703085d             *l*l*l*l*l*l*l
00094                          -0.000000211768858757441037921488d             *l*l*l*l*l*l
00095                          -0.000007406891366988815666918405d             *l*l*l*l*l
00096                          +0.000195348752382833688867602717d             *l*l*l*l
00097                          -0.154872228716328858011408442508d             *l*l*l
00098                          -0.005034130049192665941337754276d             *l*l
00099                          -2910.198469731275963567895814776421d  *l
00100                          +524263.229158876347355544567108154297d;
00101                 }
00102                 
00103                 
00104 /*              public double getLon(){
00105                         return   ((x/tilesize)-5.24287987005829e+5d)/0.02912711127225e+5d;
00106                 }
00107                 
00108                 public double getLat(){
00109                         double ll=-90, rl=90, m=0;
00110                         int i=0;
00111                         while (rl-ll>LAT_TOLERANCE){
00112                                 i++;
00113                                 m=(ll+rl)/2;
00114                                 if (latToY(m)>y)
00115                                         ll=m;
00116                                 else 
00117                                         rl=m;                           
00118                         }
00119                         //System.out.println("getLat() needed "+i+" iterations.");
00120                         return m;
00121                 }
00122         
00123                 public String toString(){               
00124                         return String.format((Locale)null,"%f5 %f5",getLat(),getLon());
00125                 }
00126         */      
00127                 static double parseDMS(String d, String m, String s){
00128                         double v=Double.parseDouble(d);
00129                         v+=Double.parseDouble(m)/60;
00130                         v+=Double.parseDouble(s)/3600;
00131                         return v;
00132                 }
00133         }
00134 
00135         
00136         
00137         public VisRoute(Node node, Vector3f pos) {
00138                 super(null, pos);
00139                 
00140                 File f=new File(node.text);
00141                 ArrayList<LatLon> pl = new ArrayList<LatLon>();
00142                 
00143                 try {
00144                         BufferedReader s = new BufferedReader(new FileReader(f));
00145                         String l;
00146                         l = s.readLine();
00147                         double lat, lon;
00148                         long t=0;
00149                         if (l.charAt(0) == 'W') {// very new format...... :-(
00150                                 System.out.println("very new format");
00151                                 Pattern   timeMatcher=Pattern.compile("(\\d+/\\d+/\\d+ \\d+:\\d+:\\d+)"),
00152                                                 latLonMatcher=Pattern.compile("(\\d+?).(\\d+)'([\\d\\.]+)\"");                          
00153                                 while ((l = s.readLine()) != null) {
00154                                         Matcher m=timeMatcher.matcher(l);
00155                                         if (m.find()) {
00156                                                 DateFormat df=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
00157                                                 df.setLenient(true);
00158                                                 Date d=df.parse(m.group(1));
00159                                                 t=d.getTime();
00160                                         }
00161                                         m=latLonMatcher.matcher(l);
00162                                         if (m.find()){
00163                                                 lat=LatLon.parseDMS(m.group(1),m.group(2),m.group(3));
00164                                                 m.find();
00165                                                 lon=LatLon.parseDMS(m.group(1),m.group(2),m.group(3));
00166                                                 
00167                                                 pl.add(new LatLon(lat, lon, false, t));
00168                                         }
00169                                 }
00170                         } else {
00171                                 while ((l = s.readLine()) != null) {
00172                                         
00173                                         String[] args=l.split(",");
00174                                         
00175                                         
00176                                         if (args.length<2) continue;                                    
00177                                         if (!args[0].equals("t"))       continue;                                       
00178                                         if (!args[1].equals("d"))       throw new RuntimeException("wrong datum");
00179                                         
00180                                         lat = Double.parseDouble(args[2]);
00181                                         lon = Double.parseDouble(args[3]);
00182                                         // skip point if not connected
00183                                         boolean skip = Integer.parseInt(args[7]) == 1;
00184 
00185                                         pl.add(new LatLon(lat, lon, skip, t));
00186                                 }
00187                         }
00188                 } catch (Exception e) {
00189                         if (e instanceof FileNotFoundException)
00190                                 System.out.println("file not found:" + f.toString());
00191                         else
00192                                 System.out.println("not a valid track-file.");
00193                         e.printStackTrace();
00194                 }
00195                 pts = (LatLon[]) pl.toArray(new LatLon[0]);
00196         }
00197 
00198         
00199         @Override
00200         void transform() {
00201                 super.transform();
00202                 //GL11.glTranslatef(50,0,-50);
00203                 GL11.glRotatef(-51.5f, 0,1,0);
00204                 GL11.glScalef(100,100,100);             
00205         }
00206         
00207         void renderSelf() {
00208                 
00209                 if (dl==0){
00210                         dl=GL11.glGenLists(1);
00211                         GL11.glNewList(dl, GL11.GL_COMPILE);
00212                         int n=pts.length;
00213                         if (n>0){
00214                                 for (LatLon gk : pts){
00215                                         double x=gk.x,y=gk.y;
00216                                         mx+=x;  my+=y;
00217                                 }
00218                                 
00219                                 mx/=n;  my/=n;
00220 //                              update global lat/lon center
00221                                 count++;
00222                                 centerx=(centerx*(count-1)+mx)/count;
00223                                 centery=(centery*(count-1)+my)/count;
00224                                 
00225                                 for (LatLon gk : pts){
00226                                         double dx=gk.x-mx,dy=gk.y-my;
00227                                         float r=(float)Math.sqrt(dx*dx+dy*dy);
00228                                         baseRadius=Math.max(baseRadius, r);
00229                                 }
00230                         }
00231                         
00232                         // double s=1d/(1L<<LeinMap.earthlevel);
00233                         int i = 0;
00234                         LatLon last=null;
00235                         GL11.glColor3f(0,1,1);
00236                         GL11.glBegin(GL11.GL_LINE_STRIP);
00237                         if (pts.length > 0)
00238                                 for (LatLon gk : pts){
00239                                         if (i++ == 0 || gk.isSkip(last)){
00240                                                 GL11.glEnd();           
00241                                                 GL11.glBegin(GL11.GL_LINE_STRIP);
00242                                         }
00243                                         else
00244                                                 GL11.glVertex3d(gk.x-mx,0,gk.y-my);
00245                                         last=gk;
00246                                 }
00247                         GL11.glEnd();
00248                         GL11.glEndList();
00249                 }
00250                 
00251                 
00252                 
00253                 
00254                 
00255 //              calculate global position by moving our center relative to global center
00256                 float x=mx-centerx, y=my-centery;
00257                 radius=1000000;//baseRadius+(float)Math.sqrt(x*x+y*y);
00258                 GL11.glTranslatef(x, 0, y);
00259                 
00260                 GL11.glDisable(GL11.GL_TEXTURE_2D);
00261                 GL11.glCallList(dl);
00262 
00263 
00264                 if ((time==0)|(index>pts.length)) {
00265                         time=pts[0].time;
00266                         index=1;
00267                 }
00268 
00269                 time+=getRoot().client.dt*1000d;
00270                 
00271                 LatLon last=pts[index-1];
00272                 LatLon next=pts[index];
00273                 
00274                 long dt=time-last.time;
00275                 float p=dt/(float)(next.time-last.time);
00276                 
00277                 if (p>1){
00278                         index=index%(pts.length-2)+1;
00279                 }
00280                 x=(float)(last.x*(1-p)+next.x*p)-mx;
00281                 y=(float)(last.y*(1-p)+next.y*p)-my;
00282         
00283                 double dx=next.x-last.x;
00284                 double dy=next.y-last.y;
00285                 
00286                 GL11.glPushMatrix();
00287                 GL11.glTranslated(x,0,y);
00288                 float atan=(float)Math.atan(dx/dy);
00289                 if (dy<0) atan+=Math.PI;
00290                 GL11.glRotatef((float)(180*atan/Math.PI),0,1,0);                
00291                 GL11.glColor3f(1,0.5f,0);
00292                 GLBoxRenderer.renderBox(1,1,2);
00293                 GL11.glPopMatrix();
00294                 
00295         /*      if (children.size()>0){
00296                         VisNode n=children.get(0);
00297                         n.position.set(x+(mx-centerx),8,y+(my-centery));
00298                         
00299                         if (n instanceof VisVideo) {
00300                                 VisVideo v=(VisVideo)n;
00301                                 if (v.tex!=null){
00302                                         time=v.tex.currenttime+pts[0].time;                                     
00303                                 }
00304                         }                       
00305                 }
00306                 */
00307         }
00308 }

Generated on Tue Apr 7 17:57:22 2009 for visclient by  doxygen 1.5.1