ticket:104 fix some pointer problems with MussaAlignedWindow
[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   void setZoom(double zoom_level);
56   //! returns the current zoom level as basepairs per pixel
57   double zoom() const;
58   //! center the provided path in the current viewport
59   void centerOnPath(const std::vector<int>&);
60
61   void setColorMapper(AnnotationColors& cm);
62   AnnotationColors& colorMapper();
63
64   //! clear our tracks and connections
65   void clear();
66   //! clear everything related to a selection
67   void clear_selection();
68
69   //! add a sequence to the back of our track container
70   void push_sequence(const Sequence &s);
71   //! add a glsequence to the back of our track container
72   void push_sequence(GlSequence &s);
73   //! return our track container
74   const std::vector<GlSequence>& sequences() const;
75
76   //! clear all the line segments between all the sequences
77   void clear_links();
78   //! define a path
79   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
80   //! returns the index of pathids based on order added by link
81   const std::set<int>& selectedPaths() const;
82   //! define our function for formating sequence copy
83   typedef std::string format_track(const Sequence& s, int left, int right);
84   //! copy sequence from selected track using formating function
85   void copySelectedTracks(std::string& copy_buffer, format_track func);
86   //! copy sequence from selected tracks as plain sequences
87   void copySelectedTracksAsString(std::string& copy_buffer);
88   //! copy sequence from selected tracks as FASTA sequences
89   void copySelectedTracksAsFasta(std::string& copy_buffer);
90   
91   
92   //! Provide a logical name for a type discriminator for our glName stack
93   enum FeatureType { MussaTrack, MussaSegment };
94
95   //! a useful point class
96   template<class T> struct point {
97     T x;
98     T y;
99
100     point(T x_, T y_):x(x_), y(y_) {}
101   };
102
103   //! a useful rectangle, where 0,0 is in the lower left
104   template<class T> struct rect {
105     T top;
106     T left;
107     T bottom;
108     T right;
109
110     rect() {}
111     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
112     T width() { return right - left; }
113     T height() { return top - bottom; }
114     void clear() { top=0; left=0; bottom=0; right=0; }
115   };
116
117   struct Segment
118   {
119     point<float> start;
120     point<float> end;
121     bool reversed;
122     std::set<int> path_ids;
123
124     Segment() : start(0.0, 0.0), end(0.0, 0.0) {}
125     Segment(float x1, float y1, float x2, float y2, bool isRC) 
126       : start(x1, y1), end(x2, y2), reversed(isRC) {}
127   };
128
129   //! data structure holding our line segments
130   /*! the vector is of size track_container.size()-1
131    *  it's indexed by the pair x1, x2 (the two x coordinates between
132    *  the two tracks
133    */
134   typedef std::pair<int, int> segment_key;
135   typedef std::map<segment_key, Segment> pair_segment_map;
136   typedef std::vector<pair_segment_map> path_segment_map_vector;
137   path_segment_map_vector path_segments;
138
139   struct TrackRegion
140   {
141     GLuint track_id;
142     int left;
143     int right;
144
145     TrackRegion():track_id(0), left(0), right(0) {};
146     TrackRegion(const TrackRegion& o)
147       : track_id(o.track_id), left(o.left), right(o.right) {}
148     TrackRegion(GLuint id, int l, int r)
149       : track_id(id), left(l), right(r) {}
150     void set(GLuint id, int l, int r) { track_id = id; left=l; right=r; };
151   };
152
153 private:
154   //! recalculate the viewable world
155   /*! depending on the size of our canvas, our zoom level and
156    *  how far we've been offset
157    */
158   void update_viewport(float center, double new_zoom);
159
160   //! determine where all the tracks should be placed
161   void update_layout();
162
163   //! convert opengl selections into the list of paths we should highlight
164   void processSelection(GLuint hits, 
165                         GLuint buffer[], 
166                         GLuint bufsize, 
167                         const rect<float>& r);
168
169   //! master scene drawing function
170   /*! draw is broken out for the opengl selection code
171    */
172   void draw() const;
173   //! draw glsequence tracks
174   void draw_tracks() const;
175   //! draw line segments between tracks
176   void draw_segments() const;
177   //! draw selection box
178   void draw_selection() const;
179
180   //! number of pixels to reserve around the edges of our canvas
181   const int border_width;
182   //! the current viewable region (world coord)
183   rect<float> cur_ortho;
184   //! how many pixels our viewport is (screen coord)
185   point<int> viewport_size;
186   //! the center of our current viewport (world coord) (used for scrollbar)
187   float viewport_center;
188   double zoom_level;
189   AnnotationColors color_mapper;
190   //! container of all the GlSequences loaded into our scene
191   std::vector<GlSequence> track_container;
192   //! counter for each path added to us via connect
193   int pathid;
194
195 protected:
196   //! where to draw our box (world coordinates)
197   rect<float> selectedRegion;
198   //! true if we have a selection
199   bool selectedMode;
200   //! indicate which paths are selected
201   std::set<int> selected_paths;
202   //! which track is selected (it only makes sense to have one track selected).
203   std::list<TrackRegion> selected_tracks;
204   typedef std::list<TrackRegion>::iterator selected_track_iterator;
205 };
206 #endif