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