define window for showing a mussa alignment
[mussa.git] / alg / glseqbrowser.hpp
1 #ifndef _GLTRACKS_H_
2 #define _GLTRACKS_H_
3
4 #include <map>
5 #include <set>
6 #include <vector>
7
8 #include "alg/annotation_colors.hpp"
9 #include "alg/sequence.hpp"
10 #include "alg/glsequence.hpp"
11
12 //! Manage rendering a collection of glSequences
13 class GlSeqBrowser
14 {
15 public:
16   GlSeqBrowser();
17   GlSeqBrowser(const GlSeqBrowser&);
18
19   //! setup the opengl canvas
20   void initializeGL();
21   //! called when our screen canvas is resized
22   void resizeGL(int width, int height);
23   //! render our scene
24   void paintGL() const;
25
26   //! select a region (using canvas coordinates)
27   void GlSeqBrowser::selectRegion(int top, int left, int bottom, int right);
28
29   //! max world left coordinate
30   float left() const;
31   //! max world right coordinate
32   float right() const;
33
34   void setViewportCenter(float x);
35   float viewportLeft() const;
36   float viewportCenter() const;
37   float viewportRight() const;
38   float viewportHeight() const;
39   float viewportWidth() const;
40
41   void setZoom(int zoom_level);
42   int zoom() const;
43
44   void setColorMapper(AnnotationColors& cm);
45   AnnotationColors& colorMapper();
46
47   //! clear our tracks and connections
48   void clear();
49
50   //! add a sequence to the back of our track container
51   void push_sequence(const Sequence &s);
52   //! add a glsequence to the back of our track container
53   void push_sequence(GlSequence &s);
54   //! return our track container
55   const std::vector<GlSequence>& sequences() const;
56
57   //! clear all the line segments between all the sequences
58   void clear_links();
59   //! define a path
60   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
61   //! returns the index of pathids based on order added by link
62   const std::set<int>& selectedPaths() const;
63
64   //! Provide a logical name for a type discriminator for our glName stack
65   enum FeatureType { MussaTrack, MussaSegment };
66
67   //! a useful point class
68   template<class T> struct point {
69     T x;
70     T y;
71
72     point(T x_, T y_):x(x_), y(y_) {}
73   };
74
75   //! a useful rectangle, where 0,0 is in the lower left
76   template<class T> struct rect {
77     T top;
78     T left;
79     T bottom;
80     T right;
81
82     rect() {}
83     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
84     T width() { return right - left; }
85     T height() { return top - bottom; }
86   };
87
88   struct Segment
89   {
90     point<float> start;
91     point<float> end;
92     bool reversed;
93     std::set<int> path_ids;
94
95     Segment() : start(0.0, 0.0), end(0.0, 0.0) {}
96     Segment(float x1, float y1, float x2, float y2, bool isRC) 
97       : start(x1, y1), end(x2, y2), reversed(isRC) {}
98   };
99
100   //! data structure holding our line segments
101   /*! the vector is of size track_container.size()-1
102    *  it's indexed by the pair x1, x2 (the two x coordinates between
103    *  the two tracks
104    */
105   typedef std::pair<int, int> segment_key;
106   typedef std::map<segment_key, Segment> pair_segment_map;
107   typedef std::vector<pair_segment_map> path_segment_map_vector;
108   path_segment_map_vector path_segments;
109 private:
110   //! recalculate the viewable world
111   /*! depending on the size of our canvas, our zoom level and
112    *  how far we've been offset
113    */
114   void update_viewport(float center, int new_zoom);
115
116   //! determine where all the tracks should be placed
117   void update_layout();
118
119   //! convert opengl selections into the list of paths we should highlight
120   void processSelection(GLuint hits, GLuint buffer[], GLuint bufsize);
121
122   //! master scene drawing function
123   /*! draw is broken out for the opengl selection code
124    */
125   void draw() const;
126   //! draw glsequence tracks
127   void draw_tracks() const;
128   //! draw line segments between tracks
129   void draw_segments() const;
130   //! draw selection box
131   void draw_selection() const;
132
133   //! number of pixels to reserve around the edges of our canvas
134   const int border;
135   //! the maximum dimensions that our scene is taking up (world coord)
136   rect<float> max_ortho;
137   //! the current viewable region (world coord)
138   rect<float> cur_ortho;
139   //! how many pixels our viewport is (screen coord)
140   point<int> viewport_size;
141   //! the center of our current viewport (world coord) (used for scrollbar)
142   float viewport_center;
143   int zoom_level;
144   AnnotationColors color_mapper;
145   //! container of all the GlSequences loaded into our scene
146   std::vector<GlSequence> track_container;
147   //! counter for each path added to us via connect
148   int pathid;
149
150 protected:
151   //! where to draw our box 
152   rect<float> selectedRegion;
153   //! true if we have a selection
154   bool selectedMode;
155   //! indicate which paths are selected
156   std::set<int> selected_paths;
157   //! which track is selected (it only makes sense to have one track selected).
158   std::set<int> selected_tracks;
159
160 };
161 #endif