NCBI C++ ToolKit
rasterizer.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OPENGL___RASTERIZER__HPP
2 #define GUI_OPENGL___RASTERIZER__HPP
3 
4 /* $Id: rasterizer.hpp 44954 2020-04-27 17:57:36Z evgeniev $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 
38 #include <gui/gui.hpp>
39 #include <gui/opengl/glpane.hpp>
40 #include <math.h>
41 
42 /** @addtogroup GUI_OPENGL
43  *
44  * @{
45  */
46 
48 
49 
50 /////////////////////////////////////////////////////////////////////////////
51 // CRasterizer
52 template<class A> class CRasterizer
53 {
54 public:
55  typedef vector<A> TRaster;
56 
58  : m_VPMin(vp_min), m_VPMax(vp_max),
59  m_Min(min), m_Max(max)
60  {
61  TVPUnit vp_len = vp_max - vp_min;
62  _ASSERT(vp_len >= 0);
63  if (vp_len <= 0) vp_len = 1;
64  m_Raster.resize(vp_len + 1);
65  m_Scale = (max - min) / vp_len;
66  }
67 
68  template <class F> void AddInterval(TModelUnit min, TModelUnit max, const A& attr, F f_add)
69  {
70  TModelUnit pix_from = (min - m_Min) / m_Scale;
71  TModelUnit pix_to_open = (max - m_Min) / m_Scale;
72 
73  int vp_len = (int)(m_Raster.size() - 1);
74  bool intersect = ! (pix_to_open <= 0 || pix_from >= vp_len);
75 
76  if(intersect) {
77  // clip by raster index extent
78  pix_from = std::max(TModelUnit(0), pix_from);
79  pix_to_open = std::min(TModelUnit(vp_len), pix_to_open);
80 
81  TModelUnit pix_left = floor(pix_from);
82  TModelUnit pix_right = std::max(TModelUnit(0), ceil(pix_to_open) - 1);
83  if (pix_to_open - pix_from < 1.0 && pix_left != pix_right) {
84  pix_right = pix_left;
85  }
86  int left_index = (int) pix_left;
87  int right_index = (int) pix_right;
88 
89  if(left_index == right_index) {
90  // everything fits withing a single pixel
91  _ASSERT(left_index < (int) m_Raster.size());
92  float fraction = float(pix_to_open - pix_from);
93  f_add(m_Raster[left_index], attr, fraction, false);
94  } else {
95  if(pix_left < pix_from) {
96  _ASSERT(left_index < (int) m_Raster.size());
97  float fraction = float(pix_left + 1 - pix_from);
98  f_add(m_Raster[left_index], attr, fraction, true);
99  left_index++;
100  }
101  if(pix_right < pix_to_open) {
102  _ASSERT(right_index < (int) m_Raster.size());
103  float fraction = float(pix_to_open - pix_right);
104  f_add(m_Raster[right_index], attr, fraction, true);
105  right_index--;
106  }
107  for(int i = left_index; i <= right_index; i++ ) {
108  _ASSERT(i < (int) m_Raster.size());
109  f_add(m_Raster[i], attr, 1.0, true);
110  }
111  }
112  }
113  }
114 
116  {
117  return (pos - m_Min) / m_Scale;
118  }
119 
120  const TRaster& GetRaster() const { return m_Raster; }
121 
122 protected:
128 
130 };
131 
132 
134 
135 /* @} */
136 
137 #endif // GUI_OPENGL___RASTERIZER__HPP
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define A(i)
Definition: ecp_curves.c:948
TModelUnit m_Max
Definition: rasterizer.hpp:126
GLdouble TModelUnit
Definition: gltypes.hpp:48
TModelUnit m_Min
Definition: rasterizer.hpp:125
const TRaster & GetRaster() const
Definition: rasterizer.hpp:120
TVPUnit m_VPMax
Definition: rasterizer.hpp:124
TVPUnit m_VPMin
Definition: rasterizer.hpp:123
TRaster m_Raster
Definition: rasterizer.hpp:129
TModelUnit Project(TModelUnit pos) const
Definition: rasterizer.hpp:115
void AddInterval(TModelUnit min, TModelUnit max, const A &attr, F f_add)
Definition: rasterizer.hpp:68
CRasterizer(TVPUnit vp_min, TVPUnit vp_max, TModelUnit min, TModelUnit max)
Definition: rasterizer.hpp:57
int TVPUnit
Definition: gltypes.hpp:47
vector< A > TRaster
Definition: rasterizer.hpp:55
TModelUnit m_Scale
Definition: rasterizer.hpp:127
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
#define F(x)
Make a parametrized function appear to have only one variable.
Definition: ncbi_math.c:342
T max(T x_, T y_)
T min(T x_, T y_)
#define _ASSERT
Modified on Sat Apr 13 11:49:15 2024 by modify_doxy.py rev. 669887