/[escript]/trunk/cusplibrary/cusp/opengl/spy/matrix_canvas.h
ViewVC logotype

Annotation of /trunk/cusplibrary/cusp/opengl/spy/matrix_canvas.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (hide annotations)
Tue May 20 04:33:15 2014 UTC (6 years, 5 months ago) by caltinay
Original Path: branches/diaplayground/cusplibrary/cusp/opengl/spy/matrix_canvas.h
File MIME type: text/plain
File size: 6577 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

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     #pragma once
18    
19     #include <string>
20     #include <limits>
21     #include <cmath>
22     #include <vector>
23    
24     #include <cusp/csr_matrix.h>
25     #include <cusp/opengl/spy/glut_2d_canvas.h>
26     #include <cusp/opengl/spy/matrix_data_panel.h>
27     #include <cusp/opengl/spy/matrix_data_cursor.h>
28    
29     namespace cusp
30     {
31     namespace opengl
32     {
33     namespace spy
34     {
35    
36     /**
37     * The matrix_canvas assumes that the matrix
38     * can be stored in memory. It loads a matrix_data_cursor and a
39     * matrix_data_panel to handle other details of the implementation.
40     */
41     template< typename IndexType, typename ValueType, typename MemorySpace >
42     class matrix_canvas
43     : public glut_2d_canvas
44     {
45     protected:
46     typedef glut_2d_canvas super;
47    
48     // large_scale_nz controls when we build the matrix _m_fast to draw
49     // quickly and then ``fill in'' later
50     const static int large_scale_nz = 524288;
51    
52     cusp::csr_matrix<IndexType, ValueType, MemorySpace> _m;
53    
54     std::vector<IndexType> irperm;
55     std::vector<IndexType> cperm;
56     std::vector<IndexType> icperm;
57     bool rperm_loaded;
58     bool cperm_loaded;
59    
60     std::vector<std::string> rlabel;
61     std::vector<std::string> clabel;
62     std::vector<ValueType> rnorm;
63     std::vector<ValueType> cnorm;
64    
65     struct {
66     ValueType min_val;
67     ValueType max_val;
68     IndexType max_degree;
69     IndexType min_degree;
70     } matrix_stats;
71    
72     std::string matrix_filename;
73     bool matrix_loaded;
74    
75     GLuint matrix_display_list;
76    
77     public:
78     matrix_canvas(int w, int h);
79    
80     void post_constructor();
81    
82     // glut functions
83     virtual void draw();
84     virtual void reshape(int w, int h);
85    
86     void motion(int w, int h);
87     void mouse_click(int button, int state, int x, int y);
88    
89     void menu(int value);
90    
91     virtual void special_key(int key, int x, int y);
92     virtual void key(unsigned char key, int x, int y);
93    
94     //
95     // control functions
96     //
97    
98     enum permutation_state_type {
99     no_permutation=0,
100     row_permutation=1,
101     column_permutation=2,
102     row_column_permutation=3
103     };
104    
105     enum normalization_state_type {
106     no_normalization=0,
107     row_normalization=1,
108     column_normalization=2,
109     row_column_normalization=3
110     };
111    
112     enum colormap_state_type {
113     first_colormap=1,
114     user_colormap=1,
115     rainbow_colormap=2,
116     bone_colormap=3,
117     spring_colormap=4,
118     last_colormap,
119     };
120    
121     void show_data_panel();
122     void hide_data_panel();
123    
124     void set_point_alpha(float a) { if (a >= 0. && a <= 1.) point_alpha = a; }
125     float get_point_alpha() { return (point_alpha); }
126    
127     float get_aspect() { return aspect; }
128     void set_aspect(float r) { if (r > 0) aspect = r; }
129    
130     void home();
131    
132     void set_permutation(permutation_state_type p) { permutation_state = p; }
133     void set_normalization(normalization_state_type n) { normalization_state = n; }
134    
135     colormap_state_type get_colormap();
136     void set_colormap(colormap_state_type c);
137     void set_next_colormap();
138    
139     void set_border_color(float r, float g, float b)
140     { border_color[0]=r; border_color[1]=g; border_color[2]=b; }
141    
142     // data loading
143     template< typename MatrixType >
144     bool load_matrix(const MatrixType& A);
145     bool load_permutations(const std::string& rperm_filename,
146     const std::string& cperm_filename);
147     bool load_labels(const std::string& rlabel_filename,
148     const std::string& clabel_filename);
149    
150     protected:
151     // matrix drawing
152     void draw_full_matrix();
153     void draw_partial_matrix(int r1, int c1, int r2, int c2);
154    
155     template <bool partial, class NRMap, class NCMap, class PRMap, class PCMap>
156     void draw_matrix(int r1, int c1, int r2, int c2,
157     ValueType min, ValueType inv_val_range, float alpha,
158     NRMap nrv, NCMap ncv, PRMap iprm, PCMap pcm);
159    
160     template <bool partial, class NRMap, class NCMap>
161     void draw_matrix_dispatch(int r1, int c1, int r2, int c2,
162     ValueType min, ValueType inv_val_range, float alpha,
163     NRMap nrv, NCMap ncv);
164    
165     template <bool partial>
166     void draw_matrix_dispatch(int r1, int c1, int r2, int c2);
167    
168     void write_svg();
169    
170     float alpha_from_zoom();
171    
172     // control variables
173     float point_alpha;
174    
175     bool data_panel_visible;
176     bool control_visible;
177    
178     permutation_state_type permutation_state;
179     normalization_state_type normalization_state;
180     colormap_state_type colormap_state;
181     bool colormap_invert;
182    
183     struct colormap_type {
184     float *map;
185     int size;
186    
187     colormap_type(float *m, int s) : map(m), size(s) {}
188     } colormap;
189    
190     float border_color[3];
191    
192     // internal functions
193     void init_window();
194     void init_display_list();
195     void init_menu();
196    
197     ValueType matrix_value(IndexType r, IndexType c);
198     const std::string& row_label(IndexType r);
199     const std::string& column_label(IndexType r);
200    
201     const std::string empty_label;
202    
203     const static int menu_file_id = 1;
204     const static int menu_exit_id = 2;
205     const static int menu_toggle_cursor_id = 3;
206    
207     const static int menu_aspect_normal = 4;
208     const static int menu_aspect_wide_12 = 5;
209     const static int menu_aspect_wide_14 = 6;
210     const static int menu_aspect_tall_21 = 7;
211     const static int menu_aspect_tall_41 = 8;
212    
213     const static int menu_colormap_rainbow = 1001;
214     const static int menu_colormap_bone = 1002;
215     const static int menu_colormap_spring = 1003;
216     const static int menu_colormap_invert = 1101;
217    
218     const static int menu_colors_white_bkg = 2001;
219     const static int menu_colors_black_bkg = 2002;
220    
221    
222     const static int panel_offset = 5;
223     const static int panel_height = 50;
224    
225     // workaround for stupid gcc bug
226     int p_offset;
227     int p_height;
228    
229     // put everything I want constructed last here
230     matrix_data_panel data_panel;
231     matrix_data_cursor data_cursor;
232     };
233    
234     } // end spy
235     } // end opengl
236     } // end cusp
237    
238     #include <cusp/opengl/spy/detail/matrix_canvas.inl>
239     #include <cusp/opengl/spy/detail/matrix_canvas_svg_output.inl>

  ViewVC Help
Powered by ViewVC 1.1.26