implement zoom in terms of base pairs per pixel
[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   //! border size
30   float border() const;
31
32   //! max world left coordinate
33   float left() const;
34   //! max world right coordinate
35   float right() const;
36
37   void setViewportCenter(float x);
38   float viewportLeft() const;
39   float viewportCenter() const;
40   float viewportRight() const;
41   float viewportHeight() const;
42   float viewportWidth() const;
43
44   //! zoom out far enough to show the full size of the sequence
45   int zoomOut();
46   //! set the current zoom level in (base pairs / pix ) * 100
47   /*! a zoomlevel of 1 is .01 base pairs per pixel or a rather exploded
48    *  100 pixels per base pair
49    */
50   void setZoom(int zoom_level);
51   //! returns the current zoom level
52   int zoom() const;
53
54   void setColorMapper(AnnotationColors& cm);
55   AnnotationColors& colorMapper();
56
57   //! clear our tracks and connections
58   void clear();
59
60   //! add a sequence to the back of our track container
61   void push_sequence(const Sequence &s);
62   //! add a glsequence to the back of our track container
63   void push_sequence(GlSequence &s);
64   //! return our track container
65   const std::vector<GlSequence>& sequences() const;
66
67   //! clear all the line segments between all the sequences
68   void clear_links();
69   //! define a path
70   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
71   //! returns the index of pathids based on order added by link
72   const std::set<int>& selectedPaths() const;
73   //! center the provided path in the current viewport
74   void centerOnPath(const std::vector<int>&);
75
76   //! Provide a logical name for a type discriminator for our glName stack
77   enum FeatureType { MussaTrack, MussaSegment };
78
79   //! a useful point class
80   template<class T> struct point {
81     T x;
82     T y;
83
84     point(T x_, T y_):x(x_), y(y_) {}
85   };
86
87   //! a useful rectangle, where 0,0 is in the lower left
88   template<class T> struct rect {
89     T top;
90     T left;
91     T bottom;
92     T right;
93
94     rect() {}
95     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
96     T width() { return right - left; }
97     T height() { return top - bottom; }
98   };
99
100   struct Segment
101   {
102     point<float> start;
103     point<float> end;
104     bool reversed;
105     std::set<int> path_ids;
106
107     Segment() : start(0.0, 0.0), end(0.0, 0.0) {}
108     Segment(float x1, float y1, float x2, float y2, bool isRC) 
109       : start(x1, y1), end(x2, y2), reversed(isRC) {}
110   };
111
112   //! data structure holding our line segments
113   /*! the vector is of size track_container.size()-1
114    *  it's indexed by the pair x1, x2 (the two x coordinates between
115    *  the two tracks
116    */
117   typedef std::pair<int, int> segment_key;
118   typedef std::map<segment_key, Segment> pair_segment_map;
119   typedef std::vector<pair_segment_map> path_segment_map_vector;
120   path_segment_map_vector path_segments;
121 private:
122   //! recalculate the viewable world
123   /*! depending on the size of our canvas, our zoom level and
124    *  how far we've been offset
125    */
126   void update_viewport(float center, int new_zoom);
127
128   //! determine where all the tracks should be placed
129   void update_layout();
130
131   //! convert opengl selections into the list of paths we should highlight
132   void processSelection(GLuint hits, GLuint buffer[], GLuint bufsize);
133
134   //! master scene drawing function
135   /*! draw is broken out for the opengl selection code
136    */
137   void draw() const;
138   //! draw glsequence tracks
139   void draw_tracks() const;
140   //! draw line segments between tracks
141   void draw_segments() const;
142   //! draw selection box
143   void draw_selection() const;
144
145   //! number of pixels to reserve around the edges of our canvas
146   const int border_width;
147   //! the current viewable region (world coord)
148   rect<float> cur_ortho;
149   //! how many pixels our viewport is (screen coord)
150   point<int> viewport_size;
151   //! the center of our current viewport (world coord) (used for scrollbar)
152   float viewport_center;
153   int zoom_level;
154   AnnotationColors color_mapper;
155   //! container of all the GlSequences loaded into our scene
156   std::vector<GlSequence> track_container;
157   //! counter for each path added to us via connect
158   int pathid;
159
160 protected:
161   //! where to draw our box 
162   rect<float> selectedRegion;
163   //! true if we have a selection
164   bool selectedMode;
165   //! indicate which paths are selected
166   std::set<int> selected_paths;
167   //! which track is selected (it only makes sense to have one track selected).
168   std::set<int> selected_tracks;
169
170 };
171 #endif