~ubuntu-branches/ubuntu/utopic/gnuplot-iostream/utopic

« back to all changes in this revision

Viewing changes to legacy/examples-blitz.cc

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2013-05-08 21:09:45 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130508210945-wrn68tn3pvcvd79r
Tags: 0~20130424.gita9d3e31-1
* [10be64a] Imported Upstream version 0~20130424.gita9d3e31
* [4454719] Enable some more tests.
* [d88265b] Add blitz, armadillo to BD to enable some more tests.
* [0603e0a] Minor update in rules and install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2013 Daniel Stahlke
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
of this software and associated documentation files (the "Software"), to deal
 
6
in the Software without restriction, including without limitation the rights
 
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
8
copies of the Software, and to permit persons to whom the Software is
 
9
furnished to do so, subject to the following conditions:
 
10
 
 
11
The above copyright notice and this permission notice shall be included in
 
12
all copies or substantial portions of the Software.
 
13
 
 
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
20
THE SOFTWARE.
 
21
*/
 
22
 
 
23
#include <vector>
 
24
#include <math.h>
 
25
// This must be included before gnuplot-iostream.h in order to support plotting blitz arrays.
 
26
#include <blitz/array.h>
 
27
 
 
28
#include "gnuplot-iostream.h"
 
29
 
 
30
// Yes, I'm including a *.cc file.  It contains main().
 
31
#include "examples-framework.cc"
 
32
 
 
33
void demo_blitz_basic() {
 
34
        // -persist option makes the window not disappear when your program exits
 
35
        Gnuplot gp("gnuplot -persist");
 
36
 
 
37
        blitz::Array<double, 2> arr(100, 100);
 
38
        {
 
39
                blitz::firstIndex i;
 
40
                blitz::secondIndex j;
 
41
                arr = (i-50) * (j-50);
 
42
        }
 
43
        gp << "set pm3d map; set palette" << std::endl;
 
44
        gp << "splot '-'" << std::endl;
 
45
        gp.send(arr);
 
46
}
 
47
 
 
48
void demo_blitz_waves_binary() {
 
49
        Gnuplot gp("gnuplot -persist");
 
50
 
 
51
        // example from Blitz manual:
 
52
        int N = 64, cycles = 3;
 
53
        double midpoint = (N-1)/2.;
 
54
        double omega = 2.0 * M_PI * cycles / double(N);
 
55
        double tau = - 10.0 / N;
 
56
        blitz::Array<double, 2> F(N, N);
 
57
        blitz::firstIndex i;
 
58
        blitz::secondIndex j;
 
59
        F = cos(omega * sqrt(pow2(i-midpoint) + pow2(j-midpoint)))
 
60
                * exp(tau * sqrt(pow2(i-midpoint) + pow2(j-midpoint)));
 
61
 
 
62
        gp << "splot '-' binary" << gp.binfmt(F) << "dx=10 dy=10 origin=(5,5,0) with pm3d notitle" << std::endl;
 
63
        gp.sendBinary(F);
 
64
}
 
65
 
 
66
void demo_blitz_sierpinski_binary() {
 
67
        Gnuplot gp("gnuplot -persist");
 
68
 
 
69
        int N = 256;
 
70
        blitz::Array<blitz::TinyVector<uint8_t, 4>, 2> F(N, N);
 
71
        for(int i=0; i<N; i++)
 
72
        for(int j=0; j<N; j++) {
 
73
                F(i, j)[0] = i;
 
74
                F(i, j)[1] = j;
 
75
                F(i, j)[2] = 0;
 
76
                F(i, j)[3] = (i&j) ? 0 : 255;
 
77
        }
 
78
 
 
79
        gp << "plot '-' binary" << gp.binfmt(F) << "with rgbalpha notitle" << std::endl;
 
80
        gp.sendBinary(F);
 
81
}
 
82
 
 
83
void demo_blitz_waves_binary_file() {
 
84
        Gnuplot gp("gnuplot -persist");
 
85
 
 
86
        // example from Blitz manual:
 
87
        int N = 64, cycles = 3;
 
88
        double midpoint = (N-1)/2.;
 
89
        double omega = 2.0 * M_PI * cycles / double(N);
 
90
        double tau = - 10.0 / N;
 
91
        blitz::Array<double, 2> F(N, N);
 
92
        blitz::firstIndex i;
 
93
        blitz::secondIndex j;
 
94
        F = cos(omega * sqrt(pow2(i-midpoint) + pow2(j-midpoint)))
 
95
                * exp(tau * sqrt(pow2(i-midpoint) + pow2(j-midpoint)));
 
96
 
 
97
        gp << "splot" << gp.binaryFile(F) << "dx=10 dy=10 origin=(5,5,0) with pm3d notitle" << std::endl;
 
98
}
 
99
 
 
100
void demo_blitz_sierpinski_binary_file() {
 
101
        Gnuplot gp("gnuplot -persist");
 
102
 
 
103
        int N = 256;
 
104
        blitz::Array<blitz::TinyVector<uint8_t, 4>, 2> F(N, N);
 
105
        for(int i=0; i<N; i++)
 
106
        for(int j=0; j<N; j++) {
 
107
                F(i, j)[0] = i;
 
108
                F(i, j)[1] = j;
 
109
                F(i, j)[2] = 0;
 
110
                F(i, j)[3] = (i&j) ? 0 : 255;
 
111
        }
 
112
 
 
113
        gp << "plot" << gp.binaryFile(F) << "with rgbalpha notitle" << std::endl;
 
114
}
 
115
 
 
116
void register_demos() {
 
117
        register_demo("basic",                        demo_blitz_basic);
 
118
        register_demo("waves_binary",           demo_blitz_waves_binary);
 
119
        register_demo("sierpinski_binary",      demo_blitz_sierpinski_binary);
 
120
        register_demo("waves_binary_file",      demo_blitz_waves_binary_file);
 
121
        register_demo("sierpinski_binary_file", demo_blitz_sierpinski_binary_file);
 
122
}