2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Manuel Juliachs
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "ColorComputer.hxx"
14 #include "DecompositionUtils.hxx"
15 #include "TriangleMeshFecDataDecomposer.hxx"
22 #include "getGraphicObjectProperty.h"
23 #include "graphicObjectProperties.h"
26 int TriangleMeshFecDataDecomposer::getDataSize(char* id)
29 int* piNumVertices = &numVertices;
31 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
36 void TriangleMeshFecDataDecomposer::fillVertices(char* id, float* buffer, int bufferLength, int elementsSize, int coordinateMask, double* scale, double* translation, int logMask)
38 double* coordinates = NULL;
41 int* piNumVertices = &numVertices;
43 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
44 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
46 for (int i = 0; i < numVertices; i++)
48 if (coordinateMask & 0x1)
50 double xi = coordinates[3 * i];
53 xi = DecompositionUtils::getLog10Value(xi);
55 buffer[elementsSize * i] = (float)(xi * scale[0] + translation[0]);
58 if (coordinateMask & 0x2)
60 double yi = coordinates[3 * i + 1];
63 yi = DecompositionUtils::getLog10Value(yi);
65 buffer[elementsSize * i + 1] = (float)(yi * scale[1] + translation[1]);
68 if (coordinateMask & 0x4)
70 double zi = coordinates[3 * i + 2];
73 zi = DecompositionUtils::getLog10Value(zi);
75 buffer[elementsSize * i + 2] = (float)(zi * scale[2] + translation[2]);
78 if (elementsSize == 4 && (coordinateMask & 0x8))
80 buffer[elementsSize * i + 3] = 1.0;
86 void TriangleMeshFecDataDecomposer::fillTextureCoordinates(char* id, float* buffer, int bufferLength)
88 char* parentFigure = NULL;
90 int* piColormapSize = &colormapSize;
91 int* colorRange = NULL;
93 double colorsNumber = 0.;
97 double* values = NULL;
98 double* zBounds = NULL;
100 double minValue = 0.;
101 double maxValue = 0.;
104 int* piNumVertices = &numVertices;
106 int bufferOffset = 0;
108 getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_string, (void**) &parentFigure);
109 /* Temporary: to avoid getting a null parent_figure property when the object is built */
110 if (strcmp(parentFigure, "") == 0)
114 getGraphicObjectProperty(id, __GO_COLOR_RANGE__, jni_int_vector, (void**) &colorRange);
115 getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
117 if (colorRange[0] != 0 || colorRange[1] != 0)
119 colorsNumber = (double) (1 + colorRange[1] - colorRange[0]);
123 colorsNumber = (double) colormapSize;
126 releaseGraphicObjectProperty(__GO_COLOR_RANGE__, colorRange, jni_int_vector, 0);
128 /** To take into account the presence of exterior colors:
129 * - We add 2 to the number of colors.
130 * - We skip the first color.
132 t = 3. / (2. * (colorsNumber + 2.));
133 scale = (colorsNumber - 1.) / (colorsNumber + 2);
135 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
136 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
137 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
139 /* Z-bounds are not taken into account if either of them is invalid */
140 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
142 minValue = zBounds[0];
143 maxValue = zBounds[1];
147 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
150 releaseGraphicObjectProperty(__GO_Z_BOUNDS__, zBounds, jni_double_vector, 0);
152 if (maxValue == minValue)
154 for (int i = 0; i < numVertices; i++)
156 buffer[bufferOffset++] = (float)minValue;
157 buffer[bufferOffset++] = 0;
158 buffer[bufferOffset++] = 0;
159 buffer[bufferOffset++] = 1;
164 for (int i = 0; i < numVertices; i++)
166 buffer[bufferOffset++] = (float)(t + scale * (values[i] - minValue) / (maxValue - minValue));
167 buffer[bufferOffset++] = 0;
168 buffer[bufferOffset++] = 0;
169 buffer[bufferOffset++] = 1;
174 void TriangleMeshFecDataDecomposer::fillColors(char* id, float* buffer, int bufferLength, int elementsSize)
177 char* parentFigure = NULL;
179 double* values = NULL;
180 double* zBounds = NULL;
181 double* colormap = NULL;
183 double minValue = 0.;
184 double maxValue = 0.;
185 double valueRange = 0.;
190 int colormapSize = 0;
191 int* piColormapSize = &colormapSize;
194 int* piNumVertices = &numVertices;
196 int minColorIndex = 0;
197 int maxColorIndex = 0;
199 int* colorRange = NULL;
200 int useOutsideColors = 0;
201 int bufferOffset = 0;
203 getGraphicObjectProperty(id, __GO_PARENT__, jni_string, (void**) &parent);
205 /* Temporary: to avoid getting a null parent_figure property when the object is built */
206 if (strcmp(parent, "") == 0)
211 getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_string, (void**) &parentFigure);
213 getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
214 getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
216 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
218 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
220 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
221 getGraphicObjectProperty(id, __GO_COLOR_RANGE__, jni_int_vector, (void**) &colorRange);
223 if (colorRange[0] != 0 || colorRange[1] != 0)
225 /* To do: use a scilab index to colormap index conversion function */
226 minColorIndex = colorRange[0] - 1;
227 maxColorIndex = colorRange[1] - 1;
229 if (minColorIndex < 0)
233 if (maxColorIndex < 0)
238 if (maxColorIndex > colormapSize-1)
240 maxColorIndex = colormapSize - 1;
242 if (minColorIndex > colormapSize-1)
244 minColorIndex = colormapSize - 1;
251 maxColorIndex = colormapSize-1;
254 releaseGraphicObjectProperty(__GO_COLOR_RANGE__, colorRange, jni_int_vector, 0);
256 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
258 /* Z-bounds are not taken into account if either of them is invalid */
259 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
263 minValue = zBounds[0];
264 maxValue = zBounds[1];
266 getGraphicObjectProperty(id, __GO_OUTSIDE_COLOR__, jni_int_vector, (void**) &outsideColors);
268 if (outsideColors[0] != 0 || outsideColors[1] != 0)
270 useOutsideColors = 1;
272 ColorComputer::getDirectColor((double) outsideColors[0] - 1.0, colormap, colormapSize, minColor);
273 ColorComputer::getDirectColor((double) outsideColors[1] - 1.0, colormap, colormapSize, maxColor);
277 releaseGraphicObjectProperty(__GO_Z_BOUNDS__, zBounds, jni_double_vector, 0);
279 /* To be verified (when reverse z bounds are specified) */
280 if (DecompositionUtils::getAbsoluteValue(maxValue - minValue) < DecompositionUtils::getMinDoubleValue())
286 valueRange = maxValue - minValue;
289 for (int i = 0; i < numVertices; i++)
291 bufferOffset = elementsSize*i;
293 if (useOutsideColors == 1)
295 if (values[i] < minValue)
297 buffer[bufferOffset] = minColor[0];
298 buffer[bufferOffset+1] = minColor[1];
299 buffer[bufferOffset+2] = minColor[2];
301 else if (values[i] > maxValue)
303 buffer[bufferOffset] = maxColor[0];
304 buffer[bufferOffset+1] = maxColor[1];
305 buffer[bufferOffset+2] = maxColor[2];
309 /* To do: replace 0.5 by a macro-definition */
310 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
315 /* To do: replace 0.5 by a macro-definition */
316 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
319 if (elementsSize == 4)
321 buffer[bufferOffset+3] = 1.0;
325 releaseGraphicObjectProperty(__GO_COLORMAP__, colormap, jni_double_vector, colormapSize);
328 void TriangleMeshFecDataDecomposer::computeMinMaxValues(double* values, int numValues, double* valueMin, double* valueMax)
330 double maxDouble = DecompositionUtils::getMaxDoubleValue();
331 double tmpValueMin = maxDouble;
332 double tmpValueMax = -maxDouble;
335 for (int i = 0; i < numValues; i++)
339 if (value < tmpValueMin)
344 if (value > tmpValueMax)
350 *valueMin = tmpValueMin;
351 *valueMax = tmpValueMax;
354 int TriangleMeshFecDataDecomposer::getIndicesSize(char* id)
357 int* piNumIndices = &numIndices;
359 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
364 int TriangleMeshFecDataDecomposer::fillIndices(char* id, int* buffer, int bufferLength, int logMask)
366 double* coordinates = NULL;
367 double* values = NULL;
370 int* piNumIndices = &numIndices;
372 int* piNumVertices = &numVertices;
374 int* triangleIndices = NULL;
379 int bufferOffset = 0;
381 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
382 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
384 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
385 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
388 if (numIndices == 0 || numVertices < 3)
393 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
395 for (int i = 0; i < numIndices; i++)
397 v0 = triangleIndices[3*i];
398 v1 = triangleIndices[3*i+1];
399 v2 = triangleIndices[3*i+2];
401 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
402 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
403 areFaceValuesValid(values, v0, v1, v2))
405 buffer[bufferOffset] = v0;
406 buffer[bufferOffset+1] = v1;
407 buffer[bufferOffset+2] = v2;
417 int TriangleMeshFecDataDecomposer::areFaceVerticesValid(double* coordinates, int v0, int v1, int v2, int logMask)
423 getVertexCoordinates(coordinates, v0, vertex0);
424 getVertexCoordinates(coordinates, v1, vertex1);
425 getVertexCoordinates(coordinates, v2, vertex2);
427 if (DecompositionUtils::isValid(vertex0[0], vertex0[1], vertex0[2]) &&
428 DecompositionUtils::isLogValid(vertex0[0], vertex0[1], vertex0[2], logMask) &&
429 DecompositionUtils::isValid(vertex1[0], vertex1[1], vertex1[2]) &&
430 DecompositionUtils::isLogValid(vertex1[0], vertex1[1], vertex1[2], logMask) &&
431 DecompositionUtils::isValid(vertex2[0], vertex2[1], vertex2[2]) &&
432 DecompositionUtils::isLogValid(vertex2[0], vertex2[1], vertex2[2], logMask))
440 int TriangleMeshFecDataDecomposer::areFaceValuesValid(double* values, int v0, int v1, int v2)
442 if (DecompositionUtils::isValid(values[v0], values[v1], values[v2]))
450 int TriangleMeshFecDataDecomposer::areFaceIndicesValid(int numVertices, int v0, int v1, int v2)
452 if (v0 < 0 || v0 >= numVertices || v1 < 0 || v1 >= numVertices || v2 < 0 || v2 >= numVertices)
460 void TriangleMeshFecDataDecomposer::getVertexCoordinates(double* coordinates, int index, double* vertexCoordinates)
462 vertexCoordinates[0] = coordinates[3*index];
463 vertexCoordinates[1] = coordinates[3*index+1];
464 vertexCoordinates[2] = coordinates[3*index+2];
467 int TriangleMeshFecDataDecomposer::getWireIndicesSize(char* id)
469 int numTriangles = 0;
470 int* piNumTriangles = &numTriangles;
472 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumTriangles);
474 return 6*numTriangles;
478 * To do: ouput shared edges once instead of twice (once per adjacent face).
480 int TriangleMeshFecDataDecomposer::fillWireIndices(char* id, int* buffer, int bufferLength, int logMask)
482 double* coordinates = NULL;
483 double* values = NULL;
486 int* piNumVertices = &numVertices;
488 int* piNumIndices = &numIndices;
489 int* triangleIndices = NULL;
494 int bufferOffset = 0;
496 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
498 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
499 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
500 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
503 if (numIndices == 0 || numVertices < 3)
508 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
510 for (int i = 0; i < numIndices; i++)
512 v0 = triangleIndices[3*i];
513 v1 = triangleIndices[3*i+1];
514 v2 = triangleIndices[3*i+2];
516 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
517 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
518 areFaceValuesValid(values, v0, v1, v2))
520 buffer[bufferOffset] = v0;
521 buffer[bufferOffset+1] = v1;
522 buffer[bufferOffset+2] = v1;
523 buffer[bufferOffset+3] = v2;
524 buffer[bufferOffset+4] = v2;
525 buffer[bufferOffset+5] = v0;