1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2010-2012 - DIGITEO - Manuel Juliachs
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #include "NgonGridData.hxx"
17 
18 extern "C" {
19 #include <string.h>
20 #include <stdlib.h>
21 
22 #include "graphicObjectProperties.h"
23 }
24 
NgonGridData(void)25 NgonGridData::NgonGridData(void)
26 {
27     numGons = 0;
28 
29     /* Grid: must be set to 4 */
30     numVerticesPerGon = 4;
31 
32     xCoordinates = NULL;
33     yCoordinates = NULL;
34     zCoordinates = NULL;
35 
36     xSize = 0;
37     ySize = 0;
38 
39     xDimensions[0] = 0;
40     xDimensions[1] = 0;
41 
42     yDimensions[0] = 0;
43     yDimensions[1] = 0;
44 
45     /* Set to 0 as a default */
46     zCoordinatesShift = 0.0;
47 }
48 
~NgonGridData(void)49 NgonGridData::~NgonGridData(void)
50 {
51     if (xSize > 0)
52     {
53         delete [] xCoordinates;
54     }
55 
56     if (ySize > 0)
57     {
58         delete [] yCoordinates;
59     }
60 
61     if (xSize > 0 && ySize > 0)
62     {
63         delete [] zCoordinates;
64     }
65 }
66 
getPropertyFromName(int propertyName)67 int NgonGridData::getPropertyFromName(int propertyName)
68 {
69     switch (propertyName)
70     {
71         case __GO_DATA_MODEL_NUM_X__ :
72             return NUM_X;
73         case __GO_DATA_MODEL_NUM_Y__ :
74             return NUM_Y;
75         case __GO_DATA_MODEL_NUM_Z__ :
76             return NUM_Z;
77         case __GO_DATA_MODEL_X_DIMENSIONS__ :
78             return X_DIMENSIONS;
79         case __GO_DATA_MODEL_Y_DIMENSIONS__ :
80             return Y_DIMENSIONS;
81         case __GO_DATA_MODEL_GRID_SIZE__ :
82             return GRID_SIZE;
83         case __GO_DATA_MODEL_X__ :
84             return X_COORDINATES;
85         case __GO_DATA_MODEL_Y__ :
86             return Y_COORDINATES;
87         case __GO_DATA_MODEL_Z__ :
88             return Z_COORDINATES;
89         case __GO_DATA_MODEL_Z_COORDINATES_SHIFT__ :
90             return Z_COORDINATES_SHIFT;
91         default :
92             return NgonData::getPropertyFromName(propertyName);
93     }
94 
95 }
96 
setDataProperty(int property,void const * value,int numElements)97 int NgonGridData::setDataProperty(int property, void const* value, int numElements)
98 {
99     switch (property)
100     {
101         case GRID_SIZE :
102             return setGridSize((int const*) value);
103         case X_COORDINATES :
104             setDataX((double const*) value, numElements);
105             break;
106         case Y_COORDINATES :
107             setDataY((double const*) value, numElements);
108             break;
109         case Z_COORDINATES :
110             setDataZ((double const*) value, numElements);
111             break;
112         case Z_COORDINATES_SHIFT :
113             setZCoordinatesShift((double const*) value);
114             break;
115         default :
116             return NgonData::setDataProperty(property, value, numElements);
117     }
118 
119     return 1;
120 }
121 
getDataProperty(int property,void ** _pvData)122 void NgonGridData::getDataProperty(int property, void **_pvData)
123 {
124     switch (property)
125     {
126         case NUM_X:
127             ((int *) *_pvData)[0] = getNumX();
128             break;
129         case NUM_Y :
130             ((int *) *_pvData)[0] = getNumY();
131             break;
132         case NUM_Z :
133             ((int *) *_pvData)[0] = getNumZ();
134             break;
135         case X_DIMENSIONS :
136             *_pvData = getXDimensions();
137             break;
138         case Y_DIMENSIONS :
139             *_pvData = getYDimensions();
140             break;
141         case X_COORDINATES :
142             *_pvData = getDataX();
143             break;
144         case Y_COORDINATES :
145             *_pvData = getDataY();
146             break;
147         case Z_COORDINATES :
148             *_pvData = getDataZ();
149             break;
150         case Z_COORDINATES_SHIFT :
151             ((double *) *_pvData)[0] = getZCoordinatesShift();
152             break;
153         default :
154             NgonData::getDataProperty(property, _pvData);
155     }
156 }
157 
setGridSize(int const * gridSize)158 int NgonGridData::setGridSize(int const* gridSize)
159 {
160     int newXSize = 0;
161     int newYSize = 0;
162     int xModified = 0;
163     int yModified = 0;
164     int zModified = 0;
165     int result = 0;
166 
167     double* newXCoordinates = NULL;
168     double* newYCoordinates = NULL;
169     double* newZCoordinates = NULL;
170 
171     result = 1;
172 
173     xModified = 0;
174     yModified = 0;
175     zModified = 0;
176 
177     if ((gridSize[0] != 1) && (gridSize[1] != 1))
178     {
179         return 0;
180     }
181 
182     if ((gridSize[2] != 1) && (gridSize[3] != 1))
183     {
184         return 0;
185     }
186 
187     newXSize = gridSize[0] * gridSize[1];
188     newYSize = gridSize[2] * gridSize[3];
189 
190 
191     if (newXSize != xSize)
192     {
193         xModified = 1;
194 
195         try
196         {
197             newXCoordinates = new double[newXSize];
198         }
199         catch (const std::exception& e)
200         {
201             e.what();
202             result = 0;
203         }
204     }
205 
206     if (newYSize != ySize)
207     {
208         yModified = 1;
209 
210         try
211         {
212             newYCoordinates = new double[newYSize];
213         }
214         catch (const std::exception& e)
215         {
216             e.what();
217             result = 0;
218         }
219     }
220 
221     if (newXSize * newYSize != xSize * ySize)
222     {
223         zModified = 1;
224 
225         try
226         {
227             newZCoordinates = new double[newXSize * newYSize];
228         }
229         catch (const std::exception& e)
230         {
231             e.what();
232             result = 0;
233         }
234     }
235 
236     if (result)
237     {
238         if (xModified)
239         {
240             if (xSize > 0)
241             {
242                 delete [] xCoordinates;
243             }
244 
245             xCoordinates = newXCoordinates;
246             xSize = newXSize;
247         }
248 
249         xDimensions[0] = gridSize[0];
250         xDimensions[1] = gridSize[1];
251 
252         if (yModified)
253         {
254             if (ySize > 0)
255             {
256                 delete [] yCoordinates;
257             }
258 
259             yCoordinates = newYCoordinates;
260             ySize = newYSize;
261         }
262 
263         yDimensions[0] = gridSize[2];
264         yDimensions[1] = gridSize[3];
265 
266         if (zModified)
267         {
268             if (xSize * ySize > 0)
269             {
270                 delete [] zCoordinates;
271             }
272 
273             zCoordinates = newZCoordinates;
274 
275             numGons = (xSize - 1) * (ySize - 1);
276         }
277 
278     }
279     else
280     {
281         /* Failed allocation(s) */
282 
283         if (xModified && (newXCoordinates != NULL))
284         {
285             delete [] newXCoordinates;
286         }
287 
288         if (yModified && (newYCoordinates != NULL))
289         {
290             delete [] newYCoordinates;
291         }
292 
293         if (zModified && (newZCoordinates != NULL))
294         {
295             delete [] newZCoordinates;
296         }
297 
298     }
299 
300     return result;
301 }
302 
getNumX(void)303 int NgonGridData::getNumX(void)
304 {
305     return xSize;
306 }
307 
getNumY(void)308 int NgonGridData::getNumY(void)
309 {
310     return ySize;
311 }
312 
getNumZ(void)313 int NgonGridData::getNumZ(void)
314 {
315     return xSize * ySize;
316 }
317 
getXDimensions(void)318 int* NgonGridData::getXDimensions(void)
319 {
320     return xDimensions;
321 }
322 
getYDimensions(void)323 int* NgonGridData::getYDimensions(void)
324 {
325     return yDimensions;
326 }
327 
setDataX(double const * data,int numElements)328 void NgonGridData::setDataX(double const* data, int numElements)
329 {
330     if (numElements > xSize)
331     {
332         return;
333     }
334 
335     for (int i = 0; i < numElements; i++)
336     {
337         xCoordinates[i] = data[i];
338     }
339 }
340 
setDataY(double const * data,int numElements)341 void NgonGridData::setDataY(double const* data, int numElements)
342 {
343     if (numElements > ySize)
344     {
345         return;
346     }
347 
348     for (int i = 0; i < numElements; i++)
349     {
350         yCoordinates[i] = data[i];
351     }
352 }
353 
setDataZ(double const * data,int numElements)354 void NgonGridData::setDataZ(double const* data, int numElements)
355 {
356     if (numElements > xSize * ySize)
357     {
358         return;
359     }
360 
361     for (int i = 0; i < numElements; i++)
362     {
363         zCoordinates[i] = data[i];
364     }
365 }
366 
setZCoordinatesShift(double const * data)367 void NgonGridData::setZCoordinatesShift(double const* data)
368 {
369     zCoordinatesShift = *data;
370 }
371 
getDataX(void)372 double* NgonGridData::getDataX(void)
373 {
374     return xCoordinates;
375 }
376 
getDataY(void)377 double* NgonGridData::getDataY(void)
378 {
379     return yCoordinates;
380 }
381 
getDataZ(void)382 double* NgonGridData::getDataZ(void)
383 {
384     return zCoordinates;
385 }
386 
getZCoordinatesShift(void)387 double NgonGridData::getZCoordinatesShift(void)
388 {
389     return zCoordinatesShift;
390 }
391