pappsomspp
Library for mass spectrometry
maptrace.cpp
Go to the documentation of this file.
1 #include <numeric>
2 #include <limits>
3 #include <vector>
4 #include <map>
5 #include <cmath>
6 #include <algorithm>
7 #include <iostream>
8 #include <iomanip>
9 
10 /////////////////////// Qt includes
11 #include <QDebug>
12 
13 
14 #include "maptrace.h"
15 #include "../processing/combiners/tracepluscombiner.h"
16 #include "../processing/combiners/traceminuscombiner.h"
17 #include "../types.h"
18 
19 
20 
21 int mapTraceMetaTypeId = qRegisterMetaType<pappso::MapTrace>("pappso::MapTrace");
22 int mapTracePtrMetaTypeId = qRegisterMetaType<pappso::MapTrace *>("pappso::MapTrace *");
23 
24 
25 namespace pappso
26 {
27 
29 {
30 }
31 
32 
34  const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
35 {
36  for(auto &dataPoint : dataPoints)
37  {
38  insert(dataPoint);
39  }
40 }
41 
42 
43 MapTrace::MapTrace(const std::vector<DataPoint> &dataPoints)
44 {
45  for(auto &dataPoint : dataPoints)
46  {
47  insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
48  }
49 }
50 
51 
53  : std::map<pappso_double, pappso_double>(other)
54 {
55 }
56 
57 
59 {
60  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()";
61 
62  for(auto &dataPoint : trace)
63  {
64  // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << "
65  // () "
66  //<< std::setprecision(15)
67  //<< "Current data point: " << dataPoint.toString().toStdString() <<
68  // std::endl;
69 
70  insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
71  }
72 
73  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
74  //<< "After construction, map size: " << size();
75 }
76 
77 
79 {
80  // Calls the destructor for each DataPoint object in the vector.
81  clear();
82 }
83 
84 
85 size_t
86 MapTrace::initialize(const std::vector<pappso_double> &xVector,
87  const std::vector<pappso_double> &yVector)
88 {
89  // Clear *this, because initialize supposes that *this will contain only the
90  // data in the vectors.
91 
92  clear();
93 
94  // Sanity check
95  if(xVector.size() != yVector.size())
96  qFatal(
97  "Fatal error at %s@%d -- %s(). "
98  "xVector and yVector must have the same size."
99  "Program aborted.",
100  __FILE__,
101  __LINE__,
102  __FUNCTION__);
103 
104  for(std::size_t iter = 0; iter < xVector.size(); ++iter)
105  {
106  insert(std::pair<pappso_double, pappso_double>(xVector.at(iter),
107  yVector.at(iter)));
108  }
109 
110  return size();
111 }
112 
113 
114 size_t
115 MapTrace::initialize(const std::map<pappso_double, pappso_double> &map)
116 {
117 
118  // Clear *this, because initialize supposes that *this will be identical to
119  // map.
120 
121  clear();
122 
123  for(auto &&pair : map)
124  {
125  insert(pair);
126  }
127 
128  return size();
129 }
130 
131 
132 MapTrace &
134 {
135 
136  if(&other == this)
137  return *this;
138 
139  // Clear *this, because initialize supposes that *this will be identical to
140  // other.
141 
142  clear();
143 
144  for(auto &pair : other)
145  {
146  insert(pair);
147  }
148 
149  return *this;
150 }
151 
152 
155 {
156  return std::make_shared<MapTrace>(*this);
157 }
158 
159 
162 {
163  return std::make_shared<const MapTrace>(*this);
164 }
165 
166 
167 std::vector<pappso_double>
169 {
170  std::vector<pappso_double> vector;
171 
172  for(auto &&pair : *this)
173  vector.push_back(pair.first);
174 
175  return vector;
176 }
177 
178 
179 std::vector<pappso_double>
181 {
182  std::vector<pappso_double> vector;
183 
184  for(auto &&pair : *this)
185  vector.push_back(pair.second);
186 
187  return vector;
188 }
189 
190 
191 std::vector<pappso_double>
193 {
194  return firstToVector();
195 }
196 
197 
198 std::vector<pappso_double>
200 {
201  return secondToVector();
202 }
203 
204 
205 Trace
207 {
208  Trace trace;
209 
210  for(auto &&pair : *this)
211  trace.push_back(DataPoint(pair.first, pair.second));
212 
213  return trace;
214 }
215 
216 
217 QString
219 {
220  // Even if the spectrum is empty, we should return an empty string.
221  QString text;
222 
223  for(auto &&pair : *this)
224  {
225 // For debugging
226 #if 0
227 
228  QString new_data_point_text = QString("%1 %2\n")
229  .arg(pair.first, 0, 'f', 10)
230  .arg(pair.second, 0, 'f', 10);
231 
232  qDebug() << "new data point text:" << new_data_point_text;
233  text.append(new_data_point_text);
234 #endif
235 
236  text.append(QString("%1 %2\n")
237  .arg(pair.first, 0, 'f', 10)
238  .arg(pair.second, 0, 'f', 10));
239  }
240 
241  return text;
242 }
243 
244 
245 } // namespace pappso
246 
247 
248 
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:67
pappso::MapTraceCstSPtr
std::shared_ptr< const MapTrace > MapTraceCstSPtr
Definition: maptrace.h:26
pappso::MapTrace::firstToVector
std::vector< pappso_double > firstToVector() const
Definition: maptrace.cpp:168
pappso::MapTrace::xValues
std::vector< pappso_double > xValues()
Definition: maptrace.cpp:192
pappso
Definition: aa.cpp:38
pappso::DataPoint
Definition: datapoint.h:20
pappso::MapTrace
Definition: maptrace.h:32
pappso::MapTrace::makeMapTraceCstSPtr
MapTraceCstSPtr makeMapTraceCstSPtr() const
Definition: maptrace.cpp:161
pappso::MapTraceSPtr
std::shared_ptr< MapTrace > MapTraceSPtr
Definition: maptrace.h:25
pappso::MapTrace::initialize
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition: maptrace.cpp:86
pappso::MapTrace::operator=
virtual MapTrace & operator=(const MapTrace &other)
Definition: maptrace.cpp:133
pappso::MapTrace::yValues
std::vector< pappso_double > yValues()
Definition: maptrace.cpp:199
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:125
pappso::MapTrace::toString
QString toString() const
Definition: maptrace.cpp:218
mapTracePtrMetaTypeId
int mapTracePtrMetaTypeId
Definition: maptrace.cpp:22
pappso::MapTrace::~MapTrace
virtual ~MapTrace()
Definition: maptrace.cpp:78
maptrace.h
pappso::MapTrace::secondToVector
std::vector< pappso_double > secondToVector() const
Definition: maptrace.cpp:180
mapTraceMetaTypeId
int mapTraceMetaTypeId
Definition: maptrace.cpp:21
pappso::MapTrace::toTrace
Trace toTrace() const
Definition: maptrace.cpp:206
pappso::MapTrace::makeMapTraceSPtr
MapTraceSPtr makeMapTraceSPtr() const
Definition: maptrace.cpp:154
pappso::MapTrace::MapTrace
MapTrace()
Definition: maptrace.cpp:28