TBTK
Need a break? Support the development by playing Polarity Puzzles
Canvas2D.h
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
17 
24 #ifndef COM_DAFER45_TBTK_CANVAS_2D
25 #define COM_DAFER45_TBTK_CANVAS_2D
26 
27 #include "TBTK/Canvas.h"
28 #include "TBTK/TBTKMacros.h"
29 
30 #include <string>
31 #include <tuple>
32 
33 namespace TBTK{
34 
35 class Canvas2D : public Canvas{
36 public:
38  Canvas2D();
39 
41  virtual ~Canvas2D();
42 
44  void setBoundsX(double minX, double maxX);
45 
47  void setBoundsY(double minY, double maxY);
48 
50  void setBounds(double minX, double maxX, double minY, double maxY);
51 
53  double getMinX() const;
54 
56  double getMaxX() const;
57 
59  double getMinY() const;
60 
62  double getMaxY() const;
63 
65  void setLabelX(const std::string &labelX);
66 
68  void setLabelY(const std::string &labelY);
69 
71  const std::string& getLabelX() const;
72 
74  const std::string& getLabelY() const;
75 
77  void setHold(bool hold);
78 
80  void plot(
81  const std::vector<double> &y,
82  const std::string &title = "",
83  const std::vector<unsigned char> &color = {0, 0, 0},
84  unsigned int size = 1
85  );
86 
88  void plot(
89  const std::vector<double> &x,
90  const std::vector<double> &y,
91  const std::string &title = "",
92  const std::vector<unsigned char> &color = {0, 0, 0},
93  unsigned int size = 1
94  );
95 
97  virtual void clear();
98 protected:
102  unsigned int getNumDataSets() const;
103 
104  /*** Get the x values for the given data set.
105  *
106  * @param dataSet The data set to get the x-values for.
107  *
108  * @return The x-values for the given data set. */
109  const std::vector<double>& getX(unsigned int dataSet) const;
110 
116  const std::vector<double>& getY(unsigned int dataSet) const;
117 
118  /*** Get the title for the given data set.
119  *
120  * @param dataSet The data set to get the title for.
121  *
122  * @return The title for the given data set. */
123  const std::string& getTitle(unsigned int dataSet) const;
124 
130  const std::vector<unsigned char>& getColor(unsigned int dataSet) const;
131 
137  unsigned int getSize(unsigned int dataSet) const;
138 private:
140  double minX, maxX, minY, maxY;
141 
143  std::string labelX, labelY;
144 
147  bool hold;
148 
150  std::vector<
151  std::tuple<
152  std::vector<double>, //x-values
153  std::vector<double>, //y-values
154  std::string, //title
155  std::vector<unsigned char>, //color
156  unsigned int //size
157  >
158  > dataSets;
159 };
160 
161 inline void Canvas2D::setBoundsX(
162  double minX,
163  double maxX
164 ){
165  TBTKAssert(
166  minX <= maxX,
167  "Canvas2D::setBoundsX()",
168  "minX has to be smaller than maxX.",
169  ""
170  );
171 
172  if(minX == maxX){
173  this->minX = minX - 1e-10;
174  this->maxX = maxX + 1e-10;
175  }
176  else{
177  this->minX = minX;
178  this->maxX = maxX;
179  }
180 }
181 
182 inline void Canvas2D::setBoundsY(
183  double minY,
184  double maxY
185 ){
186  TBTKAssert(
187  minY <= maxY,
188  "Canvas2D::setBoundsY()",
189  "minY has to be smaller than maxY.",
190  ""
191  );
192 
193  if(minY == maxY){
194  this->minY = minY - 1e-10;
195  this->maxY = maxY + 1e-10;
196  }
197  else{
198  this->minY = minY;
199  this->maxY = maxY;
200  }
201 }
202 
203 inline void Canvas2D::setBounds(
204  double minX,
205  double maxX,
206  double minY,
207  double maxY
208 ){
209  setBoundsX(minX, maxX);
210  setBoundsY(minY, maxY);
211 }
212 
213 inline double Canvas2D::getMinX() const{
214  return minX;
215 }
216 
217 inline double Canvas2D::getMaxX() const{
218  return maxX;
219 }
220 
221 inline double Canvas2D::getMinY() const{
222  return minY;
223 }
224 
225 inline double Canvas2D::getMaxY() const{
226  return maxY;
227 }
228 
229 inline void Canvas2D::setLabelX(const std::string &labelX){
230  this->labelX = labelX;
231 }
232 
233 inline void Canvas2D::setLabelY(const std::string &labelY){
234  this->labelY = labelY;
235 }
236 
237 inline const std::string& Canvas2D::getLabelX() const{
238  return labelX;
239 }
240 
241 inline const std::string& Canvas2D::getLabelY() const{
242  return labelY;
243 }
244 
245 inline void Canvas2D::setHold(bool hold){
246  this->hold = hold;
247 }
248 
249 inline void Canvas2D::plot(
250  const std::vector<double> &y,
251  const std::string &title,
252  const std::vector<unsigned char> &color,
253  unsigned int size
254 ){
255  std::vector<double> x;
256  for(unsigned int n = 0; n < y.size(); n++)
257  x.push_back(n);
258 
259  plot(x, y, title, color, size);
260 }
261 
262 inline void Canvas2D::plot(
263  const std::vector<double> &x,
264  const std::vector<double> &y,
265  const std::string &title,
266  const std::vector<unsigned char> &color,
267  unsigned int size
268 ){
269  TBTKAssert(
270  x.size() == y.size(),
271  "Canvas2D::plot()",
272  "'x' and 'y' must have the same size, but x has size '"
273  << x.size() << "' while y has size'" << y.size() << "'.",
274  ""
275  );
276  TBTKAssert(
277  color.size() == 3,
278  "Canvas2D::plot()",
279  "The color must have three components but have '"
280  << color.size() << "'.",
281  ""
282  );
283 
284  if(!hold)
285  dataSets.clear();
286 
287  dataSets.push_back(std::make_tuple(x, y, title, color, size));
288 }
289 
290 inline void Canvas2D::clear(){
291  dataSets.clear();
292 }
293 
294 inline unsigned int Canvas2D::getNumDataSets() const{
295  return dataSets.size();
296 }
297 
298 inline const std::vector<double>& Canvas2D::getX(unsigned int dataSet) const{
299  return std::get<0>(dataSets[dataSet]);
300 }
301 
302 inline const std::vector<double>& Canvas2D::getY(unsigned int dataSet) const{
303  return std::get<1>(dataSets[dataSet]);
304 }
305 
306 inline const std::string& Canvas2D::getTitle(unsigned int dataSet) const{
307  return std::get<2>(dataSets[dataSet]);
308 }
309 
310 inline const std::vector<unsigned char>& Canvas2D::getColor(
311  unsigned int dataSet
312 ) const{
313  return std::get<3>(dataSets[dataSet]);
314 }
315 
316 inline unsigned int Canvas2D::getSize(unsigned int dataSet) const{
317  return std::get<4>(dataSets[dataSet]);
318 }
319 
320 }; //End namespace TBTK
321 
322 #endif
323 
Precompiler macros.
Definition: Boolean.h:32