summaryrefslogtreecommitdiff
path: root/modules/graphic_objects/src/cpp/NgonGeneralData.cpp
blob: b8ea011b5fac48c82e07e73909679e779351cfff (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
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2010 - 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 <iostream>

#include "NgonGeneralData.hxx"
#include "DataProperties.hxx"

extern "C" {
#include "BOOL.h"

#include <string.h>

#include "graphicObjectProperties.h"
}

NgonGeneralData::NgonGeneralData(void)
{
    numGons = 0;
    numVerticesPerGon = 0;

    coordinates = NULL;

    colorValues = NULL;
    numColors = 0;
}

NgonGeneralData::~NgonGeneralData(void)
{
    if (numGons * numVerticesPerGon > 0)
    {
        delete [] coordinates;
    }

    if (numColors > 0)
    {
        delete [] colorValues;
    }
}

int NgonGeneralData::getPropertyFromName(int propertyName)
{
    switch (propertyName)
    {
        case __GO_DATA_MODEL_NUM_ELEMENTS_ARRAY__ :
            return NUM_ELEMENTS_ARRAY;
        case __GO_DATA_MODEL_COORDINATES__ :
            return COORDINATES;
        case __GO_DATA_MODEL_X__ :
            return X_COORDINATES;
        case __GO_DATA_MODEL_Y__ :
            return Y_COORDINATES;
        case __GO_DATA_MODEL_Z__ :
            return Z_COORDINATES;
        case __GO_DATA_MODEL_COLORS__ :
            return COLORS;
        case __GO_DATA_MODEL_NUM_COLORS__ :
            return NUM_COLORS;
        default :
            return NgonData::getPropertyFromName(propertyName);
    }
}

int NgonGeneralData::setDataProperty(int property, void const* value, int numElements)
{
    switch (property)
    {
        case NUM_ELEMENTS_ARRAY :
            return setNumElementsArray((int const*) value);
        case COORDINATES :
            setData((double const*) value, numElements);
            break;
        case X_COORDINATES :
            setDataX((double const*) value, numElements);
            break;
        case Y_COORDINATES :
            setDataY((double const*) value, numElements);
            break;
        case Z_COORDINATES :
            setDataZ((double const*) value, numElements);
            break;
        case COLORS :
            setColors((double const*) value, numElements);
            break;
        default :
            return NgonData::setDataProperty(property, value, numElements);
    }

    return 1;
}

void NgonGeneralData::getDataProperty(int property, void **_pvData)
{
    switch (property)
    {
        case NUM_ELEMENTS_ARRAY :
            /* Not implemented yet */
            //    getNumElementsArray();
            break;
        case COORDINATES :
            *_pvData = getData();
            break;
        case X_COORDINATES :
            *_pvData = getDataX();
            break;
        case Y_COORDINATES :
            *_pvData = getDataY();
            break;
        case Z_COORDINATES :
            *_pvData = getDataZ();
            break;
        case COLORS :
            *_pvData = getColors();
            break;
        case NUM_COLORS :
            ((int *) *_pvData)[0] = getNumColors();
            break;
        default :
            NgonData::getDataProperty(property, _pvData);
    }
}

double* NgonGeneralData::getData()
{
    return coordinates;
}

double* NgonGeneralData::getDataX(void)
{
    return coordinates;
}

double* NgonGeneralData::getDataY(void)
{
    return &coordinates[numGons * numVerticesPerGon];
}

double* NgonGeneralData::getDataZ(void)
{
    return &coordinates[2 * numGons * numVerticesPerGon];
}

/*
 * Only partially implemented
 * Must be made consistent with setNumElementsArray
 */
void NgonGeneralData::setData(double const* data, int numElements)
{
    if (numElements != numGons * numVerticesPerGon)
    {
        delete [] coordinates;

        numVerticesPerGon = numElements / numGons;
        coordinates = new double[3 * numElements];
    }

    memcpy(coordinates, data, 3 * numElements * sizeof(double));
}

void NgonGeneralData::setDataX(double const* data, int numElements)
{
    double* xCoordinates = NULL;

    xCoordinates = coordinates;
    memcpy(xCoordinates, data, numElements * sizeof(double));
}

void NgonGeneralData::setDataY(double const* data, int numElements)
{
    double* yCoordinates;

    yCoordinates = &coordinates[numGons * numVerticesPerGon];
    memcpy(yCoordinates, data, numElements * sizeof(double));
}

void NgonGeneralData::setDataZ(double const* data, int numElements)
{
    double* zCoordinates = NULL;

    zCoordinates = &coordinates[2 * numGons * numVerticesPerGon];
    memcpy(zCoordinates, data, numElements * sizeof(double));
}

int NgonGeneralData::getNumElements(void)
{
    return numGons;
}

int NgonGeneralData::setNumElementsArray(int const* numElementsArray)
{
    double* newCoordinates = NULL;
    double* newColorValues = NULL;
    int result = 1;

    /* Test whether the number of colors is valid */
    if ((numElementsArray[2] != numElementsArray[0]*numElementsArray[1]) &&
            (numElementsArray[2] != numElementsArray[0]) &&
            (numElementsArray[2] != 0))
    {
        return 0;
    }

    if (numGons * numVerticesPerGon != numElementsArray[0]*numElementsArray[1])
    {
        try
        {
            newCoordinates = new double[3 * numElementsArray[0]*numElementsArray[1]];
        }
        catch (const std::exception& e)
        {
            e.what();
            return 0;
        }

        result = 1;
    }

    if (numElementsArray[2] != this->numColors)
    {
        if (numElementsArray[2] > 0)
        {
            try
            {
                newColorValues = new double[numElementsArray[2]];
            }
            catch (const std::exception& e)
            {
                e.what();
                result = 0;
            }
        }

    }

    if (result == 1)
    {
        if (newCoordinates != NULL)
        {
            if (numGons * numVerticesPerGon > 0)
            {
                delete [] coordinates;
            }

            coordinates = newCoordinates;

            numGons = numElementsArray[0];
            numVerticesPerGon = numElementsArray[1];
        }

        if (newColorValues != NULL || numElementsArray[2] == 0)
        {
            if (this->numColors > 0)
            {
                delete [] colorValues;
            }

            colorValues = newColorValues;
            this->numColors = numElementsArray[2];
        }
    }
    else
    {
        if (newCoordinates != NULL)
        {
            delete [] newCoordinates;
        }

        if (newColorValues != NULL)
        {
            delete [] newColorValues;
        }
    }

    return result;
}

double* NgonGeneralData::getColors(void)
{
    return colorValues;
}

void NgonGeneralData::setColors(double const* colors, int numElements)
{
    if (numElements > numColors)
    {
        return;
    }
    memcpy(colorValues, colors, numElements * sizeof(double));
}

int NgonGeneralData::getNumColors(void)
{
    return numColors;
}