Scicos_blocks: clean NonLinear blocks interfaces
[scilab.git] / scilab / modules / scicos_blocks / macros / NonLinear / LOOKUP2D.sci
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 // See the file ../license.txt
20 //
21 function [x,y,typ]=LOOKUP2D(job,arg1,arg2)
22     // Copyright INRIA
23
24     x=[];
25     y=[];
26     typ=[];
27     select job
28     case "set" then
29
30         x=arg1
31         model=arg1.model
32         graphics=arg1.graphics
33         exprs=graphics.exprs
34         ok=%f;
35         SaveExit=%f
36         while %t do
37
38             Ask_again=%f
39             [ok,xx,yy,zz,Method,graf,exprs]=scicos_getvalue("2D Lookup table parameters",["Row index input values";"Column index input values";"Table data";"Lookup method(1..5)";"Launch graphic window(y/n)?"],list("vec",-1,"vec",-1,"mat",[-1,-1],"vec",1,"str",1),exprs)
40             // 1 : Interpolation-extrapolation (Bilinear)
41             // 2 : Interpolation_endvalues
42             // 3 : use input nearest
43             // 4 : use input below
44             // 5 : use input above
45             // 6 : Interpolation-extrapolation (linear)
46
47             if  ~ok then
48                 break;
49             end
50             mtd=int(Method);
51             if mtd<1 then
52                 mtd=1;
53             end;
54             if mtd>6 then
55                 mtd=6;
56             end;
57             if graf<>"y" & graf<>"Y" then
58                 graf="n";
59             end
60             exprs(5)="n";// exprs.graf='n'
61             exprs(4)=sci2exp(mtd);// pour le cas methode>7 | method<0
62
63             METHOD=getmethod(mtd);
64             if ~Ask_again then
65                 xx=xx(:);
66                 yy=yy(:);
67                 [nx,mx]=size(xx);
68                 [ny,my]=size(yy);
69                 [nz,mz]=size(zz);
70                 if ((nx<=1)|(ny<=1)) then,
71                     x_message("input row/column data size should be greater than one");
72                     Ask_again=%t;
73                 end
74                 if ~((nx==nz)&(ny==mz)) then,
75                     x_message("incompatible size of x and y");
76                     Ask_again=%t;
77                 end
78                 [ok]=test_increasing(xx);
79                 if (~ok) then
80                     x_message("Row input values must be monotonically increasing");
81                     Ask_again=%t;
82                 end
83                 [ok]=test_increasing(yy);
84                 if (~ok) then
85                     x_message("Column input values must be monotonically increasing");
86                     Ask_again=%t;
87                 end
88             end
89             if ~Ask_again then
90                 if (graf=="Y" | graf=="y") then
91                     gh=gcf();
92                     curwin=gh.figure_id;
93                     save_curwin=curwin;
94                     gh2=scf();
95                     curwin=max(winsid())+1;
96                     plot3d(xx,yy,zz,35,45,"X@Y@Z",[5,2,4]) ;
97                     curwin=save_curwin;
98                     gh.figure_id=curwin;
99                 end
100
101                 model.rpar=[xx(:);yy(:);zz(:)]
102                 model.ipar=[nx;ny;mtd]
103                 graphics.exprs=exprs;
104                 x.model=model
105                 x.graphics=graphics
106                 break
107             end
108         end
109         //======================================================================
110     case "define" then
111         model=scicos_model();
112         xx=[1:4]
113         yy=[1:3]
114         zz=[4,5,6;16,19,20;10,18,23;6, 3,-1];
115         Method=1;
116         Graf="n"
117
118         Nx=length(xx);
119         Ny=length(yy);
120         model.sim=list("lookup2d",4)
121         model.in=[1;1]
122         model.out=1
123
124         model.rpar=[xx(:);yy(:);zz(:)]
125         model.ipar=[Nx;Ny;Method]
126         model.blocktype="c"
127         model.dep_ut=[%t %f]
128         exprs=list(strcat(sci2exp(xx)),strcat(sci2exp(yy)),strcat(sci2exp(zz)),sci2exp(Method),Graf)
129         gr_i=[]
130
131         x=standard_define([2.5 2],model,exprs,gr_i)
132     end
133 endfunction
134
135 function [ok]=test_increasing(xx)
136     ok=%f
137     [nx,mx]=size(xx);// adjusting the x and y size
138
139     for i=1:mx
140         if (xx(i)<>xx(i)) then
141             xinfo("x contains no data in x("+string(i)+")");
142             return;
143         end
144
145     end
146
147     for i=1:mx-1
148         if (xx(i)>xx(i+1)) then
149             return;
150         end
151     end
152
153     ok=%t
154 endfunction
155 //============================
156 function METHOD=getmethod(order)
157     select order
158     case 1 then, METHOD="Interpolation-extrapolation(biliniear)"
159     case 2 then, METHOD="Interpolation_endvalues"
160     case 3 then, METHOD="use input nearest"
161     case 4 then, METHOD="use input below"
162     case 5 then, METHOD="use input above"
163     case 6 then, METHOD="Interpolation-extrapolation"
164     end
165 endfunction
166 //=========================================================