TBTK
Need a break? Support the development by playing Polarity Puzzles
Canvas3D.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_3D
25 #define COM_DAFER45_TBTK_CANVAS_3D
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 Canvas3D : public Canvas{
36 public:
38  Canvas3D();
39 
41  virtual ~Canvas3D();
42 
44  void setBoundsX(double minX, double maxX);
45 
47  void setBoundsY(double minY, double maxY);
48 
50  void setBoundsZ(double minZ, double maxZ);
51 
53  void setBounds(
54  double minX,
55  double maxX,
56  double minY,
57  double maxY,
58  double minZ,
59  double maxZ
60  );
61 
63  double getMinX() const;
64 
66  double getMaxX() const;
67 
69  double getMinY() const;
70 
72  double getMaxY() const;
73 
75  double getMinZ() const;
76 
78  double getMaxZ() const;
79 
81  void setLabelX(const std::string &labelX);
82 
84  void setLabelY(const std::string &labelY);
85 
87  void setLabelZ(const std::string &labelZ);
88 
90  const std::string& getLabelX() const;
91 
93  const std::string& getLabelY() const;
94 
96  const std::string& getLabelZ() const;
97 
99  void setHold(bool hold);
100 
102  void setTopView(bool topView);
103 
105  bool getTopView() const;
106 
108  void plot(
109  const std::vector<std::vector<double>> &z,
110  const std::string &title = "",
111  const std::vector<unsigned char> &color = {0, 0, 0}
112  );
113 
115  void plot(
116  const std::vector<double> &x,
117  const std::vector<double> &y,
118  const std::vector<std::vector<double>> &values,
119  const std::string &title = "",
120  const std::vector<unsigned char> &color = {0, 0, 0}
121  );
122 
124  virtual void clear();
125 protected:
129  unsigned int getNumDataSets() const;
130 
131  /*** Get the x values for the given data set.
132  *
133  * @param dataSet The data set to get the x-values for.
134  *
135  * @return The x-values for the given data set. */
136  const std::vector<double>& getX(unsigned int dataSet) const;
137 
138  /*** Get the y values for the given data set.
139  *
140  * @param dataSet The data set to get the y-values for.
141  *
142  * @return The y-values for the given data set. */
143  const std::vector<double>& getY(unsigned int dataSet) const;
144 
150  const std::vector<std::vector<double>>& getZ(
151  unsigned int dataSet
152  ) const;
153 
154  /*** Get the title for the given data set.
155  *
156  * @param dataSet The data set to get the title for.
157  *
158  * @return The title for the given data set. */
159  const std::string& getTitle(unsigned int dataSet) const;
160 
166  const std::vector<unsigned char>& getColor(unsigned int dataSet) const;
167 private:
169  double minX, maxX, minY, maxY, minZ, maxZ;
170 
172  std::string labelX, labelY, labelZ;
173 
176  bool hold;
177 
179  bool topView;
180 
182  std::vector<
183  std::tuple<
184  std::vector<double>, //x-values
185  std::vector<double>, //y-values
186  std::vector<std::vector<double>>, //z-values
187  std::string, //title
188  std::vector<unsigned char> //color
189  >
190  > dataSets;
191 };
192 
193 inline void Canvas3D::setBoundsX(
194  double minX,
195  double maxX
196 ){
197  TBTKAssert(
198  minX <= maxX,
199  "Canvas3D::setBoundsX()",
200  "minX has to be smaller than maxX.",
201  ""
202  );
203 
204  if(minX == maxX){
205  this->minX = minX - 1e-10;
206  this->maxX = maxX + 1e-10;
207  }
208  else{
209  this->minX = minX;
210  this->maxX = maxX;
211  }
212 }
213 
214 inline void Canvas3D::setBoundsY(
215  double minY,
216  double maxY
217 ){
218  TBTKAssert(
219  minY <= maxY,
220  "Canvas3D::setBoundsY()",
221  "minY has to be smaller than maxY.",
222  ""
223  );
224 
225  if(minY == maxY){
226  this->minY = minY - 1e-10;
227  this->maxY = maxY + 1e-10;
228  }
229  else{
230  this->minY = minY;
231  this->maxY = maxY;
232  }
233 }
234 
235 inline void Canvas3D::setBoundsZ(
236  double minZ,
237  double maxZ
238 ){
239  TBTKAssert(
240  minZ <= maxZ,
241  "Canvas3D::setBoundsZ()",
242  "minZ has to be smaller than maxZ.",
243  ""
244  );
245 
246  if(minZ == maxZ){
247  this->minZ = minZ - 1e-10;
248  this->maxZ = maxZ + 1e-10;
249  }
250  else{
251  this->minZ = minZ;
252  this->maxZ = maxZ;
253  }
254 }
255 
256 inline void Canvas3D::setBounds(
257  double minX,
258  double maxX,
259  double minY,
260  double maxY,
261  double minZ,
262  double maxZ
263 ){
264  setBoundsX(minX, maxX);
265  setBoundsY(minY, maxY);
266  setBoundsY(minZ, maxZ);
267 }
268 
269 inline double Canvas3D::getMinX() const{
270  return minX;
271 }
272 
273 inline double Canvas3D::getMaxX() const{
274  return maxX;
275 }
276 
277 inline double Canvas3D::getMinY() const{
278  return minY;
279 }
280 
281 inline double Canvas3D::getMaxY() const{
282  return maxY;
283 }
284 
285 inline double Canvas3D::getMinZ() const{
286  return minZ;
287 }
288 
289 inline double Canvas3D::getMaxZ() const{
290  return maxZ;
291 }
292 
293 inline void Canvas3D::setLabelX(const std::string &labelX){
294  this->labelX = labelX;
295 }
296 
297 inline void Canvas3D::setLabelY(const std::string &labelY){
298  this->labelY = labelY;
299 }
300 
301 inline void Canvas3D::setLabelZ(const std::string &labelZ){
302  this->labelZ = labelZ;
303 }
304 
305 inline const std::string& Canvas3D::getLabelX() const{
306  return labelX;
307 }
308 
309 inline const std::string& Canvas3D::getLabelY() const{
310  return labelY;
311 }
312 
313 inline const std::string& Canvas3D::getLabelZ() const{
314  return labelZ;
315 }
316 
317 inline void Canvas3D::setHold(bool hold){
318  this->hold = hold;
319 }
320 
321 inline void Canvas3D::setTopView(bool topView){
322  this->topView = topView;
323 }
324 
325 inline bool Canvas3D::getTopView() const{
326  return topView;
327 }
328 
329 inline void Canvas3D::plot(
330  const std::vector<std::vector<double>> &z,
331  const std::string &title,
332  const std::vector<unsigned char> &color
333 ){
334  TBTKAssert(
335  z.size() > 0,
336  "Canvas3D::plot()",
337  "z is empty.",
338  ""
339  );
340 
341  std::vector<double> x;
342  for(unsigned int n = 0; n < z.size(); n++)
343  x.push_back(n);
344 
345  std::vector<double> y;
346  for(unsigned int n = 0; n < z[0].size(); n++)
347  y.push_back(n);
348 
349  plot(x, y, z, title, color);
350 }
351 
352 inline void Canvas3D::plot(
353  const std::vector<double> &x,
354  const std::vector<double> &y,
355  const std::vector<std::vector<double>> &z,
356  const std::string &title,
357  const std::vector<unsigned char> &color
358 ){
359  TBTKAssert(
360  x.size() == z.size(),
361  "Canvas3D::plot()",
362  "'x' and 'z' must have the same size, but z has size '"
363  << x.size() << "' while z has size'" << z.size() << "'.",
364  ""
365  );
366  for(unsigned int n = 0; n < z.size(); n++){
367  TBTKAssert(
368  y.size() == z[n].size(),
369  "Canvas3D::plot()",
370  "'y' and 'z[ << n << ]' must have the same size, but y"
371  << " has size'" << y.size() << "' while z[" << n
372  << "]' has size '" << z[n].size() << "'.",
373  ""
374  );
375  }
376  TBTKAssert(
377  color.size() == 3,
378  "Canvas3D::plot()",
379  "The color must have three components but have '"
380  << color.size() << "'.",
381  ""
382  );
383 
384  if(!hold)
385  dataSets.clear();
386 
387  dataSets.push_back(std::make_tuple(x, y, z, title, color));
388 }
389 
390 inline void Canvas3D::clear(){
391  dataSets.clear();
392 }
393 
394 inline unsigned int Canvas3D::getNumDataSets() const{
395  return dataSets.size();
396 }
397 
398 inline const std::vector<double>& Canvas3D::getX(unsigned int dataSet) const{
399  return std::get<0>(dataSets[dataSet]);
400 }
401 
402 inline const std::vector<double>& Canvas3D::getY(unsigned int dataSet) const{
403  return std::get<1>(dataSets[dataSet]);
404 }
405 
406 inline const std::vector<std::vector<double>>& Canvas3D::getZ(
407  unsigned int dataSet
408 ) const{
409  return std::get<2>(dataSets[dataSet]);
410 }
411 
412 inline const std::string& Canvas3D::getTitle(unsigned int dataSet) const{
413  return std::get<3>(dataSets[dataSet]);
414 }
415 
416 inline const std::vector<unsigned char>& Canvas3D::getColor(
417  unsigned int dataSet
418 ) const{
419  return std::get<4>(dataSets[dataSet]);
420 }
421 
422 }; //End namespace TBTK
423 
424 #endif
425 
Precompiler macros.
Definition: Boolean.h:32