1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2011 - DIGITEO - Manuel Juliachs
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
#include "DecompositionUtils.hxx"
#include "ColorComputer.hxx"
#include "Fac3DColorComputer.hxx"
#include "Fac3DDecomposer.hxx"
extern "C"
{
#include <stdlib.h>
#include <string.h>
#include "getGraphicObjectProperty.h"
#include "graphicObjectProperties.h"
}
int Fac3DDecomposer::getDataSize(int id)
{
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
return numVerticesPerGon * numGons;
}
void Fac3DDecomposer::fillVertices(int id, float* buffer, int bufferLength, int elementsSize, int coordinateMask, double* scale, double* translation, int logMask)
{
double* x = NULL;
double* y = NULL;
double* z = NULL;
int i = 0;
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
int bufferOffset = 0;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
getGraphicObjectProperty(id, __GO_DATA_MODEL_X__, jni_int, (void**) &x);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Y__, jni_int, (void**) &y);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_int, (void**) &z);
for (i = 0; i < numVerticesPerGon * numGons; i++)
{
if (coordinateMask & 0x1)
{
double xi = x[i];
if (logMask & 0x1)
{
xi = DecompositionUtils::getLog10Value(x[i]);
}
buffer[bufferOffset] = (float)(xi * scale[0] + translation[0]);
}
if (coordinateMask & 0x2)
{
double yi = y[i];
if (logMask & 0x2)
{
yi = DecompositionUtils::getLog10Value(y[i]);
}
buffer[bufferOffset + 1] = (float)(yi * scale[1] + translation[1]);
}
if (coordinateMask & 0x4)
{
double zi = z[i];
if (logMask & 0x4)
{
zi = DecompositionUtils::getLog10Value(z[i]);
}
buffer[bufferOffset + 2] = (float)(zi * scale[2] + translation[2]);
}
if (elementsSize == 4 && (coordinateMask & 0x8))
{
buffer[bufferOffset + 3] = 1.0;
}
bufferOffset += 4;
}
}
void Fac3DDecomposer::fillTextureCoordinates(int id, float* buffer, int bufferLength)
{
int parentFigure = 0;
int* pparentFigure = &parentFigure;
int parent = 0;
int* pparent = &parent;
double* colors = NULL;
double* colormap = NULL;
double* z = NULL;
double color = 0.;
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
int numColors = 0;
int* piNumColors = &numColors;
int colormapSize = 0;
int* piColormapSize = &colormapSize;
int colorFlag = 0;
int* piColorFlag = &colorFlag;
int dataMapping = 0;
int* piDataMapping = &dataMapping;
int perVertex = 0;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_COLORS__, jni_int, (void**) &piNumColors);
getGraphicObjectProperty(id, __GO_DATA_MODEL_COLORS__, jni_double_vector, (void**) &colors);
parent = getParentObject(id);
/* Temporary: to avoid getting a null parent_figure property when the object is built */
if (parent == 0)
{
return;
}
getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_int, (void**) &pparentFigure);
if (parentFigure == 0)
{
return;
}
getGraphicObjectProperty(id, __GO_COLOR_FLAG__, jni_int, (void**) &piColorFlag);
getGraphicObjectProperty(id, __GO_DATA_MAPPING__, jni_int, (void**) &piDataMapping);
/* Do not fill */
if (colorFlag == 0)
{
return;
}
getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
if (numColors == numGons * numVerticesPerGon)
{
perVertex = 1;
}
else if (numColors == numGons)
{
perVertex = 0;
}
getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_int, (void**) &z);
if (colorFlag == 1)
{
fillNormalizedZColorsTextureCoordinates(buffer, bufferLength, colormap, colormapSize, z, numGons, numVerticesPerGon);
}
else if (colorFlag > 1 && numColors == 0)
{
/*
* The color buffer must be filled with the color_mode value.
* To do: correctly take into account Nan and infinite values.
*/
int colorMode = 0;
int* piColorMode = &colorMode;
getGraphicObjectProperty(id, __GO_COLOR_MODE__, jni_int, (void**) &piColorMode);
color = (double) colorMode;
color = DecompositionUtils::getAbsoluteValue(color);
fillConstantColorsTextureCoordinates(buffer, bufferLength, colormap, colormapSize,
color, numGons, numVerticesPerGon);
}
else
{
fillDataColorsTextureCoordinates(buffer, bufferLength, colormap, colormapSize,
colors, colorFlag, perVertex, dataMapping, numGons, numVerticesPerGon);
}
releaseGraphicObjectProperty(__GO_COLORMAP__, colormap, jni_double_vector, colormapSize);
}
void Fac3DDecomposer::fillNormalizedZColorsTextureCoordinates(float* buffer, int bufferLength, double* colormap, int colormapSize,
double* z, int numGons, int numVerticesPerGon)
{
double zavg = 0.;
double zMin = 0.;
double zMax = 0.;
double zRange = 0.;
double minDoubleValue = 0.;
float index = 0.;
int i = 0;
int j = 0;
int bufferOffset = 0;
computeMinMaxValues(z, numGons * numVerticesPerGon, numGons, numVerticesPerGon, ALL_VALUES, &zMin, &zMax);
minDoubleValue = DecompositionUtils::getMinDoubleValue();
/* To be verified */
if ((zMax - zMin) < minDoubleValue)
{
zRange = 1.0;
}
else
{
zRange = zMax - zMin;
}
for (i = 0; i < numGons; i++)
{
/* Per-face average */
zavg = computeAverageValue(&z[i * numVerticesPerGon], numVerticesPerGon);
index = (float)((ColorComputer::getIndex(zavg, zMin, zRange, Z_COLOR_OFFSET, 0, colormapSize - 1) + 2.0 + COLOR_TEXTURE_OFFSET) / (float) (colormapSize + 2));
for (j = 0; j < numVerticesPerGon; j++)
{
buffer[bufferOffset++] = index;
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 1.0;
}
}
}
void Fac3DDecomposer::fillConstantColorsTextureCoordinates(float* buffer, int bufferLength, double* colormap, int colormapSize,
double colorValue, int numGons, int numVerticesPerGon)
{
int bufferOffset = 0;
double index = (ColorComputer::getClampedDirectIndex(colorValue - 1.0, colormapSize) + 2.0 + COLOR_TEXTURE_OFFSET) / (float) (colormapSize + 2);
for (int i = 0; i < numGons * numVerticesPerGon; i++)
{
buffer[bufferOffset++] = (float)index;
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 1.0;
}
}
void Fac3DDecomposer::fillDataColorsTextureCoordinates(float* buffer, int bufferLength, double* colormap, int colormapSize,
double* colors, int colorFlag, int perVertex, int dataMapping, int numGons, int numVerticesPerGon)
{
double colMin = 0.;
double colRange = 0.;
double color = 0.;
double colorTextureOffset = 0.;
double index = 0.;
int bufferOffset = 0;
int numColors = 0;
Fac3DColorComputer colorComputer;
if (perVertex)
{
numColors = numGons * numVerticesPerGon;
}
else
{
numColors = numGons;
}
colorComputer = Fac3DColorComputer(colors, numColors, colorFlag, dataMapping, numGons, numVerticesPerGon);
/* 0: colors are scaled */
if (dataMapping == 0)
{
colorComputer.getColorRangeValue(&colMin, &colRange);
}
/*
* The color texture offset value is used to center color sub-intervals
* on integer index values when interpolated shading is used or to be sure
* to fetch the correct color value when flat shading is used.
*/
colorTextureOffset = COLOR_TEXTURE_OFFSET;
for (int i = 0; i < numGons; i++)
{
for (int j = 0; j < numVerticesPerGon; j++)
{
color = colorComputer.getOutputFacetColor(i, j);
if (dataMapping == 1)
{
color = DecompositionUtils::getAbsoluteValue(color);
index = ColorComputer::getClampedDirectIndex(color - 1.0 , colormapSize);
}
else if (dataMapping == 0)
{
index = ColorComputer::getIndex(color, colMin, colRange, COLOR_OFFSET, 0, colormapSize - 1);
}
/* The offset corresponding to the black and white colors must added to the index and the colormap size. */
buffer[bufferOffset++] = (float) ((index + colorTextureOffset + 2.0) / (double) (colormapSize + 2));
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 0;
buffer[bufferOffset++] = 1.0;
}
}
}
double Fac3DDecomposer::computeAverageValue(double* values, int numVertices)
{
double averageValue = 0.0;
for (int i = 0; i < numVertices; i++)
{
averageValue += values[i];
}
averageValue /= (double) numVertices;
return averageValue;
}
void Fac3DDecomposer::computeMinMaxValues(double* values, int numValues, int numGons, int numVerticesPerGon, int minMaxComputation,
double* valueMin, double* valueMax)
{
double maxDouble = DecompositionUtils::getMaxDoubleValue();
double tmpValueMin = maxDouble;
double tmpValueMax = -maxDouble;
double value = 0.;
int numIterations = 0;
if (minMaxComputation != ALL_VALUES)
{
numIterations = numGons;
}
else
{
numIterations = numValues;
}
for (int i = 0; i < numIterations; i++)
{
if (minMaxComputation == FIRST_VERTEX_VALUE)
{
value = values[i * numVerticesPerGon];
}
else if (minMaxComputation == FACE_AVERAGE)
{
value = computeAverageValue(&values[i * numVerticesPerGon], numVerticesPerGon);
}
else
{
value = values[i];
}
if (DecompositionUtils::isValid(value))
{
if (value < tmpValueMin)
{
tmpValueMin = value;
}
if (value > tmpValueMax)
{
tmpValueMax = value;
}
}
}
*valueMin = tmpValueMin;
*valueMax = tmpValueMax;
}
int Fac3DDecomposer::getIndicesSize(int id)
{
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
if (numVerticesPerGon < 2)
{
return 0;
}
return 3 * (numVerticesPerGon - 2) * numGons;
}
/*
* To do: use a polygon triangulation algorithm, as the fan decomposition used may produce
* overlapping triangles for non-convex polygons.
*/
int Fac3DDecomposer::fillIndices(int id, int* buffer, int bufferLength, int logMask)
{
double* x = NULL;
double* y = NULL;
double* z = NULL;
double xc = 0.;
double yc = 0.;
double zc = 0.;
double* colors = NULL;
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
int numColors = 0;
int* piNumColors = &numColors;
int colorFlag = 0;
int* piColorFlag = &colorFlag;
int dataMapping = 0;
int* piDataMapping = &dataMapping;
int bufferOffset = 0;
int vertexOffset = 0;
Fac3DColorComputer colorComputer;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_COLORS__, jni_int, (void**) &piNumColors);
getGraphicObjectProperty(id, __GO_DATA_MODEL_COLORS__, jni_double_vector, (void**) &colors);
getGraphicObjectProperty(id, __GO_COLOR_FLAG__, jni_int, (void**) &piColorFlag);
getGraphicObjectProperty(id, __GO_DATA_MAPPING__, jni_int, (void**) &piDataMapping);
/* At least 3 vertices per N-gon are required to output triangles. */
if (numVerticesPerGon < 3)
{
return 0;
}
colorComputer = Fac3DColorComputer(colors, numColors, colorFlag, dataMapping, numGons, numVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_X__, jni_double_vector, (void**) &x);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Y__, jni_double_vector, (void**) &y);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_double_vector, (void**) &z);
for (int i = 0; i < numGons; i++)
{
int isValid = 1;
for (int j = 0; j < numVerticesPerGon; j++)
{
xc = x[vertexOffset + j];
yc = y[vertexOffset + j];
zc = z[vertexOffset + j];
if (!DecompositionUtils::isValid(xc, yc, zc) || !DecompositionUtils::isLogValid(xc, yc, zc, logMask))
{
isValid = 0;
break;
}
}
if (isValid == 0 || colorComputer.isFacetColorValid(i) == 0)
{
vertexOffset += numVerticesPerGon;
continue;
}
/* Performs a fan decomposition, vertices are ordered counter-clockwise. */
for (int j = 0; j < numVerticesPerGon - 2; j++)
{
buffer[bufferOffset] = vertexOffset;
buffer[bufferOffset + 1] = vertexOffset + j + 2;
buffer[bufferOffset + 2] = vertexOffset + j + 1;
bufferOffset += 3;
}
vertexOffset += numVerticesPerGon;
}
return bufferOffset;
}
int Fac3DDecomposer::getWireIndicesSize(int id)
{
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
return 2 * numVerticesPerGon * numGons;
}
int Fac3DDecomposer::fillWireIndices(int id, int* buffer, int bufferLength, int logMask)
{
double* x = NULL;
double* y = NULL;
double* z = NULL;
double xc = 0.;
double yc = 0.;
double zc = 0.;
int numVerticesPerGon = 0;
int* piNumVerticesPerGon = &numVerticesPerGon;
int numGons = 0;
int* piNumGons = &numGons;
int bufferOffset = 0;
int vertexOffset = 0;
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_VERTICES_PER_GON__, jni_int, (void**) &piNumVerticesPerGon);
getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_GONS__, jni_int, (void**) &piNumGons);
/* At least 2 vertices per N-gon are required to output segments. */
if (numVerticesPerGon < 2)
{
return 0;
}
getGraphicObjectProperty(id, __GO_DATA_MODEL_X__, jni_double_vector, (void**) &x);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Y__, jni_double_vector, (void**) &y);
getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_double_vector, (void**) &z);
for (int i = 0; i < numGons; i++)
{
int isValid = 1;
for (int j = 0; j < numVerticesPerGon; j++)
{
xc = x[vertexOffset + j];
yc = y[vertexOffset + j];
zc = z[vertexOffset + j];
if (!DecompositionUtils::isValid(xc, yc, zc) || !DecompositionUtils::isLogValid(xc, yc, zc, logMask))
{
isValid = 0;
break;
}
}
if (isValid == 0)
{
vertexOffset += numVerticesPerGon;
continue;
}
for (int j = 0; j < numVerticesPerGon; j++)
{
buffer[bufferOffset] = vertexOffset + j;
buffer[bufferOffset + 1] = vertexOffset + (j + 1) % numVerticesPerGon;
bufferOffset += 2;
}
vertexOffset += numVerticesPerGon;
}
return bufferOffset;
}
|