add some includes
[scilab.git] / scilab / modules / scicos_blocks / src / c / bouncexy.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 bouncexy.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief BOUNCEXY has to be used with bounce_ball block
27    \see BOUNCEXY.sci in macros/scicos_blocks/Misc/
28 */
29 #include "CurrentObjectsManagement.h"
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 #include "ObjectStructure.h"
37 #include "DrawingBridge.h"
38
39 /** \fn bouncexy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
40     \brief Function to draw or redraw the window
41 */
42 void bouncexy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
43 {
44   scoGraphicalObject pAxes;
45   scoGraphicalObject pTemp;
46   double * z;
47   double *rpar;
48   int *ipar, nipar;  
49   int i,j;
50   int dimension;
51   double ymin, ymax, xmin, xmax;
52   int win;
53   int number_of_subwin;
54   int number_of_curves_by_subwin;
55   int * colors;
56   int imode;
57   double * size_balls;
58   double radius_max;
59
60   /*Retrieving Parameters*/
61   rpar = GetRparPtrs(block);
62   ipar = GetIparPtrs(block);
63   nipar = GetNipar(block);
64   win = ipar[0];
65   if (win == -1)
66     {
67       win = 20000 + get_block_number() ; 
68     }
69   dimension = 2;
70   imode = ipar[1];
71   number_of_curves_by_subwin = GetInPortRows(block,1);
72   radius_max = 0;
73   size_balls = (double*)scicos_malloc(number_of_curves_by_subwin*sizeof(double));
74   z = GetDstate(block);
75   for(i = 0 ; i < number_of_curves_by_subwin ; i++)
76     {
77       size_balls[i] = z[6*i+2];
78       if(radius_max < size_balls[i])
79         {
80           radius_max = size_balls[i];
81         }
82     }
83   number_of_subwin = 1;
84   xmin = rpar[0];
85   xmax = rpar[1];
86   ymin = rpar[2];
87   ymax = rpar[3];
88   colors = (int*)scicos_malloc(number_of_curves_by_subwin*sizeof(int));
89   for(i = 0 ; i < number_of_curves_by_subwin ; i++)
90     {
91       colors[i] = ipar[i+2];
92     }
93   if(firstdraw == 1)
94     {
95       /*Allocating memory*/
96       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
97     }
98   /*Creating the Scope*/
99   scoInitOfWindow(*pScopeMemory, dimension, win, NULL, NULL, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
100   if(scoGetScopeActivation(*pScopeMemory) == 1)
101     {
102   pTemp = scoGetPointerScopeWindow(*pScopeMemory);
103   pAxes = scoGetPointerAxes(*pScopeMemory,0);
104
105   pSUBWIN_FEATURE(pAxes)->isoview = TRUE;
106
107   (pSUBWIN_FEATURE(pAxes)->axes).axes_visible[0] = FALSE;
108   (pSUBWIN_FEATURE(pAxes)->axes).axes_visible[1] = FALSE;
109
110   sciSetIsBoxed(pAxes, FALSE);
111   sciSetPixmapMode(pTemp, TRUE);
112
113   pFIGURE_FEATURE(pTemp)->wshow = 1;
114
115   for(j = 0 ; j < number_of_curves_by_subwin ; j++)
116     {
117       scoAddSphereForShortDraw(*pScopeMemory, 0, j, size_balls[j], colors[j]);
118     }
119   scoAddRectangleForLongDraw(*pScopeMemory,0,0,xmin,(ymax-fabs(ymin)),fabs(xmax-xmin),fabs(ymax-ymin));
120   sciDrawObj(scoGetPointerLongDraw(*pScopeMemory,0,0));
121     }
122   scicos_free(colors);
123   scicos_free(size_balls);
124
125 }
126
127 /** \fn void bouncexy(scicos_block * block,int flag)
128     \brief the computational function
129     \param block A pointer to a scicos_block
130     \param flag An integer which indicates the state of the block (init, update, ending)
131 */
132 void bouncexy(scicos_block * block,int flag)
133 {
134   ScopeMemory * pScopeMemory;
135   scoGraphicalObject pShortDraw;
136   scoGraphicalObject pLongDraw;
137   double * z;
138   double t;
139   int i;
140   double * u1, *u2;
141   double * size_balls;
142   switch(flag) 
143     {
144     case Initialization:
145       {
146         bouncexy_draw(block,&pScopeMemory,1);
147         break;
148       }
149     case StateUpdate:
150       {
151
152         /*Retreiving Scope in the block->work*/
153         scoRetrieveScopeMemory(block->work,&pScopeMemory);
154         if(scoGetScopeActivation(pScopeMemory) == 1)
155           {
156             t = get_scicos_time();
157         /*If window has been destroyed we recreate it*/
158         if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
159           {
160             bouncexy_draw(block,&pScopeMemory,0);
161           }
162
163         //Cannot be factorized depends of the scope
164         size_balls = (double*)scicos_malloc(scoGetNumberOfCurvesBySubwin(pScopeMemory,0)*sizeof(double));
165         z = GetDstate(block);
166         for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
167           {
168             size_balls[i] = z[6*i+2];
169           }
170         u1 = GetRealInPortPtrs(block,1);
171         u2 = GetRealInPortPtrs(block,2);
172         for (i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
173           {
174             pShortDraw  = scoGetPointerShortDraw(pScopeMemory,0,i);
175             pLongDraw  = scoGetPointerLongDraw(pScopeMemory,0,i);
176             pARC_FEATURE(pShortDraw)->x = u1[i]-size_balls[i]/2;
177             pARC_FEATURE(pShortDraw)->y = u2[i]+size_balls[i]/2;
178
179           }
180
181         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
182         if (sciGetPixmapMode(scoGetPointerScopeWindow(pScopeMemory)))
183           {
184             C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
185             sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
186           }
187         else
188           {
189             sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
190           }
191
192         scicos_free(size_balls);
193           }
194         break;
195       }
196     case Ending:
197       {
198         scoRetrieveScopeMemory(block->work, &pScopeMemory);
199         if(scoGetScopeActivation(pScopeMemory) == 1)
200           {
201             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
202             pShortDraw = sciGetCurrentFigure();
203             pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
204             pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
205           }
206         scoFreeScopeMemory(block->work, &pScopeMemory);
207         break;  
208       }
209     }
210 }