use lines if a quad would be smaller than a pixel
[mussa.git] / alg / glseqbrowser.hpp
1 #ifndef _GLTRACKS_H_
2 #define _GLTRACKS_H_
3 #include <boost/shared_ptr.hpp>
4
5 #include <map>
6 #include <set>
7 #include <vector>
8
9 #include "alg/annotation_colors.hpp"
10 #include "alg/sequence.hpp"
11 #include "alg/glsequence.hpp"
12 #include "alg/sequence_location.hpp"
13 #include "alg/track_region.hpp"
14
15 //! Manage rendering a collection of glSequences
16 class GlSeqBrowser
17 {
18 public:
19   GlSeqBrowser();
20   GlSeqBrowser(const GlSeqBrowser&);
21
22   //! setup the opengl canvas
23   void initializeGL();
24   //! called when our screen canvas is resized
25   void resizeGL(int width, int height);
26   //! render our scene
27   void paintGL() const;
28
29   //! select a region (using canvas coordinates)
30   void selectRegion(int top, int left, int bottom, int right);
31
32   //! border size
33   float border() const;
34
35   //! max world left coordinate
36   float left() const;
37   //! max world right coordinate
38   float right() const;
39   // return how wide is a pixel in world coordinates?
40   float get_pixel_width() const;
41
42   void setViewportCenter(float x);
43   //! return world coordinate of the left side of the viewport
44   float viewportLeft() const;
45   //! return world coordinate of the center of the viewport
46   float viewportCenter() const;
47   //! return world coordinate of the right side of the viewport
48   float viewportRight() const;
49   //! return height of the viewport in world coordinates
50   float viewportHeight() const;
51   //! return width of the viewport in world coordinates
52   float viewportWidth() const;
53
54   //! zoom out far enough to show the full size of the sequence
55   double zoomOut();
56   //! zoom in to a reasonable sequence view
57   double zoomToSequence();
58   //! set the current zoom level in (base pairs / pix )
59   //! its a double as zoom levels of [0.01, 1.0] make nice sequence views
60   void setZoom(double zoom_level);
61   //! returns the current zoom level as basepairs per pixel
62   double zoom() const;
63   //! center the provided path in the current viewport
64   void centerOnPath(const std::vector<int>&);
65
66   void setColorMapper(boost::shared_ptr<AnnotationColors> cm);
67   const AnnotationColors& colorMapper();
68
69   //! clear our tracks and connections
70   void clear();
71   //! clear everything related to a selection
72   void clear_selection();
73
74   //! add a sequence to the back of our track container (makes copy of s)
75   void push_sequence(const Sequence& s);
76   //! add a sequence to the back of our track container
77   void push_sequence(boost::shared_ptr<Sequence> s);
78   //! add a glsequence to the back of our track container
79   void push_sequence(GlSequence s);
80   //! add a glsequence to the back of our track container
81   void push_sequence(boost::shared_ptr<GlSequence> gs);
82   //! return our track container
83   const std::vector<boost::shared_ptr<GlSequence> >& sequences() const;
84
85   //! clear all the line segments between all the sequences
86   void clear_links();
87   //! define a path
88   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
89   //! set selected paths (it'd be nice if this could be a templated function)
90   void setSelectedPaths(std::vector<int> c);
91   //! returns the index of pathids based on order added by link
92   const std::set<int>& selectedPaths() const;
93
94   //! set selected tracks (it'd be nice if this could be a templated function)
95   void appendSelectedTrack(GLuint track_index, int left, int right);
96
97   //! return list of selected tracks
98   std::list<TrackRegion> selectedTracks() const;
99
100   //! copy sequence from selected track using formating function
101   template<class Item>
102   void copySelectedTracks(std::list<Item>& result, 
103              Item (*format_track)(boost::shared_ptr<Sequence> s,
104                                   int left, 
105                                   int right));
106   //! copy sequence from selected tracks as FASTA sequences
107   void copySelectedTracksAsFasta(std::string& copy_buffer);
108   //! copy sequence from selected tracks as a list of sequences
109   void copySelectedTracksAsSequences(std::list<Sequence>& result);
110   //! copy sequence from selected tracks as plain sequences
111   void copySelectedTracksAsString(std::string& copy_buffer);
112
113   //! copy tracks as a sequence and its coordinates
114   void copySelectedTracksAsSeqLocation(std::list<SequenceLocation>& result);
115   
116   
117   //! Provide a logical name for a type discriminator for our glName stack
118   enum FeatureType { MussaTrack, MussaSegment };
119
120   //! a useful point class
121   template<class T> struct point {
122     T x;
123     T y;
124
125     point(T x_, T y_):x(x_), y(y_) {}
126   };
127
128   //! a useful rectangle, where 0,0 is in the lower left
129   template<class T> struct rect {
130     T top;
131     T left;
132     T bottom;
133     T right;
134
135     rect() {}
136     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
137     T width() { return right - left; }
138     T height() { return top - bottom; }
139     void clear() { top=0; left=0; bottom=0; right=0; }
140   };
141
142   struct Segment
143   {
144     point<float> start;
145     point<float> end;
146     bool reversed;
147     int length;
148     // each integer represents an index into our list of paths
149     std::set<int> path_ids;
150
151     Segment() : start(0.0, 0.0), end(0.0, 0.0), reversed(false), length(0) {}
152     Segment(float x1, float y1, float x2, float y2, bool isRC, int length_) 
153       : start(x1, y1), end(x2, y2), reversed(isRC), length(length_) {}
154   };
155
156   //! data structure holding our line segments
157   /*! the vector is of size track_container.size()-1
158    *  it's indexed by the pair x1, x2 (the two x coordinates between
159    *  the two tracks
160    */
161   typedef std::pair<int, int> segment_key;  
162   typedef std::map<segment_key, Segment> pair_segment_map;
163   typedef std::vector<pair_segment_map> path_segment_map_vector;
164   path_segment_map_vector path_segments;
165
166 private:
167   //! recalculate the viewable world
168   /*! depending on the size of our canvas, our zoom level and
169    *  how far we've been offset
170    */
171   void update_viewport(float center, double new_zoom);
172
173   //! determine where all the tracks should be placed
174   void update_layout();
175
176   //! convert opengl selections into the list of paths we should highlight
177   void processSelection(GLuint hits, 
178                         GLuint buffer[], 
179                         GLuint bufsize, 
180                         const rect<float>& r);
181
182   //! master scene drawing function
183   /*! draw is broken out for the opengl selection code
184    */
185   void draw() const;
186   //! draw glsequence tracks
187   void draw_tracks() const;
188   //! draw line segments between tracks
189   void draw_segments() const;
190   //! draw selection box
191   void draw_selection() const;
192
193   //! number of pixels to reserve around the edges of our canvas
194   const int border_width;
195   //! the current viewable region (world coord)
196   rect<float> cur_ortho;
197   //! how many pixels our viewport is (screen coord)
198   point<int> viewport_size;
199   //! the center of our current viewport (world coord) (used for scrollbar)
200   float viewport_center;
201   double zoom_level;
202   boost::shared_ptr<AnnotationColors> color_mapper;
203   //! container of all the GlSequences loaded into our scene
204   std::vector<boost::shared_ptr<GlSequence> > track_container;
205   //! counter for each path added to us via connect
206   int pathid;
207
208 protected:
209   //! where to draw our box (world coordinates)
210   rect<float> selectedRegion;
211   //! true if we have a selection
212   bool selectedMode;
213   //! indicate which paths are selected
214   std::set<int> selected_paths;
215   //! which track is selected (it only makes sense to have one track selected).
216   std::list<TrackRegion> selected_tracks;
217   typedef std::list<TrackRegion>::iterator selected_track_iterator;
218 };
219 #endif