1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2011-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 #ifndef NGONGRIDMATPLOTDATA_DECOMPOSER_HXX
17 #define NGONGRIDMATPLOTDATA_DECOMPOSER_HXX
18 
19 #include <string>
20 #include "ColorComputer.hxx"
21 
22 extern "C"
23 {
24 #include "Matplot.h"
25 }
26 
27 /**
28  * NgonGridMatplotData decomposer class
29  * Determines the vertices and the segments indices to be rendered
30  * as a function of the decomposed Matplot object's properties.
31  *
32  */
33 
34 class NgonGridMatplotDataDecomposer : public NgonGridDataDecomposer
35 {
36 
37 private :
38 
39     /** NgonGridMatplotData decomposer instance */
40     static NgonGridMatplotDataDecomposer* decomposer;
41 
42 protected :
43 
44     /**
45      * Fills a buffer with vertex data from a grid.
46      * In the case of Matplot objects, vertex coordinates are computed using the coordinates of the grid's lower-left corner
47      * and the distances between two adjacent vertices for both the x and y axes, which we label respectively (xmin, ymin) and
48      * (xd, yd). The relevant values are used as the x and y coordinate arrays (see the parameter descriptions).
49      * @param[out] the buffer to fill.
50      * @param[in] the buffer length in number of elements.
51      * @param[in] the number of coordinates taken by one element in the buffer.
52      * @param[in] the byte mask specifying which coordinates are filled (1 for X, 2 for Y, 4 for Z).
53      * @param[in] the conversion scale factor to apply to data.
54      * @param[in] the conversion translation factor to apply to data.
55      * @param[in] the bit mask specifying whether logarithmic coordinates are used.
56      * @param[in] the grid x-coordinate array (2 elements: xmin, xd).
57      * @param[in] the grid y-coordinate array (2 elements: ymin, yd).
58      * @param[in] the grid z-coordinate array.
59      * @param[in] the grid's number of vertices along the x-axis.
60      * @param[in] the grid's number of vertices along the y-axis.
61      */
62     virtual void fillGridVertices(float* buffer, int bufferLength, int elementsSize, int coordinateMask, double* scale, double* translation, int logMask,
63                                   double* x, double* y, double* z, int numX, int numY);
64 
65     /**
66      * Determines whether a facet is valid.
67      * The facet is identified by its lower left-corner (i,j).
68      * For a Matplot object, facet (i,j)'s validity depends only on its z coordinate and its (i,j) value,
69      * edge validity being therefore irrelevant. The flag specifying per-node values should be 0,
70      * as Matplot grid values are defined per facet, although it is actually unused.
71      * @param[in] the grid z-coordinate array.
72      * @param[in] the grid value array.
73      * @param[in] a flag indicating whether grid values are defined per node (1) or per facet (0). Unused.
74      * @param[in] the grid's number of vertices along the x-axis.
75      * @param[in] the grid's number of vertices along the y-axis.
76      * @param[in] the lower-left corner's x index.
77      * @param[in] the lower-left corner's y index.
78      * @param[in] a flag specifying whether logarithmic coordinates are used. Unused.
79      * @param[in] (i,j) to (i,j+1) edge validity flag. Unused.
80      * @param[out] a pointer to the output (i+1,j) to (i+1,j+1) edge validity flag. Always set to 1.
81      * @return 1 if the facet is valid, 0 if it is not.
82      */
83     virtual int isFacetValid(double* z, double* values, int perNodeValues, int numX, int numY, int i, int j, int logUsed, int currentEdgeValid, int* nextEdgeValid);
84 
85     /**
86      * Determines whether the left edge of a facet is valid.
87      * For a Matplot object, edge validity is irrelevant, it is therefore always equal to 1.
88      * @param[in] the grid z-coordinate array.
89      * @param[in] the grid value array.
90      * @param[in] a flag indicating whether grid values are defined per node (1) or per facet (0).
91      * @param[in] the grid's number of vertices along the x-axis.
92      * @param[in] the grid's number of vertices along the y-axis.
93      * @param[in] the lower-left corner's x index.
94      * @param[in] the lower-left corner's y index.
95      * @param[in] a flag specifying whether logarithmic coordinates are used.
96      * @return Always 1.
97      */
98     virtual int isFacetEdgeValid(double* z, double* values, int perNodeValues, int numX, int numY, int i, int j, int logUsed);
99 
100 public :
101 
102     /**
103      * Returns the class single instance.
104      * @return the class instance.
105      */
get(void)106     static NgonGridMatplotDataDecomposer* get(void)
107     {
108         if (decomposer == NULL)
109         {
110             decomposer = new NgonGridMatplotDataDecomposer();
111         }
112 
113         return decomposer;
114     }
115 
116     /**
117      * Fills the given buffer with vertex data from the given object.
118      * @param[in] the id of the given object.
119      * @param[out] the buffer to fill.
120      * @param[in] the buffer length in number of elements.
121      * @param[in] the number of coordinates taken by one element in the buffer.
122      * @param[in] the byte mask specifying which coordinates are filled (1 for X, 2 for Y, 4 for Z).
123      * @param[in] the conversion scale factor to apply to data.
124      * @param[in] the conversion translation factor to apply to data.
125      * @param[in] the bit mask specifying whether logarithmic coordinates are used.
126      */
127     static void fillVertices(int id, float* buffer, int bufferLength, int elementsSize, int coordinateMask, double* scale, double* translation, int logMask);
128 
129     /**
130      * Fills the given buffer with color data from the given object.
131      * @param[in] the id of the given object.
132      * @param[out] the buffer to fill.
133      * @param[in] the buffer length in number of elements.
134      * @param[in] the number of components taken by one element in the buffer (3 or 4).
135      */
136     static void fillColors(int id, float* buffer, int bufferLength, int elementsSize);
137 
138     /**
139      * Fills the given buffer with indices data of the given object.
140      * @param[in] the given object id.
141      * @param[out] the buffer to fill.
142      * @param[in] the buffer length.
143      * @param[in] the bit mask specifying whether logarithmic coordinates are used.
144      * @return the number of indices actually written.
145      */
146     static int fillIndices(int id, int* buffer, int bufferLength, int logMask);
147 
148     template <typename T>
fillColorsByIndex(T * indices,float * buffer,int elementsSize,const int nbRow,const int nbCol,double * colormap,const int colormapSize)149     inline static void fillColorsByIndex(T * indices, float * buffer, int elementsSize, const int nbRow, const int nbCol, double * colormap, const int colormapSize)
150     {
151         float facetColor[3];
152         T index;
153         int bufferOffset = 0;
154 
155         for (int j = 0; j < nbRow; j++)
156         {
157             for (int i = 0; i < nbCol; i++)
158             {
159                 index = indices[nbRow - 1 + i * nbRow - j];
160                 ColorComputer::getDirectColor(index - 1, colormap, colormapSize, facetColor);
161                 writeFacetColorToBuffer(buffer, bufferOffset, facetColor, elementsSize);
162                 bufferOffset += 4 * elementsSize;
163             }
164         }
165     }
166 
167     static void getRGBAData(ImageType imagetype, DataType datatype, GLType gltype, void * data, float * buffer, int elementsSize, const int nbRow, const int nbCol, double * colormap, const int colormapSize);
168 
169 };
170 
171 #endif
172