File: examples-blitz.cc

package info (click to toggle)
gnuplot-iostream 0~20140302.gitc8919a0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 688 kB
  • ctags: 595
  • sloc: cpp: 2,926; sh: 122; makefile: 66
file content (122 lines) | stat: -rw-r--r-- 3,913 bytes parent folder | download | duplicates (3)
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
/*
Copyright (c) 2013 Daniel Stahlke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <vector>
#include <math.h>
// This must be included before gnuplot-iostream.h in order to support plotting blitz arrays.
#include <blitz/array.h>

#include "gnuplot-iostream.h"

// Yes, I'm including a *.cc file.  It contains main().
#include "examples-framework.cc"

void demo_blitz_basic() {
	// -persist option makes the window not disappear when your program exits
	Gnuplot gp("gnuplot -persist");

	blitz::Array<double, 2> arr(100, 100);
	{
		blitz::firstIndex i;
		blitz::secondIndex j;
		arr = (i-50) * (j-50);
	}
	gp << "set pm3d map; set palette" << std::endl;
	gp << "splot '-'" << std::endl;
	gp.send(arr);
}

void demo_blitz_waves_binary() {
	Gnuplot gp("gnuplot -persist");

	// example from Blitz manual:
	int N = 64, cycles = 3;
	double midpoint = (N-1)/2.;
	double omega = 2.0 * M_PI * cycles / double(N);
	double tau = - 10.0 / N;
	blitz::Array<double, 2> F(N, N);
	blitz::firstIndex i;
	blitz::secondIndex j;
	F = cos(omega * sqrt(pow2(i-midpoint) + pow2(j-midpoint)))
		* exp(tau * sqrt(pow2(i-midpoint) + pow2(j-midpoint)));

	gp << "splot '-' binary" << gp.binfmt(F) << "dx=10 dy=10 origin=(5,5,0) with pm3d notitle" << std::endl;
	gp.sendBinary(F);
}

void demo_blitz_sierpinski_binary() {
	Gnuplot gp("gnuplot -persist");

	int N = 256;
	blitz::Array<blitz::TinyVector<uint8_t, 4>, 2> F(N, N);
	for(int i=0; i<N; i++)
	for(int j=0; j<N; j++) {
		F(i, j)[0] = i;
		F(i, j)[1] = j;
		F(i, j)[2] = 0;
		F(i, j)[3] = (i&j) ? 0 : 255;
	}

	gp << "plot '-' binary" << gp.binfmt(F) << "with rgbalpha notitle" << std::endl;
	gp.sendBinary(F);
}

void demo_blitz_waves_binary_file() {
	Gnuplot gp("gnuplot -persist");

	// example from Blitz manual:
	int N = 64, cycles = 3;
	double midpoint = (N-1)/2.;
	double omega = 2.0 * M_PI * cycles / double(N);
	double tau = - 10.0 / N;
	blitz::Array<double, 2> F(N, N);
	blitz::firstIndex i;
	blitz::secondIndex j;
	F = cos(omega * sqrt(pow2(i-midpoint) + pow2(j-midpoint)))
		* exp(tau * sqrt(pow2(i-midpoint) + pow2(j-midpoint)));

	gp << "splot" << gp.binaryFile(F) << "dx=10 dy=10 origin=(5,5,0) with pm3d notitle" << std::endl;
}

void demo_blitz_sierpinski_binary_file() {
	Gnuplot gp("gnuplot -persist");

	int N = 256;
	blitz::Array<blitz::TinyVector<uint8_t, 4>, 2> F(N, N);
	for(int i=0; i<N; i++)
	for(int j=0; j<N; j++) {
		F(i, j)[0] = i;
		F(i, j)[1] = j;
		F(i, j)[2] = 0;
		F(i, j)[3] = (i&j) ? 0 : 255;
	}

	gp << "plot" << gp.binaryFile(F) << "with rgbalpha notitle" << std::endl;
}

void register_demos() {
	register_demo("basic",                        demo_blitz_basic);
	register_demo("waves_binary",           demo_blitz_waves_binary);
	register_demo("sierpinski_binary",      demo_blitz_sierpinski_binary);
	register_demo("waves_binary_file",      demo_blitz_waves_binary_file);
	register_demo("sierpinski_binary_file", demo_blitz_sierpinski_binary_file);
}