Changeset 2199


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

Totally untested new domainToPixel method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/util/DisplayUtil.java

    r2164 r2199  
    2727import java.awt.event.InputEvent; 
    2828import java.awt.event.KeyEvent; 
    29 import java.awt.Dimension; 
    3029import java.rmi.RemoteException; 
    3130import java.util.Hashtable; 
     
    331330  } 
    332331 
    333   /** Converts the given cursor coordinates to domain coordinates. */ 
    334   public static double[] cursorToDomain(DisplayImpl d, double[] cursor) { 
    335     return cursorToDomain(d, null, cursor); 
    336   } 
    337  
    338   /** Converts the given cursor coordinates to domain coordinates. */ 
    339   public static double[] cursorToDomain(DisplayImpl d, 
    340     RealType[] types, double[] cursor) 
    341   { 
    342     if (d == null) return null; 
    343  
     332  /** 
     333   * Gets scale values (multiplier and offset) for the X, Y and Z maps 
     334   * corresponding to the given RealTypes (or the first ScalarMaps to 
     335   * X, Y and Z if types is null). If no mapping to a spatial axis is 
     336   * found, that component of the array will be null. 
     337   * 
     338   * @return Scale array of size [3][2], with the first dimension 
     339   * corresponding to X, Y or Z, and the second giving multiplier and offset. 
     340   * For example, cursor_x = scale[0][0] * domain_x + scale[0][1]. 
     341   */ 
     342  public static double[][] getScaleValues(DisplayImpl d, RealType[] types) { 
    344343    // locate x, y and z mappings 
    345344    Vector maps = d.getMapVector(); 
     
    363362    } 
    364363 
    365     // adjust for scale 
    366     double[] scaleOffset = new double[2]; 
     364    // get scale values 
     365    double[][] scale = new double[3][]; 
    367366    double[] dummy = new double[2]; 
    368     double[] values = new double[3]; 
    369     if (mapX == null) values[0] = Double.NaN; 
     367    if (mapX == null) scale[0] = null; 
    370368    else { 
    371       mapX.getScale(scaleOffset, dummy, dummy); 
    372       values[0] = (cursor[0] - scaleOffset[1]) / scaleOffset[0]; 
    373     } 
    374     if (mapY == null) values[1] = Double.NaN; 
     369      scale[0] = new double[2]; 
     370      mapX.getScale(scale[0], dummy, dummy); 
     371    } 
     372    if (mapY == null) scale[1] = null; 
    375373    else { 
    376       mapY.getScale(scaleOffset, dummy, dummy); 
    377       values[1] = (cursor[1] - scaleOffset[1]) / scaleOffset[0]; 
    378     } 
    379     if (mapZ == null) values[2] = Double.NaN; 
     374      scale[1] = new double[2]; 
     375      mapY.getScale(scale[1], dummy, dummy); 
     376    } 
     377    if (mapZ == null) scale[2] = null; 
    380378    else { 
    381       mapZ.getScale(scaleOffset, dummy, dummy); 
    382       values[2] = (cursor[2] - scaleOffset[1]) / scaleOffset[0]; 
    383     } 
    384  
    385     return values; 
     379      scale[2] = new double[2]; 
     380      mapZ.getScale(scale[2], dummy, dummy); 
     381    } 
     382    return scale; 
     383  } 
     384 
     385  /** Converts the given cursor coordinates to domain coordinates. */ 
     386  public static double[] cursorToDomain(DisplayImpl d, 
     387    RealType[] types, double[] cursor) 
     388  { 
     389    if (d == null) return null; 
     390    double[][] scale = getScaleValues(d, types); 
     391    double[] domain = new double[3]; 
     392    for (int i=0; i<3; i++) { 
     393      domain[i] = scale[i] == null ? Double.NaN : 
     394        (cursor[i] - scale[i][1]) / scale[i][0]; 
     395    } 
     396    return domain; 
     397  } 
     398 
     399  /** Converts the given domain coordinates to cursor coordinates. */ 
     400  public static double[] domainToCursor(DisplayImpl d, 
     401    RealType[] types, double[] domain) 
     402  { 
     403    if (d == null) return null; 
     404    double[][] scale = getScaleValues(d, types); 
     405    double[] cursor = new double[3]; 
     406    for (int i=0; i<3; i++) { 
     407      cursor[i] = scale[i] == null ? Double.NaN : 
     408        scale[i][0] * domain[i] + scale[i][1]; 
     409    } 
     410    return cursor; 
     411  } 
     412 
     413  /** Converts the given cursor coordinates to domain coordinates. */ 
     414  public static double[] cursorToDomain(DisplayImpl d, double[] cursor) { 
     415    return cursorToDomain(d, null, cursor); 
     416  } 
     417 
     418  /** Converts the given domain coordinates to cursor coordinates. */ 
     419  public static double[] domainToCursor(DisplayImpl d, double[] domain) { 
     420    return domainToCursor(d, null, domain); 
    386421  } 
    387422 
     
    394429  } 
    395430 
     431  /** Converts the given cursor coordinates to pixel coordinates. */ 
     432  public static int[] cursorToPixel(DisplayImpl d, double[] cursor) { 
     433    if (d == null) return null; 
     434    MouseBehavior mb = d.getDisplayRenderer().getMouseBehavior(); 
     435    return mb.getScreenCoords(cursor); 
     436  } 
     437 
    396438  /** Converts the given pixel coordinates to domain coordinates. */ 
    397439  public static double[] pixelToDomain(DisplayImpl d, int x, int y) { 
    398440    return cursorToDomain(d, pixelToCursor(d, x, y)); 
     441  } 
     442 
     443  /** Converts the given domain coordinates to pixel coordinates. */ 
     444  public static int[] domainToPixel(DisplayImpl d, double[] domain) { 
     445    return cursorToPixel(d, domainToCursor(d, domain)); 
    399446  } 
    400447 
     
    451498  } 
    452499 
    453   /** Converts the given domain coordinates to pixel coordinates */ 
    454   public static double[] domainToPixel(DisplayImpl d, double x, double y) { 
    455     double[] v = {x, y}; 
    456  
    457     Dimension dim = d.getComponent().getSize(); 
    458      
    459     double[] a, b, c, aa, bb, cc; 
    460     aa = pixelToDomain(d, 0, 0); 
    461     bb = pixelToDomain(d, dim.width, 0); 
    462     cc = pixelToDomain(d, 0, dim.height); 
    463  
    464     a = new double[] {aa[0], aa[1]}; // pixel to domain returns a double[] of 
    465     b = new double[] {bb[0], bb[1]}; // length 3 
    466     c = new double[] {cc[0], cc[1]}; 
    467  
    468     double[] xx, yy; 
    469     xx = MathUtil.getProjection(a, b, v, false); 
    470     yy = MathUtil.getProjection(a, c, v, false); 
    471      
    472     double xb, yb; 
    473     xb = MathUtil.getDistance (a, xx); 
    474     yb = MathUtil.getDistance (a, yy); 
    475  
    476     double wb, hb; 
    477     wb = MathUtil.getDistance (a, b); 
    478     hb = MathUtil.getDistance (a, c); 
    479  
    480     double dpx, dpy; 
    481     dpx =  (dim.width * xb / wb); 
    482     dpy =  (dim.height * yb / hb); 
    483  
    484     return new double[] {dpx, dpy}; 
    485   } 
    486500} 
Note: See TracChangeset for help on using the changeset viewer.