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