1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2004-2006 - INRIA - Fabrice Leray
4  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
5  * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
6  *
7  * Copyright (C) 2012 - 2016 - Scilab Enterprises
8  *
9  * This file is hereby licensed under the terms of the GNU GPL v2.0,
10  * pursuant to article 5.3.4 of the CeCILL v.2.1.
11  * This file was originally licensed under the terms of the CeCILL v2.1,
12  * and continues to be available under such terms.
13  * For more information, see the COPYING file which you should have received
14  * along with this program.
15  *
16  */
17 
18 /*------------------------------------------------------------------------*/
19 /* file: sci_xfpolys.c                                                    */
20 /* desc : interface for xfpolys routine                                   */
21 /*------------------------------------------------------------------------*/
22 
23 #include "gw_graphics.h"
24 #include "api_scilab.h"
25 #include "BuildObjects.h"
26 #include "Scierror.h"
27 #include "sciCall.h"
28 #include "DrawObjects.h"
29 #include "localization.h"
30 
31 #include "CurrentObject.h"
32 #include "HandleManagement.h"
33 #include "createGraphicObject.h"
34 #include "graphicObjectProperties.h"
35 #include "getGraphicObjectProperty.h"
36 #include "setGraphicObjectProperty.h"
37 
38 /*--------------------------------------------------------------------------*/
sci_xfpolys(char * fname,void * pvApiCtx)39 int sci_xfpolys(char *fname, void *pvApiCtx)
40 {
41     SciErr sciErr;
42 
43     int* piAddrl1 = NULL;
44     double* l1 = NULL;
45     int* piAddrl2 = NULL;
46     double* l2 = NULL;
47     int* piAddr3 = NULL;
48     int* l3 = NULL;
49 
50     int m1 = 0, n1 = 0;
51     int m2 = 0, n2 = 0;
52     int m3 = 0, n3 = 0;
53     int mn2 = 0;
54 
55     int v1 = 0;                 /* v1 is the flag used for flat (v1==1) or interpolated (v1==2) shading */
56     int i = 0;
57     long hdl = 0;
58 
59     int iSubWinUID = 0;
60     int iFigureUID = 0;
61     int iCompoundUID = 0;
62     int iSubWinForeground = 0;
63 
64     int iImmediateDrawing = 0;
65     int *piImmediateDrawing = &iImmediateDrawing;
66     int iFalse = 0;
67 
68     int iColorMapSize = 0;
69     int* piColorMapSize = &iColorMapSize;
70     int iForeGround = 0;
71     int* piForeGround = &iForeGround;
72 
73     int iVisible = 0;
74     int *piVisible = &iVisible;
75 
76     int iType = 0;
77     int *piType = &iType;
78 
79     CheckInputArgument(pvApiCtx, 2, 3);
80 
81     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
82     if (sciErr.iErr)
83     {
84         printError(&sciErr, 0);
85         return 1;
86     }
87 
88     // Retrieve a matrix of double at position 1.
89     sciErr = getMatrixOfDouble(pvApiCtx, piAddrl1, &m1, &n1, &l1);
90     if (sciErr.iErr)
91     {
92         printError(&sciErr, 0);
93         Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
94         return 1;
95     }
96 
97     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl2);
98     if (sciErr.iErr)
99     {
100         printError(&sciErr, 0);
101         return 1;
102     }
103 
104     // Retrieve a matrix of double at position 2.
105     sciErr = getMatrixOfDouble(pvApiCtx, piAddrl2, &m2, &n2, &l2);
106     if (sciErr.iErr)
107     {
108         printError(&sciErr, 0);
109         Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2);
110         return 1;
111     }
112 
113     //CheckSameDims
114     if (m1 != m2 || n1 != n2)
115     {
116         Scierror(999, _("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"), fname, 1, m1, n1);
117         return 1;
118     }
119 
120     mn2 = m2 * n2;
121     if (mn2 == 0)
122     {
123         AssignOutputVariable(pvApiCtx, 1) = 0;
124         ReturnArguments(pvApiCtx);
125         return 0;
126     }
127 
128     if (nbInputArgument(pvApiCtx) == 3)
129     {
130         sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr3);
131         if (sciErr.iErr)
132         {
133             printError(&sciErr, 0);
134             return 1;
135         }
136 
137         // Retrieve a matrix of double at position 3.
138         sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, piAddr3, &m3, &n3, &l3);
139         if (sciErr.iErr)
140         {
141             printError(&sciErr, 0);
142             Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 3);
143             return 1;
144         }
145 
146 
147         if (m3 * n3 == m1 * n1)
148         {
149             //CheckSameDims
150             if (m1 != m3 || n1 != n3)
151             {
152                 Scierror(999, _("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"), fname, 1, m1, n1);
153                 return 1;
154             }
155 
156             v1 = 2;             /* interpolated shading */
157 
158             if (m3 != 3 && m3 != 4)
159             {
160                 Scierror(999, _("%s: Interpolated shading only works for polygons of size %d or %d\n"), fname, 3, 4);
161                 return 0;
162             }
163         }
164         else
165         {
166             //CheckVector
167             if (m3 != 1 && n3 != 1)
168             {
169                 Scierror(999, _("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 3);
170                 return 1;
171             }
172 
173             //CheckDimProp
174             if (m3 * n3 != n2)
175             {
176                 Scierror(999, _("%s: Wrong size for input arguments: Incompatible sizes.\n"), fname);
177                 return 1;
178             }
179 
180             v1 = 1;             /* flat shading */
181         }
182     }
183     else
184     {
185         int un = 1, ix = 0;
186 
187         sciErr = allocMatrixOfDoubleAsInteger(pvApiCtx, 3, un, n2, &l3);
188         if (sciErr.iErr)
189         {
190             printError(&sciErr, 0);
191             Scierror(999, _("%s: Memory allocation error.\n"), fname);
192             return 1;
193         }
194 
195         for (ix = 0; ix < n2; ++ix)
196         {
197             *(int*)(l3 + ix) = 0;
198         }
199         m3 = n3 = 1;
200     }
201 
202     iSubWinUID = getOrCreateDefaultSubwin();
203     iFigureUID = iSubWinUID;
204     iType = 0;
205     while (iType != __GO_FIGURE__)
206     {
207         iFigureUID = getParentObject(iFigureUID);
208         getGraphicObjectProperty(iFigureUID, __GO_TYPE__, jni_int, (void **) &piType);
209     }
210 
211     getGraphicObjectProperty(iFigureUID, __GO_IMMEDIATE_DRAWING__, jni_bool, (void **)&piImmediateDrawing);
212     setGraphicObjectProperty(iFigureUID, __GO_IMMEDIATE_DRAWING__, &iFalse, jni_bool, 1);
213 
214     //get color map size
215     getGraphicObjectProperty(iFigureUID, __GO_COLORMAP_SIZE__, jni_int, (void**)&piColorMapSize);
216 
217     //get current foreground color
218     getGraphicObjectProperty(iSubWinUID, __GO_LINE_COLOR__, jni_int, (void**)&piForeGround);
219 
220     // Create compound.
221     iCompoundUID = createGraphicObject(__GO_COMPOUND__);
222     setGraphicObjectProperty(iCompoundUID, __GO_VISIBLE__, &iFalse, jni_bool, 1);
223     /* Sets the parent-child relationship for the Compound */
224     setGraphicObjectRelationship(iSubWinUID, iCompoundUID);
225 
226     for (i = 0; i < n1; ++i)
227     {
228         if (m3 == 1 || n3 == 1) /* color vector specified */
229         {
230             if (*(int*)(l3 + i) == 0)
231             {
232                 if (iForeGround == -1)
233                 {
234                     iSubWinForeground = iColorMapSize + 1;
235                 }
236                 else if (iForeGround == -2)
237                 {
238                     iSubWinForeground = iColorMapSize + 2;
239                 }
240                 else
241                 {
242                     iSubWinForeground = iForeGround;
243                 }
244 
245                 Objpoly((l1 + (i * m1)), (l2 + (i * m1)), m1, 1, iSubWinForeground, &hdl);
246             }
247             else
248             {
249                 Objfpoly((l1 + (i * m1)), (l2 + (i * m1)), m1, (int*)(l3 + i), &hdl, v1);
250             }
251         }
252         else                    /* we have a color matrix used for interpolated shading : one color per vertex */
253         {
254             Objfpoly((l1 + (i * m1)), (l2 + (i * m1)), m1, (int*)(l3 + i * m3), &hdl, v1);
255         }
256 
257         // Add newly created object to Compound
258         setGraphicObjectRelationship(iCompoundUID, getObjectFromHandle(hdl));
259     }
260 
261     setCurrentObject(iCompoundUID);
262 
263     setGraphicObjectProperty(iFigureUID, __GO_IMMEDIATE_DRAWING__, piImmediateDrawing, jni_bool, 1);
264     getGraphicObjectProperty(iFigureUID, __GO_VISIBLE__, jni_bool, (void **)&piVisible);
265 
266     setGraphicObjectProperty(iCompoundUID, __GO_VISIBLE__, &iVisible, jni_bool, 1);
267 
268     AssignOutputVariable(pvApiCtx, 1) = 0;
269     ReturnArguments(pvApiCtx);
270     return 0;
271 }
272 
273 /*--------------------------------------------------------------------------*/
274