fdc6ea3a3a3135b4d9e043227ac70dbb8e567390
[scilab.git] / scilab / modules / scicos_blocks / src / c / cscopxy.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 /**
23    \file cscopxy.c
24    \author Benoit Bayol
25    \version 1.0
26    \date September 2006 - January 2007
27    \brief CSCOPXY is a scope in 2D which draw its input as a XY scope, there is no animation, everything is keep in memory instead of CANIMXY
28    \see CSCOPXY.sci in macros/scicos_blocks/Sinks/
29 */
30 /*--------------------------------------------------------------------------*/ 
31 #include "CurrentObjectsManagement.h"
32 #include "scoMemoryScope.h"
33 #include "scoWindowScope.h"
34 #include "scoMisc.h"
35 #include "scoGetProperty.h"
36 #include "scoSetProperty.h"
37 #include "scicos_block4.h"
38 #include "DrawingBridge.h"
39 #include "SetJavaProperty.h"
40 #include "MALLOC.h"
41 #include "dynlib_scicos_blocks.h"
42 /*--------------------------------------------------------------------------*/ 
43 /** \fn cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
44     \brief Function to draw or redraw the window
45 */
46 SCICOS_BLOCKS_IMPEXP void cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
47 {
48   int * ipar = NULL; //Integer Parameters
49   int color_flag = 0; //Flag on Color
50   int color[2];
51   int line_size = 0;
52   int animed = 0;
53   int win = 0; //Windows ID : To give a name to the window
54   int buffer_size = 0; //Buffer Size
55   int win_pos[2]; //Position of the Window
56   int win_dim[2]; //Dimension of the Window
57   int nipar = 0;
58   double * rpar = NULL; //Reals parameters
59   double xmin = 0., xmax = 0., ymin = 0., ymax = 0.; //Ymin and Ymax are vectors here
60   int number_of_subwin = 0;
61   int number_of_curves_by_subwin = 0;
62   int dimension = 2;
63   int i = 0;
64   char *label = NULL;
65   scoGraphicalObject ShortDraw;
66   scoGraphicalObject LongDraw;
67
68   ipar = GetIparPtrs(block);
69   nipar = GetNipar(block);
70   rpar = GetRparPtrs(block);
71   win = ipar[0];
72   color_flag = ipar[1];
73   buffer_size = ipar[2];
74   color[0] = ipar[3];
75   color[1] = ipar[3];
76   line_size = ipar[4];
77   animed = ipar[5];
78   win_pos[0] = ipar[6];
79   win_pos[1] = ipar[7];
80   win_dim[0] = ipar[8];
81   win_dim[1] = ipar[9];
82   xmin = rpar[0];
83   xmax = rpar[1];
84   ymin = rpar[2];
85   ymax = rpar[3];
86   label = GetLabelPtrs(block);
87
88   number_of_subwin = 1;
89   number_of_curves_by_subwin = ipar[10]; //it is a trick to recognize the type of scope, not sure it is a good way because normally a curve is the combination of a short and a longdraw
90   if(firstdraw == 1)
91     {
92       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
93       scoSetShortDrawSize(*pScopeMemory,0,buffer_size);
94       scoSetLongDrawSize(*pScopeMemory,0,5000);
95     }
96
97   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
98   if(scoGetScopeActivation(*pScopeMemory) == 1)
99     {
100       for( i = 0 ; i < number_of_curves_by_subwin ; i++)
101         {
102           scoAddPolylineForShortDraw(*pScopeMemory,0,i,color[0]);
103           scoAddPolylineForLongDraw(*pScopeMemory,0,i,color[0]);
104           ShortDraw = scoGetPointerShortDraw(*pScopeMemory,0,i);
105           LongDraw = scoGetPointerLongDraw(*pScopeMemory,0,i);
106           /* Set ShortDraw properties */
107           sciSetLineWidth(ShortDraw, line_size);
108           sciSetMarkSize(ShortDraw, line_size);
109           /* Set LongDraw properties */
110           sciSetLineWidth(LongDraw, line_size);
111           sciSetMarkSize(LongDraw, line_size);
112         }
113       scoAddTitlesScope(*pScopeMemory,label,"x","y",NULL);
114     }
115
116         /* use only single buffering to be sure to draw on the screen */
117         sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
118 }
119 /*--------------------------------------------------------------------------*/ 
120 /** \fn void cscopxy(scicos_block * block, int flag)
121     \brief the computational function
122     \param block A pointer to a scicos_block
123     \param flag An int which indicates the state of the block (init, update, ending)
124 */
125 SCICOS_BLOCKS_IMPEXP void cscopxy(scicos_block * block, int flag)
126 {
127   /* Declarations*/
128   ScopeMemory * pScopeMemory = NULL;
129   double *u1 = NULL,*u2 = NULL;
130   scoGraphicalObject Pinceau; 
131   int NbrPtsShort = 0;
132   int i = 0;
133
134   /* State Machine Control */
135   switch(flag)
136     {
137     case Initialization:
138       {
139         cscopxy_draw(block,&pScopeMemory,1);
140         break; //Break of the switch condition don t forget it
141       } //End of Initialization
142
143     case StateUpdate:
144       {
145         scoRetrieveScopeMemory(block->work,&pScopeMemory);
146         if(scoGetScopeActivation(pScopeMemory) == 1)
147           {
148             
149         /* Charging Elements */
150         if (scoGetPointerScopeWindow(pScopeMemory) == NULL) // If the window has been destroyed we recreate it
151           {
152             cscopxy_draw(block,&pScopeMemory,0);
153           }
154
155         u1 = GetRealInPortPtrs(block,1);
156         u2 = GetRealInPortPtrs(block,2);
157         
158         for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0); i++)
159           {
160             Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
161             NbrPtsShort = pPOLYLINE_FEATURE(Pinceau)->n1;
162             pPOLYLINE_FEATURE(Pinceau)->pvx[NbrPtsShort] = u1[i];
163             pPOLYLINE_FEATURE(Pinceau)->pvy[NbrPtsShort] = u2[i];
164             pPOLYLINE_FEATURE(Pinceau)->n1++;
165           }
166         
167         scoDrawScopeXYStyle(pScopeMemory);
168           }
169         break; //Break of the switch don t forget it !
170       }//End of stateupdate
171       
172       //This case is activated when the simulation is done or when we close scicos
173     case Ending:
174       {
175         scoRetrieveScopeMemory(block->work, &pScopeMemory);
176         if(scoGetScopeActivation(pScopeMemory) == 1)
177           {
178             /*sciSetUsedWindow(scoGetWindowID(pScopeMemory));*/
179             
180                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
181                         scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
182                         if (figure != NULL)
183                         {
184                                 for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0); i++)
185                                 {
186                                         Pinceau = scoGetPointerLongDraw(pScopeMemory,0,i);
187                                         forceRedraw(Pinceau);
188                                 }
189                         
190                                 //Here Pinceau = Window
191                                 /*Pinceau = sciGetCurrentFigure();*/
192                                 /*pFIGURE_FEATURE(Pinceau)->user_data = NULL;
193                                 pFIGURE_FEATURE(Pinceau)->size_of_user_data = 0;*/
194                                 clearUserData(figure);
195                                 /* restore double buffering */
196                                 sciSetJavaUseSingleBuffer(figure, FALSE);
197                         }
198         }
199         scoFreeScopeMemory(block->work, &pScopeMemory);
200         break; //Break of the switch
201       }
202       //free the memory which is allocated at each turn by some variables
203     }
204 }
205 /*--------------------------------------------------------------------------*/