libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
datapoint.cpp
Go to the documentation of this file.
1// Copyright 2019, 2020 Filippo Rusconi
2// License: GPLv3+
3
4/////////////////////// StdLib includes
5#include <vector>
6#include <cmath>
7
8
9/////////////////////// Qt includes
10#include <QDebug>
11#include <QDataStream>
12#include <QRegularExpressionMatch>
13
14
15/////////////////////// Local includes
16#include "datapoint.h"
22
23
25 qRegisterMetaType<pappso::DataPoint>("pappso::DataPoint");
26
27
29 qRegisterMetaType<pappso::DataPointCstSPtr>("pappso::DataPointCstSPtr");
30
31namespace pappso
32{
33
34
38
39
40DataPoint::DataPoint(std::pair<pappso_double, pappso_double> pair)
41 : x(pair.first), y(pair.second)
42{
43}
44
45
46DataPoint::DataPoint(const QString &text)
47{
48 if(!initialize(text))
50 "Failed to initialize the DataPoint object using the provided string.");
51}
52
53DataPoint::DataPoint(const DataPoint &other) : x(other.x), y(other.y)
54{
55}
56
57
58
59// For debugging purposes.
60// DataPoint::~DataPoint()
61//{
62////qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
63////<< "Calling destructor for DataPoint.";
64//}
65
66
69{
70 return std::make_shared<const DataPoint>(*this);
71}
72
73
74void
76{
77 this->x = x;
78 this->y = y;
79}
80
81
82void
84{
85 x = other.x;
86 y = other.y;
87}
88
89
90bool
91DataPoint::initialize(const QString &text)
92{
93
94 QRegularExpressionMatch regExpMatch;
95
96 regExpMatch = Utils::xyMassDataFormatRegExp.match(text);
97
98 if(!regExpMatch.hasMatch())
99 return false;
100
101 bool ok = false;
102
103 double key = regExpMatch.captured(1).toDouble(&ok);
104
105 if(!ok)
106 return false;
107
108 // Note that group 2 is the separator group.
109
110 double val = regExpMatch.captured(3).toDouble(&ok);
111
112 if(!ok)
113 return false;
114
115 x = key;
116 y = val;
117
118 return true;
119}
120
121
122void
124{
125 x = -1;
126 y = 0;
127}
128
129
130bool
132{
133 return (x >= 0);
134}
135
136
137QString
139{
140 return QString("%1 %2").arg(x, 0, 'f', 15).arg(y, 0, 'f', 15);
141}
142
143
144QString
145DataPoint::toString(int decimals) const
146{
147 return QString("%1 %2").arg(x, 0, 'f', decimals).arg(y, 0, 'f', decimals);
148}
149
150
151QDataStream &
152operator<<(QDataStream &out, const DataPoint &dataPoint)
153{
154 out << dataPoint.x;
155 out << dataPoint.y;
156
157 return out;
158}
159
160
161QDataStream &
162operator>>(QDataStream &in, DataPoint &dataPoint)
163{
164
165 if(in.atEnd())
166 {
167 throw PappsoException(
168 QString("error in QDataStream unserialize operator>> of massSpectrum "
169 "dataPoint:\nread datastream failed status=%1")
170 .arg(in.status()));
171 }
172 in >> dataPoint.x;
173 in >> dataPoint.y;
174
175 return in;
176}
177
178
179void
181{
182 x += value;
183}
184
185
186void
188{
189 y += value;
190}
191
192bool
194{
195 return ((x == other.x) && (y == other.y));
196}
197
198
199DataPoint &
201{
202 x = other.x;
203 y = other.y;
204
205 return *this;
206}
207
208
209} // namespace pappso
static QRegularExpression xyMassDataFormatRegExp
Definition utils.h:60
int dataPointCstSPtrMetaTypeId
Definition datapoint.cpp:28
int dataPointMetaTypeId
Definition datapoint.cpp:24
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
QDataStream & operator<<(QDataStream &outstream, const MassSpectrum &massSpectrum)
QDataStream & operator>>(QDataStream &instream, MassSpectrum &massSpectrum)
double pappso_double
A type definition for doubles.
Definition types.h:61
std::shared_ptr< const DataPoint > DataPointCstSPtr
Definition datapoint.h:18
void incrementY(pappso_double value)
DataPoint()=default
pappso_double x
Definition datapoint.h:24
bool isValid() const
QString toString() const
void initialize(pappso_double x, pappso_double y)
Definition datapoint.cpp:75
bool operator==(const DataPoint &other) const
void incrementX(pappso_double value)
DataPoint & operator=(const DataPoint &other)
pappso_double y
Definition datapoint.h:25
DataPointCstSPtr makeDataPointCstSPtr() const
Definition datapoint.cpp:68