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 /** To take into account the presence of exterior colors:
127 * - We add 2 to the number of colors.
128 * - We skip the first color.
130 t = 3. / (2. * (colorsNumber + 2.));
131 scale = (colorsNumber - 1.) / (colorsNumber + 2);
133 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
134 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
135 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
137 /* Z-bounds are not taken into account if either of them is invalid */
138 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
140 minValue = zBounds[0];
141 maxValue = zBounds[1];
145 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
148 if (maxValue == minValue)
150 for (int i = 0; i < numVertices; i++)
152 buffer[bufferOffset++] = (float)minValue;
153 buffer[bufferOffset++] = 0;
154 buffer[bufferOffset++] = 0;
155 buffer[bufferOffset++] = 1;
160 for (int i = 0; i < numVertices; i++)
162 buffer[bufferOffset++] = (float)(t + scale * (values[i] - minValue) / (maxValue - minValue));
163 buffer[bufferOffset++] = 0;
164 buffer[bufferOffset++] = 0;
165 buffer[bufferOffset++] = 1;
170 void TriangleMeshFecDataDecomposer::fillColors(char* id, float* buffer, int bufferLength, int elementsSize)
173 char* parentFigure = NULL;
175 double* values = NULL;
176 double* zBounds = NULL;
177 double* colormap = NULL;
179 double minValue = 0.;
180 double maxValue = 0.;
181 double valueRange = 0.;
186 int colormapSize = 0;
187 int* piColormapSize = &colormapSize;
190 int* piNumVertices = &numVertices;
192 int minColorIndex = 0;
193 int maxColorIndex = 0;
195 int* colorRange = NULL;
196 int useOutsideColors = 0;
197 int bufferOffset = 0;
199 getGraphicObjectProperty(id, __GO_PARENT__, jni_string, (void**) &parent);
201 /* Temporary: to avoid getting a null parent_figure property when the object is built */
202 if (strcmp(parent, "") == 0)
207 getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_string, (void**) &parentFigure);
209 getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
210 getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
212 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
214 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
216 getGraphicObjectProperty(id, __GO_Z_BOUNDS__, jni_double_vector, (void**) &zBounds);
217 getGraphicObjectProperty(id, __GO_COLOR_RANGE__, jni_int_vector, (void**) &colorRange);
219 if (colorRange[0] != 0 || colorRange[1] != 0)
221 /* To do: use a scilab index to colormap index conversion function */
222 minColorIndex = colorRange[0] - 1;
223 maxColorIndex = colorRange[1] - 1;
225 if (minColorIndex < 0)
229 if (maxColorIndex < 0)
234 if (maxColorIndex > colormapSize-1)
236 maxColorIndex = colormapSize - 1;
238 if (minColorIndex > colormapSize-1)
240 minColorIndex = colormapSize - 1;
247 maxColorIndex = colormapSize-1;
250 computeMinMaxValues(values, numVertices, &minValue, &maxValue);
252 /* Z-bounds are not taken into account if either of them is invalid */
253 if ((zBounds[0] != 0.0 || zBounds[1] != 0.0) && (DecompositionUtils::isValid(zBounds[0]) && DecompositionUtils::isValid(zBounds[1])) && (zBounds[0] != zBounds[1]))
257 minValue = zBounds[0];
258 maxValue = zBounds[1];
260 getGraphicObjectProperty(id, __GO_OUTSIDE_COLOR__, jni_int_vector, (void**) &outsideColors);
262 if (outsideColors[0] != 0 || outsideColors[1] != 0)
264 useOutsideColors = 1;
266 ColorComputer::getDirectColor((double) outsideColors[0] - 1.0, colormap, colormapSize, minColor);
267 ColorComputer::getDirectColor((double) outsideColors[1] - 1.0, colormap, colormapSize, maxColor);
271 /* To be verified (when reverse z bounds are specified) */
272 if (DecompositionUtils::getAbsoluteValue(maxValue - minValue) < DecompositionUtils::getMinDoubleValue())
278 valueRange = maxValue - minValue;
281 for (int i = 0; i < numVertices; i++)
283 bufferOffset = elementsSize*i;
285 if (useOutsideColors == 1)
287 if (values[i] < minValue)
289 buffer[bufferOffset] = minColor[0];
290 buffer[bufferOffset+1] = minColor[1];
291 buffer[bufferOffset+2] = minColor[2];
293 else if (values[i] > maxValue)
295 buffer[bufferOffset] = maxColor[0];
296 buffer[bufferOffset+1] = maxColor[1];
297 buffer[bufferOffset+2] = maxColor[2];
301 /* To do: replace 0.5 by a macro-definition */
302 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
307 /* To do: replace 0.5 by a macro-definition */
308 ColorComputer::getColor(values[i], minValue, valueRange, 0.5, colormap, minColorIndex, maxColorIndex, colormapSize, &buffer[bufferOffset]);
311 if (elementsSize == 4)
313 buffer[bufferOffset+3] = 1.0;
319 void TriangleMeshFecDataDecomposer::computeMinMaxValues(double* values, int numValues, double* valueMin, double* valueMax)
321 double maxDouble = DecompositionUtils::getMaxDoubleValue();
322 double tmpValueMin = maxDouble;
323 double tmpValueMax = -maxDouble;
326 for (int i = 0; i < numValues; i++)
330 if (value < tmpValueMin)
335 if (value > tmpValueMax)
341 *valueMin = tmpValueMin;
342 *valueMax = tmpValueMax;
345 int TriangleMeshFecDataDecomposer::getIndicesSize(char* id)
348 int* piNumIndices = &numIndices;
350 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
355 int TriangleMeshFecDataDecomposer::fillIndices(char* id, int* buffer, int bufferLength, int logMask)
357 double* coordinates = NULL;
358 double* values = NULL;
361 int* piNumIndices = &numIndices;
363 int* piNumVertices = &numVertices;
365 int* triangleIndices = NULL;
370 int bufferOffset = 0;
372 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
373 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
375 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
376 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
379 if (numIndices == 0 || numVertices < 3)
384 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
386 for (int i = 0; i < numIndices; i++)
388 v0 = triangleIndices[3*i];
389 v1 = triangleIndices[3*i+1];
390 v2 = triangleIndices[3*i+2];
392 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
393 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
394 areFaceValuesValid(values, v0, v1, v2))
396 buffer[bufferOffset] = v0;
397 buffer[bufferOffset+1] = v1;
398 buffer[bufferOffset+2] = v2;
408 int TriangleMeshFecDataDecomposer::areFaceVerticesValid(double* coordinates, int v0, int v1, int v2, int logMask)
414 getVertexCoordinates(coordinates, v0, vertex0);
415 getVertexCoordinates(coordinates, v1, vertex1);
416 getVertexCoordinates(coordinates, v2, vertex2);
418 if (DecompositionUtils::isValid(vertex0[0], vertex0[1], vertex0[2]) &&
419 DecompositionUtils::isLogValid(vertex0[0], vertex0[1], vertex0[2], logMask) &&
420 DecompositionUtils::isValid(vertex1[0], vertex1[1], vertex1[2]) &&
421 DecompositionUtils::isLogValid(vertex1[0], vertex1[1], vertex1[2], logMask) &&
422 DecompositionUtils::isValid(vertex2[0], vertex2[1], vertex2[2]) &&
423 DecompositionUtils::isLogValid(vertex2[0], vertex2[1], vertex2[2], logMask))
431 int TriangleMeshFecDataDecomposer::areFaceValuesValid(double* values, int v0, int v1, int v2)
433 if (DecompositionUtils::isValid(values[v0], values[v1], values[v2]))
441 int TriangleMeshFecDataDecomposer::areFaceIndicesValid(int numVertices, int v0, int v1, int v2)
443 if (v0 < 0 || v0 >= numVertices || v1 < 0 || v1 >= numVertices || v2 < 0 || v2 >= numVertices)
451 void TriangleMeshFecDataDecomposer::getVertexCoordinates(double* coordinates, int index, double* vertexCoordinates)
453 vertexCoordinates[0] = coordinates[3*index];
454 vertexCoordinates[1] = coordinates[3*index+1];
455 vertexCoordinates[2] = coordinates[3*index+2];
458 int TriangleMeshFecDataDecomposer::getWireIndicesSize(char* id)
460 int numTriangles = 0;
461 int* piNumTriangles = &numTriangles;
463 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumTriangles);
465 return 6*numTriangles;
469 * To do: ouput shared edges once instead of twice (once per adjacent face).
471 int TriangleMeshFecDataDecomposer::fillWireIndices(char* id, int* buffer, int bufferLength, int logMask)
473 double* coordinates = NULL;
474 double* values = NULL;
477 int* piNumVertices = &numVertices;
479 int* piNumIndices = &numIndices;
480 int* triangleIndices = NULL;
485 int bufferOffset = 0;
487 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_INDICES__, jni_int, (void**) &piNumIndices);
489 getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES__, jni_int, (void**) &piNumVertices);
490 getGraphicObjectProperty(id, __GO_DATA_MODEL_COORDINATES__, jni_double_vector, (void**) &coordinates);
491 getGraphicObjectProperty(id, __GO_DATA_MODEL_VALUES__, jni_double_vector, (void**) &values);
494 if (numIndices == 0 || numVertices < 3)
499 getGraphicObjectProperty(id, __GO_DATA_MODEL_INDICES__, jni_int_vector, (void**) &triangleIndices);
501 for (int i = 0; i < numIndices; i++)
503 v0 = triangleIndices[3*i];
504 v1 = triangleIndices[3*i+1];
505 v2 = triangleIndices[3*i+2];
507 if (areFaceIndicesValid(numVertices, v0, v1, v2) &&
508 areFaceVerticesValid(coordinates, v0, v1, v2, logMask) &&
509 areFaceValuesValid(values, v0, v1, v2))
511 buffer[bufferOffset] = v0;
512 buffer[bufferOffset+1] = v1;
513 buffer[bufferOffset+2] = v1;
514 buffer[bufferOffset+3] = v2;
515 buffer[bufferOffset+4] = v2;
516 buffer[bufferOffset+5] = v0;