Debian Codegraph @

texmf-dist/doc/latex/metaplot/examples/metacontour.cc

raw
1 /* MetaContour, version 0.1 -------------------------------------- */
2 /* Copyright(C) 2004, Brooks Moses */
3 /* */
4 /* This version of MetaContour is made available under the Gnu */
5 /* Public License; see metacontour_main.cc for details. */
6 /* */
7 /* This is a very pre-release version of MetaContour, distributed */
8 /* primarily as an example of a use of MetaPlot. It can be */
9 /* compiled with gcc using the line: */
10 /* */
11 /* g++ metacontour.cc cpoint.cc metacontour_main.cc */
12 /* */
13 /*-----------------------------------------------------------------*/
14
15 #include"metacontour.h"
16 using namespace std;
17
18 MetapostContours::MetapostContours(cparray& PlotData,
19 vector<double>& ContourListIn,
20 string& PlotNameIn)
21 {
22 ContourList = ContourListIn;
23 PlotName = PlotNameIn;
24
25 GridCommand = "addto " + PlotName + ".Grid doublepath ";
26 DrawCommand = "addto " + PlotName + ".LinePlot doublepath ";
27 FillCommand = "addto " + PlotName + ".FillPlot contour ";
28
29 GridStream << "picture " << PlotName << ".Grid; "
30 << PlotName << ".Grid := nullpicture;\n";
31 LinePlotStream << "picture " << PlotName << ".LinePlot; "
32 << PlotName << ".LinePlot := nullpicture;\n";
33 FillPlotStream << "picture " << PlotName << ".FillPlot; "
34 << PlotName << ".FillPlot := nullpicture;\n";
35
36
37 /* Construct contours */
38
39 cpoint x00; cpoint x01; cpoint x10; cpoint x11;
40 for (int j=1; j<PlotData.j_dim(); j++) {
41 x00 = PlotData(0,j-1);
42 x01 = PlotData(0,j);
43 DrawGrid(x00, x01);
44 }
45 for (int i=1; i<PlotData.i_dim(); i++) {
46 x00 = PlotData(i-1,0);
47 x10 = PlotData(i,0);
48 DrawGrid(x00, x10);
49 for (int j=1; j<PlotData.j_dim(); j++) {
50 x00 = PlotData(i-1, j-1);
51 x01 = PlotData(i-1, j );
52 x10 = PlotData( i , j-1);
53 x11 = PlotData( i , j );
54 DrawGrid(x10, x11);
55 DrawGrid(x01, x11);
56 PlotSquare(x00, x10, x11, x01);
57 }
58 }
59 }
60
61
62 /* Start plotting a given grid square */
63 //
64 // Search Region: x3--------x2
65 // |oooooooooo|
66 // |oooooooooo|
67 // |oooooooooo|
68 // |oooooooooo|
69 // x4--------x1
70 //
71 void MetapostContours::PlotSquare(cpoint x1, cpoint x2,
72 cpoint x3, cpoint x4)
73 {
74 /* Organize things so x4 is smallest */
75
76 if (x1.z < x4.z) { // 1 is lowest of (1,4)
77 if (x2.z < x1.z) { // 2 is lowest of (1,2,3,4)
78 if (x3.z < x2.z) { // 3 is lowest of all
79 cpoint xtemp = x4; x4 = x3; x3 = x2; x2 = x1; x1 = xtemp;
80 } else { // 2 is lowest of all
81 cpoint xtemp = x4; x4 = x2; x2 = xtemp; xtemp = x3; x3 = x1; x1 = xtemp;
82 }
83 } else { // 1 is lowest of (1,2,4)
84 if (x3.z < x1.z) { // 3 is lowest of all
85 cpoint xtemp = x4; x4 = x3; x3 = x2; x2 = x1; x1 = xtemp;
86 } else { // 1 is lowest of all
87 cpoint xtemp = x4; x4 = x1; x1 = x2; x2 = x3; x3 = xtemp;
88 }
89 }
90 } else { // 4 is lowest of (1,4)
91 if (x2.z < x4.z) { // 2 is lowest of (1,2,4)
92 if (x3.z < x2.z) { // 3 is lowest of all
93 cpoint xtemp = x4; x4 = x3; x3 = x2; x2 = x1; x1 = xtemp;
94 } else { // 2 is lowest of all
95 cpoint xtemp = x4; x4 = x2; x2 = xtemp; xtemp = x3; x3 = x1; x1 = xtemp;
96 }
97 } else { // 4 is lowest of (1,2,4)
98 if (x3.z < x4.z) { // 3 is lowest of all
99 cpoint xtemp = x4; x4 = x3; x3 = x2; x2 = x1; x1 = xtemp;
100 } // 4 is lowest of all, do nothing
101 }
102 }
103
104 /* Find initial level, draw square, start rest if appropriate */
105 if (ContourList[ContourList.size()-1] < x4.z) {
106 DrawFilled(ContourList.size()-1, x1, x4, x3, x2);
107 } else {
108 int contourLevel = 0;
109 while (ContourList[contourLevel] < x4.z &&
110 contourLevel < ContourList.size()-1) contourLevel++;
111 DrawFilled(contourLevel-1, x1, x4, x3, x2);
112 PlotPent(x1, x2, x3, x4, contourLevel);
113 }
114 }
115
116
117 /* Continue plotting for a corner of a grid square */
118 //
119 // Search Region: x3--------x2
120 // |..........|
121 // |........oo|
122 // |......oooo|
123 // |....oooooo|
124 // x4--------x1
125 //
126 void MetapostContours::PlotCorner(cpoint x1, cpoint x2,
127 cpoint x3, cpoint x4, int contourLevel)
128 {
129 if (contourLevel >= ContourList.size()) return;
130 double zc2 = ContourList[contourLevel];
131 if (x1.z > zc2) {
132 /* another corner */
133 cpoint y1 = x4.interpolateto(x1, zc2);
134 cpoint y2 = x2.interpolateto(x1, zc2);
135 DrawFilled(contourLevel, y1, y2, x1);
136 DrawLine(contourLevel, y1, y2);
137 PlotCorner(x1, x2, x3, x4, contourLevel+1);
138 }
139 }
140
141
142 /* Continue plotting for a two-corner side of a grid square */
143 //
144 // Search Region: x3--------x2
145 // |.....ooooo|
146 // |.....ooooo|
147 // |.....ooooo|
148 // |.....ooooo|
149 // x4--------x1
150 //
151 void MetapostContours::PlotRect(cpoint x1, cpoint x2,
152 cpoint x3, cpoint x4, int contourLevel)
153 {
154 if (contourLevel >= ContourList.size()) return;
155 double zc2 = ContourList[contourLevel];
156 if (x1.z > zc2) {
157 if (x2.z > zc2) {
158 /* another rectangle */
159 cpoint y1 = x4.interpolateto(x1, zc2);
160 cpoint y2 = x3.interpolateto(x2, zc2);
161 DrawFilled(contourLevel, y1, y2, x2, x1);
162 DrawLine(contourLevel, y1, y2);
163 PlotRect(x1, x2, x3, x4, contourLevel+1);
164 } else {
165 /* corner around x1 */
166 cpoint y1 = x4.interpolateto(x1, zc2);
167 cpoint y2 = x2.interpolateto(x1, zc2);
168 DrawFilled(contourLevel, y1, y2, x1);
169 DrawLine(contourLevel, y1, y2);
170 PlotCorner(x1, x2, x3, x4, contourLevel+1);
171 }
172 } else {
173 if (x2.z > zc2) {
174 /* corner around x2 */
175 cpoint y1 = x1.interpolateto(x2, zc2);
176 cpoint y2 = x3.interpolateto(x2, zc2);
177 DrawFilled(contourLevel, y1, y2, x2);
178 DrawLine(contourLevel, y1, y2);
179 PlotCorner(x2, x3, x4, x1, contourLevel+1);
180 }
181 }
182 }
183
184
185 /* Continue plotting for a three-corner pentagon of a grid square */
186 //
187 // Search Region: x3--------x2
188 // |oooooooooo|
189 // |..oooooooo|
190 // |....oooooo|
191 // |......oooo|
192 // x4--------x1
193 //
194 void MetapostContours::PlotPent(cpoint x1, cpoint x2,
195 cpoint x3, cpoint x4, int contourLevel)
196 {
197 if (contourLevel >= ContourList.size()) return;
198 double zc2 = ContourList[contourLevel];
199 if (x1.z > zc2) {
200 if (x3.z > zc2) {
201 if (x2.z < zc2) {
202 /* a saddle-point hexagon, or two corners */
203 cpoint y1 = x4.interpolateto(x1, zc2);
204 cpoint y2 = x2.interpolateto(x1, zc2);
205 cpoint y3 = x2.interpolateto(x3, zc2);
206 cpoint y4 = x4.interpolateto(x3, zc2);
207 double zcenter = cpoint::saddlepoint(x1, x2, x3, x4);
208 if (zcenter < zc2) {
209 /* two corners around x1 and x3 */
210 DrawFilled(contourLevel, y1, y2, x1);
211 DrawLine(contourLevel, y1, y2);
212 PlotCorner(x1, x2, x3, x4, contourLevel+1);
213 DrawFilled(contourLevel, y3, y4, x3);
214 DrawLine(contourLevel, y3, y4);
215 PlotCorner(x3, x4, x1, x2, contourLevel+1);
216 } else {
217 /* a saddle-point hexagon */
218 DrawFilled(contourLevel, y1, y4, x3, y3, y2, x1);
219 DrawLine(contourLevel, y1, y4);
220 DrawLine(contourLevel, y3, y2);
221 PlotHex(x1, x2, x3, x4, contourLevel+1, zcenter);
222 }
223 } else {
224 /* another pentagon */
225 cpoint y1 = x4.interpolateto(x1, zc2);
226 cpoint y2 = x4.interpolateto(x3, zc2);
227 DrawFilled(contourLevel, y1, y2, x3, x2, x1);
228 DrawLine(contourLevel, y1, y2);
229 PlotPent(x1, x2, x3, x4, contourLevel+1);
230 }
231 } else { // (x3.z < zc2)
232 if (x2.z > zc2) {
233 /* rectangle around x1, x2 */
234 cpoint y1 = x4.interpolateto(x1, zc2);
235 cpoint y2 = x3.interpolateto(x2, zc2);
236 DrawFilled(contourLevel, y1, y2, x2, x1);
237 DrawLine(contourLevel, y1, y2);
238 PlotRect(x1, x2, x3, x4, contourLevel+1);
239 } else {
240 /* corner around x1 */
241 cpoint y1 = x4.interpolateto(x1, zc2);
242 cpoint y2 = x2.interpolateto(x1, zc2);
243 DrawFilled(contourLevel, y1, y2, x1);
244 DrawLine(contourLevel, y1, y2);
245 PlotCorner(x1, x2, x3, x4, contourLevel+1);
246 }
247 }
248 } else { // (x1.z < zc2)
249 if (x3.z > zc2) {
250 if (x2.z > zc2) {
251 /* rectangle around x2, x3 */
252 cpoint y1 = x1.interpolateto(x2, zc2);
253 cpoint y2 = x4.interpolateto(x3, zc2);
254 DrawFilled(contourLevel, y1, y2, x3, x2);
255 DrawLine(contourLevel, y1, y2);
256 PlotRect(x2, x3, x4, y1, contourLevel+1);
257 } else {
258 /* corner around x3 */
259 cpoint y1 = x2.interpolateto(x3, zc2);
260 cpoint y2 = x4.interpolateto(x3, zc2);
261 DrawFilled(contourLevel, y1, y2, x3);
262 DrawLine(contourLevel, y1, y2);
263 PlotCorner(x3, x4, x1, x2, contourLevel+1);
264 }
265 } else {
266 if (x2.z > zc2) {
267 /* corner around x2 */
268 cpoint y1 = x1.interpolateto(x2, zc2);
269 cpoint y2 = x3.interpolateto(x2, zc2);
270 DrawFilled(contourLevel, y1, y2, x2);
271 DrawLine(contourLevel, y1, y2);
272 PlotCorner(x2, x3, x4, x1, contourLevel+1);
273 }
274 }
275 }
276 }
277
278
279 /* Continue plotting for a two-corner hex in a grid square */
280 //
281 // Search Region: x3--------x2
282 // |oooooo....|
283 // |oooooooo..|
284 // |..oooooooo|
285 // |....oooooo|
286 // x4--------x1
287 //
288 void MetapostContours::PlotHex(cpoint x1, cpoint x2,
289 cpoint x3, cpoint x4,
290 int contourLevel, double zcenter)
291 {
292 if (contourLevel >= ContourList.size()) return;
293 double zc2 = ContourList[contourLevel];
294 if (x1.z > zc2) {
295 if (x3.z > zc2) {
296 /* another saddle-point hexagon, or two corners */
297 cpoint y1 = x4.interpolateto(x1, zc2);
298 cpoint y2 = x2.interpolateto(x1, zc2);
299 cpoint y3 = x2.interpolateto(x3, zc2);
300 cpoint y4 = x4.interpolateto(x3, zc2);
301 if (zcenter < zc2) {
302 /* two corners around x1 and x3 */
303 DrawFilled(contourLevel, y1, y2, x1);
304 DrawLine(contourLevel, y1, y2);
305 PlotCorner(x1, x2, x3, x4, contourLevel+1);
306 DrawFilled(contourLevel, y3, y4, x3);
307 DrawLine(contourLevel, y3, y4);
308 PlotCorner(x3, x4, x1, x2, contourLevel+1);
309 } else {
310 /* another saddle-point hexagon */
311 DrawFilled(contourLevel, y1, y4, x3, y3, y2, x1);
312 DrawLine(contourLevel, y1, y4);
313 DrawLine(contourLevel, y3, y2);
314 PlotHex(x1, x2, x3, x4, contourLevel+1, zcenter);
315 }
316 } else {
317 /* corner around x1 */
318 cpoint y1 = x4.interpolateto(x1, zc2);
319 cpoint y2 = x2.interpolateto(x1, zc2);
320 DrawFilled(contourLevel, y1, y2, x1);
321 DrawLine(contourLevel, y1, y2);
322 PlotCorner(x1, x2, x3, x4, contourLevel+1);
323 }
324 } else {
325 if (x3.z > zc2) {
326 /* corner around x3 */
327 cpoint y1 = x2.interpolateto(x3, zc2);
328 cpoint y2 = x4.interpolateto(x3, zc2);
329 DrawFilled(contourLevel, y1, y2, x2);
330 DrawLine(contourLevel, y1, y2);
331 PlotCorner(x3, x4, x1, x2, contourLevel+1);
332 }
333 }
334 }
335
336 void MetapostContours::DrawGrid (cpoint x1, cpoint x2) {
337 GridStream << GridCommand << x1.metapoint() << "--" << x2.metapoint()
338 << ";\n";
339 }
340
341 void MetapostContours::DrawLine (int contourLevel, cpoint x1, cpoint x2) {
342 LinePlotStream << DrawCommand << x1.metapoint() << "--" << x2.metapoint()
343 << " withcolor contourcolor"
344 << contourLevel+1 << ";\n";
345 }
346
347 void MetapostContours::DrawFilled (int contourLevel, cpoint x1, cpoint x2, cpoint x3) {
348 FillPlotStream << FillCommand << x1.metapoint() << "--" << x2.metapoint()
349 << "--" << x3.metapoint()
350 << "--cycle withcolor contourcolor" << contourLevel+1 << ";\n";
351 }
352
353 void MetapostContours::DrawFilled (int contourLevel, cpoint x1, cpoint x2, cpoint x3,
354 cpoint x4) {
355 FillPlotStream << FillCommand << x1.metapoint() << "--" << x2.metapoint()
356 << "--" << x3.metapoint() << "--" << x4.metapoint()
357 << "--cycle withcolor contourcolor" << contourLevel+1 << ";\n";
358 }
359
360 void MetapostContours::DrawFilled (int contourLevel, cpoint x1, cpoint x2, cpoint x3,
361 cpoint x4, cpoint x5) {
362 FillPlotStream << FillCommand << x1.metapoint() << "--" << x2.metapoint()
363 << "--" << x3.metapoint() << "--" << x4.metapoint()
364 << "--" << x5.metapoint()
365 << "--cycle withcolor contourcolor" << contourLevel+1 << ";\n";
366 }
367
368 void MetapostContours::DrawFilled (int contourLevel, cpoint x1, cpoint x2, cpoint x3,
369 cpoint x4, cpoint x5, cpoint x6) {
370 FillPlotStream << FillCommand << x1.metapoint() << "--" << x2.metapoint()
371 << "--" << x3.metapoint() << "--" << x4.metapoint()
372 << "--" << x5.metapoint() << "--" << x6.metapoint()
373 << "--cycle withcolor contourcolor" << contourLevel+1 << ";\n";
374 }
375
376