1 |
caltinay |
4955 |
/* |
2 |
|
|
* Copyright 2008-2013 Steven Dalton |
3 |
|
|
* |
4 |
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
|
* you may not use this file except in compliance with the License. |
6 |
|
|
* You may obtain a copy of the License at |
7 |
|
|
* |
8 |
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
|
* |
10 |
|
|
* Unless required by applicable law or agreed to in writing, software |
11 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
|
* See the License for the specific language governing permissions and |
14 |
|
|
* limitations under the License. |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
/** |
18 |
|
|
* @file matrix_canvas_svg_output.cc |
19 |
|
|
* Functions to save the current view as an SVG file. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
/* |
23 |
|
|
* David Gleich |
24 |
|
|
* 1 August 2007 |
25 |
|
|
* Copyright, Stanford University |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include <stdio.h> |
29 |
|
|
#include <stdlib.h> |
30 |
|
|
#include <iostream> |
31 |
|
|
|
32 |
|
|
#include <cusp/opengl/spy/matrix_canvas.h> |
33 |
|
|
|
34 |
|
|
namespace cusp |
35 |
|
|
{ |
36 |
|
|
namespace opengl |
37 |
|
|
{ |
38 |
|
|
namespace spy |
39 |
|
|
{ |
40 |
|
|
|
41 |
|
|
static void color2rgb(float *color, int& r, int &g, int &b) |
42 |
|
|
{ |
43 |
|
|
r = (int)(color[0]*255.0f); |
44 |
|
|
g = (int)(color[1]*255.0f); |
45 |
|
|
b = (int)(color[2]*255.0f); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
template< typename IndexType, typename ValueType, typename MemorySpace > |
49 |
|
|
void matrix_canvas<IndexType,ValueType,MemorySpace>::write_svg() |
50 |
|
|
{ |
51 |
|
|
std::cout << "writing matrix to spy.svg ... " << std::endl; |
52 |
|
|
FILE *svgfile = fopen("spy.svg", "wt"); |
53 |
|
|
if (!svgfile) { |
54 |
|
|
printf("spy.svg not writable\n"); |
55 |
|
|
return; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"); |
59 |
|
|
|
60 |
|
|
float onepx = scale_to_world(1.0f); |
61 |
|
|
int r,g,b; |
62 |
|
|
|
63 |
|
|
{ |
64 |
|
|
GLint view[4]; |
65 |
|
|
|
66 |
|
|
glGetIntegerv(GL_VIEWPORT, view); |
67 |
|
|
color2rgb(background_color, r, g, b); |
68 |
|
|
fprintf(svgfile, "<svg viewbox=\"%i %i %i %i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"%d\" height=\"%d\">\n", |
69 |
|
|
0, 0, view[2], view[3], view[2], view[3]); |
70 |
|
|
fprintf(svgfile, "<rect x=\"%i\" y=\"%i\" width=\"%i\" height=\"%i\" fill=\"rgb(%i,%i,%i)\" />\n", |
71 |
|
|
0, 0, view[2], view[3], r,g,b); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
{ |
75 |
|
|
GLint view[4]; |
76 |
|
|
GLdouble model[16], proj[16], total[16]; |
77 |
|
|
|
78 |
|
|
glGetIntegerv(GL_VIEWPORT, view); |
79 |
|
|
glGetDoublev(GL_MODELVIEW_MATRIX, model); |
80 |
|
|
glGetDoublev(GL_PROJECTION_MATRIX, proj); |
81 |
|
|
|
82 |
|
|
fprintf(svgfile, "<g transform=\"translate(%lf,%lf)\">\n", |
83 |
|
|
(double)view[2]/2.0, (double)view[3]/2.0); |
84 |
|
|
|
85 |
|
|
fprintf(svgfile, "<g transform=\"scale(%lf,%lf)\">\n", |
86 |
|
|
(double)view[2]/2.0, (double)view[3]/-2.0); |
87 |
|
|
|
88 |
|
|
// compute the matrix product, matrices in column-major |
89 |
|
|
for (int i = 0; i < 4; i++) |
90 |
|
|
{ |
91 |
|
|
for (int j = 0; j < 4; j++) |
92 |
|
|
{ |
93 |
|
|
// i is the column, j is the row |
94 |
|
|
GLdouble sum = 0.0; |
95 |
|
|
for (int k = 0; k < 4; k++) |
96 |
|
|
{ |
97 |
|
|
// dot product between row j in proj, and col i in model |
98 |
|
|
sum += proj[4*k+j]*model[4*i+k]; |
99 |
|
|
} |
100 |
|
|
total[i*4+j] = sum; |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
fprintf(svgfile, "<g transform=\"matrix(%lf,%lf,%lf,%lf,%lf,%lf)\">\n", |
105 |
|
|
total[0], total[1], total[4], total[5], total[12], total[13]); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
// |
109 |
|
|
// write the border |
110 |
|
|
// |
111 |
|
|
int m = _m.num_rows; |
112 |
|
|
int n = _m.num_cols; |
113 |
|
|
|
114 |
|
|
color2rgb(border_color, r, g, b); |
115 |
|
|
fprintf(svgfile, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke-width=\"%f\" stroke-opacity=\"%f\" stroke=\"rgb(%i,%i,%i)\" />\n", |
116 |
|
|
-0.5f,-0.5f,-0.5f,m-0.5f,onepx/2,1.0,r,g,b); |
117 |
|
|
fprintf(svgfile, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke-width=\"%f\" stroke-opacity=\"%f\" stroke=\"rgb(%i,%i,%i)\" />\n", |
118 |
|
|
-0.5f,m-0.5f,n-0.5f,m-0.5f,onepx/2,1.0,r,g,b); |
119 |
|
|
fprintf(svgfile, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke-width=\"%f\" stroke-opacity=\"%f\" stroke=\"rgb(%i,%i,%i)\" />\n", |
120 |
|
|
n-0.5f,m-0.5f,n-0.5f,-0.5f,onepx/2,1.0,r,g,b); |
121 |
|
|
fprintf(svgfile, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke-width=\"%f\" stroke-opacity=\"%f\" stroke=\"rgb(%i,%i,%i)\" />\n", |
122 |
|
|
n-0.5f,-0.5f,-0.5f,-0.5f,onepx/2,1.0,r,g,b); |
123 |
|
|
|
124 |
|
|
// |
125 |
|
|
// write through the matrix |
126 |
|
|
// |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
{ |
130 |
|
|
ValueType v; |
131 |
|
|
int colormap_entry; |
132 |
|
|
|
133 |
|
|
//float pts=(zoom / (virtual_width*aspect/(float)width)); |
134 |
|
|
|
135 |
|
|
float x1,y1,x2,y2; |
136 |
|
|
world_extents(x1,y1,x2,y2); |
137 |
|
|
|
138 |
|
|
int r1=(int)floor(y1),r2=(int)floor(y2); |
139 |
|
|
int c1=(int)floor(x1),c2=(int)floor(x2); |
140 |
|
|
|
141 |
|
|
ValueType max_val = matrix_stats.max_val; |
142 |
|
|
ValueType min_val = matrix_stats.min_val; |
143 |
|
|
|
144 |
|
|
if (max_val - min_val <= 0) |
145 |
|
|
{ |
146 |
|
|
// this sets min_val to something reasonable, and |
147 |
|
|
// shows the high end of the colormap if the values |
148 |
|
|
// are all equal |
149 |
|
|
min_val = max_val - 1.0; |
150 |
|
|
} |
151 |
|
|
ValueType inv_val_range = 1.0/(max_val - min_val); |
152 |
|
|
|
153 |
|
|
float alpha = alpha_from_zoom(); |
154 |
|
|
|
155 |
|
|
if (normalization_state != no_normalization) { |
156 |
|
|
min_val = 0.0; |
157 |
|
|
max_val = 1.0f; |
158 |
|
|
inv_val_range = 1.0f; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
for (int pi = std::max(0,r1); pi < std::min(r2, m); ++pi) |
162 |
|
|
{ |
163 |
|
|
if (pi-std::max(0,r1)>0 && (pi-std::max(0,r1)) % 10000 == 0 && |
164 |
|
|
std::min(r2, m) - std::max(0,r1) >= 20000) { |
165 |
|
|
std::cout << " writing row " << (pi-std::max(0,r1)) |
166 |
|
|
<< " of " << std::min(r2, m) - std::max(0,r1) |
167 |
|
|
<< std::endl; |
168 |
|
|
} |
169 |
|
|
// i is the real row in the matrix for the pith row |
170 |
|
|
// of the display |
171 |
|
|
int i=pi; |
172 |
|
|
if (permutation_state == row_permutation || |
173 |
|
|
permutation_state == row_column_permutation) { |
174 |
|
|
i=irperm[i]; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
for (IndexType ri = _m.row_offsets[i]; ri < _m.row_offsets[i+1]; ++ri) |
178 |
|
|
{ |
179 |
|
|
// j is the real column in the matrix for the pjth |
180 |
|
|
// column of the display |
181 |
|
|
int j = _m.column_indices[ri]; |
182 |
|
|
int pj = j; |
183 |
|
|
if (permutation_state == column_permutation || |
184 |
|
|
permutation_state == row_column_permutation) { |
185 |
|
|
pj = cperm[pj]; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
// skip all the columns outside |
189 |
|
|
if (pj < c1 || pj > c2) { continue; } |
190 |
|
|
|
191 |
|
|
v = _m.values[ri]; |
192 |
|
|
if (normalization_state == row_normalization || |
193 |
|
|
normalization_state == row_column_normalization) { |
194 |
|
|
v*=rnorm[i]; |
195 |
|
|
} |
196 |
|
|
if (normalization_state == column_normalization || |
197 |
|
|
normalization_state == row_column_normalization) { |
198 |
|
|
v*=cnorm[j]; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
// scale v to the range [0,1] |
202 |
|
|
v = v - min_val; |
203 |
|
|
v = v*inv_val_range; |
204 |
|
|
|
205 |
|
|
if (!colormap_invert) { |
206 |
|
|
colormap_entry = (int)(v*(colormap.size-1)); |
207 |
|
|
} else { |
208 |
|
|
colormap_entry = (int)(v*(colormap.size-1)); |
209 |
|
|
colormap_entry=colormap.size-1-colormap_entry; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
color2rgb(&colormap.map[colormap_entry*3],r,g,b); |
213 |
|
|
|
214 |
|
|
fprintf(svgfile, "<circle cx=\"%g\" cy=\"%g\" r=\"%g\" fill=\"rgb(%i,%i,%i)\" opacity=\"%g\"/>\n", |
215 |
|
|
(float)pi,(float)pj, (std::max)(0.5f,onepx/2.0f), r,g,b, alpha); |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
fprintf(svgfile,"</g>\n"); |
222 |
|
|
fprintf(svgfile,"</g>\n"); |
223 |
|
|
fprintf(svgfile,"</g>\n"); |
224 |
|
|
fprintf(svgfile, "</svg>\n"); |
225 |
|
|
|
226 |
|
|
fclose(svgfile); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
} // end spy |
230 |
|
|
} // end opengl |
231 |
|
|
} // end cusp |