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
24int dataPointMetaTypeId = qRegisterMetaType<pappso::DataPoint>("pappso::DataPoint");
25
26
28 qRegisterMetaType<pappso::DataPointCstSPtr>("pappso::DataPointCstSPtr");
29
30namespace pappso
31{
32
33
37
38
39DataPoint::DataPoint(std::pair<pappso_double, pappso_double> pair) : x(pair.first), y(pair.second)
40{
41}
42
43
44DataPoint::DataPoint(const QString &text)
45{
46 if(!initialize(text))
48 "Failed to initialize the DataPoint object using the provided string.");
49}
50
51DataPoint::DataPoint(const DataPoint &other) : x(other.x), y(other.y)
52{
53}
54
55
56// For debugging purposes.
57// DataPoint::~DataPoint()
58//{
59////qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
60////<< "Calling destructor for DataPoint.";
61//}
62
63
66{
67 return std::make_shared<const DataPoint>(*this);
68}
69
70
71void
73{
74 this->x = x;
75 this->y = y;
76}
77
78
79void
81{
82 x = other.x;
83 y = other.y;
84}
85
86
87bool
88DataPoint::initialize(const QString &text)
89{
90
91 QRegularExpressionMatch regExpMatch;
92
93 regExpMatch = Utils::xyMassDataFormatRegExp.match(text);
94
95 if(!regExpMatch.hasMatch())
96 return false;
97
98 bool ok = false;
99
100 double key = regExpMatch.captured(1).toDouble(&ok);
101
102 if(!ok)
103 return false;
104
105 // Note that group 2 is the separator group.
106
107 double val = regExpMatch.captured(3).toDouble(&ok);
108
109 if(!ok)
110 return false;
111
112 x = key;
113 y = val;
114
115 return true;
116}
117
118
119void
121{
122 x = -1;
123 y = 0;
124}
125
126
127bool
129{
130 return (x >= 0);
131}
132
133
134QString
136{
137 return QString("%1 %2").arg(x, 0, 'f', 15).arg(y, 0, 'f', 15);
138}
139
140
141QString
142DataPoint::toString(int decimals) const
143{
144 return QString("%1 %2").arg(x, 0, 'f', decimals).arg(y, 0, 'f', decimals);
145}
146
147
148QDataStream &
149operator<<(QDataStream &out, const DataPoint &dataPoint)
150{
151 out << dataPoint.x;
152 out << dataPoint.y;
153
154 return out;
155}
156
157
158QDataStream &
159operator>>(QDataStream &in, DataPoint &dataPoint)
160{
161
162 if(in.atEnd())
163 {
164 throw PappsoException(QString("error in QDataStream unserialize operator>> of massSpectrum "
165 "dataPoint:\nread datastream failed status=%1")
166 .arg(in.status()));
167 }
168 in >> dataPoint.x;
169 in >> dataPoint.y;
170
171 return in;
172}
173
174
175void
177{
178 x += value;
179}
180
181
182void
184{
185 y += value;
186}
187
188bool
190{
191 return ((x == other.x) && (y == other.y));
192}
193
194
195DataPoint &
197{
198 x = other.x;
199 y = other.y;
200
201 return *this;
202}
203
204
205} // namespace pappso
static QRegularExpression xyMassDataFormatRegExp
Regular expression matching <numerical value><non-numerical*><numericalvalue>.
Definition utils.h:60
int dataPointCstSPtrMetaTypeId
Definition datapoint.cpp:27
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:60
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:72
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:65