f58f82e4138855436d7d79988913b62287121dfb
[scilab.git] / scilab / modules / scicos_blocks / src / c / cmat3d.c
1 /*  Scicos
2 *
3 *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * See the file ./license.txt
20 */
21 /**
22    \file cmat3d.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CMAT3D is a scope which connect a matrix to a plot3d. Values of the matrix are the values at the nodes.
27    \see CMAT3D.sci in macros/scicos_blocks/Sinks/
28 */
29
30 #include "scoMemoryScope.h"
31 #include "scoWindowScope.h"
32 #include "scoMisc.h"
33 #include "scoGetProperty.h"
34 #include "scoSetProperty.h"
35 #include "scicos_block4.h"
36
37 /** \fn cmat3d_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
38     \brief Function to draw or redraw the window
39 */
40 void cmat3d_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
41 {
42   /*Declarations*/
43   int i; //As usual
44   int * ipar; //Integer Parameters
45   int win_pos[2]; //Position of the Window
46   int win_dim[2]; //Dimension of the Window
47   int dimension = 3;
48   double * rpar; //Reals parameters
49   double  ymin, ymax; //Ymin and Ymax are vectors here
50   double  xmin, xmax;
51   double zmin, zmax;
52   int number_of_curves_by_subwin;
53   int number_of_subwin;
54   double * mat;
55   int size_mat;
56   int size_in_x;
57   int size_in_y;
58   scoGraphicalObject pShortDraw;
59
60   /*Retrieve parameters from the scicos_model() which has been created thanks to the interfacing function*/
61   rpar = GetRparPtrs(block);
62   ipar = GetIparPtrs(block);
63
64   number_of_subwin = 1;
65
66   win_pos[0] = -1;
67   win_pos[1] = -1;
68   win_dim[0] = -1;
69   win_dim[1] = -1;
70
71   size_mat = ipar[2];
72   mat = (double*)scicos_malloc(size_mat*sizeof(double));
73   for(i = 0 ; i < size_mat ; i++)
74     {
75       mat[i] = rpar[i];
76     }
77   size_in_x = GetInPortSize(block,1,1);
78   size_in_y = GetInPortSize(block,1,2);
79   if(ipar[3] == 1)
80     {
81       xmax = size_in_x;
82       xmin = 0;
83       ymax = size_in_y;
84       ymin = 0;
85     }
86   else
87     {
88       xmin = rpar[size_mat];
89       xmax = rpar[size_mat+1];
90       ymin = rpar[size_mat+2];
91       ymax = rpar[size_mat+3];
92     }
93
94   zmin = ipar[0];
95   zmax = ipar[1];
96   number_of_curves_by_subwin = 1;
97
98   /*Allocating memory for scope only if the window has to be created and not redraw*/
99   if(firstdraw == 1)
100     {
101       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
102     }
103
104   /*Creating the Scope with axes*/
105   scoInitOfWindow(*pScopeMemory, dimension, -1, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, &zmin, &zmax);
106   if(scoGetScopeActivation(*pScopeMemory) == 1)
107     {
108       /*Here we put the special window feature like pixmap or text title
109         Dont forget that the function scoAddTitleScope redraws the window at end so it would be a good idea to put it at the end*/
110       sciSetPixmapMode(scoGetPointerScopeWindow(*pScopeMemory),TRUE);
111       pFIGURE_FEATURE(scoGetPointerScopeWindow(*pScopeMemory))->wshow = 1;
112
113       sciSetColormap(scoGetPointerScopeWindow(*pScopeMemory), mat , size_mat/3, 3);
114
115       pSUBWIN_FEATURE(scoGetPointerAxes(*pScopeMemory,0))->alpha = 50;
116       pSUBWIN_FEATURE(scoGetPointerAxes(*pScopeMemory,0))->theta = 280;
117   
118       /*Adding graphic elements like plot3d or polyline and so*/
119       if(ipar[3] == 1)
120         {
121           scoAddPlot3dForShortDraw(*pScopeMemory,0,0,GetInPortSize(block,1,1),GetInPortSize(block,1,2));
122         }
123       else
124         {
125           double h_x,h_y;
126           scoAddPlot3dForShortDraw(*pScopeMemory,0,0,GetInPortSize(block,1,1),GetInPortSize(block,1,2));
127           pShortDraw = scoGetPointerShortDraw(*pScopeMemory,0,0);
128           h_x = fabs((xmax-xmin)/(GetInPortSize(block,1,1)-1));
129           h_y = fabs((ymax-ymin)/(GetInPortSize(block,1,2)-1));
130       
131           for(i = 0 ; i < size_in_x ; i++)
132             {
133               pSURFACE_FEATURE(pShortDraw)->pvecx[i] = xmin + i*h_x;
134             } 
135           for(i = 0 ; i < size_in_y ; i++)
136             {
137               pSURFACE_FEATURE(pShortDraw)->pvecy[i] = ymin + i*h_y;
138             } 
139         }
140       scoAddTitlesScope(*pScopeMemory,"x","y","z");
141     }
142   /*Dont forget to free your scicos_malloc or MALLOC*/
143   scicos_free(mat);
144
145 }
146
147 /** \fn void cmat3d(scicos_block * block, int flag)
148     \brief the computational function
149     \param block A pointer to a scicos_block
150     \param flag An integer which indicates the state of the block (init, update, ending)
151 */
152 void cmat3d(scicos_block * block, int flag)
153 {
154   /* Declarations */
155   ScopeMemory * pScopeMemory;
156   scoGraphicalObject pShortDraw;
157   double * u1;
158   int i,j;
159   int dim_i, dim_j;
160  
161   /* State Machine Control */
162   switch(flag)
163     {
164       /*Flag 4*/
165     case Initialization:
166       {
167         /*We create the window for the first time, so 1 is in parameters*/
168         cmat3d_draw(block,&pScopeMemory,1);
169         break; //dont forget the break
170       }
171       /*Flag 2*/
172     case StateUpdate:
173       {
174         /*Retreiving Scope in the block->work*/
175         scoRetrieveScopeMemory(block->work,&pScopeMemory);
176         if(scoGetScopeActivation(pScopeMemory) == 1)
177           {
178
179             /* If window has been destroyed we recreate it */
180             if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
181               {
182                 //0 here because of the recreation
183                 cmat3d_draw(block,&pScopeMemory,0);
184               }
185             /*Here some allocations and calcul wich are necessary*/
186             pShortDraw = scoGetPointerShortDraw(pScopeMemory,0,0);
187
188             u1 = GetInPortPtrs(block,1);
189             dim_i = GetInPortRows(block,1);
190             dim_j = GetInPortCols(block,1);
191
192             for(i = 0 ; i < dim_i ; i++)
193               {
194             
195                 for(j = 0; j < dim_j ; j++)
196                   {
197                     pSURFACE_FEATURE(pShortDraw)->pvecz[j+i*dim_j] = u1[j+dim_j*i];
198                   }
199               }
200         
201             /*Here is the draw instructions*/
202             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
203             if(sciGetPixmapMode(scoGetPointerScopeWindow(pScopeMemory)))
204               {
205                 C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
206               }
207             sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
208           }
209         break; //dont forget the break
210       }
211       /*Flag 5*/
212     case Ending:
213       {
214         /*Retrieve Memory*/
215         scoRetrieveScopeMemory(block->work, &pScopeMemory);
216         /*Here we can add specific instructions to be sure that we have stick short and longdraw if we need it. Cscope for example stick the last short to the long to have one curve to move*/
217         /*Free Memory*/
218         if(scoGetScopeActivation(pScopeMemory) == 1)
219           {
220             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
221             pShortDraw = sciGetCurrentFigure();
222             pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
223             pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
224           }
225         scoFreeScopeMemory(block->work, &pScopeMemory);
226         break;
227       }
228     }
229 }