6257
|
1 /* Contour lines for function evaluated on a grid. |
|
2 |
|
3 Copyright (C) 2004 Shai Ayal |
|
4 |
|
5 Adapted to an oct file from the stand alone contourl by Victro Munoz |
|
6 Copyright (C) 2004 Victor Munoz |
|
7 |
|
8 Based on contour plot routine (plcont.c) in PLPlot package |
|
9 http://plplot.org/ |
|
10 |
|
11 Copyright (C) 1995, 2000, 2001 Maurice LeBrun |
|
12 Copyright (C) 2000, 2002 Joao Cardoso |
|
13 Copyright (C) 2000, 2001, 2002, 2004 Alan W. Irwin |
|
14 Copyright (C) 2004 Andrew Ross |
|
15 |
|
16 This file is part of Octave. |
|
17 |
|
18 Octave is free software; you can redistribute it and/or modify it |
|
19 under the terms of the GNU General Public License as published by the |
7016
|
20 Free Software Foundation; either version 3 of the License, or (at your |
|
21 option) any later version. |
6257
|
22 |
|
23 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
25 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
26 for more details. |
|
27 |
|
28 You should have received a copy of the GNU General Public License |
7016
|
29 along with Octave; see the file COPYING. If not, see |
|
30 <http://www.gnu.org/licenses/>. |
6257
|
31 |
|
32 */ |
|
33 |
|
34 #ifdef HAVE_CONFIG_H |
|
35 #include <config.h> |
|
36 #endif |
|
37 |
|
38 #include "quit.h" |
|
39 |
|
40 #include "defun-dld.h" |
|
41 #include "error.h" |
|
42 #include "oct-obj.h" |
|
43 |
|
44 static Matrix this_contour; |
|
45 static Matrix contourc; |
|
46 static int elem; |
|
47 |
|
48 // this is the quanta in which we increase this_contour |
|
49 #define CONTOUR_QUANT 50 |
|
50 |
|
51 // cl_add_point(x,y); |
|
52 // |
|
53 // Add a coordinate point (x,y) to this_contour |
|
54 |
|
55 static void |
|
56 cl_add_point (double x, double y) |
|
57 { |
|
58 if (elem % CONTOUR_QUANT == 0) |
|
59 this_contour = this_contour.append (Matrix (2, CONTOUR_QUANT, 0)); |
|
60 |
|
61 this_contour (0, elem) = x; |
|
62 this_contour (1, elem) = y; |
|
63 elem++; |
|
64 } |
|
65 |
|
66 // cl_end_contour(); |
|
67 // |
|
68 // Adds contents of current contour to contourc. |
|
69 // this_contour.cols () - 1; |
|
70 |
|
71 static void |
|
72 cl_end_contour (void) |
|
73 { |
|
74 if (elem > 2) |
|
75 { |
|
76 this_contour (1, 0) = elem - 1; |
|
77 contourc = contourc.append (this_contour.extract_n (0, 0, 2, elem)); |
|
78 } |
|
79 this_contour = Matrix (); |
|
80 elem = 0; |
|
81 } |
|
82 |
|
83 // cl_start_contour(flev,x,y); |
|
84 // |
|
85 // Start a new contour, and adds contents of current one to contourc |
|
86 |
|
87 static void |
|
88 cl_start_contour (double flev, double x, double y) |
|
89 { |
|
90 cl_end_contour (); |
|
91 this_contour.resize (2, 0); |
|
92 cl_add_point (flev, flev); |
|
93 cl_add_point (x, y); |
|
94 } |
|
95 |
|
96 static void |
|
97 cl_drawcn (RowVector & X, RowVector & Y, Matrix & Z, double flev, |
|
98 int krow, int kcol, double lastx, double lasty, int startedge, |
|
99 Matrix & ipts) |
|
100 { |
|
101 |
|
102 int kx = 0, lx = Z.cols () - 1, ky = 0, ly = Z.rows () - 1; |
|
103 |
|
104 double f[4]; |
|
105 double px[4], py[4], locx[4], locy[4]; |
|
106 int iedge[4]; |
|
107 int num, first, inext, kcolnext, krownext; |
|
108 |
|
109 px[0] = X (krow + 1); |
|
110 px[1] = X (krow); |
|
111 px[2] = X (krow); |
|
112 px[3] = X (krow + 1); |
|
113 py[0] = Y (kcol); |
|
114 py[1] = Y (kcol); |
|
115 py[2] = Y (kcol + 1); |
|
116 py[3] = Y (kcol + 1); |
|
117 |
|
118 f[0] = Z (krow + 1, kcol) - flev; |
|
119 f[1] = Z (krow, kcol) - flev; |
|
120 f[2] = Z (krow, kcol + 1) - flev; |
|
121 f[3] = Z (krow + 1, kcol + 1) - flev; |
|
122 |
|
123 for (int i = 0, j = 1; i < 4; i++, j = (j + 1) % 4) |
|
124 { |
|
125 iedge[i] = (f[i] * f[j] > 0.0) ? -1 : ((f[i] * f[j] < 0.0) ? 1 : 0); |
|
126 } |
|
127 |
|
128 // Mark this square as done |
|
129 ipts(krow,kcol) = 1; |
|
130 |
|
131 // Check if no contour has been crossed i.e. iedge[i] = -1 |
|
132 if (iedge[0] == -1 && iedge[1] == -1 && iedge[2] == -1 && iedge[3] == -1) |
|
133 return; |
|
134 |
|
135 // Check if this is a completely flat square - in which case ignore it |
|
136 if (f[0] == 0.0 && f[1] == 0.0 && f[2] == 0.0 && f[3] == 0.0) |
|
137 return; |
|
138 |
|
139 // Calculate intersection points |
|
140 num = 0; |
|
141 if (startedge < 0) |
|
142 { |
|
143 first = 1; |
|
144 } |
|
145 else |
|
146 { |
|
147 locx[num] = lastx; |
|
148 locy[num] = lasty; |
|
149 num++; |
|
150 first = 0; |
|
151 } |
|
152 |
|
153 for (int k = 0, i = (startedge < 0 ? 0 : startedge); k < 4; |
|
154 k++, i = (i + 1) % 4) |
|
155 { |
|
156 if (i == startedge) |
|
157 continue; |
|
158 |
|
159 // If the contour is an edge check it hasn't already been done |
|
160 if (f[i] == 0.0 && f[(i + 1) % 4] == 0.0) |
|
161 { |
|
162 kcolnext = kcol; |
|
163 krownext = krow; |
|
164 if (i == 0) |
|
165 kcolnext--; |
|
166 if (i == 1) |
|
167 krownext--; |
|
168 if (i == 2) |
|
169 kcolnext++; |
|
170 if (i == 3) |
|
171 krownext++; |
|
172 if (kcolnext < kx || kcolnext >= lx || krownext < ky |
|
173 || krownext >= ly || ipts(krownext,kcolnext) == 1) |
|
174 continue; |
|
175 } |
|
176 if (iedge[i] == 1 || f[i] == 0.0) |
|
177 { |
|
178 int j = (i + 1) % 4; |
|
179 if (f[i] != 0.0) |
|
180 { |
|
181 locx[num] = |
|
182 (px[i] * fabs (f[j]) + px[j] * fabs (f[i])) / fabs (f[j] - |
|
183 f[i]); |
|
184 locy[num] = |
|
185 (py[i] * fabs (f[j]) + py[j] * fabs (f[i])) / fabs (f[j] - |
|
186 f[i]); |
|
187 } |
|
188 else |
|
189 { |
|
190 locx[num] = px[i]; |
|
191 locy[num] = py[i]; |
|
192 } |
|
193 // If this is the start of the contour then move to the point |
|
194 if (first == 1) |
|
195 { |
|
196 cl_start_contour (flev, locx[num], locy[num]); |
|
197 first = 0; |
|
198 } |
|
199 else |
|
200 { |
|
201 // Link to the next point on the contour |
|
202 cl_add_point (locx[num], locy[num]); |
|
203 // Need to follow contour into next grid box |
|
204 // Easy case where contour does not pass through corner |
|
205 if (f[i] != 0.0) |
|
206 { |
|
207 kcolnext = kcol; |
|
208 krownext = krow; |
|
209 inext = (i + 2) % 4; |
|
210 if (i == 0) |
|
211 kcolnext--; |
|
212 if (i == 1) |
|
213 krownext--; |
|
214 if (i == 2) |
|
215 kcolnext++; |
|
216 if (i == 3) |
|
217 krownext++; |
|
218 if (kcolnext >= kx && kcolnext < lx |
|
219 && krownext >= ky && krownext < ly |
|
220 && ipts(krownext,kcolnext) == 0) |
|
221 { |
|
222 cl_drawcn (X, Y, Z, flev, krownext, kcolnext, |
|
223 locx[num], locy[num], inext, ipts); |
|
224 } |
|
225 } |
|
226 // Hard case where contour passes through corner. This |
|
227 // is still not perfect - it may lose the contour which |
|
228 // won't upset the contour itself (we can find it again |
|
229 // later) but might upset the labelling (which is only |
|
230 // relevant for the PLPlot implementation, since we |
|
231 // don't worry about labels---for now!) |
|
232 else |
|
233 { |
|
234 kcolnext = kcol; |
|
235 krownext = krow; |
|
236 inext = (i + 2) % 4; |
|
237 if (i == 0) |
|
238 { |
|
239 kcolnext--; |
|
240 krownext++; |
|
241 } |
|
242 if (i == 1) |
|
243 { |
|
244 krownext--; |
|
245 kcolnext--; |
|
246 } |
|
247 if (i == 2) |
|
248 { |
|
249 kcolnext++; |
|
250 krownext--; |
|
251 } |
|
252 if (i == 3) |
|
253 { |
|
254 krownext++; |
|
255 kcolnext++; |
|
256 } |
|
257 if (kcolnext >= kx && kcolnext < lx |
|
258 && krownext >= ky && krownext < ly |
|
259 && ipts(krownext,kcolnext) == 0) |
|
260 { |
|
261 cl_drawcn (X, Y, Z, flev, krownext, kcolnext, |
|
262 locx[num], locy[num], inext, ipts); |
|
263 } |
|
264 |
|
265 } |
|
266 if (first == 1) |
|
267 { |
|
268 // Move back to first point |
|
269 cl_start_contour (flev, locx[num], locy[num]); |
|
270 first = 0; |
|
271 } |
|
272 else |
|
273 { |
|
274 first = 1; |
|
275 } |
|
276 num++; |
|
277 } |
|
278 } |
|
279 } |
|
280 } |
|
281 |
|
282 static void |
|
283 cl_cntr (RowVector & X, RowVector & Y, Matrix & Z, double flev) |
|
284 { |
|
285 Matrix ipts (Z.rows (), Z.cols (), 0); |
|
286 |
|
287 for (int krow = 0; krow < Z.rows () - 1; krow++) |
|
288 { |
|
289 for (int kcol = 0; kcol < Z.cols () - 1; kcol++) |
|
290 { |
|
291 if (ipts(krow,kcol) == 0) |
|
292 { |
|
293 cl_drawcn (X, Y, Z, flev, krow, kcol, 0.0, 0.0, -2, ipts); |
|
294 } |
|
295 } |
|
296 } |
|
297 } |
|
298 |
|
299 DEFUN_DLD (__contourc__, args, , |
|
300 "-*- texinfo -*-\n\ |
|
301 @deftypefn {Loadable Function} {} __contourc__ (@var{x}, @var{y}, @var{z}, @var{levels})\n\ |
6945
|
302 Undocumented internal function.\n\ |
6257
|
303 @end deftypefn") |
|
304 { |
|
305 octave_value retval; |
|
306 |
|
307 if (args.length () == 4) |
|
308 { |
|
309 RowVector X = args (0).row_vector_value (); |
|
310 RowVector Y = args (1).row_vector_value (); |
|
311 Matrix Z = args (2).matrix_value ().transpose (); |
|
312 RowVector L = args (3).row_vector_value (); |
|
313 |
|
314 if (! error_state) |
|
315 { |
|
316 contourc.resize (2, 0); |
|
317 |
|
318 for (int i = 0; i < L.length (); i++) |
|
319 cl_cntr (X, Y, Z, L (i)); |
|
320 |
|
321 cl_end_contour (); |
|
322 |
|
323 retval = contourc; |
|
324 } |
|
325 else |
|
326 error ("__contourc__: invalid argument values"); |
|
327 } |
|
328 else |
|
329 print_usage (); |
|
330 |
|
331 return retval; |
|
332 } |