Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DataPlot.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "DataPlot.hh"
26 #include <Base/Debug/Exception.hh>
27 #include <Base/ImageUtils/ImageDraw.hh>
28 #include <limits>
29 
30 using namespace BIAS;
31 using namespace std;
33 {
34  PlotArea_ = &img;
35  PlotAreaWidth_ = PlotArea_->GetWidth();
36  PlotAreaHeight_ = PlotArea_->GetHeight();
37  BorderX_ = 2;
38  BorderY_ = 2;
39  AutoRangeX_ = true;
40  AutoRangeY_ = true;
41  ChannelCount_ = 3;
42  if (img.GetChannelCount() != ChannelCount_) BEXCEPTION("");
43  DrawIda_ = PlotArea_->GetImageDataArray();
44  BGColour_ = ColourRGB<unsigned char>(0,0,0);
45  PenColour_ = ColourRGB<unsigned char>(255,255,0);
46  DrawStyle_ = Points;
47  Thickness_ = 1;
48 }
49 
51 {
52 }
53 
55 {
56 DrawStyle_ = style;
57 LastVX_=-1;
58 LastVY_=-1;
59 
60 }
61 
62 
63 void DataPlot::SetXRange(float min,float max)
64 {
65  AutoRangeX_ = false;
66  MaxX_ = max;
67  MinX_ = min;
68 }
69 
70 void DataPlot::SetYRange(float min,float max)
71 {
72  AutoRangeY_ = false;
73  MaxY_ = max;
74  MinY_ = min;
75 }
76 
77 void DataPlot::SetAutoRange(bool x,bool y)
78 {
79  AutoRangeX_ = x;
80  AutoRangeY_ = y;
81 }
82 
84 {
85  unsigned int vx1,vy,vx2;
86  WindowToViewport_(MinX_,0,vx1,vy);
87  WindowToViewport_(MaxX_,0,vx2,vy);
88  DrawLine_(vx1,vy,vx2,vy);
89 
90 }
91 
92 
93 
95 {
96  unsigned int vx,vy1,vy2;
97  WindowToViewport_(0,MinY_,vx,vy1);
98  WindowToViewport_(0,MaxY_,vx,vy2);
99  DrawLine_(vx,vy1,vx,vy2);
100 
101 }
102 
103 void DataPlot::Plot(const std::vector<float> &data)
104 {
105  if (AutoRangeX_) {
106  MinX_ = 0.0f;
107  MaxX_ = float(data.size()-1);
108  }
109  if (AutoRangeY_) {
110  MinY_ = numeric_limits<float>::max();
111  MaxY_ = numeric_limits<float>::min();
112  for (unsigned int i=0;i<data.size();i++){
113  if (data[i]>MaxY_) MaxY_ = data[i];
114  if (data[i]<MinY_) MinY_ = data[i];
115  }
116  }
117  for (unsigned int i=0;i<data.size();i++)
118  Plot(float(i),data[i]);
119 }
120 
121 void DataPlot::Plot(const std::vector<float> &x,const std::vector<float> &y)
122 {
123  if (AutoRangeX_) {
124  MinX_ = numeric_limits<float>::max();
125  MaxX_ = numeric_limits<float>::min();
126  for (unsigned int i=0;i<x.size();i++){
127  if (x[i]>MaxX_) MaxX_ = x[i];
128  if (x[i]<MinX_) MinX_ = x[i];
129  }
130  }
131  if (AutoRangeY_) {
132  MinY_ = numeric_limits<float>::max();
133  MaxY_ = numeric_limits<float>::min();
134  for (unsigned int i=0;i<y.size();i++){
135  if (y[i]>MaxY_) MaxY_ = y[i];
136  if (y[i]<MinY_) MinY_ = y[i];
137  }
138  }
139  for (unsigned int i=0;i<x.size();i++)
140  Plot(x[i],y[i]);
141 }
142 
143 
144 int DataPlot::Plot(float x,float y)
145 {
146  unsigned int vx=0,vy=0;
147  int res = WindowToViewport_(x,y,vx,vy);
148  if (res<0) return res;
149  switch (DrawStyle_) {
150  case Points:
151  for (unsigned int c=0;c<ChannelCount_;c++){
152  int d = Thickness_;
153  for (int dx =-d;dx<d;dx++)
154  for (int dy =-d;dy<d;dy++){
155  int xx = vx+dx;
156  int yy = vy+dy;
157  DrawIda_[yy][xx*ChannelCount_+c] = PenColour_[c];
158  }
159  }
160  break;
161  case Lines:
162  if (LastVX_>=0&&LastVY_>=0)
163  DrawLine_(LastVX_,LastVY_,vx,vy);
164  LastVX_ = vx;
165  LastVY_ = vy;
166  break;
167  case Cross:
168  for (unsigned int c=0;c<ChannelCount_;c++) {
169  for (int dx =-5;dx<5;dx++) {
170  int xx = int(vx)+dx;
171  DrawIda_[vy][xx*ChannelCount_+c] = PenColour_[c];
172  }
173  for (int dy =-5;dy<5;dy++) {
174  int yy = int(vy) + dy;
175  DrawIda_[yy][vx*ChannelCount_+c] = PenColour_[c];
176  }
177  }
178  break;
179  }
180  return 0;
181 }
182 
184 {
185  LastVY_ = -1;
186  LastVX_ = -1;
187 }
188 
190 {
191  for (unsigned int y=0;y<PlotAreaHeight_;y++)
192  for (unsigned int x=0;x<PlotAreaWidth_;x++)
193  for (unsigned int c=0;c<ChannelCount_;c++)
194  DrawIda_[y][x*ChannelCount_+c] = BGColour_[c];
195 
196 }
197 
198 int DataPlot::WindowToViewport_(float wx,float wy, unsigned int &vx,unsigned int &vy)
199 {
200  float vpw = float(PlotAreaWidth_ - 2*BorderX_);
201  float vph = float(PlotAreaHeight_ - 2*BorderY_);
202  float RangeX = MaxX_ - MinX_;
203  float RangeY = MaxY_ - MinY_;
204  float tx = (wx - MinX_)/RangeX * vpw + float(BorderX_);
205  float ty = PlotAreaHeight_ - ((wy - MinY_)/RangeY * vph + float(BorderY_));
206  if (tx <0 || tx > PlotAreaWidth_) return -1;
207  if (ty <0 || ty > PlotAreaHeight_) return -2;
208  vx = (int)rint(tx);
209  vy = (int)rint(ty);
210  return 0;
211 }
212 
213 
214 
215 void DataPlot::DrawLine_(unsigned int x,unsigned int y,unsigned int x2, unsigned int y2)
216 {
217  ImageDraw<unsigned char>::Line(*PlotArea_,x,y,x2,y2,PenColour_,Thickness_);
218 }
void DrawXAxis()
Definition: DataPlot.cpp:83
void Plot(const std::vector< float > &data)
Definition: DataPlot.cpp:103
DataPlot(Image< unsigned char > &PlotArea)
Definition: DataPlot.cpp:32
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void DrawYAxis()
Definition: DataPlot.cpp:94
void CutLinePlot()
Definition: DataPlot.cpp:183
static int Line(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value[])
lines
Definition: ImageDraw.cpp:404
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
void SetXRange(float min, float max)
Definition: DataPlot.cpp:63
void DrawLine_(unsigned int x, unsigned int y, unsigned int x2, unsigned int y2)
Definition: DataPlot.cpp:215
void SetYRange(float min, float max)
Definition: DataPlot.cpp:70
void SetAutoRange(bool x, bool y)
Definition: DataPlot.cpp:77
void SetDrawingStyle(enum DrawingStyle)
Definition: DataPlot.cpp:54
int WindowToViewport_(float wx, float wy, unsigned int &vx, unsigned int &vy)
Definition: DataPlot.cpp:198