# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2017-12-09 15:58:29 # Node ID df800179c68c793ed112485cb0a4f6b28f355c74 # Parent f8c253a6ab5f4588dfbf6cca221bfb3546a0cfcc added linear scale index buffer diff --git a/src/framebuffer2.h b/src/framebuffer2.h --- a/src/framebuffer2.h +++ b/src/framebuffer2.h @@ -25,7 +25,7 @@ struct Range { - double min, max; + double start, end; }; /// Abstract base class for all frame buffers. diff --git a/src/indexbuffer.h b/src/indexbuffer.h --- a/src/indexbuffer.h +++ b/src/indexbuffer.h @@ -24,7 +24,9 @@ #include "framebuffer2.h" /// A simple frame buffer that simply returns requested index as -/// value. +/// sample value. +/// +/// @note This buffer isn't for storing data. class IndexBuffer : public ResizableBuffer { public: diff --git a/src/linindexbuffer.cpp b/src/linindexbuffer.cpp new file mode 100644 --- /dev/null +++ b/src/linindexbuffer.cpp @@ -0,0 +1,57 @@ + /* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#include + +#include "linindexbuffer.h" + +LinIndexBuffer::LinIndexBuffer(unsigned n, Range lim) +{ + Q_ASSERT(n > 0); + + _size = n; + setLimits(lim); +} + +unsigned LinIndexBuffer::size() const +{ + return _size; +} + +double LinIndexBuffer::sample(unsigned i) const +{ + return _limits.start + i * _step; +} + +Range LinIndexBuffer::limits() const +{ + return _limits; +} + +void LinIndexBuffer::resize(unsigned n) +{ + _size = n; + setLimits(_limits); // called to update `_step` +} + +void LinIndexBuffer::setLimits(Range lim) +{ + _limits = lim; + _step = (lim.end - lim.start) / (_size-1); +} diff --git a/src/linindexbuffer.h b/src/linindexbuffer.h new file mode 100644 --- /dev/null +++ b/src/linindexbuffer.h @@ -0,0 +1,50 @@ + /* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#ifndef LININDEXBUFFER_H +#define LININDEXBUFFER_H + +// IMPORTANT TODO: rename to "framebuffer.h" when stream work is done. +#include "framebuffer2.h" + +/// A dynamic frame buffer that start and end values can be set and +/// intermediate values are calculated linearly. +/// +/// @note This buffer isn't for storing data. +class LinIndexBuffer : public ResizableBuffer +{ +public: + LinIndexBuffer(unsigned n, Range lim); + LinIndexBuffer(unsigned n, double min, double max) : + LinIndexBuffer(n, {min, max}) {}; + + unsigned size() const; + double sample(unsigned i) const; + Range limits() const; + void resize(unsigned n); + /// Sets minimum and maximum sample values of the buffer. + void setLimits(Range lim); + +private: + unsigned _size; + Range _limits; + double _step; +}; + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(Test EXCLUDE_FROM_ALL ../src/sink.cpp ../src/source.cpp ../src/indexbuffer.cpp + ../src/linindexbuffer.cpp ) add_test(NAME test1 COMMAND Test) qt5_use_modules(Test Widgets) diff --git a/tests/test.cpp b/tests/test.cpp --- a/tests/test.cpp +++ b/tests/test.cpp @@ -23,6 +23,7 @@ #include "samplepack.h" #include "source.h" #include "indexbuffer.h" +#include "linindexbuffer.h" TEST_CASE("samplepack with no X", "[memory]") { @@ -192,13 +193,46 @@ TEST_CASE("IndexBuffer", "[memory, buffe REQUIRE(buf.sample(i) == i); } auto l = buf.limits(); - REQUIRE(l.min == 0); - REQUIRE(l.max == 9); + REQUIRE(l.start == 0); + REQUIRE(l.end == 9); buf.resize(20); REQUIRE(buf.size() == 20); REQUIRE(buf.sample(15) == 15); l = buf.limits(); - REQUIRE(l.min == 0); - REQUIRE(l.max == 19); + REQUIRE(l.start == 0); + REQUIRE(l.end == 19); } + +TEST_CASE("LinIndexBuffer", "[memory, buffer]") +{ + LinIndexBuffer buf(10, 0., 3.0); + + REQUIRE(buf.size() == 10); + REQUIRE(buf.sample(0) == 0.); + REQUIRE(buf.sample(9) == 3.0); + REQUIRE(buf.sample(4) == Approx(1+1/3.)); + + auto l = buf.limits(); + REQUIRE(l.start == 0.); + REQUIRE(l.end == 3.); + + buf.resize(20); + REQUIRE(buf.size() == 20); + REQUIRE(buf.sample(0) == 0.); + REQUIRE(buf.sample(9) == Approx(9.*3./19.)); + REQUIRE(buf.sample(4) == Approx(4.*3./19.)); + REQUIRE(buf.sample(19) == 3.0); + + l = buf.limits(); + REQUIRE(l.start == 0.); + REQUIRE(l.end == 3.0); + + buf.setLimits({-5., 5.}); + l = buf.limits(); + REQUIRE(l.start == -5.0); + REQUIRE(l.end == 5.0); + + REQUIRE(buf.sample(0) == -5.0); + REQUIRE(buf.sample(19) == 5.0); +}