FecFacetDrawerGL.java

Go to the documentation of this file.
00001 /*
00002  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
00003  * Copyright (C) 2008 - INRIA - Jean-Baptiste Silvy
00004  * desc : Class drawing the facets of a fec object 
00005  * 
00006  * This file must be used under the terms of the CeCILL.
00007  * This source file is licensed as described in the file COPYING, which
00008  * you should have received as part of this distribution.  The terms
00009  * are also available at    
00010  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
00011  *
00012  */
00013 
00014 package org.scilab.modules.renderer.fecDrawing;
00015 
00016 import javax.media.opengl.GL;
00017 
00018 import org.scilab.modules.renderer.AutoDrawableObjectGL;
00019 import org.scilab.modules.renderer.utils.geom3D.ColoredTriangle;
00020 import org.scilab.modules.renderer.utils.geom3D.TriangleDecomposition;
00021 import org.scilab.modules.renderer.utils.geom3D.Vector3D;
00022 import org.scilab.modules.renderer.utils.glTools.GLTools;
00023 
00024 
00029 public class FecFacetDrawerGL extends AutoDrawableObjectGL {
00030 
00031         private static final int TRIANGLE_NB_VERTEX = 3;
00032         
00033         private double zMin;
00034         private double zMax;
00035         private int colOutLow;
00036         private int colOutUp;
00037         private int colMin;
00038         private int colMax;
00039         
00043         public FecFacetDrawerGL() {
00044                 super();
00045         }
00046         
00056         public void setFacetParameters(double zMin, double zMax, int colMin,
00057                                                                    int colMax, int colOutLow, int colOutUp) {
00058                 
00059         
00060                 if (colMin == 0 && colMax == 0) {
00061                         // default colmin and colmax
00062                         // use the whole colormap
00063                         this.colMin = 0;
00064                         this.colMax = getColorMap().getSize() - 1;
00065                 } else {
00066                         // colMin and colMax are scilab indices
00067                         this.colMin = getColorMap().convertScilabToColorMapIndex(colMin);
00068                         this.colMax = getColorMap().convertScilabToColorMapIndex(colMax);
00069                 }
00070                 
00071                 if (colOutLow <= -1) {
00072                         // when outside of colormap use the lowest value in the colormap
00073                         this.colOutLow = this.colMin;
00074                 } else if (colOutLow == 0) {
00075                         // don't display polygon
00076                         this.colOutLow = -1;
00077                 } else {
00078                         // colOutLow is a valid colormap index
00079                         this.colOutLow = getColorMap().convertScilabToColorMapIndex(colOutLow);
00080                 }
00081                 
00082                 if (colOutUp <= -1) {
00083                         // when outside of colormap use the highest value in the colormap
00084                         this.colOutUp = this.colMax;
00085                 } else if (colOutUp == 0) {
00086                         // don't display polygon
00087                         this.colOutUp = -1;
00088                 } else {
00089                         // colOutLow is a valid colormap index
00090                         this.colOutUp = getColorMap().convertScilabToColorMapIndex(colOutUp);
00091                 }
00092                 
00093                 
00094                 
00095                 
00096                 this.zMin = zMin;
00097                 this.zMax = zMax;
00098                 
00099         }
00100         
00108         protected double fecValueToColorMapIndex(double value, double zMin, double zMax) {
00109                 //return ((value - zMin) / (zMax - zMin) * (colMax - colMin + 1.0) + colMin);
00110                 return (colMax - colMin) / (zMax - zMin) * (value - zMax) + colMax;
00111                 
00112                 //return (colMax - colMin - 1) / (zMax - zMin) * (value - zMax) + colMax;
00113                 //return (colMax - colMin - zMin) / (zMax - zMin) * (value - zMax) + colMax;
00114         }       
00115         
00125         public void drawFec(double[] xCoords, double[] yCoords, double[] values,
00126                                                 int[] firstPoints, int[] secondPoints, int[] thirdPoints) {
00127                 
00128                 // find min and max of values
00129                 double[] minMax = findValuesBounds(values);
00130                 int nbTriangles = firstPoints.length;
00131                 
00132                 if (zMin == zMax) {
00133                         zMin = minMax[0];
00134                         zMax = minMax[1];
00135                 }
00136                 
00137                 GL gl = getGL();
00138 
00139                 Vector3D[] triangleCoords = new Vector3D[TRIANGLE_NB_VERTEX];
00140                 double[] triangleColors = new double[TRIANGLE_NB_VERTEX];
00141                 
00142                 GLTools.pushPolygonsBack(gl);
00143         
00144                 for (int i = 0; i < nbTriangles; i++) {
00145                         triangleCoords[0] = new Vector3D(xCoords[firstPoints[i]], yCoords[firstPoints[i]], 0.0);
00146                         triangleCoords[1] = new Vector3D(xCoords[secondPoints[i]], yCoords[secondPoints[i]], 0.0);
00147                         triangleCoords[2] = new Vector3D(xCoords[thirdPoints[i]], yCoords[thirdPoints[i]], 0.0);
00148                         
00149                         triangleColors[0] = fecValueToColorMapIndex(values[firstPoints[i]], zMin, zMax);
00150                         triangleColors[1] = fecValueToColorMapIndex(values[secondPoints[i]], zMin, zMax);
00151                         triangleColors[2] = fecValueToColorMapIndex(values[thirdPoints[i]], zMin, zMax);
00152                                                                 
00153                         ColoredTriangle ct = new ColoredTriangle(triangleCoords[0],
00154                                                                                                          triangleCoords[1],
00155                                                                                                          triangleCoords[2],
00156                                                                                                          triangleColors[0],
00157                                                                                                          triangleColors[1],
00158                                                                                                          triangleColors[2]);
00159                         TriangleDecomposition td = ct.decomposeTriangle();
00160                         
00161                         paintFec(td, gl);                       
00162                 }
00163                 
00164                 GLTools.endPushPolygonsBack(gl);
00165                 
00166                 
00167                 // create a color map for fec objects
00168                 // create a special colormap to use with the fec object
00169 //              FecColorMap specialCM = new FecColorMap(getColorMap());
00170 //              specialCM.setRealBounds(minMax[0], minMax[1]);
00171 //              specialCM.setFecWeirdValues(zMin, zMax, colMin, colMax, colOutLow, colOutUp);
00172 //              
00173 //              
00174 //              // draw facets using the new colormap
00175 //              GL gl = getGL();
00176 //              int nbTriangles = firstPoints.length;
00177 //              
00178 //              Texture colormapTexture = specialCM.getTexture();
00179 //              colormapTexture.enable();
00180 //              colormapTexture.bind();
00181 //              
00182 //              // push back polygons from the box lines
00183 //              GLTools.pushPolygonsBack(gl);
00184 //              
00185 //              gl.glColor3d(0.0, 1.0, 0.0);
00186 //              
00187 //              gl.glBegin(GL.GL_TRIANGLES);
00188 //              for (int i = 0; i < nbTriangles; i++) {
00189 //                      specialCM.useValue(gl, values[firstPoints[i]]);
00190 //                      gl.glVertex3d(xCoords[firstPoints[i]], yCoords[firstPoints[i]], 0.0);
00191 //                      specialCM.useValue(gl, values[secondPoints[i]]);
00192 //                      gl.glVertex3d(xCoords[secondPoints[i]], yCoords[secondPoints[i]], 0.0);
00193 //                      specialCM.useValue(gl, values[thirdPoints[i]]);
00194 //                      gl.glVertex3d(xCoords[thirdPoints[i]], yCoords[thirdPoints[i]], 0.0);
00195 //              }
00196 //              gl.glEnd();
00197 //              
00198 //              GLTools.endPushPolygonsBack(gl);
00199 //              
00200 //              colormapTexture.disable();
00201                 
00202         }
00203         
00209         private double[] findValuesBounds(double[] values) {
00210                 double minVal = values[0];
00211                 double maxVal = values[0];
00212                 for (int i = 0; i < values.length; i++) {
00213                         if (values[i] > maxVal) {
00214                                 maxVal = values[i];
00215                         } else if (values[i] < minVal) {
00216                                 minVal = values[i];
00217                         }
00218                 }
00219                 double[] res = {minVal, maxVal};
00220                 return res;
00221         }
00222         
00228         private void paintFec(TriangleDecomposition td, GL gl) {
00229                                 
00230                 gl.glBegin(GL.GL_TRIANGLES);
00231                 
00232                 for (int j = 0; j < td.getNbPolygons(); j++) {
00233                         int color = td.getPolygonColor(j);
00234                         double[] polyColor = getColorFecPolygon(color);
00235                         
00236                         if (polyColor != null) {                                
00237                                 
00238                                 gl.glColor3d(polyColor[0], polyColor[1], polyColor[2]);
00239                                 Vector3D[] polygon = td.getPolygon(j);
00240                                 
00241                                 paintPolygone(gl, polygon);
00242                                 
00243 //                              // first triangle & following triangles
00244 //                              for (int k = 1; k < polygon.length - 1; k++) {
00245 //                                      gl.glVertex3d(polygon[0].getX(), polygon[0].getY(), polygon[0].getZ());
00246 //                                      gl.glVertex3d(polygon[k].getX(), polygon[k].getY(), polygon[k].getZ());
00247 //                                      gl.glVertex3d(polygon[k + 1].getX(), polygon[k + 1].getY(), polygon[k + 1].getZ());
00248 //                              }                               
00249                         }
00250                 }
00251                 
00252                 gl.glEnd();
00253                 
00254         }
00255         
00261         private double[] getColorFecPolygon(int colorIndex) {
00262                 
00263                 double[] polyColor = null;
00264                 
00265                 if (colorIndex < colMin) {
00266                         if (colOutLow >= 0) {
00267                                 polyColor = getColorMap().getColor(colOutLow);
00268                         }
00269                 } else if (colorIndex > colMax) {
00270                         if (colOutUp >= 0) {
00271                                 polyColor = getColorMap().getColor(colOutUp);
00272                         }
00273                 } else {
00274                         polyColor = getColorMap().getColor(colorIndex);
00275                 }
00276                 
00277                 return polyColor;
00278         }
00279         
00285         public void paintPolygone(GL gl, Vector3D[] polygon) {
00286                 
00287                 // first triangle & following triangles
00288                 for (int k = 1; k < polygon.length - 1; k++) {
00289                         gl.glVertex3d(polygon[0].getX(), polygon[0].getY(), polygon[0].getZ());
00290                         gl.glVertex3d(polygon[k].getX(), polygon[k].getY(), polygon[k].getZ());
00291                         gl.glVertex3d(polygon[k + 1].getX(), polygon[k + 1].getY(), polygon[k + 1].getZ());
00292                 }
00293                 
00294         }
00295         
00296 }

Generated on Tue Sep 9 17:48:30 2008 for Scilab [trunk] by  doxygen 1.5.5