src/org/math/plot/plots/CloudPlot3D.java

00001 /*
00002  * Created on 13 juil. 07 by richet
00003  */
00004 package org.math.plot.plots;
00005 
00006 import java.awt.Color;
00007 
00008 import javax.swing.JFrame;
00009 
00010 import org.math.plot.FrameView;
00011 import org.math.plot.Plot3DPanel;
00012 import org.math.plot.PlotPanel;
00013 import org.math.plot.render.AbstractDrawer;
00014 
00015 public class CloudPlot3D extends Plot {
00016 
00017         double[][] topNW;
00018 
00019         double[][] topNE;
00020 
00021         double[][] topSW;
00022 
00023         double[][] topSE;
00024 
00025         double[][] botNW;
00026 
00027         double[][] botNE;
00028 
00029         double[][] botSW;
00030 
00031         double[][] botSE;
00032 
00033         double[] width_constant = { -1, -1 };
00034 
00035         double[][] XY;
00036 
00037         float[] f;
00038 
00039         boolean fill_shape = true;
00040 
00041         public CloudPlot3D(String n, Color c, double[][] _XYcard, double wX, double wY, double wZ) {
00042                 super(n, c);
00043                 splitXYf(_XYcard);
00044                 width_constant = new double[] { wX, wY, wZ };
00045 
00046                 build();
00047         }
00048 
00049         private void splitXYf(double[][] xycard) {
00050                 XY = new double[xycard.length][3];
00051                 f = new float[xycard.length];
00052                 float normf = 0;
00053                 for (int i = 0; i < xycard.length; i++) {
00054                         XY[i][0] = xycard[i][0];
00055                         XY[i][1] = xycard[i][1];
00056                         XY[i][2] = xycard[i][2];
00057                         f[i] = (float) xycard[i][3];
00058                         normf += f[i];//Math.max(normf, f[i]);
00059                 }
00060 
00061                 for (int i = 0; i < f.length; i++) {
00062                         f[i] = f[i] / normf;
00063                 }
00064 
00065         }
00066 
00067         private void build() {
00068                 if (width_constant[0] > 0) {
00069                         topNW = new double[XY.length][];
00070                         topNE = new double[XY.length][];
00071                         topSW = new double[XY.length][];
00072                         topSE = new double[XY.length][];
00073                         botNW = new double[XY.length][];
00074                         botNE = new double[XY.length][];
00075                         botSW = new double[XY.length][];
00076                         botSE = new double[XY.length][];
00077                         for (int i = 0; i < XY.length; i++) {
00078                                 topNW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] + width_constant[1] / 2 };
00079                                 topNE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] + width_constant[1] / 2 };
00080                                 topSW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] + width_constant[1] / 2 };
00081                                 topSE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] + width_constant[1] / 2 };
00082                                 botNW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] - width_constant[1] / 2 };
00083                                 botNE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] - width_constant[1] / 2 };
00084                                 botSW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] - width_constant[1] / 2 };
00085                                 botSE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] - width_constant[1] / 2 };
00086                         }
00087                 }
00088         }
00089 
00090         public void plot(AbstractDrawer draw, Color c) {
00091                 if (!visible)
00092                         return;
00093 
00094                 draw.canvas.includeInBounds(botSW[0]);
00095                 draw.canvas.includeInBounds(topNE[XY.length - 1]);
00096 
00097                 draw.setColor(c);
00098                 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00099                 for (int i = 0; i < XY.length; i++) {
00100                         if (f[i] > 0) {
00101                                 draw.fillPolygon(f[i], topNW[i], topNE[i], topSE[i], topSW[i]);
00102                                 draw.fillPolygon(f[i], botNW[i], botNE[i], botSE[i], botSW[i]);
00103 
00104                                 draw.fillPolygon(f[i], botNW[i], botNE[i], topNE[i], topNW[i]);
00105                                 draw.fillPolygon(f[i], botSW[i], botSE[i], topSE[i], topSW[i]);
00106 
00107                                 draw.fillPolygon(f[i], botNW[i], botSW[i], topSW[i], topNW[i]);
00108                                 draw.fillPolygon(f[i], botNE[i], botSE[i], topSE[i], topNE[i]);
00109                         }
00110                 }
00111         }
00112 
00113         @Override
00114         public void setData(double[][] d) {
00115                 splitXYf(d);
00116         }
00117 
00118         @Override
00119         public double[][] getData() {
00120                 return XY;
00121         }
00122 
00123         public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00124                 for (int i = 0; i < XY.length; i++) {
00125                         int[] screenCoord = draw.project(XY[i]);
00126 
00127                         if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00128                                         && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00129                                 return XY[i];
00130                 }
00131                 return null;
00132         }
00133 
00134         public static void main(String[] args) {
00135                 Plot3DPanel p = new Plot3DPanel();
00136 
00137                 //triangular random cloud (as sum of two uniform random numbers)
00138                 double[][] cloud = new double[100][3];
00139                 for (int i = 0; i < cloud.length; i++) {
00140                         cloud[i][0] = Math.random() + Math.random();
00141                         cloud[i][1] = Math.random() + Math.random();
00142                         cloud[i][2] = Math.random() + Math.random();
00143                 }
00144                 p.addCloudPlot("cloud", Color.RED, cloud, 3, 3, 3);
00145 
00146                 double[][] cloud2 = new double[100][3];
00147                 for (int i = 0; i < cloud.length; i++) {
00148                         cloud2[i][0] = 2 + Math.random() + Math.random();
00149                         cloud2[i][1] = 2 + Math.random() + Math.random();
00150                         cloud2[i][2] = 2 + Math.random() + Math.random();
00151                 }
00152                 p.addCloudPlot("cloud2", Color.RED, cloud2, 3, 3, 3);
00153 
00154                 p.setLegendOrientation(PlotPanel.SOUTH);
00155                 new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00156         }
00157 
00158 }

Generated on Wed Sep 5 21:44:02 2007 for jmathplot by  doxygen 1.5.1