00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00062
00063 this.colMin = 0;
00064 this.colMax = getColorMap().getSize() - 1;
00065 } else {
00066
00067 this.colMin = getColorMap().convertScilabToColorMapIndex(colMin);
00068 this.colMax = getColorMap().convertScilabToColorMapIndex(colMax);
00069 }
00070
00071 if (colOutLow <= -1) {
00072
00073 this.colOutLow = this.colMin;
00074 } else if (colOutLow == 0) {
00075
00076 this.colOutLow = -1;
00077 } else {
00078
00079 this.colOutLow = getColorMap().convertScilabToColorMapIndex(colOutLow);
00080 }
00081
00082 if (colOutUp <= -1) {
00083
00084 this.colOutUp = this.colMax;
00085 } else if (colOutUp == 0) {
00086
00087 this.colOutUp = -1;
00088 } else {
00089
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
00110 return (colMax - colMin) / (zMax - zMin) * (value - zMax) + colMax;
00111
00112
00113
00114 }
00115
00125 public void drawFec(double[] xCoords, double[] yCoords, double[] values,
00126 int[] firstPoints, int[] secondPoints, int[] thirdPoints) {
00127
00128
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
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
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
00244
00245
00246
00247
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
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 }