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 buffer[elementsSize*i] = (float)coordinates[3*i];
49 buffer[elementsSize*i +1] = (float)coordinates[3*i+1];
50 buffer[elementsSize*i +2] = (float)coordinates[3*i+2];
52 if (elementsSize == 4)
54 buffer[elementsSize*i +3] = 1.0;
60 void TriangleMeshFecDataDecomposer::fillTextureCoordinates(char* id, float* buffer, int bufferLength)
62 char* parentFigure = NULL;
64 int* piColormapSize = &colormapSize;
65 int* colorRange = NULL;
67 double colorsNumber = 0.;
71 double* values = NULL;
72 double* zBounds = NULL;
78 int* piNumVertices = &numVertices;
82 getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_string, (void**) &parentFigure);
83 /* Temporary: to avoid getting a null parent_figure property when the object is built */
84 if (strcmp(parentFigure, "") == 0)
88 getGraphicObjectProperty(id, __GO_COLOR_RANGE__, jni_int_vector, (void**) &colorRange);
89 getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
91 if (colorRange[0] != 0 || colorRange[1] != 0)
93 colorsNumber = (double) (1 + colorRange[1] - colorRange[0]);
97 colorsNumber = (double) colormapSize;
100 /** To take into account the presence of exterior colors:
101 * - We add 2 to the number of colors.
102 * - We skip the first color.
104 t = 3. / (2. * (colorsNumber + 2.));
105 scale = (colorsNumber - 1.) / (colorsNumber + 2);
107 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
108 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
109 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
111 /* Z-bounds are not taken into account if either of them is invalid */
112 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
114 minValue = zBounds[0];
115 maxValue = zBounds[1];
119 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
122 if (maxValue == minValue)
124 for (int i = 0; i < numVertices; i++)
126 buffer[bufferOffset++] = (float)minValue;
127 buffer[bufferOffset++] = 0;
128 buffer[bufferOffset++] = 0;
129 buffer[bufferOffset++] = 1;
134 for (int i = 0; i < numVertices; i++)
136 buffer[bufferOffset++] = (float)(t + scale * (values[i] - minValue) / (maxValue - minValue));
137 buffer[bufferOffset++] = 0;
138 buffer[bufferOffset++] = 0;
139 buffer[bufferOffset++] = 1;
144 void TriangleMeshFecDataDecomposer::fillColors(char* id, float* buffer, int bufferLength, int elementsSize)
147 char* parentFigure = NULL;
149 double* values = NULL;
150 double* zBounds = NULL;
151 double* colormap = NULL;
153 double minValue = 0.;
154 double maxValue = 0.;
155 double valueRange = 0.;
160 int colormapSize = 0;
161 int* piColormapSize = &colormapSize;
164 int* piNumVertices = &numVertices;
166 int minColorIndex = 0;
167 int maxColorIndex = 0;
169 int* colorRange = NULL;
170 int useOutsideColors = 0;
171 int bufferOffset = 0;
173 getGraphicObjectProperty(id, __GO_PARENT__, jni_string, (void**) &parent);
175 /* Temporary: to avoid getting a null parent_figure property when the object is built */
176 if (strcmp(parent, "") == 0)
181 getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_string, (void**) &parentFigure);
183 getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
184 getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
186 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
188 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
190 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
191 getGraphicObjectProperty(id, __GO_COLOR_RANGE__, jni_int_vector, (void**) &colorRange);
193 if (colorRange[0] != 0 || colorRange[1] != 0)
195 /* To do: use a scilab index to colormap index conversion function */
196 minColorIndex = colorRange[0] - 1;
197 maxColorIndex = colorRange[1] - 1;
199 if (minColorIndex < 0)
203 if (maxColorIndex < 0)
208 if (maxColorIndex > colormapSize-1)
210 maxColorIndex = colormapSize - 1;
212 if (minColorIndex > colormapSize-1)
214 minColorIndex = colormapSize - 1;
221 maxColorIndex = colormapSize-1;
224 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
226 /* Z-bounds are not taken into account if either of them is invalid */
227 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
231 minValue = zBounds[0];
232 maxValue = zBounds[1];
234 getGraphicObjectProperty(id, __GO_OUTSIDE_COLOR__, jni_int_vector, (void**) &outsideColors);
236 if (outsideColors[0] != 0 || outsideColors[1] != 0)
238 useOutsideColors = 1;
240 ColorComputer::getDirectColor((double) outsideColors[0] - 1.0, colormap, colormapSize, minColor);
241 ColorComputer::getDirectColor((double) outsideColors[1] - 1.0, colormap, colormapSize, maxColor);
245 /* To be verified (when reverse z bounds are specified) */
246 if (DecompositionUtils::getAbsoluteValue(maxValue - minValue) < DecompositionUtils::getMinDoubleValue())
252 valueRange = maxValue - minValue;
255 for (int i = 0; i < numVertices; i++)
257 bufferOffset = elementsSize*i;
259 if (useOutsideColors == 1)
261 if (values[i] < minValue)
263 buffer[bufferOffset] = minColor[0];
264 buffer[bufferOffset+1] = minColor[1];
265 buffer[bufferOffset+2] = minColor[2];
267 else if (values[i] > maxValue)
269 buffer[bufferOffset] = maxColor[0];
270 buffer[bufferOffset+1] = maxColor[1];
271 buffer[bufferOffset+2] = maxColor[2];
275 /* To do: replace 0.5 by a macro-definition */
276 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
281 /* To do: replace 0.5 by a macro-definition */
282 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
285 if (elementsSize == 4)
287 buffer[bufferOffset+3] = 1.0;
293 void TriangleMeshFecDataDecomposer::computeMinMaxValues(double* values, int numValues, double* valueMin, double* valueMax)
295 double maxDouble = DecompositionUtils::getMaxDoubleValue();
296 double tmpValueMin = maxDouble;
297 double tmpValueMax = -maxDouble;
300 for (int i = 0; i < numValues; i++)
304 if (value < tmpValueMin)
309 if (value > tmpValueMax)
315 *valueMin = tmpValueMin;
316 *valueMax = tmpValueMax;
319 int TriangleMeshFecDataDecomposer::getIndicesSize(char* id)
322 int* piNumIndices = &numIndices;
324 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
329 int TriangleMeshFecDataDecomposer::fillIndices(char* id, int* buffer, int bufferLength, int logMask)
331 double* coordinates = NULL;
332 double* values = NULL;
335 int* piNumIndices = &numIndices;
337 int* piNumVertices = &numVertices;
339 int* triangleIndices = NULL;
344 int bufferOffset = 0;
346 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
347 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
349 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
350 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
353 if (numIndices == 0 || numVertices < 3)
358 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
360 for (int i = 0; i < numIndices; i++)
362 v0 = triangleIndices[3*i];
363 v1 = triangleIndices[3*i+1];
364 v2 = triangleIndices[3*i+2];
366 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
367 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
368 areFaceValuesValid(values, v0, v1, v2))
370 buffer[bufferOffset] = v0;
371 buffer[bufferOffset+1] = v1;
372 buffer[bufferOffset+2] = v2;
382 int TriangleMeshFecDataDecomposer::areFaceVerticesValid(double* coordinates, int v0, int v1, int v2, int logMask)
388 getVertexCoordinates(coordinates, v0, vertex0);
389 getVertexCoordinates(coordinates, v1, vertex1);
390 getVertexCoordinates(coordinates, v2, vertex2);
392 if (DecompositionUtils::isValid(vertex0[0], vertex0[1], vertex0[2]) &&
393 DecompositionUtils::isLogValid(vertex0[0], vertex0[1], vertex0[2], logMask) &&
394 DecompositionUtils::isValid(vertex1[0], vertex1[1], vertex1[2]) &&
395 DecompositionUtils::isLogValid(vertex1[0], vertex1[1], vertex1[2], logMask) &&
396 DecompositionUtils::isValid(vertex2[0], vertex2[1], vertex2[2]) &&
397 DecompositionUtils::isLogValid(vertex2[0], vertex2[1], vertex2[2], logMask))
405 int TriangleMeshFecDataDecomposer::areFaceValuesValid(double* values, int v0, int v1, int v2)
407 if (DecompositionUtils::isValid(values[v0], values[v1], values[v2]))
415 int TriangleMeshFecDataDecomposer::areFaceIndicesValid(int numVertices, int v0, int v1, int v2)
417 if (v0 < 0 || v0 >= numVertices || v1 < 0 || v1 >= numVertices || v2 < 0 || v2 >= numVertices)
425 void TriangleMeshFecDataDecomposer::getVertexCoordinates(double* coordinates, int index, double* vertexCoordinates)
427 vertexCoordinates[0] = coordinates[3*index];
428 vertexCoordinates[1] = coordinates[3*index+1];
429 vertexCoordinates[2] = coordinates[3*index+2];
432 int TriangleMeshFecDataDecomposer::getWireIndicesSize(char* id)
434 int numTriangles = 0;
435 int* piNumTriangles = &numTriangles;
437 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumTriangles);
439 return 6*numTriangles;
443 * To do: ouput shared edges once instead of twice (once per adjacent face).
445 int TriangleMeshFecDataDecomposer::fillWireIndices(char* id, int* buffer, int bufferLength, int logMask)
447 double* coordinates = NULL;
448 double* values = NULL;
451 int* piNumVertices = &numVertices;
453 int* piNumIndices = &numIndices;
454 int* triangleIndices = NULL;
459 int bufferOffset = 0;
461 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
463 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
464 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
465 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
468 if (numIndices == 0 || numVertices < 3)
473 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
475 for (int i = 0; i < numIndices; i++)
477 v0 = triangleIndices[3*i];
478 v1 = triangleIndices[3*i+1];
479 v2 = triangleIndices[3*i+2];
481 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
482 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
483 areFaceValuesValid(values, v0, v1, v2))
485 buffer[bufferOffset] = v0;
486 buffer[bufferOffset+1] = v1;
487 buffer[bufferOffset+2] = v1;
488 buffer[bufferOffset+3] = v2;
489 buffer[bufferOffset+4] = v2;
490 buffer[bufferOffset+5] = v0;