00001 /* 00002 * Created on 31 mai 2005 by richet 00003 */ 00004 package org.math.plot.render; 00005 00006 import org.math.plot.plotObjects.*; 00007 00008 public abstract class Projection { 00009 00010 int[][] baseScreenCoords; 00011 00012 public static double DEFAULT_BORDER = 0.15; 00013 00014 protected double borderCoeff = DEFAULT_BORDER; 00015 00016 protected AWTDrawer draw; 00017 00018 public Projection(AWTDrawer _draw) { 00019 draw = _draw; 00020 } 00021 00022 protected void initBaseCoordsProjection() { 00023 // System.out.println("Projection.initBaseCoordsProjection"); 00024 baseScreenCoords = new int[draw.canvas.base.baseCoords.length][2]; 00025 for (int i = 0; i < draw.canvas.base.dimension + 1; i++) { 00026 double[] ratio = baseCoordsScreenProjectionRatio(draw.canvas.base.baseCoords[i]); 00027 baseScreenCoords[i][0] = (int) (draw.canvas.getWidth() * (borderCoeff + (1 - 2 * borderCoeff) * ratio[0])); 00028 baseScreenCoords[i][1] = (int) (draw.canvas.getHeight() - draw.canvas.getHeight() * (borderCoeff + (1 - 2 * borderCoeff) * ratio[1])); 00029 } 00030 } 00031 00032 // /////////////////////////////////////////// 00033 // ////// move methods /////////////////////// 00034 // /////////////////////////////////////////// 00035 00036 public void translate(int[] screenTranslation) { 00037 for (int i = 0; i < draw.canvas.base.dimension + 1; i++) { 00038 baseScreenCoords[i][0] = baseScreenCoords[i][0] + screenTranslation[0]; 00039 baseScreenCoords[i][1] = baseScreenCoords[i][1] + screenTranslation[1]; 00040 } 00041 } 00042 00043 public void dilate(int[] screenOrigin, double[] screenRatio) { 00044 // System.out.println("screenOrigin = "+screenOrigin[0]+" , 00045 // "+screenOrigin[1]); 00046 // System.out.println("screenRatio = "+screenRatio[0]+" , 00047 // "+screenRatio[1]); 00048 for (int i = 0; i < draw.canvas.base.dimension + 1; i++) { 00049 // System.out.println("baseScreenCoords["+i+"] = 00050 // "+baseScreenCoords[i][0]+" , "+baseScreenCoords[i][1]); 00051 baseScreenCoords[i][0] = (int) ((baseScreenCoords[i][0] - screenOrigin[0]) / screenRatio[0]); 00052 baseScreenCoords[i][1] = (int) ((baseScreenCoords[i][1] - screenOrigin[1]) / screenRatio[1]); 00053 // System.out.println(" -> baseScreenCoords["+i+"] = 00054 // "+baseScreenCoords[i][0]+" , "+baseScreenCoords[i][1]); 00055 } 00056 } 00057 00058 // /////////////////////////////////////////// 00059 // ////// projection method ////////////////// 00060 // /////////////////////////////////////////// 00061 00062 public int[] screenProjection(double... pC) { 00063 // System.out.println("Projection.screenProjection("+Array.toString(pC)+")"); 00064 double[] sC = new double[2]; 00065 sC[0] = baseScreenCoords[0][0]; 00066 sC[1] = baseScreenCoords[0][1]; 00067 for (int i = 0; i < draw.canvas.base.dimension; i++) { 00068 if (draw.canvas.base.axesScales[i].equalsIgnoreCase(Base.LOGARITHM)) { 00069 sC[0] += ((log(pC[i]) - log(draw.canvas.base.baseCoords[0][i])) / (log(draw.canvas.base.baseCoords[i + 1][i]) - log(draw.canvas.base.baseCoords[0][i]))) 00070 * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]); 00071 sC[1] += ((log(pC[i]) - log(draw.canvas.base.baseCoords[0][i])) / (log(draw.canvas.base.baseCoords[i + 1][i]) - log(draw.canvas.base.baseCoords[0][i]))) 00072 * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]); 00073 } else if (draw.canvas.base.axesScales[i].equalsIgnoreCase(Base.LINEAR)||draw.canvas.base.axesScales[i].equalsIgnoreCase(Base.STRINGS)) { 00074 sC[0] += ((pC[i] - draw.canvas.base.baseCoords[0][i]) / (draw.canvas.base.baseCoords[i + 1][i] - draw.canvas.base.baseCoords[0][i])) 00075 * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]); 00076 sC[1] += ((pC[i] - draw.canvas.base.baseCoords[0][i]) / (draw.canvas.base.baseCoords[i + 1][i] - draw.canvas.base.baseCoords[0][i])) 00077 * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]); 00078 } 00079 } 00080 00081 if (draw.base_offset != null) { 00082 for (int i = 0; i < draw.canvas.base.dimension; i++) { 00083 sC[0] += draw.base_offset[i] * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]); 00084 sC[1] += draw.base_offset[i] * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]); 00085 } 00086 } 00087 00088 if (draw.screen_offset != null) { 00089 sC[0] += draw.screen_offset[0]; 00090 sC[1] += draw.screen_offset[1]; 00091 } 00092 00093 return new int[] { (int) sC[0], (int) sC[1] }; 00094 } 00095 00096 public int[] screenProjectionBaseRatio(double... rC) { 00097 double[] sC = new double[2]; 00098 sC[0] = baseScreenCoords[0][0]; 00099 sC[1] = baseScreenCoords[0][1]; 00100 for (int i = 0; i < draw.canvas.base.dimension; i++) { 00101 sC[0] += rC[i] * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]); 00102 sC[1] += rC[i] * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]); 00103 } 00104 00105 if (draw.base_offset != null) { 00106 for (int i = 0; i < draw.canvas.base.dimension; i++) { 00107 sC[0] += draw.base_offset[i] * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]); 00108 sC[1] += draw.base_offset[i] * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]); 00109 } 00110 } 00111 00112 if (draw.screen_offset != null) { 00113 sC[0] += draw.screen_offset[0]; 00114 sC[1] += draw.screen_offset[1]; 00115 } 00116 00117 return new int[] { (int) sC[0], (int) sC[1] }; 00118 } 00119 00120 private double log(double x) { 00121 return Math.log(x); 00122 } 00123 00124 protected abstract double[] baseCoordsScreenProjectionRatio(double[] xyz); 00125 00126 }