/usr/share/doc/mathgl/mathgl_en/mathgl_en_10.html is in mathgl-doc-en 2.1.3.1-4ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This manual is for MathGL (version 2.1.2), a collection of classes and routines for scientific plotting. Please report any errors in this manual to mathgl.abalakin@gmail.org.
Copyright C 2008-2012 Alexey A. Balakin.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License."
-->
<!-- Created on December 22, 2013 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>MathGL 2.1.2: 2.1 Basic usage</title>
<meta name="description" content="MathGL 2.1.2: 2.1 Basic usage">
<meta name="keywords" content="MathGL 2.1.2: 2.1 Basic usage">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Basic-usage"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Using-MathGL-window" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Basic-usage-1"></a>
<h2 class="section">2.1 Basic usage</h2>
<p>MathGL library can be used by several manners. Each has positive and negative sides:
</p><ul>
<li>
<em>Using of MathGL library features for creating graphical window (requires FLTK, Qt or GLUT libraries).</em>
<p>Positive side is the possibility to view the plot at once and to modify it (rotate, zoom or switch on transparency or lighting) by hand or by mouse. Negative sides are: the need of X-terminal and limitation consisting in working with the only one set of data at a time.
</p>
</li><li>
<em>Direct writing to file in bitmap or vector format without creation of graphical window.</em>
<p>Positive aspects are: batch processing of similar data set (for example, a set of resulting data files for different calculation parameters), running from the console program (including the cluster calculation), fast and automated drawing, saving pictures for further analysis (or demonstration). Negative sides are: the usage of the external program for picture viewing. Also, the data plotting is non-visual. So, you have to imagine the picture (view angles, lighting and so on) before the plotting. I recommend to use graphical window for determining the optimal parameters of plotting on the base of some typical data set. And later use these parameters for batch processing in console program.
</p>
</li><li>
<em>Drawing in memory with the following displaying by other graphical program.</em>
<p>In this case the programmer has more freedom in selecting the window libraries (not only FLTK, Qt or GLUT), in positioning and surroundings control and so on. I recommend to use such way for “stand alone” programs.
</p>
</li><li>
<em>Using FLTK or Qt widgets provided by MathGL</em>
<p>Here one can use a set of standard widgets which support export to many file formats, copying to clipboard, handle mouse and so on.
</p></li></ul>
<p>MathGL drawing can be created not only by object oriented languages (like, C++ or Python), but also by pure C or Fortran-like languages. The usage of last one is mostly identical to usage of classes (except the different function names). But there are some differences. C functions must have argument HMGL (for graphics) and/or HMDT (for data arrays) which specifies the object for drawing or manipulating (changing). Fortran users may regard these variables as integer. So, firstly the user has to create this object by function mgl_create_*() and has to delete it after the using by function mgl_delete_*().
</p>
<p>Let me consider the aforesaid in more detail.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Using-MathGL-window">2.1.1 Using MathGL window</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Drawing-to-file">2.1.2 Drawing to file</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Animation">2.1.3 Animation</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Drawing-in-memory">2.1.4 Drawing in memory</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Using-QMathGL">2.1.5 Using QMathGL</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#MathGL-and-PyQt">2.1.6 MathGL and PyQt</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#MathGL-and-MPI">2.1.7 MathGL and MPI</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr size="6">
<a name="Using-MathGL-window"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Basic-usage" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Drawing-to-file" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-MathGL-window-1"></a>
<h3 class="subsection">2.1.1 Using MathGL window</h3>
<a name="index-window"></a>
<a name="index-widgets"></a>
<p>The “interactive” way of drawing in MathGL consists in window creation with help of class <code>mglWindow</code> or <code>mglGLUT</code> (see <a href="mathgl_en_47.html#Widget-classes">Widget classes</a>) and the following drawing in this window. There is a corresponding code:
</p><pre class="verbatim">#include <mgl2/window.h>
int sample(mglGraph *gr)
{
gr->Rotate(60,40);
gr->Box();
return 0;
}
//-----------------------------------------------------
int main(int argc,char **argv)
{
mglWindow gr(sample,"MathGL examples");
return gr.Run();
}
</pre><p>Here callback function <code>sample</code> is defined. This function does all drawing. Other function <code>main</code> is entry point function for console program. For compilation, just execute the command
</p><pre class="verbatim">gcc test.cpp -lmgl-wnd -lmgl
</pre>
<p>Alternatively you can create yours own class inherited from class <code>mglDraw</code> and re-implement the function <code>Draw()</code> in it:
</p><pre class="verbatim">#include <mgl2/window.h>
class Foo : public mglDraw
{
public:
int Draw(mglGraph *gr);
};
//-----------------------------------------------------
int Foo::Draw(mglGraph *gr)
{
gr->Rotate(60,40);
gr->Box();
return 0;
}
//-----------------------------------------------------
int main(int argc,char **argv)
{
Foo foo;
mglWindow gr(&foo,"MathGL examples");
return gr.Run();
}
</pre><p>Or use pure C-functions:
</p><pre class="verbatim">#include <mgl2/mgl_cf.h>
int sample(HMGL gr, void *)
{
mgl_rotate(gr,60,40,0);
mgl_box(gr);
}
int main(int argc,char **argv)
{
HMGL gr;
gr = mgl_create_graph_qt(sample,"MathGL examples",0,0);
return mgl_qt_run();
/* generally I should call mgl_delete_graph() here,
* but I omit it in main() function. */
}
</pre>
<p>The similar code can be written for <code>mglGLUT</code> window (function <code>sample()</code> is the same):
</p><pre class="verbatim">#include <mgl2/glut.h>
int main(int argc,char **argv)
{
mglGLUT gr(sample,"MathGL examples");
return 0;
}
</pre>
<p>The rotation, shift, zooming, switching on/off transparency and lighting can be done with help of tool-buttons (for <code>mglWindow</code>) or by hot-keys: ‘<samp>a</samp>’, ‘<samp>d</samp>’, ‘<samp>w</samp>’, ‘<samp>s</samp>’ for plot rotation, ‘<samp>r</samp>’ and ‘<samp>f</samp>’ switching on/off transparency and lighting. Press ‘<samp>x</samp>’ for exit (or closing the window).
</p>
<p>In this example function <code>sample</code> rotates axes (<code>Rotate()</code>, see section <a href="mathgl_en_33.html#Subplots-and-rotation">Subplots and rotation</a>) and draws the bounding box (<code>Box()</code>). Drawing is placed in separate function since it will be used on demand when window canvas needs to be redrawn.
</p>
<hr size="6">
<a name="Drawing-to-file"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-MathGL-window" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Animation" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Drawing-to-file-1"></a>
<h3 class="subsection">2.1.2 Drawing to file</h3>
<p>Another way of using MathGL library is the direct writing of the picture to the file. It is most usable for plot creation during long calculation or for using of small programs (like Matlab or Scilab scripts) for visualizing repetitive sets of data. But the speed of drawing is much higher in comparison with a script language.
</p>
<p>The following code produces a bitmap PNG picture:
</p><pre class="verbatim">#include <mgl2/mgl.h>
int main(int ,char **)
{
mglGraph gr;
gr.Alpha(true); gr.Light(true);
sample(&gr); // The same drawing function.
gr.WritePNG("test.png"); // Don't forget to save the result!
return 0;
}
</pre><p> For compilation, you need only libmgl library not the one with widgets
</p><pre class="verbatim">gcc test.cpp -lmgl
</pre><p>This can be important if you create a console program in computer/cluster where X-server (and widgets) is inaccessible.
</p>
<p>The only difference from the previous variant (using windows) is manual switching on the transparency <code>Alpha</code> and lightning <code>Light</code>, if you need it. The usage of frames (see <a href="#Animation">Animation</a>) is not advisable since the whole image is prepared each time. If function <code>sample</code> contains frames then only last one will be saved to the file. In principle, one does not need to separate drawing functions in case of direct file writing in consequence of the single calling of this function for each picture. However, one may use the same drawing procedure to create a plot with changeable parameters, to export in different file types, to emphasize the drawing code and so on. So, in future I will put the drawing in the separate function.
</p>
<p>The code for export into other formats (for example, into vector EPS file) looks the same:
</p><pre class="verbatim">#include <mgl2/mgl.h>
int main(int ,char **)
{
mglGraph gr;
gr.Light(true);
sample(&gr); // The same drawing function.
gr.WriteEPS("test.eps"); // Don't forget to save the result!
return 0;
}
</pre><p>The difference from the previous one is using other function <code>WriteEPS()</code> for EPS format instead of function <code>WritePNG()</code>. Also, there is no switching on of the plot transparency <code>Alpha</code> since EPS format does not support it.
</p>
<hr size="6">
<a name="Animation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Drawing-to-file" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Drawing-in-memory" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Animation-1"></a>
<h3 class="subsection">2.1.3 Animation</h3>
<p>Widget classes (<code>mglWindow</code>, <code>mglGLUT</code>) support a delayed drawing, when all plotting functions are called once at the beginning of writing to memory lists. Further program displays the saved lists faster. Resulting redrawing will be faster but it requires sufficient memory. Several lists (frames) can be displayed one after another (by pressing ‘<samp>,</samp>’, ‘<samp>.</samp>’) or run as cinema. To switch these feature on one needs to modify function <code>sample</code>:
</p><pre class="verbatim">int sample(mglGraph *gr)
{
gr->NewFrame(); // the first frame
gr->Rotate(60,40);
gr->Box();
gr->EndFrame(); // end of the first frame
gr->NewFrame(); // the second frame
gr->Box();
gr->Axis("xy");
gr->EndFrame(); // end of the second frame
return gr->GetNumFrame(); // returns the frame number
}
</pre><p>First, the function creates a frame by calling <code>NewFrame()</code> for rotated axes and draws the bounding box. The function <code>EndFrame()</code> <strong>must be</strong> called after the frame drawing! The second frame contains the bounding box and axes <code>Axis("xy")</code> in the initial (unrotated) coordinates. Function <code>sample</code> returns the number of created frames <code>GetNumFrame()</code>.
</p>
<p>Note, that such kind of animation is rather slow and not well suitable for visualization of running calculations. For the last case one can use <code>Update()</code> function. The most simple case for doing this is to use <code>mglDraw</code> class and reimplement its <code>Calc()</code> method.
</p><pre class="verbatim">#include <mgl2/window.h>
class Foo : public mglDraw
{
mglPoint pnt; // some result of calculation
public:
mglWindow *Gr; // graphics to be updated
int Draw(mglGraph *gr);
void Calc();
} foo;
//-----------------------------------------------------
void Foo::Calc()
{
for(int i=0;i<30;i++) // do calculation
{
sleep(2); // which can be very long
pnt = mglPoint(2*mgl_rnd()-1,2*mgl_rnd()-1);
Gr->Update(); // update window
}
}
//-----------------------------------------------------
int Foo::Draw(mglGraph *gr)
{
gr->Line(mglPoint(),pnt,"Ar2");
gr->Box();
return 0;
}
//-----------------------------------------------------
int main(int argc,char **argv)
{
mglWindow gr(&foo,"MathGL examples");
foo.Gr = &gr; foo.Run();
return gr.Run();
}
</pre>
<p>Previous sample can be run in C++ only since it use C++ class mglDraw. However similar idea can be used even in Fortran or SWIG-based (Python/Octave/...) if one use FLTK window. Such limitation come from the Qt requirement to be run in the primary thread only. The sample code will be:
</p><pre class="verbatim">int main(int argc,char **argv)
{
mglWindow gr("test"); // create window
gr.RunThr(); // run event loop in separate thread
for(int i=0;i<10;i++) // do calculation
{
sleep(1); // which can be very long
pnt = mglPoint(2*mgl_rnd()-1,2*mgl_rnd()-1);
gr.Clf(); // make new drawing
gr.Line(mglPoint(),pnt,"Ar2");
char str[10] = "i=0"; str[2] = '0'+i;
gr.Puts(mglPoint(),"");
gr.Update(); // update window when you need it
}
return 0; // finish calculations and close the window
}
</pre>
<p>Pictures with <strong>animation can be saved in file(s)</strong> as well. You can: export in animated GIF, or save each frame in separate file (usually JPEG) and convert these files into the movie (for example, by help of ImageMagic). Let me show both methods.
</p>
<p><a name="GIF"></a>
The simplest methods is making animated GIF. There are 3 steps: (1) open GIF file by <code>StartGIF()</code> function; (2) create the frames by calling <code>NewFrame()</code> before and <code>EndFrame()</code> after plotting; (3) close GIF by <code>CloseGIF()</code> function. So the simplest code for “running” sinusoid will look like this:
</p><pre class="verbatim">#include <mgl2/mgl.h>
int main(int ,char **)
{
mglGraph gr;
mglData dat(100);
char str[32];
gr.StartGIF("sample.gif");
for(int i=0;i<40;i++)
{
gr.NewFrame(); // start frame
gr.Box(); // some plotting
for(int j=0;j<dat.nx;j++)
dat.a[j]=sin(M_PI*j/dat.nx+M_PI*0.05*i);
gr.Plot(dat,"b");
gr.EndFrame(); // end frame
}
gr.CloseGIF();
return 0;
}
</pre>
<p><a name="MPEG"></a>
The second way is saving each frame in separate file (usually JPEG) and later make the movie from them. MathGL have special function for saving frames – it is <code>WriteFrame()</code>. This function save each frame with automatic name ‘<samp>frame0001.jpg, frame0002.jpg</samp>’ and so on. Here prefix ‘<samp>frame</samp>’ is defined by <var>PlotId</var> variable of <code>mglGraph</code> class. So the similar code will look like this:
</p><pre class="verbatim">#include <mgl2/mgl.h>
int main(int ,char **)
{
mglGraph gr;
mglData dat(100);
char str[32];
for(int i=0;i<40;i++)
{
gr.NewFrame(); // start frame
gr.Box(); // some plotting
for(int j=0;j<dat.nx;j++)
dat.a[j]=sin(M_PI*j/dat.nx+M_PI*0.05*i);
gr.Plot(dat,"b");
gr.EndFrame(); // end frame
gr.WriteFrame(); // save frame
}
return 0;
}
</pre>
<p>Created files can be converted to movie by help of a lot of programs. For example, you can use ImageMagic (command ‘<samp>convert frame*.jpg movie.mpg</samp>’), MPEG library, GIMP and so on.
</p>
<p>Finally, you can use <code>mglconv</code> tool for doing the same with MGL scripts (see section <a href="mathgl_en_7.html#Utilities">Utilities for parsing MGL</a>).
</p>
<hr size="6">
<a name="Drawing-in-memory"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Animation" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Using-QMathGL" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Drawing-in-memory-1"></a>
<h3 class="subsection">2.1.4 Drawing in memory</h3>
<p>The last way of MathGL using is the drawing in memory. Class <code>mglGraph</code> allows one to create a bitmap picture in memory. Further this picture can be displayed in window by some window libraries (like wxWidgets, FLTK, Windows GDI and so on). For example, the code for drawing in wxWidget library looks like:
</p><pre class="verbatim">void MyForm::OnPaint(wxPaintEvent& event)
{
int w,h,x,y;
GetClientSize(&w,&h); // size of the picture
mglGraph gr(w,h);
gr.Alpha(true); // draws something using MathGL
gr.Light(true);
sample(&gr,NULL);
wxImage img(w,h,gr.GetRGB(),true);
ToolBar->GetSize(&x,&y); // gets a height of the toolbar if any
wxPaintDC dc(this); // and draws it
dc.DrawBitmap(wxBitmap(img),0,y);
}
</pre><p>The drawing in other libraries is most the same.
</p>
<p>For example, FLTK code will look like
</p><pre class="verbatim">void Fl_MyWidget::draw()
{
mglGraph gr(w(),h());
gr.Alpha(true); // draws something using MathGL
gr.Light(true);
sample(&gr,NULL);
fl_draw_image(gr.GetRGB(), x(), y(), gr.GetWidth(), gr.GetHeight(), 3);
}
</pre><p>Qt code will look like
</p><pre class="verbatim">void MyWidget::paintEvent(QPaintEvent *)
{
mglGraph gr(w(),h());
gr.Alpha(true); // draws something using MathGL
gr.Light(true); gr.Light(0,mglPoint(1,0,-1));
sample(&gr,NULL);
// Qt don't support RGB format as is. So, let convert it to BGRN.
long w=gr.GetWidth(), h=gr.GetHeight();
unsigned char *buf = new uchar[4*w*h];
gr.GetBGRN(buf, 4*w*h)
QPixmap pic = QPixmap::fromImage(QImage(*buf, w, h, QImage::Format_RGB32));
QPainter paint;
paint.begin(this); paint.drawPixmap(0,0,pic); paint.end();
delete []buf;
}
</pre>
<hr size="6">
<a name="Using-QMathGL"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Drawing-in-memory" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#MathGL-and-PyQt" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-QMathGL-1"></a>
<h3 class="subsection">2.1.5 Using QMathGL</h3>
<p>MathGL have several interface widgets for different widget libraries. There are QMathGL for Qt, Fl_MathGL for FLTK. These classes provide control which display MathGL graphics. Unfortunately there is no uniform interface for widget classes because all libraries have slightly different set of functions, features and so on. However the usage of MathGL widgets is rather simple. Let me show it on the example of QMathGL.
</p>
<p>First of all you have to define the drawing function or inherit a class from <code>mglDraw</code> class. After it just create a window and setup QMathGL instance as any other Qt widget:
</p><pre class="verbatim">#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include <mgl2/qmathgl.h>
int main(int argc,char **argv)
{
QApplication a(argc,argv);
QMainWindow *Wnd = new QMainWindow;
Wnd->resize(810,610); // for fill up the QMGL, menu and toolbars
Wnd->setWindowTitle("QMathGL sample");
// here I allow to scroll QMathGL -- the case
// then user want to prepare huge picture
QScrollArea *scroll = new QScrollArea(Wnd);
// Create and setup QMathGL
QMathGL *QMGL = new QMathGL(Wnd);
//QMGL->setPopup(popup); // if you want to setup popup menu for QMGL
QMGL->setDraw(sample);
// or use QMGL->setDraw(foo); for instance of class Foo:public mglDraw
QMGL->update();
// continue other setup (menu, toolbar and so on)
scroll->setWidget(QMGL);
Wnd->setCentralWidget(scroll);
Wnd->show();
return a.exec();
}
</pre>
<hr size="6">
<a name="MathGL-and-PyQt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-QMathGL" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#MathGL-and-MPI" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="MathGL-and-PyQt-1"></a>
<h3 class="subsection">2.1.6 MathGL and PyQt</h3>
<p>Generally SWIG based classes (including the Python one) are the same as C++ classes. However, there are few tips for using MathGL with PyQt. Below I place a very simple python code which demonstrate how MathGL can be used with PyQt. This code is mostly written by Prof. Dr. Heino Falcke. You can just copy it to a file <code>mgl-pyqt-test.py</code> and execute it from python shell by command <code>execfile("mgl-pyqt-test.py")</code>
</p>
<pre class="verbatim">from PyQt4 import QtGui,QtCore
from mathgl import *
import sys
app = QtGui.QApplication(sys.argv)
qpointf=QtCore.QPointF()
class hfQtPlot(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.img=(QtGui.QImage())
def setgraph(self,gr):
self.buffer='\t'
self.buffer=self.buffer.expandtabs(4*gr.GetWidth()*gr.GetHeight())
gr.GetBGRN(self.buffer,len(self.buffer))
self.img=QtGui.QImage(self.buffer, gr.GetWidth(),gr.GetHeight(),QtGui.QImage.Format_ARGB32)
self.update()
def paintEvent(self, event):
paint = QtGui.QPainter()
paint.begin(self)
paint.drawImage(qpointf,self.img)
paint.end()
BackgroundColor=[1.0,1.0,1.0]
size=100
gr=mglGraph()
y=mglData(size)
#y.Modify("((0.7*cos(2*pi*(x+.2)*500)+0.3)*(rnd*0.5+0.5)+362.135+10000.)")
y.Modify("(cos(2*pi*x*10)+1.1)*1000.*rnd-501")
x=mglData(size)
x.Modify("x^2");
def plotpanel(gr,x,y,n):
gr.SubPlot(2,2,n)
gr.SetXRange(x)
gr.SetYRange(y)
gr.AdjustTicks()
gr.Axis()
gr.Box()
gr.Label("x","x-Axis",1)
gr.Label("y","y-Axis",1)
gr.ClearLegend()
gr.AddLegend("Legend: "+str(n),"k")
gr.Legend()
gr.Plot(x,y)
gr.Clf(BackgroundColor[0],BackgroundColor[1],BackgroundColor[2])
gr.SetPlotFactor(1.5)
plotpanel(gr,x,y,0)
y.Modify("(cos(2*pi*x*10)+1.1)*1000.*rnd-501")
plotpanel(gr,x,y,1)
y.Modify("(cos(2*pi*x*10)+1.1)*1000.*rnd-501")
plotpanel(gr,x,y,2)
y.Modify("(cos(2*pi*x*10)+1.1)*1000.*rnd-501")
plotpanel(gr,x,y,3)
gr.WritePNG("test.png","Test Plot")
qw = hfQtPlot()
qw.show()
qw.setgraph(gr)
qw.raise_()
</pre>
<hr size="6">
<a name="MathGL-and-MPI"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#MathGL-and-PyQt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_11.html#Advanced-usage" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="MathGL-and-MPI-1"></a>
<h3 class="subsection">2.1.7 MathGL and MPI</h3>
<p>For using MathGL in MPI program you just need to: (1) plot its own part of data for each running node; (2) collect resulting graphical information in a single program (for example, at node with rank=0); (3) save it. The sample code below demonstrate this for very simple sample of surface drawing.
</p>
<p>First you need to initialize MPI
</p><pre class="verbatim">#include <stdio.h>
#include <mgl2/mgl.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
// initialize MPI
int rank=0, numproc=1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numproc);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank==0) printf("Use %d processes.\n", numproc);
</pre>
<p>Next step is data creation. For simplicity, I create data arrays with the same sizes for all nodes. At this, you have to create <code>mglGraph</code> object too.
</p>
<pre class="verbatim"> // initialize data similarly for all nodes
mglData a(128,256);
mglGraph gr;
</pre>
<p>Now, data should be filled by numbers. In real case, it should be some kind of calculations. But I just fill it by formula.
</p>
<pre class="verbatim"> // do the same plot for its own range
char buf[64];
sprintf(buf,"xrange %g %g",2.*rank/numproc-1,2.*(rank+1)/numproc-1);
gr.Fill(a,"sin(2*pi*x)",buf);
</pre>
<p>It is time to plot the data. Don’t forget to set proper axis range(s) by using parametric form or by using options (as in the sample).
</p>
<pre class="verbatim"> // plot data in each node
gr.Clf(); // clear image before making the image
gr.Rotate(40,60);
gr.Surf(a,"",buf);
</pre>
<p>Finally, let send graphical information to node with rank=0.
</p>
<pre class="verbatim"> // collect information
if(rank!=0) gr.MPI_Send(0);
else for(int i=1;i<numproc;i++) gr.MPI_Recv(i);
</pre>
<p>Now, node with rank=0 have whole image. It is time to save the image to a file. Also, you can add a kind of annotations here – I draw axis and bounding box in the sample.
</p>
<pre class="verbatim"> if(rank==0)
{
gr.Box(); gr.Axis(); // some post processing
gr.WritePNG("test.png"); // save result
}
</pre>
<p>In my case the program is done, and I finalize MPI. In real program, you can repeat the loop of data calculation and data plotting as many times as you need.
</p>
<pre class="verbatim"> MPI_Finalize();
return 0;
}
</pre>
<p>You can type ‘<samp>mpic++ test.cpp -lmgl && mpirun -np 8 ./a.out</samp>’ for compilation and running the sample program on 8 nodes. Note, that you have to set enable-mpi=ON at MathGL configure to use this feature.
</p>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#MathGL-and-PyQt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_11.html#Advanced-usage" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>Build Daemon user</em> on <em>December 22, 2013</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|