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