Changeset 2372


Ignore:
Timestamp:
03/01/07 15:02:26 (13 years ago)
Author:
sorber
Message:

Provisional fix of indexing 0/1 bug for loading, saving overlays.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/overlays/OverlayIO.java

    r2284 r2372  
    7373 
    7474    // tracks addresses of stored freeforms 
    75     Vector loadedFreeforms = new Vector(); 
     75    Vector loadedNodedObjects = new Vector(); 
    7676    boolean nodesChanged = false; // used in event INIT, state NODES 
    77     int numberOfFreeformsRead = 0; 
    78     int numFreeformsRestored = 0; 
     77    int numberOfNodedObjectsRead = 0; 
     78    int numNodedObjectsRestored = 0; 
    7979 
    8080    // tracks line number for error messages 
     
    159159 
    160160        } else if (state == NODES) { 
    161           if (numberOfFreeformsRead == loadedFreeforms.size()) { 
    162             String s = "more Freeform node lists than Freeforms (" + numberOfFreeformsRead 
     161          if (numberOfNodedObjectsRead == loadedNodedObjects.size()) { 
     162            String s = "more \"Noded Object\" (Freeforms, Polylines) node" 
     163              + " lists" + "than Noded Objects (" + numberOfNodedObjectsRead 
    163164              + ") specified in table"; 
    164165            displayErrorMsg(owner, lineNum, s); 
     
    168169          // store nodes of previously read freeform 
    169170          if (nodesChanged) { 
    170             OverlayFreeform of = (OverlayFreeform) loadedFreeforms.elementAt(numFreeformsRestored++); 
     171            OverlayNodedObject ono = (OverlayNodedObject) 
     172              loadedNodedObjects.elementAt(numNodedObjectsRestored++); 
    171173            float[][] temp = new float[2][numNodes]; 
    172174            for (int i=0; i<2; i++) System.arraycopy(nodes[i], 0, temp[i], 0, numNodes); 
    173             of.setNodes(temp); 
     175            ono.setNodes(temp); 
    174176            nodes = new float[2][50]; 
    175177            numNodes = 0; 
    176178          } 
    177           numberOfFreeformsRead++; 
     179          numberOfNodedObjectsRead++; 
    178180          nodesChanged = true; 
    179181        } 
     
    197199            try { 
    198200              int p = Integer.parseInt(st.nextToken()); 
    199               if (p >= 0 && p < lengths[i]) { // is coordinate within range? 
    200                 pos[i] = p; 
     201              if (p > 0 && p <= lengths[i]) {  
     202                // Is coordinate within range? 
     203                // Remember, in the overlay.txt files dimensional position 
     204                // coordinates are indexed from 1; inside visbio,  
     205                // they're indexed from 0. 
     206                pos[i] = p-1; // shift from external to internal indexing  
    201207                tok++; 
    202208              } else { 
     
    260266          if (obj == null) continue; 
    261267 
    262           if (obj instanceof OverlayFreeform) loadedFreeforms.add(obj); 
     268          if (obj instanceof OverlayNodedObject) loadedNodedObjects.add(obj); 
    263269 
    264270          int r = MathUtil.positionToRaster(lengths, pos); 
     271          //System.out.print("["); // TEMP 
     272          //for (int i=0; i< pos.length; i++) System.out.print(i + " "); // TEMP 
     273          //System.out.println("]"); // TEMP 
     274          //System.out.println("r = " + r); // TEMP 
    265275          // this error should never fire--will be caught above ("is coordinate w/in range?") 
    266276          /* 
     
    309319          if (numNodes == nodes[0].length) { 
    310320            float[][] temp = new float[2][numNodes*2]; 
    311             for (int i=0; i<2; i++) System.arraycopy(nodes[i], 0, temp[i], 0, numNodes); 
     321            for (int i=0; i<2; i++)  
     322              System.arraycopy(nodes[i], 0, temp[i], 0, numNodes); 
    312323            nodes = temp; 
    313324          } 
     
    320331      displayErrorMsg(owner, lineNum, "no overlays found"); 
    321332      return null; 
    322     } else if (loadedFreeforms != null) { 
    323       if (numFreeformsRestored + 1 < loadedFreeforms.size()) { 
     333    }  
     334    else if (loadedNodedObjects.size() > 0) { 
     335      if (numNodedObjectsRestored + 1 < loadedNodedObjects.size()) { 
    324336        displayErrorMsg(owner, lineNum, "missing node lists for one or more Freeforms"); 
    325337        return null; 
    326       } else { 
     338      }  
     339      else { 
    327340        // store last freeform read 
    328         OverlayFreeform of = (OverlayFreeform) loadedFreeforms.elementAt(numFreeformsRestored++); 
     341        OverlayNodedObject ono = (OverlayNodedObject) 
     342          loadedNodedObjects.elementAt(numNodedObjectsRestored++); 
    329343        float[][] temp = new float[2][numNodes]; 
    330344        for (int i=0; i<2; i++) System.arraycopy(nodes[i], 0, temp[i], 0, numNodes); 
    331         of.setNodes(temp); 
     345        ono.setNodes(temp); 
    332346      } 
    333347    } 
     
    342356    int[] lengths = trans.getLengths(); 
    343357    Vector[] overlays = trans.overlays; 
    344     Vector savedFreeforms = new Vector(); 
     358    Vector savedNodedObjects = new Vector(); 
     359    int freeformCount = 0; 
     360    int polylineCount = 0; 
    345361 
    346362    // file header 
     
    360376      int[] pos = MathUtil.rasterToPosition(lengths, i); 
    361377      StringBuffer sb = new StringBuffer(); 
    362       for (int p=0; p<pos.length; p++) sb.append(pos[p] + "\t"); 
     378      // add 1 to shift indices for humans 
     379      for (int p=0; p<pos.length; p++) sb.append((pos[p]+1) + "\t"); 
    363380      String posString = sb.toString(); 
    364381      for (int j=0; j<overlays[i].size(); j++) { 
    365382        OverlayObject obj = (OverlayObject) overlays[i].elementAt(j); 
    366         if (obj instanceof OverlayFreeform) savedFreeforms.add(obj); 
     383        if (obj instanceof OverlayNodedObject) savedNodedObjects.add(obj); 
    367384        out.print(obj.toString()); 
    368385        out.print("\t"); 
     
    388405    } 
    389406 
    390     // nodes of freeforms, one node per line 
    391     for (int i=0; i<savedFreeforms.size(); i++) { 
    392       OverlayFreeform of = (OverlayFreeform) savedFreeforms.get(i); 
     407    // nodes of noded objects, one node per line 
     408    for (int i=0; i<savedNodedObjects.size(); i++) { 
     409      OverlayNodedObject ono = (OverlayNodedObject) savedNodedObjects.get(i); 
    393410      out.println(); 
    394411      float xx1, xx2, yy1, yy2; 
    395       xx1 = of.getX(); 
    396       yy1 = of.getY(); 
    397       xx2 = of.getX2(); 
    398       yy2 = of.getY2(); 
     412      xx1 = ono.getX(); 
     413      yy1 = ono.getY(); 
     414      xx2 = ono.getX2(); 
     415      yy2 = ono.getY2(); 
     416       
    399417      // nodes header 
    400       out.println("# Freeform line " + i + " (" + xx1 + "," + yy1 + ")(" + xx2 + "," + yy2 + ")"); 
     418      int k = 0; 
     419      if (ono instanceof OverlayFreeform) k = ++freeformCount; 
     420      else if (ono instanceof OverlayPolyline) k = ++polylineCount; 
     421 
     422      out.println("# " + ono + " " + k + " (" + xx1 + "," + yy1 + ")(" + xx2 + "," + yy2 + ")"); 
     423 
    401424      out.println("X\tY"); 
    402       float[][] nodes = of.getNodes(); 
    403       for (int j=0; j<nodes[0].length; j++) { 
    404         out.println(nodes[0][j]+"\t"+nodes[1][j]); 
     425      // print the nodes themselves 
     426      for (int j=0; j<ono.getNumNodes(); j++) { 
     427        float[] c = ono.getNodeCoords(j); 
     428        out.println(c[0]+"\t"+c[1]); 
    405429      } 
    406430    } 
     
    473497    // if/elseif/else clauses describe possible transitions from that state. 
    474498    // 
    475     // As a result of the state machine, I've managed to put most the error messages for 
     499    // As a result of using the state machine approach, I've managed to put most the error messages for 
    476500    // unexpected lines in one place (if (event == BARF) under loadOverlays); however 
    477501    // there are still many cases which generate errors elsewhere in loadOverlays. 
     
    481505    int state = WAIT, event = BARF; 
    482506    if (current == WAIT) { 
    483       if (input.matches("^\\s*$") || input.startsWith("#")) {state = WAIT; event = IGNORE;} 
    484       else if (input.startsWith("Overlay")) {state = TABLE; event = INIT;} 
    485       else {state = WAIT; event = BARF;} 
     507      if (input.matches("^\\s*$") || input.startsWith("#")) { 
     508        state = WAIT; event = IGNORE; 
     509      } 
     510      else if (input.startsWith("Overlay")) { 
     511        state = TABLE; event = INIT; 
     512      } 
     513      else { 
     514        state = WAIT; event = BARF; 
     515      } 
    486516    } else if (current == TABLE) { 
    487       if (input.equals("")) {state = TABLE; event = IGNORE;} 
    488       else if (input.matches("^\\s*#\\s*[Ff][Rr][Ee][Ee][Ff][Oo][Rr][Mm].*")) {state = NODES; event = INIT;} 
     517      if (input.equals("")) { 
     518        state = TABLE; event = IGNORE; 
     519      } 
     520      else if (input.matches("^\\s*#\\s*[Ff][Rr][Ee][Ee][Ff][Oo][Rr][Mm].*") ||  
     521          input.matches("^\\s*#\\s*[Pp][Oo][Ll][Yy][Ll][Ii][Nn][Ee].*")) { 
     522        state = NODES; event = INIT; 
     523      } 
    489524      else if (input.startsWith("Line") || input.startsWith("Freeform") 
    490           || input.startsWith("Marker") || input.startsWith("Text") || input.startsWith("Oval") 
    491           || input.startsWith("Box") || input.startsWith("Arrow")) { 
    492         state = TABLE; 
    493         event = PARSE; 
     525          || input.startsWith("Marker") || input.startsWith("Text") || 
     526          input.startsWith("Oval") || input.startsWith("Box") || 
     527          input.startsWith("Arrow") || input.startsWith("Polyline")) { 
     528        state = TABLE; event = PARSE; 
    494529      } 
    495530      else if (input.startsWith("#")) {state = TABLE; event = IGNORE;} // must check for freeform header first 
    496531      else { 
    497         event = BARF; 
    498         state = TABLE; 
     532        event = BARF; state = TABLE; 
    499533      } 
    500534    } else if (current == NODES) { 
    501       if (input.equals("")) {state = NODES; event = IGNORE;} 
    502       else if (input.matches("^\\s*#\\s*[Ff][Rr][Ee][Ee][Ff][Oo][Rr][Mm].*")) {state = NODES; event = INIT;} 
    503       else if (input.startsWith("#") || input.matches("^[Xx]\t[Yy]")) {state = NODES; event = IGNORE;} 
    504       else if (input.matches("^[0-9]+\\.[0-9]+\\s[0-9]+\\.[0-9]+$")) {state = NODES; event = PARSE;} 
     535      if (input.equals("")) { 
     536        state = NODES; event = IGNORE; 
     537      } 
     538      else if (input.matches("^\\s*#\\s*[Ff][Rr][Ee][Ee][Ff][Oo][Rr][Mm].*") ||  
     539          input.matches("^\\s*#\\s*[Pp][Oo][Ll][Yy][Ll][Ii][Nn][Ee].*")) { 
     540        state = NODES; event = INIT; 
     541      } 
     542      else if (input.startsWith("#") || input.matches("^[Xx]\t[Yy]")) { 
     543        state = NODES; event = IGNORE; 
     544      } 
     545      else if (input.matches("^[0-9]+\\.[0-9]+\\s[0-9]+\\.[0-9]+$")) { 
     546        state = NODES; event = PARSE; 
     547      } 
    505548      else { 
    506         state = NODES; 
    507         event = BARF; 
     549        state = NODES; event = BARF; 
    508550      } 
    509551    } 
Note: See TracChangeset for help on using the changeset viewer.