Skip to content

Commit 51f2d26

Browse files
Plot decimation
1 parent 04436b0 commit 51f2d26

File tree

4 files changed

+51
-88
lines changed

4 files changed

+51
-88
lines changed

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ cdhdrs: $(CDHDRS:%=include/%) graphcode.cd $(CLASSDESC)
143143

144144
include/graphcode.cd: graphcode/graphcode.h
145145

146-
# newarrays needs to be preexpanded ...
147-
#include/newarrays.cd: include/newarrays.exh
148-
# classdesc -I $(CDINCLUDE) -I $(ECOLAB_HOME)/include $(ACTIONS) <$< >$@
149-
150146
include/newarrays.exh: include/newarrays.h utils/wrap
151147
gcc -E -P $(CONT_FLAG) $< |wrap >$@
152148

include/plot.h

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,19 @@ namespace ecolab
6969
void extendPalette() {palette.push_back(LineStyle());}
7070

7171
private:
72-
std::string m_image;
7372
std::vector<std::vector<double> > x;
7473
std::vector<std::vector<double> > y;
75-
74+
std::vector<unsigned> decimation, decimate;
75+
const unsigned maxPoints=1000; ///< maximum number of points to plot without decimation
76+
7677
CLASSDESC_ACCESS(Plot);
7778

7879
// internal common routine for add()
7980
void addNew(cairo::Surface& surf, bool doRedraw,
8081
const unsigned *startPen, const unsigned *finishPen, int numPt);
8182

83+
84+
8285
double iflogx(double x) const {return logx? log10(x): x;}
8386
double iflogy(double y) const {return logy? log10(y): y;}
8487

@@ -139,21 +142,6 @@ namespace ecolab
139142
/// set min/max to autoscale on contained data
140143
void setMinMax();
141144

142-
/// set/get the Tk image that this class writes to.
143-
std::string Image() const {return m_image;}
144-
std::string Image(const std::string& im, bool transparency=true);
145-
// string image(TCL_args args) {
146-
// if (args.count>0)
147-
// {
148-
// string imgName=args.str();
149-
// bool transparency=true;
150-
// if (args.count) transparency=args;
151-
// return Image(imgName,transparency);
152-
// }
153-
// else
154-
// return Image();
155-
// }
156-
157145
/// draw the plot onto a given surface
158146
virtual void draw(cairo::Surface&);
159147
void draw(cairo_t*, double width, double height) const;
@@ -183,14 +171,6 @@ namespace ecolab
183171
/// remove all pens from pen and above from plot
184172
void removePensFrom(unsigned pen);
185173

186-
// //serialisation support (surface is not auto-serialisable)
187-
// void pack(classdesc::pack_t& p) const override {p<<m_image<<x<<y<<minx<<maxx<<miny<<maxy;}
188-
//
189-
// void unpack(classdesc::pack_t& p) override {
190-
// p>>m_image>>x>>y>>minx>>maxx>>miny>>maxy;
191-
// Image(m_image);
192-
// }
193-
194174
/// add a point to the graph withour redrwaing anything
195175
void addPt(unsigned pen, double x, double y);
196176

@@ -222,16 +202,23 @@ namespace ecolab
222202
const array_ns::array<unsigned>& pens,
223203
double x1, const array_ns::array<double>& y1);
224204

225-
template <class C>
226-
void add(cairo::Surface& surf, unsigned pen, const C& x1, const C& y1)
205+
void checkAddPen(unsigned pen)
227206
{
228-
assert(x1.size()==y1.size());
229-
bool doRedraw=false;
230207
if (pen>=x.size())
231208
{
232209
x.resize(pen+1);
233210
y.resize(pen+1);
211+
decimation.resize(pen+1,1);
212+
decimate.resize(pen+1);
234213
}
214+
}
215+
216+
template <class C>
217+
void add(cairo::Surface& surf, unsigned pen, const C& x1, const C& y1)
218+
{
219+
assert(x1.size()==y1.size());
220+
bool doRedraw=false;
221+
checkAddPen(pen);
235222
for (size_t i=0; i<x1.size(); ++i)
236223
{
237224
doRedraw|=x1[i]<minx||x1[i]>maxx || y1[i]<miny||y1[i]>maxy;
@@ -245,11 +232,7 @@ namespace ecolab
245232
/// \a x and \a y should have the same size, if not, the larger is truncated
246233
void setPen(unsigned pen, const double* xx, const double* yy, size_t sz)
247234
{
248-
if (pen>=x.size())
249-
{
250-
x.resize(pen+1);
251-
y.resize(pen+1);
252-
}
235+
checkAddPen(pen);
253236
x[pen].assign(xx, xx+sz);
254237
y[pen].assign(yy, yy+sz);
255238
}

models/panmictic_ecolab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def step():
4949
nsp=len(ecolab.species)
5050
statusBar.configure(text=f't={ecolab.tstep()} nsp:{nsp}')
5151
plot('No. species',ecolab.tstep(),nsp)
52-
plot('Density',ecolab.tstep(),ecolab.density(), pens=ecolab.species())
52+
#plot('Density',ecolab.tstep(),ecolab.density(), pens=ecolab.species())
5353
plot('Conn-Nsp',ecolab.connectivity(),nsp)
5454
plot('Conn*density',ecolab.tstep(),ecolab.connectivity()*nsp)
5555

src/plot.cc

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,6 @@ namespace ecolab
251251
int PlotSurface::width() const {return surface? surface->width():0;}
252252
int PlotSurface::height() const {return surface? surface->height():0;}
253253

254-
255-
string Plot::Image(const string& im, bool transparency)
256-
{
257-
#if defined(TK)
258-
// Tk_PhotoHandle photo = Tk_FindPhoto(interp(), im.c_str());
259-
// if (photo)
260-
// {
261-
// surface.reset(new cairo::TkPhotoSurface(photo,transparency));
262-
// cairo_surface_set_device_offset(surface->surface(),0.5*surface->width(),0.5*surface->height());
263-
// }
264-
// redraw();
265-
#endif
266-
return m_image=im;
267-
}
268-
269254
string Plot::axisLabel(double x, double scale, bool percent) const
270255
{
271256
char label[30];
@@ -311,8 +296,10 @@ namespace ecolab
311296

312297
void Plot::removePensFrom(unsigned pen)
313298
{
314-
if (x.size()>pen) x.resize(pen);
315-
if (y.size()>pen) y.resize(pen);
299+
x.resize(pen);
300+
y.resize(pen);
301+
decimation.resize(pen);
302+
decimate.resize(pen);
316303
redraw();
317304
}
318305

@@ -1101,6 +1088,7 @@ namespace ecolab
11011088
void Plot::clear()
11021089
{
11031090
x.clear(); y.clear();
1091+
decimation.clear(); decimate.clear();
11041092
redraw();
11051093
}
11061094

@@ -1167,13 +1155,25 @@ namespace ecolab
11671155

11681156
void Plot::addPt(unsigned pen, double xx, double yy)
11691157
{
1170-
if (pen>=x.size())
1158+
checkAddPen(pen);
1159+
if (decimate[pen]==0)
11711160
{
1172-
x.resize(pen+1);
1173-
y.resize(pen+1);
1161+
x[pen].push_back(xx);
1162+
y[pen].push_back(yy);
1163+
if (x[pen].size()>maxPoints)
1164+
{
1165+
decimation[pen]*=2;
1166+
for (size_t i=0; i<x[pen].size(); i+=2)
1167+
{
1168+
x[pen][i/2]=x[pen][i];
1169+
y[pen][i/2]=y[pen][i];
1170+
}
1171+
x[pen].resize(x[pen].size()/2);
1172+
y[pen].resize(y[pen].size()/2);
1173+
}
11741174
}
1175-
x[pen].push_back(xx);
1176-
y[pen].push_back(yy);
1175+
if (++decimate[pen]>=decimation[pen])
1176+
decimate[pen]=0;
11771177
}
11781178

11791179
void Plot::add(cairo::Surface& surf, unsigned pen,
@@ -1189,33 +1189,17 @@ namespace ecolab
11891189
{
11901190
bool doRedraw=x1<minx||x1>maxx;
11911191
unsigned maxPen=max(pens);
1192-
if (maxPen>=x.size())
1193-
{
1194-
x.resize(maxPen+1);
1195-
y.resize(maxPen+1);
1196-
}
1197-
for (size_t p=0; p<y1.size(); ++p)
1198-
{
1199-
unsigned pen=pens[p];
1200-
double yy=y1[pen];
1201-
doRedraw|=yy<miny || yy>maxy;
1202-
x[pen].push_back(x1);
1203-
y[pen].push_back(yy);
1204-
}
1205-
addNew(surf, doRedraw, pens.begin(), pens.end(), 1);
1206-
}
1207-
1208-
// void Plot::plot(TCL_args args)
1209-
// {
1210-
// if (args.count>1)
1211-
// {
1212-
// double x=args;
1213-
// array_ns::array<double> y; args>>y;
1214-
// assert(args.count==0);
1215-
// array_ns::array<unsigned> pens=pcoord(y.size());
1216-
// add(pens,x,y);
1217-
// }
1218-
// }
1192+
checkAddPen(maxPen);
1193+
for (size_t p=0; p<y1.size(); ++p)
1194+
{
1195+
unsigned pen=pens[p];
1196+
double yy=y1[pen];
1197+
doRedraw|=yy<miny || yy>maxy;
1198+
x[pen].push_back(x1);
1199+
y[pen].push_back(yy);
1200+
}
1201+
addNew(surf, doRedraw, pens.begin(), pens.end(), 1);
1202+
}
12191203

12201204
string stripPangoMarkup(const string& markedUptext)
12211205
{

0 commit comments

Comments
 (0)