00001 package org.math.plot.plots;
00002
00003 import java.awt.Color;
00004
00005 import javax.swing.JFrame;
00006
00007 import org.math.plot.FrameView;
00008 import org.math.plot.Plot2DPanel;
00009 import org.math.plot.render.AbstractDrawer;
00010 import org.math.plot.utils.Array;
00011
00016 public class GaussianDensityLayerPlot extends LayerPlot {
00017
00018 public static int WIDHT = 2;
00019
00020 int axis;
00021
00022 Color gradC_0sigma, gradC_1sigma, gradC_2sigma, gradC_3sigma;
00023
00024 double constant_sigma = 0;
00025
00026 double[] sigma;
00027
00028 private float[][] gausspdf_sigma;
00029
00030 public GaussianDensityLayerPlot(Plot p, int ax, double sigma) {
00031 this(p, ax, null);
00032 constant_sigma = sigma;
00033
00034 gausspdf_sigma = new float[1][4];
00035 for (int i = 0; i < gausspdf_sigma.length; i++)
00036 for (int j = 0; j < 4; j++)
00037 gausspdf_sigma[i][j] = (float) (Math.exp(-(j * j)
00038 / (2.0 * constant_sigma * constant_sigma)));
00039
00040 }
00041
00042
00043
00044
00045
00051 public GaussianDensityLayerPlot(Plot p, int ax, double[] sigma) {
00052 super("Gauss quantile of " + p.name, p);
00053 if (sigma != null)
00054 Array.checkLength(sigma, p.getData().length);
00055 this.sigma = sigma;
00056 axis = ax;
00057
00058 if (sigma != null) {
00059 gausspdf_sigma = new float[sigma.length][4];
00060 for (int i = 0; i < gausspdf_sigma.length; i++) {
00061 for (int j = 0; j < 4; j++)
00062 gausspdf_sigma[i][j] = (float) (Math.exp(-(j * j) / (2.0 * sigma[i] * sigma[i])));
00063 }
00064 }
00065
00066 }
00067
00068 public double getQuantilesValue(int numCoord) {
00069 return sigma[numCoord];
00070 }
00071
00072 public int getAxe() {
00073 return axis;
00074 }
00075
00076 public void plot(AbstractDrawer draw, Color c) {
00077 if (!plot.visible)
00078 return;
00079
00080 draw.setColor(c);
00081
00082 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00083 draw.setLineWidth(WIDHT);
00084 if (constant_sigma == 0)
00085 for (int i = 0; i < plot.getData().length; i++) {
00086 gradC_0sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][0])));
00087 gradC_1sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][1])));
00088 gradC_2sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][2])));
00089 gradC_3sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][3])));
00090
00091 double[] d = Array.getRowCopy(plot.getData(), i);
00092 double[] d2 = Array.getRowCopy(plot.getData(), i);
00093 d2[axis] += sigma[i];
00094 draw.setGradient(d, gradC_0sigma, d2, gradC_1sigma);
00095 draw.drawLine(d, d2);
00096
00097 d[axis] += sigma[i];
00098 d2[axis] += sigma[i];
00099 draw.setGradient(d, gradC_1sigma, d2, gradC_2sigma);
00100 draw.drawLine(d, d2);
00101
00102 d[axis] += sigma[i];
00103 d2[axis] += sigma[i];
00104 draw.setGradient(d, gradC_2sigma, d2, gradC_3sigma);
00105 draw.drawLine(d, d2);
00106
00107 d = Array.getRowCopy(plot.getData(), i);
00108 d2 = Array.getRowCopy(plot.getData(), i);
00109 d2[axis] -= sigma[i];
00110 draw.setGradient(d2, gradC_1sigma, d, gradC_0sigma);
00111 draw.drawLine(d2, d);
00112
00113 d[axis] -= sigma[i];
00114 d2[axis] -= sigma[i];
00115 draw.setGradient(d2, gradC_2sigma, d, gradC_1sigma);
00116 draw.drawLine(d2, d);
00117
00118 d[axis] -= sigma[i];
00119 d2[axis] -= sigma[i];
00120 draw.setGradient(d2, gradC_3sigma, d, gradC_2sigma);
00121 draw.drawLine(d2, d);
00122 }
00123 else {
00124 gradC_0sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][0])));
00125 gradC_1sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][1])));
00126 gradC_2sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][2])));
00127 gradC_3sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][3])));
00128
00129 for (int i = 0; i < plot.getData().length; i++) {
00130
00131 double[] d = Array.getRowCopy(plot.getData(), i);
00132 double[] d2 = Array.getRowCopy(plot.getData(), i);
00133 d2[axis] += constant_sigma;
00134 draw.setGradient(d, gradC_0sigma, d2, gradC_1sigma);
00135 draw.drawLine(d, d2);
00136
00137 d[axis] += constant_sigma;
00138 d2[axis] += constant_sigma;
00139 draw.setGradient(d, gradC_1sigma, d2, gradC_2sigma);
00140 draw.drawLine(d, d2);
00141
00142 d[axis] += constant_sigma;
00143 d2[axis] += constant_sigma;
00144 draw.setGradient(d, gradC_2sigma, d2, gradC_3sigma);
00145 draw.drawLine(d, d2);
00146
00147 d = Array.getRowCopy(plot.getData(), i);
00148 d2 = Array.getRowCopy(plot.getData(), i);
00149 d2[axis] -= constant_sigma;
00150 draw.setGradient(d2, gradC_1sigma, d, gradC_0sigma);
00151 draw.drawLine(d2, d);
00152
00153 d[axis] -= constant_sigma;
00154 d2[axis] -= constant_sigma;
00155 draw.setGradient(d2, gradC_2sigma, d, gradC_1sigma);
00156 draw.drawLine(d2, d);
00157
00158 d[axis] -= constant_sigma;
00159 d2[axis] -= constant_sigma;
00160 draw.setGradient(d2, gradC_3sigma, d, gradC_2sigma);
00161 draw.drawLine(d2, d);
00162 }
00163 }
00164 draw.resetGradient();
00165 draw.setLineWidth(AbstractDrawer.DEFAULT_LINE_WIDTH);
00166
00167 }
00168
00169 @Override
00170 public void setData(double[][] d) {
00171 sigma = d[0];
00172 }
00173
00174 @Override
00175 public double[][] getData() {
00176 return new double[][] { sigma };
00177 }
00178
00179 public static void main(String[] args) {
00180 double[] sXYZ = null;
00181
00182 Plot2DPanel p2 = new Plot2DPanel();
00183 for (int i = 0; i < 2; i++) {
00184 double[][] XYZ = new double[10][2];
00185 sXYZ = new double[10];
00186 for (int j = 0; j < XYZ.length; j++) {
00187 XYZ[j][0] = Math.random();
00188 XYZ[j][1] = Math.random();
00189 sXYZ[j] = Math.random();
00190 }
00191
00192 p2.addScatterPlot("toto" + i, XYZ);
00193 }
00194 p2.getPlot(0).addGaussQuantiles(1, sXYZ);
00195 p2.getPlot(1).addGaussQuantiles(1, 0.1);
00196
00197 new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00198 }
00199 }