summaryrefslogtreecommitdiff
path: root/modules/graphics/macros/contourf.sci
blob: 49dc0dd774a9689e1cbcef3c8fd66e9622f1af33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
// Copyright (C) DIGITEO - 2012 - Allan CORNET
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function contourf(x, y, z, nv, style, strf, leg, rect, nax)

    [nout, nin] = argn(0);

    if nin == 0 then   // demo
        t = -%pi:0.1:%pi;
        m = sin(t)' * cos(t);
        contourf(t,t,m);
        return;
    end

    if nin <= 0 then
        x=1:10;
    end
    if nin <= 1 then
        y=1:10;
    end
    if nin <= 2 then
        z=rand(size(x,"*"), size(y,"*"));
    end
    if nin <= 3 then
        zmin=min(z);
        zmax=max(z);
        nv = zmin + (1:10) * (zmax-zmin)/(11);
    end
    if nin <= 5 then
        strf="021";
    end
    if nin <= 6 then
        leg=" ";
    end
    if nin <= 7 then
        rect=[0,0,1,1];
    end
    if nin <= 8 then
        nax=[1,10,1,10];
    end
    if x==[] then
        x=1:size(z,"r");
    end
    if y==[] then
        y=1:size(z,"c");
    end

    if type(x) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real vector expected.\n"), "contourf", 1));
    end

    if type(y) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real vector expected.\n"), "contourf", 2));
    end

    if type(z) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "contourf", 3));
    end

    if type(nv) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "contourf", 4));
    end

    if type(strf) <> 10 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "contourf", 6));
    end

    if type(leg) <> 10 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "contourf", 7));
    end

    if type(rect) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "contourf", 8));
    end

    if type(nax) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d:  Real matrix expected.\n"), "contourf", 9));
    end


    nvs=size(nv,"*") ;
    if nvs==1 then
        nvs=nv;
        zmin=min(z);
        zmax=max(z);
        nv = zmin + (1:nvs)*(zmax-zmin)/(nvs+1);
    end;

    if nin <= 4 then
        style = -1*ones(1, nvs);
    end

    if type(style) <> 1 then
        error(999, msprintf(gettext("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "contourf", 5));
    end

    if nin <= 7 then
        rect=[min(x), min(y), max(x), max(y)];
    end

    if ~isvector(x) then
        error(999, msprintf(gettext("%s: Wrong size for input argument #%d: Real vector expected.\n"), "contourf", 1));
    end

    if ~isvector(y) then
        error(999, msprintf(gettext("%s: Wrong size for input argument #%d: Real vector expected.\n"), "contourf", 2));
    end

    if size(strf, "*") <> 1 then
        error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "contourf", 6));
    end

    if size(leg, "*") <> 1 then
        error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "contourf", 7));
    end

    nv1 = nv;
    [mz,nz] = size(z);
    minz = min(z);
    maxz = max(z);

    // Surround the matrix by a very low region to get closed contours, and
    // replace any NaN with low numbers as well.
    zz=[ %nan * ones(1,nz+2) + %nan;
    %nan * ones(mz,1)   + %nan, z, %nan * ones(mz,1)   + %nan;
    %nan * ones(1,nz+2) + %nan];

    kk=find(isnan(zz(:)));

    zz(kk)=minz-1e4*(maxz-minz)+zeros(kk);

    xx = [2 * x(1) - x(2); x(:); 2 * x(mz) - x(mz - 1)];
    yy = [2 * y(1) - y(2); y(:); 2 * y(nz) - y(nz - 1)];

    // Internal call to get the contours
    [x1,y1]=contour2di(xx,yy,zz,nv);
    CS=[x1;y1];
    // Find the indices of the curves in the c matrix, and get the
    // area of closed curves in order to draw patches correctly.
    ii = 1;
    ncurves = 0;
    I = [];
    Area=[];

    while (ii < size(CS,2)),
        nl=CS(2,ii);
        ncurves = ncurves + 1;
        I(ncurves) = ii;
        xp=CS(1,ii+(1:nl));  // First patch
        yp=CS(2,ii+(1:nl));
        Area(ncurves)=sum(diff(xp).*(yp(1:nl-1)+yp(2:nl))/2);
        ii = ii + nl + 1;
    end

    lp=xget("lastpattern");

    if size(nv,"*") > 1 // case where nv is a vector defining the level curve values
        if  size(nv,"*") > lp
            error(msprintf(gettext("%s: Colormap too small"),"contourf"));
        end
    else
        if nv > lp
            error(msprintf(gettext("%s: Colormap too small"),"contourf"));
            return ;
        end
    end

    min_nv=min(nv);
    max_nv=max(nv);

    plot2d([min(xx);max(xx)],[min(yy);max(yy)],0,strf,leg,rect,nax);

    // Plot patches in order of decreasing size. This makes sure that
    // all the lev1es get drawn, not matter if we are going up a hill or
    // down into a hole. When going down we shift levels though, you can
    // tell whether we are going up or down by checking the sign of the
    // area (since curves are oriented so that the high side is always
    // the same side). Lowest curve is largest and encloses higher data
    // always.

    draw_min=1;
    H=[];
    [FA,IA]=gsort(abs(Area));

    drawlater(); // postpon the drawing here
    a=gca();
    old_foreground = a.foreground;
    pat=xget("pattern");
    for jj=IA',
        nl=CS(2,I(jj));
        lev1=CS(1,I(jj));
        if (lev1 ~= minz | draw_min) then
            xp=CS(1,I(jj)+(1:nl));
            yp=CS(2,I(jj)+(1:nl));
            pat=size(find( nv <= lev1),"*");
            xset("pattern",pat);
            xfpoly(xp,yp)
        end
    end

    if style(1)<>-1 then
        contour2d(xx,yy,zz,nv,style,"000",leg,rect,nax);
    end
    a.foreground = old_foreground;
    drawnow(); // draw all now!

endfunction