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 |
|
20 Free Software Foundation; either version 2, or (at your option) any |
|
21 later version. |
|
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 |
|
29 along with Octave; see the file COPYING. If not, write to the Free |
|
30 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
31 02110-1301, USA. |
|
32 |
|
33 */ |
|
34 |
|
35 #ifdef HAVE_CONFIG_H |
|
36 #include <config.h> |
|
37 #endif |
|
38 |
|
39 #include "quit.h" |
|
40 |
|
41 #include "defun-dld.h" |
|
42 #include "error.h" |
|
43 #include "oct-obj.h" |
|
44 |
|
45 static Matrix this_contour; |
|
46 static Matrix contourc; |
|
47 static int elem; |
|
48 |
|
49 // this is the quanta in which we increase this_contour |
|
50 #define CONTOUR_QUANT 50 |
|
51 |
|
52 // cl_add_point(x,y); |
|
53 // |
|
54 // Add a coordinate point (x,y) to this_contour |
|
55 |
|
56 static void |
|
57 cl_add_point (double x, double y) |
|
58 { |
|
59 if (elem % CONTOUR_QUANT == 0) |
|
60 this_contour = this_contour.append (Matrix (2, CONTOUR_QUANT, 0)); |
|
61 |
|
62 this_contour (0, elem) = x; |
|
63 this_contour (1, elem) = y; |
|
64 elem++; |
|
65 } |
|
66 |
|
67 // cl_end_contour(); |
|
68 // |
|
69 // Adds contents of current contour to contourc. |
|
70 // this_contour.cols () - 1; |
|
71 |
|
72 static void |
|
73 cl_end_contour (void) |
|
74 { |
|
75 if (elem > 2) |
|
76 { |
|
77 this_contour (1, 0) = elem - 1; |
|
78 contourc = contourc.append (this_contour.extract_n (0, 0, 2, elem)); |
|
79 } |
|
80 this_contour = Matrix (); |
|
81 elem = 0; |
|
82 } |
|
83 |
|
84 // cl_start_contour(flev,x,y); |
|
85 // |
|
86 // Start a new contour, and adds contents of current one to contourc |
|
87 |
|
88 static void |
|
89 cl_start_contour (double flev, double x, double y) |
|
90 { |
|
91 cl_end_contour (); |
|
92 this_contour.resize (2, 0); |
|
93 cl_add_point (flev, flev); |
|
94 cl_add_point (x, y); |
|
95 } |
|
96 |
|
97 static void |
|
98 cl_drawcn (RowVector & X, RowVector & Y, Matrix & Z, double flev, |
|
99 int krow, int kcol, double lastx, double lasty, int startedge, |
|
100 Matrix & ipts) |
|
101 { |
|
102 |
|
103 int kx = 0, lx = Z.cols () - 1, ky = 0, ly = Z.rows () - 1; |
|
104 |
|
105 double f[4]; |
|
106 double px[4], py[4], locx[4], locy[4]; |
|
107 int iedge[4]; |
|
108 int num, first, inext, kcolnext, krownext; |
|
109 |
|
110 px[0] = X (krow + 1); |
|
111 px[1] = X (krow); |
|
112 px[2] = X (krow); |
|
113 px[3] = X (krow + 1); |
|
114 py[0] = Y (kcol); |
|
115 py[1] = Y (kcol); |
|
116 py[2] = Y (kcol + 1); |
|
117 py[3] = Y (kcol + 1); |
|
118 |
|
119 f[0] = Z (krow + 1, kcol) - flev; |
|
120 f[1] = Z (krow, kcol) - flev; |
|
121 f[2] = Z (krow, kcol + 1) - flev; |
|
122 f[3] = Z (krow + 1, kcol + 1) - flev; |
|
123 |
|
124 for (int i = 0, j = 1; i < 4; i++, j = (j + 1) % 4) |
|
125 { |
|
126 iedge[i] = (f[i] * f[j] > 0.0) ? -1 : ((f[i] * f[j] < 0.0) ? 1 : 0); |
|
127 } |
|
128 |
|
129 // Mark this square as done |
|
130 ipts(krow,kcol) = 1; |
|
131 |
|
132 // Check if no contour has been crossed i.e. iedge[i] = -1 |
|
133 if (iedge[0] == -1 && iedge[1] == -1 && iedge[2] == -1 && iedge[3] == -1) |
|
134 return; |
|
135 |
|
136 // Check if this is a completely flat square - in which case ignore it |
|
137 if (f[0] == 0.0 && f[1] == 0.0 && f[2] == 0.0 && f[3] == 0.0) |
|
138 return; |
|
139 |
|
140 // Calculate intersection points |
|
141 num = 0; |
|
142 if (startedge < 0) |
|
143 { |
|
144 first = 1; |
|
145 } |
|
146 else |
|
147 { |
|
148 locx[num] = lastx; |
|
149 locy[num] = lasty; |
|
150 num++; |
|
151 first = 0; |
|
152 } |
|
153 |
|
154 for (int k = 0, i = (startedge < 0 ? 0 : startedge); k < 4; |
|
155 k++, i = (i + 1) % 4) |
|
156 { |
|
157 if (i == startedge) |
|
158 continue; |
|
159 |
|
160 // If the contour is an edge check it hasn't already been done |
|
161 if (f[i] == 0.0 && f[(i + 1) % 4] == 0.0) |
|
162 { |
|
163 kcolnext = kcol; |
|
164 krownext = krow; |
|
165 if (i == 0) |
|
166 kcolnext--; |
|
167 if (i == 1) |
|
168 krownext--; |
|
169 if (i == 2) |
|
170 kcolnext++; |
|
171 if (i == 3) |
|
172 krownext++; |
|
173 if (kcolnext < kx || kcolnext >= lx || krownext < ky |
|
174 || krownext >= ly || ipts(krownext,kcolnext) == 1) |
|
175 continue; |
|
176 } |
|
177 if (iedge[i] == 1 || f[i] == 0.0) |
|
178 { |
|
179 int j = (i + 1) % 4; |
|
180 if (f[i] != 0.0) |
|
181 { |
|
182 locx[num] = |
|
183 (px[i] * fabs (f[j]) + px[j] * fabs (f[i])) / fabs (f[j] - |
|
184 f[i]); |
|
185 locy[num] = |
|
186 (py[i] * fabs (f[j]) + py[j] * fabs (f[i])) / fabs (f[j] - |
|
187 f[i]); |
|
188 } |
|
189 else |
|
190 { |
|
191 locx[num] = px[i]; |
|
192 locy[num] = py[i]; |
|
193 } |
|
194 // If this is the start of the contour then move to the point |
|
195 if (first == 1) |
|
196 { |
|
197 cl_start_contour (flev, locx[num], locy[num]); |
|
198 first = 0; |
|
199 } |
|
200 else |
|
201 { |
|
202 // Link to the next point on the contour |
|
203 cl_add_point (locx[num], locy[num]); |
|
204 // Need to follow contour into next grid box |
|
205 // Easy case where contour does not pass through corner |
|
206 if (f[i] != 0.0) |
|
207 { |
|
208 kcolnext = kcol; |
|
209 krownext = krow; |
|
210 inext = (i + 2) % 4; |
|
211 if (i == 0) |
|
212 kcolnext--; |
|
213 if (i == 1) |
|
214 krownext--; |
|
215 if (i == 2) |
|
216 kcolnext++; |
|
217 if (i == 3) |
|
218 krownext++; |
|
219 if (kcolnext >= kx && kcolnext < lx |
|
220 && krownext >= ky && krownext < ly |
|
221 && ipts(krownext,kcolnext) == 0) |
|
222 { |
|
223 cl_drawcn (X, Y, Z, flev, krownext, kcolnext, |
|
224 locx[num], locy[num], inext, ipts); |
|
225 } |
|
226 } |
|
227 // Hard case where contour passes through corner. This |
|
228 // is still not perfect - it may lose the contour which |
|
229 // won't upset the contour itself (we can find it again |
|
230 // later) but might upset the labelling (which is only |
|
231 // relevant for the PLPlot implementation, since we |
|
232 // don't worry about labels---for now!) |
|
233 else |
|
234 { |
|
235 kcolnext = kcol; |
|
236 krownext = krow; |
|
237 inext = (i + 2) % 4; |
|
238 if (i == 0) |
|
239 { |
|
240 kcolnext--; |
|
241 krownext++; |
|
242 } |
|
243 if (i == 1) |
|
244 { |
|
245 krownext--; |
|
246 kcolnext--; |
|
247 } |
|
248 if (i == 2) |
|
249 { |
|
250 kcolnext++; |
|
251 krownext--; |
|
252 } |
|
253 if (i == 3) |
|
254 { |
|
255 krownext++; |
|
256 kcolnext++; |
|
257 } |
|
258 if (kcolnext >= kx && kcolnext < lx |
|
259 && krownext >= ky && krownext < ly |
|
260 && ipts(krownext,kcolnext) == 0) |
|
261 { |
|
262 cl_drawcn (X, Y, Z, flev, krownext, kcolnext, |
|
263 locx[num], locy[num], inext, ipts); |
|
264 } |
|
265 |
|
266 } |
|
267 if (first == 1) |
|
268 { |
|
269 // Move back to first point |
|
270 cl_start_contour (flev, locx[num], locy[num]); |
|
271 first = 0; |
|
272 } |
|
273 else |
|
274 { |
|
275 first = 1; |
|
276 } |
|
277 num++; |
|
278 } |
|
279 } |
|
280 } |
|
281 } |
|
282 |
|
283 static void |
|
284 cl_cntr (RowVector & X, RowVector & Y, Matrix & Z, double flev) |
|
285 { |
|
286 Matrix ipts (Z.rows (), Z.cols (), 0); |
|
287 |
|
288 for (int krow = 0; krow < Z.rows () - 1; krow++) |
|
289 { |
|
290 for (int kcol = 0; kcol < Z.cols () - 1; kcol++) |
|
291 { |
|
292 if (ipts(krow,kcol) == 0) |
|
293 { |
|
294 cl_drawcn (X, Y, Z, flev, krow, kcol, 0.0, 0.0, -2, ipts); |
|
295 } |
|
296 } |
|
297 } |
|
298 } |
|
299 |
|
300 DEFUN_DLD (__contourc__, args, , |
|
301 "-*- texinfo -*-\n\ |
|
302 @deftypefn {Loadable Function} {} __contourc__ (@var{x}, @var{y}, @var{z}, @var{levels})\n\ |
|
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 } |