/*Copyright © 2017 Hasan Yavuz ÖzderyaThis file is part of serialplot.serialplot is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with serialplot. If not, see <http://www.gnu.org/licenses/>.*/#include "framebuffer.h"FrameBuffer::FrameBuffer(size_t size){_size = size;data = new double[_size]();headIndex = 0;brInvalid = false;_brCache.setCoords(0, 0, size, 0);}FrameBuffer::~FrameBuffer(){delete[] data;}void FrameBuffer::resize(size_t size){int offset = size - _size;if (offset == 0) return;double* newData = new double[size];// move data to new arrayint fill_start = offset > 0 ? offset : 0;for (int i = fill_start; i < int(size); i++){newData[i] = sample(i - offset);}// fill the beginning of the new dataif (fill_start > 0){for (int i = 0; i < fill_start; i++){newData[i] = 0;}}// data is ready, clean and re-pointdelete data;data = newData;headIndex = 0;_size = size;// invalidate bounding rectanglebrInvalid = true;}void FrameBuffer::addSamples(double* samples, size_t size){unsigned shift = size;if (shift < _size){unsigned x = _size - headIndex; // distance of `head` to endif (shift <= x) // there is enough room at the end of array{for (size_t i = 0; i < shift; i++){data[i+headIndex] = samples[i];}if (shift == x) // we used all the room at the end{headIndex = 0;}else{headIndex += shift;}}else // there isn't enough room{for (size_t i = 0; i < x; i++) // fill the end part{data[i+headIndex] = samples[i];}for (size_t i = 0; i < (shift-x); i++) // continue from the beginning{data[i] = samples[i+x];}headIndex = shift-x;}}else // number of new samples equal or bigger than current size{int x = shift - _size;for (size_t i = 0; i < _size; i++){data[i] = samples[i+x];}headIndex = 0;}// invalidate cachebrInvalid = true;}void FrameBuffer::clear(){for (size_t i=0; i < _size; i++) data[i] = 0.;_brCache.setCoords(0, 0, _size, 0);}size_t FrameBuffer::size() const{return _size;}QRectF FrameBuffer::boundingRect() const{if (brInvalid) updateBoundingRect();return _brCache;}void FrameBuffer::updateBoundingRect() const{double minValue = data[0];double maxValue = data[0];for (size_t i = 0; i < _size; i++){if (data[i] > maxValue){maxValue = data[i];}else if (data[i] < minValue){minValue = data[i];}}_brCache.setTop(minValue);_brCache.setBottom(maxValue);brInvalid = false;}double FrameBuffer::sample(size_t i) const{size_t index = headIndex + i;if (index >= _size) index -= _size;return data[index];}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。