NCBI C++ ToolKit
histogram_graph.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: histogram_graph.cpp 47392 2023-03-06 19:00:01Z evgeniev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 #include <gui/opengl/glhelpers.hpp>
36 #include <gui/opengl/irender.hpp>
37 
38 
40 
41 
43 : m_MinColor(1.0f, 1.0f, 1.0f),
44  m_MaxColor(0.0f, 0.0f, 0.0f),
45  m_Margin(1)
46 {
47 }
48 
49 
51 : m_Horz(b_horz)
52 {
53  m_ModelRect.Init(0.0, 0.0, 1.0, 1.0);
54 
57 }
58 
59 
61 {
62 }
63 
65 {
66  m_DataSource = ds;
67 }
68 
69 
71 {
72  return m_DataSource.GetPointer();
73 }
74 
75 
77 {
78  return m_DataSource.GetPointer();
79 }
80 
81 
83 {
84  //LOG_POST(" CHistogramGraph::Render()");
85  //LOG_POST(" Viewport " << m_VPRect.ToString() << "\n Visible " << m_ModelRect.ToString());
86  IRender& gl = GetGl();
87 
88  pane.OpenPixels();
89  //glColor3d(0.2, 0.2, 0.2);
91  gl.ColorC(color);
92 
93  gl.PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
94  gl.RectC(m_VPRect);
95  pane.Close();
96 
97  const static double kMaxRatio = 4.0;
98  const static double kRatio = 2.0;
99 
100  // trivial implementation
101  if(m_DataSource) {
102  const TModelRect& rc_vis = pane.GetVisibleRect();
103  TModelUnit start = m_DataSource->GetStart();
104  TModelUnit stop = m_DataSource->GetStop();
105 
106  // check if we need to update the datasource
107  TModelUnit vis_start = m_Horz ? rc_vis.Left() : rc_vis.Bottom();
108  TModelUnit vis_stop = m_Horz ? rc_vis.Right() : rc_vis.Top();
109  bool outside = vis_start < start || vis_stop > (stop + 1);
110 
111  TModelUnit ratio = (vis_stop - vis_start) / (stop - start + 1);
112  bool bad_scale = ratio > kMaxRatio || ratio < (1 / kMaxRatio);
113 
114  if(outside || bad_scale) {
115  TModelUnit new_start, new_stop;
116  TModelUnit center = (vis_start + vis_stop) / 2;
117  TModelUnit range = (vis_stop - vis_start) * kRatio;
118 
119  double max_pos = m_DataSource->GetLimit();
120  range = min(range, max_pos);
121 
122  new_start = center - range / 2;
123  new_stop = new_start + range;
124  if(new_start < 0) {
125  new_start = 0;
126  new_stop = new_start + range;
127  } else if(new_stop > max_pos) {
128  new_stop = max_pos;
129  new_start = new_stop - range;
130  }
131 
132  m_DataSource->Update(new_start, new_stop);
133  }
134 
135  x_RenderHistogram(pane);
136  }
137 }
138 
139 
141 {
142  pane.OpenOrtho();
143 
144  CGlAttrGuard guard(GL_LINE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT );
145  IRender& gl = GetGl();
146 
147  gl.Enable(GL_BLEND);
148  gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
149  gl.PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
150 
151  TModelUnit start = m_DataSource->GetStart();
152  TModelUnit z1, z2;
153 
154  TModelUnit offset_x = pane.GetOffsetX();
155  TModelUnit offset_y = pane.GetOffsetY();
156 
157  if(m_Horz) {
158  z1 = m_ModelRect.Bottom() - offset_y;
159  z2 = m_ModelRect.Top() - offset_y;
160  } else {
161  z1 = m_ModelRect.Left() - offset_x;
162  z2 = m_ModelRect.Right() - offset_x;
163  }
164  z1 += m_Properties.m_Margin;
165  z2 -= m_Properties.m_Margin;
166 
167  TModelUnit max_v = m_DataSource->GetMaxValue();
168  TModelUnit d = m_DataSource->GetStep();
169 
170  auto count = m_DataSource->GetCount();
171  for( auto i = 0; i < count; i++ ) {
172  double value = m_DataSource->GetValue(i);
173  double norm = (max_v > 0.00001) ? (value / max_v) : 0;
174  int index = m_ColorTable.GetIndex(norm);
175  const CRgbaColor& color = m_ColorTable.GetColor(index);
176  gl.ColorC(color);
177 
178  double pos = start + i * d;
179  if(m_Horz) {
180  TModelUnit x1 = pos - offset_x;
181  gl.Rectd(x1, z1, x1 + d, z2);
182  } else {
183  TModelUnit y1 = pos - offset_y;
184  gl.Rectd(z1, y1, z2, y1 + d);
185  }
186  }
187  gl.Disable(GL_LINE_SMOOTH);
188  gl.Disable(GL_BLEND);
189 
190  pane.Close();
191 }
192 
193 
195 {
196  static const int kGraphSize = 12;
197  return TVPPoint(kGraphSize, kGraphSize);
198 }
199 
200 
201 bool CHistogramGraph::NeedTooltip(CGlPane& /*pane*/, int vp_x, int vp_y)
202 {
203  return m_VPRect.PtInRect(vp_x, vp_y);
204 }
205 
206 
208 {
209  string s = "Features : " + m_DataSource->GetLabel();
210  return s;
211 }
212 
213 
215 {
216  return m_Properties;
217 }
218 
219 
221 {
222  m_Properties = props;
224 }
225 
226 
CGlAttrGuard - guard class for restoring OpenGL attributes.
Definition: glutils.hpp:130
class CGlPane
Definition: glpane.hpp:62
virtual ~CHistogramGraph()
CHistogramGraph(bool b_horz)
virtual bool NeedTooltip(CGlPane &pane, int vp_x, int vp_y)
void SetProperties(const SProperties &props)
virtual IHistogramGraphDS * GetDataSource()
CIRef< IHistogramGraphDS > m_DataSource
virtual string GetTooltip()
CRgbaGradColorTable m_ColorTable
virtual TVPPoint PreferredSize()
virtual void x_RenderHistogram(CGlPane &pane)
virtual void SetDataSource(IHistogramGraphDS *ds)
SProperties m_Properties
virtual void Render(CGlPane &pane)
const SProperties & GetProperties() const
TModelRect m_ModelRect
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
IHistogramGraphDS interface.
char value[7]
Definition: config.c:431
GLdouble TModelUnit
Definition: gltypes.hpp:48
virtual void Enable(GLenum glstate)=0
bool OpenOrtho()
Definition: glpane.hpp:427
void Init()
Definition: glrect.hpp:62
CGlPoint< TVPUnit > TVPPoint
Definition: gltypes.hpp:50
T Top() const
Definition: glrect.hpp:84
virtual void BlendFunc(GLenum sfactor, GLenum dfactor)=0
Options to be used when GL_BLEND is enabled.
T Bottom() const
Definition: glrect.hpp:82
bool OpenPixels()
Definition: glpane.hpp:432
IRender & GetGl()
convenience function for getting current render manager
void RectC(const TVPRect &rc)
Definition: irender.hpp:197
T Right() const
Definition: glrect.hpp:83
TModelUnit GetOffsetY() const
Definition: glpane.hpp:415
void SetSize(size_t size)
void FillGradient(const CRgbaColor &start_c, const CRgbaColor &end_c)
initialize the whole table with gradient colors in [start_c, end_c] range
T Left() const
Definition: glrect.hpp:81
bool PtInRect(T x, T y) const
Definition: glrect.hpp:154
void Close(void)
Definition: glpane.cpp:178
virtual void PolygonMode(GLenum face, GLenum mode)=0
Set the polygon rasterization mode.
int GetIndex(double norm) const
returns color index by the normalized value; normalized values is a value in the [0....
TModelRect & GetVisibleRect(void)
Definition: glpane.hpp:357
virtual void Disable(GLenum glstate)=0
glDisable()
TModelUnit GetOffsetX() const
Definition: glpane.hpp:410
virtual void ColorC(const CRgbaColor &c)=0
Set current color (glColor{3,4}{f,d}{v,})
void Rectd(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
Definition: irender.hpp:193
CRgbaColor & GetColor(size_t i)
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
n background color
int i
range(_Ty, _Ty) -> range< _Ty >
T min(T x_, T y_)
Modified on Sat Dec 02 09:20:38 2023 by modify_doxy.py rev. 669887