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"
17#include "../types.h"
18#include "../utils.h"
19#include "../pappsoexception.h"
20#include "../exception/exceptionoutofrange.h"
21#include "../exception/exceptionnotpossible.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(const DataPoint &other) : x(other.x), y(other.y)
41{
42}
43
44
48
49
50DataPoint::DataPoint(std::pair<pappso_double, pappso_double> pair)
51 : x(pair.first), y(pair.second)
52{
53}
54
55
56DataPoint::DataPoint(const QString &text)
57{
58 if(!initialize(text))
60 "Failed to initialize the DataPoint object using the provided string.");
61}
62
63
64// For debugging purposes.
65// DataPoint::~DataPoint()
66//{
67////qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
68////<< "Calling destructor for DataPoint.";
69//}
70
71
74{
75 return std::make_shared<const DataPoint>(*this);
76}
77
78
79void
81{
82 this->x = x;
83 this->y = y;
84}
85
86
87void
89{
90 x = other.x;
91 y = other.y;
92}
93
94
95bool
96DataPoint::initialize(const QString &text)
97{
98
99 QRegularExpressionMatch regExpMatch;
100
101 regExpMatch = Utils::xyMassDataFormatRegExp.match(text);
102
103 if(!regExpMatch.hasMatch())
104 return false;
105
106 bool ok = false;
107
108 double key = regExpMatch.captured(1).toDouble(&ok);
109
110 if(!ok)
111 return false;
112
113 // Note that group 2 is the separator group.
114
115 double val = regExpMatch.captured(3).toDouble(&ok);
116
117 if(!ok)
118 return false;
119
120 x = key;
121 y = val;
122
123 return true;
124}
125
126
127void
129{
130 x = -1;
131 y = 0;
132}
133
134
135bool
137{
138 return (x >= 0);
139}
140
141
142QString
144{
145 return QString("%1 %2").arg(x, 0, 'f', 15).arg(y, 0, 'f', 15);
146}
147
148
149QString
150DataPoint::toString(int decimals) const
151{
152 return QString("%1 %2").arg(x, 0, 'f', decimals).arg(y, 0, 'f', decimals);
153}
154
155
156QDataStream &
157operator<<(QDataStream &out, const DataPoint &dataPoint)
158{
159 out << dataPoint.x;
160 out << dataPoint.y;
161
162 return out;
163}
164
165
166QDataStream &
167operator>>(QDataStream &in, DataPoint &dataPoint)
168{
169
170 if(in.atEnd())
171 {
172 throw PappsoException(
173 QString("error in QDataStream unserialize operator>> of massSpectrum "
174 "dataPoint:\nread datastream failed status=%1")
175 .arg(in.status()));
176 }
177 in >> dataPoint.x;
178 in >> dataPoint.y;
179
180 return in;
181}
182
183
184void
186{
187 x += value;
188}
189
190
191void
193{
194 y += value;
195}
196
197bool
199{
200 return ((x == other.x) && (y == other.y));
201}
202
203
204DataPoint &
206{
207 x = other.x;
208 y = other.y;
209
210 return *this;
211}
212
213
214} // namespace pappso
static QRegularExpression xyMassDataFormatRegExp
Definition utils.h:59
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:50
std::shared_ptr< const DataPoint > DataPointCstSPtr
Definition datapoint.h:18
void incrementY(pappso_double value)
pappso_double x
Definition datapoint.h:23
bool isValid() const
QString toString() const
void initialize(pappso_double x, pappso_double y)
Definition datapoint.cpp:80
bool operator==(const DataPoint &other) const
void incrementX(pappso_double value)
DataPoint & operator=(const DataPoint &other)
pappso_double y
Definition datapoint.h:24
DataPointCstSPtr makeDataPointCstSPtr() const
Definition datapoint.cpp:73