transform_function.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
8 
9 namespace PJ
10 {
12 {
13  static unsigned order = 0;
14  _order = order++;
15 }
16 
17 std::vector<const PlotData*>& TransformFunction::dataSources()
18 {
19  return _src_vector;
20 }
21 
23  const std::vector<const PlotData*>& src_vect,
24  std::vector<PlotData*>& dst_vect)
25 {
26  if (numInputs() >= 0 && src_vect.size() != numInputs())
27  {
28  throw std::runtime_error("Wrong number of input data sources "
29  "in setDataSource");
30  }
31  if (numOutputs() >= 0 && dst_vect.size() != numOutputs())
32  {
33  throw std::runtime_error("Wrong number of output data destinations");
34  }
35  _data = data;
36  _src_vector = src_vect;
37  _dst_vector = dst_vect;
38 }
39 
41 {
42  _last_timestamp = std::numeric_limits<double>::lowest();
43 }
44 
46 {
47  const PlotData* src_data = _src_vector.front();
48  PlotData* dst_data = _dst_vector.front();
49  if (src_data->size() == 0)
50  {
51  return;
52  }
53  dst_data->setMaximumRangeX(src_data->maximumRangeX());
54  if (dst_data->size() != 0)
55  {
56  _last_timestamp = dst_data->back().x;
57  }
58 
59  int pos = src_data->getIndexFromX(_last_timestamp);
60  size_t index = pos < 0 ? 0 : static_cast<size_t>(pos);
61 
62  while (index < src_data->size())
63  {
64  const auto& in_point = src_data->at(index);
65 
66  if (in_point.x >= _last_timestamp)
67  {
68  auto out_point = calculateNextPoint(index);
69  if (out_point)
70  {
71  dst_data->pushBack(std::move(out_point.value()));
72  }
73  _last_timestamp = in_point.x;
74  }
75  index++;
76  }
77 }
78 
80 {
81  auto it = instance()->creators_.find(name);
82  if (it == instance()->creators_.end())
83  {
84  return {};
85  }
86  return it->second();
87 }
88 
90 {
91  static TransformFactory* _ptr(nullptr);
92  if (!qApp->property("TransformFactory").isValid() && !_ptr)
93  {
94  _ptr = _transform_factory_ptr_from_macro;
95  qApp->setProperty("TransformFactory", QVariant::fromValue(_ptr));
96  }
97  else if (!_ptr)
98  {
99  _ptr = qvariant_cast<TransformFactory*>(qApp->property("TransformFactory"));
100  }
101  else if (!qApp->property("TransformFactory").isValid())
102  {
103  qApp->setProperty("TransformFactory", QVariant::fromValue(_ptr));
104  }
105  return _ptr;
106 }
107 
108 const std::set<std::string>& TransformFactory::registeredTransforms()
109 {
110  return instance()->names_;
111 }
112 
114 {
115  if (_src_vector.empty())
116  {
117  return nullptr;
118  }
119  return _src_vector.front();
120 }
121 
122 } // namespace PJ
PJ::TransformFunction::setData
virtual void setData(PlotDataMapRef *data, const std::vector< const PlotData * > &src_vect, std::vector< PlotData * > &dst_vect)
Definition: transform_function.cpp:22
PJ::TimeseriesBase
Definition: timeseries.h:16
PJ::TransformFunction::numOutputs
virtual int numOutputs() const =0
PJ::TransformFunction::dataSources
std::vector< const PlotData * > & dataSources()
Definition: transform_function.cpp:17
PJ::TransformFunction::numInputs
virtual int numInputs() const =0
PJ::TransformFactory
---— The factory to create instances of a SeriesTransform ----------—
Definition: transform_function.h:120
PJ::TransformFunction::order
unsigned order() const
Definition: transform_function.h:69
PJ::TransformFunction::Ptr
std::shared_ptr< TransformFunction > Ptr
Definition: transform_function.h:27
PJ::TransformFunction_SISO::calculate
void calculate() override
Definition: transform_function.cpp:45
PJ::TimeseriesBase::pushBack
void pushBack(const Point &p) override
Definition: timeseries.h:61
PJ::TransformFactory::instance
static TransformFactory * instance()
Definition: transform_function.cpp:89
PJ::TransformFunction_SISO::reset
void reset() override
Definition: transform_function.cpp:40
PJ::TransformFunction::_src_vector
std::vector< const PlotData * > _src_vector
Definition: transform_function.h:78
PJ::PlotDataBase::back
const Point & back() const
Definition: plotdatabase.h:250
transform_function.h
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
PJ::TransformFactory::registeredTransforms
static const std::set< std::string > & registeredTransforms()
Definition: transform_function.cpp:108
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
PJ::TransformFunction::_order
unsigned _order
Definition: transform_function.h:82
PJ::TimeseriesBase::setMaximumRangeX
void setMaximumRangeX(double max_range)
Definition: timeseries.h:42
PJ::TransformFunction::TransformFunction
TransformFunction()
Definition: transform_function.cpp:11
PJ::TimeseriesBase::getIndexFromX
int getIndexFromX(double x) const
Definition: timeseries.h:106
PJ::TransformFunction::_data
PlotDataMapRef * _data
Definition: transform_function.h:80
PJ::TransformFunction_SISO::calculateNextPoint
virtual std::optional< PlotData::Point > calculateNextPoint(size_t index)=0
PJ::PlotDataBase::size
virtual size_t size() const
Definition: plotdatabase.h:183
PJ::TransformFactory::names_
std::set< std::string > names_
Definition: transform_function.h:133
PJ::TimeseriesBase::maximumRangeX
double maximumRangeX() const
Definition: timeseries.h:48
PJ
Definition: dataloader_base.h:16
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::TransformFunction_SISO::dataSource
const PlotData * dataSource() const
Definition: transform_function.cpp:113
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
PJ::TransformFactory::creators_
std::map< std::string, std::function< TransformFunction::Ptr()> > creators_
Definition: transform_function.h:131
PJ::PlotDataMapRef
Definition: plotdata.h:34
PJ::TransformFunction::_dst_vector
std::vector< PlotData * > _dst_vector
Definition: transform_function.h:79
PJ::TransformFunction_SISO::_last_timestamp
double _last_timestamp
Definition: transform_function.h:115
PJ::TransformFactory::create
static TransformFunction::Ptr create(const std::string &name)
Definition: transform_function.cpp:79
nullptr
#define nullptr
Definition: backward.hpp:386


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:48