summaryrefslogtreecommitdiff
path: root/modules/graphic_objects/src/cpp/NgonGridMatplotData.cpp
blob: a650d55329b75be6ec0ea63225b534704203cdc6 (plain)
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2011-2012 - DIGITEO - Manuel Juliachs
 *  Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
 *
 *  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 "NgonGridMatplotData.hxx"
#include "Texture.hxx"

#include <climits>

extern "C" {
#include <string.h>
#include <stdlib.h>

#include "graphicObjectProperties.h"
}

const bool NgonGridMatplotData::isLittleEndian = NgonGridMatplotData::initEndian();
bool NgonGridMatplotData::isABGRSupported = false;

NgonGridMatplotData::NgonGridMatplotData(void)
{
    this->data = NULL;
    this->dataSize = 0;
    this->scilabData = NULL;
    this->type = 0;
    this->imagetype = MATPLOT_NONE;
}

NgonGridMatplotData::~NgonGridMatplotData(void)
{
    disposeTextureData();
    if (this->scilabData)
    {
        delete[] (unsigned char *)this->scilabData;
        this->scilabData = NULL;
    }
}

int NgonGridMatplotData::getPropertyFromName(int propertyName)
{
    switch (propertyName)
    {
        case __GO_DATA_MODEL_GRID_SIZE__ :
            return GRID_SIZE;
        case __GO_DATA_MODEL_MATPLOT_BOUNDS__ :
            return MATPLOT_BOUNDS;
        case __GO_DATA_MODEL_MATPLOT_TYPE__ :
            return MATPLOT_TYPE;
        case __GO_DATA_MODEL_Z__ :
            return Z_COORDINATES;
        case __GO_DATA_MODEL_MATPLOT_GL_TYPE__ :
            return MATPLOT_GL_TYPE;
        case __GO_DATA_MODEL_MATPLOT_DATA_INFOS__ :
            return MATPLOT_DATA_INFOS;
        case __GO_DATA_MODEL_MATPLOT_DATA_TYPE__ :
            return MATPLOT_DATA_TYPE;
        case __GO_DATA_MODEL_MATPLOT_DATA_ORDER__ :
            return MATPLOT_DATA_ORDER;
        case __GO_DATA_MODEL_MATPLOT_IMAGE_TYPE__ :
            return MATPLOT_IMAGE_TYPE;
        case __GO_DATA_MODEL_MATPLOT_IMAGE_DATA__ :
            return MATPLOT_IMAGE_DATA;
        case __GO_DATA_MODEL_MATPLOT_IMAGE_DATASIZE__ :
            return MATPLOT_IMAGE_DATASIZE;
        default :
            return NgonGridData::getPropertyFromName(propertyName);
    }
}

int NgonGridMatplotData::setDataProperty(int property, void const* value, int numElements)
{
    switch (property)
    {
        case GRID_SIZE :
        {
            return setGridSize((int const*) value);
        }
        case MATPLOT_BOUNDS :
        {
            setBounds((double const*) value);
            break;
        }
        case Z_COORDINATES :
        {
            setDataZ((double const*) value, numElements);
            break;
        }
        case MATPLOT_IMAGE_TYPE :
        {
            return setImageType(*((int const*) value));
        }
        case MATPLOT_DATA_INFOS :
        {
            setDataInfos(*((int const*) value));
            break;
        }
        case MATPLOT_DATA_TYPE :
        {
            setDataType(*((int const*) value));
            break;
        }
        case MATPLOT_DATA_ORDER :
        {
            setDataOrder(*((int const*) value));
            break;
        }
        case MATPLOT_IMAGE_DATA :
        {
            setImageData((void const *) value, numElements);
            break;
        }
        default :
        {
            return NgonGridData::setDataProperty(property, value, numElements);
        }
    }

    return 1;
}

void NgonGridMatplotData::getDataProperty(int property, void **_pvData)
{
    switch (property)
    {
        case MATPLOT_BOUNDS :
        {
            *_pvData = getBounds();
            break;
        }
        case MATPLOT_TYPE :
        {
            ((int *) *_pvData)[0] = getType();
            break;
        }
        case Z_COORDINATES :
        {
            *_pvData = getScilabData();
            break;
        }
        case MATPLOT_IMAGE_TYPE :
        {
            ((int *) *_pvData)[0] = getImageType();
            break;
        }
        case MATPLOT_GL_TYPE :
        {
            ((int *) *_pvData)[0] = getGLType();
            break;
        }
        case MATPLOT_DATA_INFOS :
        {
            ((int *) *_pvData)[0] = getDataInfos();
            break;
        }
        case MATPLOT_DATA_TYPE :
        {
            ((int *) *_pvData)[0] = getDataType();
            break;
        }
        case MATPLOT_DATA_ORDER :
        {
            ((int *) *_pvData)[0] = getDataOrder();
            break;
        }
        case MATPLOT_IMAGE_DATA :
        {
            *_pvData = getImageData();
            break;
        }
        case MATPLOT_IMAGE_DATASIZE :
        {
            ((unsigned int *) *_pvData)[0] = getImageDataSize();
            break;
        }
        default :
        {
            NgonGridData::getDataProperty(property, _pvData);
            break;
        }
    }
}

/*
 * To be done: refactoring with NgonGridData, as these two classes'
 * setGridSize methods are almost identical.
 */
int NgonGridMatplotData::setGridSize(int const* gridSize)
{
    int newXSize = 0;
    int newYSize = 0;
    int xModified = 0;
    int yModified = 0;
    int zModified = 0;
    int result = 0;
    int formerGridSize = 0;

    double* newXCoordinates = NULL;
    double* newYCoordinates = NULL;
    double* newZCoordinates = NULL;

    result = 1;

    xModified = 0;
    yModified = 0;
    zModified = 0;

    if ((gridSize[0] != 1) && (gridSize[1] != 1))
    {
        return 0;
    }

    if ((gridSize[2] != 1) && (gridSize[3] != 1))
    {
        return 0;
    }

    newXSize = gridSize[0] * gridSize[1];
    newYSize = gridSize[2] * gridSize[3];

    if (newXSize != xSize)
    {
        xModified = 1;

        try
        {
            newXCoordinates = new double[newXSize];
        }
        catch (const std::exception& e)
        {
            e.what();
            result = 0;
        }
    }

    if (newYSize != ySize)
    {
        yModified = 1;

        try
        {
            newYCoordinates = new double[newYSize];
        }
        catch (const std::exception& e)
        {
            e.what();
            result = 0;
        }
    }

    if (!result || (!xModified && !yModified))
    {
        return result;
    }

    if (xSize > 0 && ySize > 0)
    {
        formerGridSize = (xSize - 1) * (ySize - 1);
    }
    else
    {
        formerGridSize = 0;
    }

    if ((newXSize - 1) * (newYSize - 1) != formerGridSize)
    {
        zModified = 1;

        try
        {
            newZCoordinates = new double[(newXSize - 1) * (newYSize - 1)];
        }
        catch (const std::exception& e)
        {
            e.what();
            result = 0;
        }
    }

    if (result)
    {
        if (xModified)
        {
            if (xSize > 0)
            {
                delete [] xCoordinates;
            }

            xCoordinates = newXCoordinates;
            xSize = newXSize;
        }

        xDimensions[0] = gridSize[0];
        xDimensions[1] = gridSize[1];

        if (yModified)
        {
            if (ySize > 0)
            {
                delete [] yCoordinates;
            }

            yCoordinates = newYCoordinates;
            ySize = newYSize;
        }

        yDimensions[0] = gridSize[2];
        yDimensions[1] = gridSize[3];

        if (zModified)
        {
            if (formerGridSize > 0)
            {
                delete [] zCoordinates;
            }

            zCoordinates = newZCoordinates;

            numGons = (xSize - 1) * (ySize - 1);

        }

        if (xModified || yModified)
        {
            computeCoordinates();
        }

    }
    else
    {
        /* Failed allocation(s) */

        if (xModified && (newXCoordinates != NULL))
        {
            delete [] newXCoordinates;
        }

        if (yModified && (newYCoordinates != NULL))
        {
            delete [] newYCoordinates;
        }

        if (zModified && (newZCoordinates != NULL))
        {
            delete [] newZCoordinates;
        }

    }

    return result;
}

void NgonGridMatplotData::setBounds(double const* bounds)
{
    if (bounds == NULL)
    {
        /* Type 0 object: bounds are computed from element indices by the setGridSize method */
        type = 0;
    }
    else
    {
        /* To be done: test whether max > min */
        boundingRectangle[0] = bounds[0];
        boundingRectangle[1] = bounds[2];
        boundingRectangle[2] = bounds[1];
        boundingRectangle[3] = bounds[3];

        type = 1;
    }
}

double* NgonGridMatplotData::getBounds(void)
{
    return boundingRectangle;
}

void NgonGridMatplotData::computeCoordinates(void)
{
    if (type == 0)
    {
        for (int i = 0; i < xSize; i++)
        {
            xCoordinates[i] = 0.5 + (double) i;
        }

        for (int i = 0; i < ySize; i++)
        {
            yCoordinates[i] = 0.5 + (double) i;
        }

        boundingRectangle[0] = 0.5;
        boundingRectangle[1] = 0.5 + (double) (xSize - 1);
        boundingRectangle[2] = 0.5;
        boundingRectangle[3] = 0.5 + (double) (ySize - 1);
    }
    else
    {
        double min = boundingRectangle[0];
        double max = boundingRectangle[1];
        int numElements = 0;


        if (xSize == 1)
        {
            numElements = 1;
        }
        else
        {
            numElements = xSize - 1;
        }

        for (int i = 0; i < xSize; i++)
        {
            xCoordinates[i] = min + (double) i * (max - min) / (double) numElements;
        }

        min = boundingRectangle[2];
        max = boundingRectangle[3];

        if (ySize == 1)
        {
            numElements = 1;
        }
        else
        {
            numElements = ySize - 1;
        }

        for (int i = 0; i < ySize; i++)
        {
            yCoordinates[i] = min + (double) i * (max - min) / (double) numElements;
        }
    }
}

void NgonGridMatplotData::setDataZ(double const* data, int numElements)
{
    if (numElements > (xSize - 1) * (ySize - 1))
    {
        return;
    }

    for (int i = 0; i < numElements; i++)
    {
        zCoordinates[i] = data[i];
    }
}

int NgonGridMatplotData::setImageType(int imagetype)
{
    const ImageType type = (ImageType)imagetype;

    if (this->imagetype != type)
    {
        int grid[4] = {xSize, 1, ySize, 1};
        if (this->datatype == MATPLOT_UChar || this->datatype == MATPLOT_Char)
        {
            if (this->imagetype == MATPLOT_RGB)
            {
                grid[2] = (ySize - 1) * 3 + 1;
            }
            else if (this->imagetype == MATPLOT_RGBA || this->imagetype == MATPLOT_ARGB)
            {
                grid[2] = (ySize - 1) * 4 + 1;
            }

            if (type == MATPLOT_RGB)
            {
                if ((grid[2] - 1) % 3 != 0)
                {
                    return 0;
                }
                grid[2] = (grid[2] - 1) / 3 + 1;
            }
            else if (type == MATPLOT_RGBA || type == MATPLOT_ARGB)
            {
                if ((grid[2] - 1) % 4 != 0)
                {
                    return 0;
                }
                grid[2] = (grid[2] - 1) / 4 + 1;
            }

            this->setGridSize(grid);
        }

        this->imagetype = type;

        if (this->scilabData)
        {
            setImageData(this->scilabData, (xSize - 1) * (ySize - 1));
        }
    }

    return 1;
}

int NgonGridMatplotData::getImageType()
{
    return (int)this->imagetype;
}

int NgonGridMatplotData::getGLType()
{
    return (int)this->gltype;
}

int NgonGridMatplotData::getDataType()
{
    return (int)this->datatype;
}

void NgonGridMatplotData::setDataType(int datatype)
{
    if (this->datatype != (DataType)datatype)
    {
        this->datatype = (DataType)datatype;
        disposeTextureData();
    }
}

int NgonGridMatplotData::getDataOrder()
{
    return (int)this->dataorder;
}

void NgonGridMatplotData::setDataOrder(int dataorder)
{
    this->dataorder = (DataOrder)dataorder;
}

void * NgonGridMatplotData::getImageData()
{
    if (this->data)
    {
        return this->data;
    }
    else if (this->scilabData)
    {
        setImageData(this->scilabData, (xSize - 1) * (ySize - 1));
        return this->data;
    }

    return NULL;
}

void * NgonGridMatplotData::getScilabData()
{
    return this->scilabData;
}

unsigned int NgonGridMatplotData::getImageDataSize()
{
    return this->dataSize;
}

void NgonGridMatplotData::setDataInfos(int infos)
{
    setDataType(infos & 0xFF);
    setDataOrder((infos & 0xFF00) >> 8);
    setImageType((infos & 0xFF0000) >> 16);
}

int NgonGridMatplotData::getDataInfos()
{
    return buildMatplotType(this->datatype, this->dataorder, this->imagetype);
}

void NgonGridMatplotData::setImageData(void const* data, const int numElements)
{
    if (!data)
    {
        disposeTextureData();
        return;
    }

    unsigned int dataSize = 0;
    const int N = ySize - 1;
    const int M = xSize - 1;
    const int NM = N * M;

    if (numElements > NM)
    {
        return;
    }

    if (data != this->scilabData)
    {
        if (scilabData)
        {
            delete[] (unsigned char *)scilabData;
            scilabData = NULL;
        }

        unsigned int _size;

        switch (datatype)
        {
            case MATPLOT_HM3_Char :
            case MATPLOT_HM3_UChar :
                _size = numElements * 3;
                break;
            case MATPLOT_HM3_Double :
                _size = numElements * sizeof(double) * 3;
                break;
            case MATPLOT_HM4_Char :
            case MATPLOT_HM4_UChar :
                _size = numElements * 4;
                break;
            case MATPLOT_HM4_Double :
                _size = numElements * sizeof(double) * 4;
                break;
            case MATPLOT_HM1_Char :
            case MATPLOT_HM1_UChar :
            case MATPLOT_Char :
            case MATPLOT_UChar :
                _size = numElements;
                break;
            case MATPLOT_Int :
            case MATPLOT_UInt :
                _size = numElements * sizeof(int);
                break;
            case MATPLOT_Short :
            case MATPLOT_UShort :
                _size = numElements * sizeof(short);
                break;
            case MATPLOT_HM1_Double :
            case MATPLOT_Double :
                _size = numElements * sizeof(double);
                break;
        }
        this->scilabData = new unsigned char[_size];
        // todo: on peut ameliorer ca
        // pr certains type de donnees (et certains modes) scilabData == data
        // dc on peut eviter cette copie
        memcpy(this->scilabData, data, _size);
    }

    void * dest = this->data;
    if (this->imagetype == MATPLOT_INDEX)
    {
        this->gltype = MATPLOT_GL_RGBA_BYTE;
    }
    else if (Texture::getImage(data, numElements, this->datatype, this->imagetype, &(this->data), &(this->dataSize), &(this->gltype)))
    {
        if (dest)
        {
            delete[] (unsigned char *)dest;
        }
    }

    //std::cout << this->imagetype << "::" << this->datatype << "::" << this->dataorder << "::" << this->dataSize << "::" << (void*)this->data << std::endl;
}

int NgonGridMatplotData::getType(void)
{
    return type;
}

bool NgonGridMatplotData::initEndian()
{
    const int num = 1;

    return *(char *)&num == 1;
}

void NgonGridMatplotData::setABGRSupported(bool _isABGRSupported)
{
    isABGRSupported = _isABGRSupported;
}

void NgonGridMatplotData::disposeTextureData(void)
{
    if (this->data)
    {
        delete[] (unsigned char *)this->data;
        this->data = NULL;
        this->dataSize = 0;
    }
}