TBTK
PlotCanvas.h
1 /* Copyright 2017 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 
23 #ifndef COM_DAFER45_TBTK_CANVAS
24 #define COM_DAFER45_TBTK_CANVAS
25 
26 #include "TBTK/TBTKMacros.h"
27 
28 #include <string>
29 
30 #include <opencv2/core/core.hpp>
31 #include <opencv2/highgui/highgui.hpp>
32 #include <opencv2/imgproc/imgproc.hpp>
33 
34 namespace TBTK{
35 namespace Plot{
36 
37 class PlotCanvas{
38 public:
40  PlotCanvas();
41 
43  ~PlotCanvas();
44 
46  void setWidth(unsigned int width);
47 
49  void setHeight(unsigned int height);
50 
52  void setPadding(
53  double paddingLeft,
54  double paddingRight,
55  double paddingBottom,
56  double paddingTop
57  );
58 
60  void setBoundsX(double minX, double maxX);
61 
63  void setBoundsY(double minY, double maxY);
64 
66  void setBounds(double minX, double maxX, double minY, double maxY);
67 
69  double getMinX() const;
70 
72  double getMaxX() const;
73 
75  double getMinY() const;
76 
78  double getMaxY() const;
79 
81  void setLabelX(const std::string &labelX);
82 
84  void setLabelY(const std::string &labelY);
85 
87  void setShowColorBox(bool showColorBox);
88 
90  bool getShowColorBox() const;
91 
93  void setBoundsColor(double minColor, double maxColor);
94 
96  void setCanvas(cv::Mat &canvas);
97 
99  const cv::Mat& getCanvas() const;
100 
102  void clear();
103 
105  void setPixel(
106  unsigned int x,
107  unsigned int y,
108  unsigned char red,
109  unsigned char green,
110  unsigned char blue
111  );
112 
114  void drawLine(
115  double x0,
116  double y0,
117  double x1,
118  double y1,
119  const std::vector<unsigned char> &color,
120  unsigned int width
121  );
122 
124  void drawCircle(
125  double x,
126  double y,
127  unsigned int size,
128  const std::vector<unsigned char> &color
129  );
130 
132  void save(std::string filename) const;
133 private:
135  cv::Mat canvas;
136 
138  double width, height;
139 
141  unsigned int paddingLeft, paddingRight, paddingBottom, paddingTop;
142 
144  double minX, maxX, minY, maxY;
145 
147  std::string labelX, labelY;
148 
150  bool showColorBox;
151 
153  double minColor, maxColor;
154 
156  static constexpr unsigned int COLOR_BOX_WINDOW_WIDTH = 100;
157 
159  void drawColorBox();
160 public:
163  cv::Point getCVPoint(double x, double y) const;
164 
166  void drawAxes();
167 };
168 
169 inline void PlotCanvas::setWidth(unsigned int width){
170  this->width = width;
171 }
172 
173 inline void PlotCanvas::setHeight(unsigned int height){
174  this->height = height;
175 }
176 
178  double paddingLeft,
179  double paddingRight,
180  double paddingBottom,
181  double paddingTop
182 ){
183  this->paddingLeft = paddingLeft;
184  this->paddingRight = paddingRight;
185  this->paddingBottom = paddingBottom;
186  this->paddingTop = paddingTop;
187 }
188 
190  double minX,
191  double maxX
192 ){
193  TBTKAssert(
194  minX <= maxX,
195  "Canvas::setBoundsX()",
196  "minX has to be smaller than maxX.",
197  ""
198  );
199 
200  if(minX == maxX){
201  this->minX = minX - 1e-10;
202  this->maxX = maxX + 1e-10;
203  }
204  else{
205  this->minX = minX;
206  this->maxX = maxX;
207  }
208 }
209 
211  double minY,
212  double maxY
213 ){
214  TBTKAssert(
215  minY <= maxY,
216  "Canvas::setBoundsY()",
217  "minY has to be smaller than maxY.",
218  ""
219  );
220 
221  if(minY == maxY){
222  this->minY = minY - 1e-10;
223  this->maxY = maxY + 1e-10;
224  }
225  else{
226  this->minY = minY;
227  this->maxY = maxY;
228  }
229 }
230 
232  double minX,
233  double maxX,
234  double minY,
235  double maxY
236 ){
237  setBoundsX(minX, maxX);
238  setBoundsY(minY, maxY);
239 }
240 
241 inline double PlotCanvas::getMinX() const{
242  return minX;
243 }
244 
245 inline double PlotCanvas::getMaxX() const{
246  return maxX;
247 }
248 
249 inline double PlotCanvas::getMinY() const{
250  return minY;
251 }
252 
253 inline double PlotCanvas::getMaxY() const{
254  return maxY;
255 }
256 
257 inline void PlotCanvas::setLabelX(const std::string &labelX){
258  this->labelX = labelX;
259 }
260 
261 inline void PlotCanvas::setLabelY(const std::string &labelY){
262  this->labelY = labelY;
263 }
264 
265 inline void PlotCanvas::setShowColorBox(bool showColorBox){
266  this->showColorBox = showColorBox;
267 }
268 
269 inline bool PlotCanvas::getShowColorBox() const{
270  return showColorBox;
271 }
272 
273 inline void PlotCanvas::setBoundsColor(double minColor, double maxColor){
274  this->minColor = minColor;
275  this->maxColor = maxColor;
276 }
277 
278 inline void PlotCanvas::setCanvas(cv::Mat &canvas){
279  this->canvas = canvas;
280 }
281 
282 inline const cv::Mat& PlotCanvas::getCanvas() const{
283  return canvas;
284 }
285 
286 inline cv::Point PlotCanvas::getCVPoint(double x, double y) const{
287  double width = maxX - minX;
288  double height = maxY - minY;
289  return cv::Point(
290  paddingLeft + (1 - (paddingLeft + paddingRight + showColorBox*COLOR_BOX_WINDOW_WIDTH)/(double)canvas.cols)*canvas.cols*(x - minX)/(double)width,
291  canvas.rows - 1 - (paddingBottom + (1 - (paddingBottom + paddingTop)/(double)canvas.rows)*canvas.rows*(y - minY)/(double)height)
292  );
293 }
294 
295 inline void PlotCanvas::clear(){
296  canvas = cv::Mat::zeros(height, width, CV_8UC3);
297  cv::rectangle(
298  canvas,
299  cvPoint(0, 0),
300  cvPoint(width-1, height-1),
301  cv::Scalar(255, 255, 255),
302  CV_FILLED,
303  8,
304  0
305  );
306 }
307 
309  unsigned int x,
310  unsigned int y,
311  unsigned char red,
312  unsigned char green,
313  unsigned char blue
314 ){
315  canvas.at<cv::Vec3b>(y, x)[0] = blue;
316  canvas.at<cv::Vec3b>(y, x)[1] = green;
317  canvas.at<cv::Vec3b>(y, x)[2] = red;
318 }
319 
321  double x0,
322  double y0,
323  double x1,
324  double y1,
325  const std::vector<unsigned char> &color,
326  unsigned int width
327 ){
328  //Clip lines
329  if(x1 < x0){
330  double temp = x0;
331  x0 = x1;
332  x1 = temp;
333  temp = y0;
334  y0 = y1;
335  y1 = temp;
336  }
337  if(x0 < minX && x1 < minX)
338  return;
339  if(x0 > maxX && x1 > maxX)
340  return;
341  if(x0 < minX){
342  if(x1 - x0 != 0)
343  y0 += (minX - x0)*(y1 - y0)/(x1 - x0);
344  x0 = minX;
345  }
346  if(x1 > maxX){
347  if(x1 - x0 != 0)
348  y1 -= (x1 - maxX)*(y1 - y0)/(x1 - x0);
349  x1 = maxX;
350  }
351  if(y0 < minY && y1 < minY)
352  return;
353  if(y0 > maxY && y1 > maxY)
354  return;
355  if(y0 < minY){
356  if(y1 - y0 != 0)
357  x0 += (minY - y0)*(x1 - x0)/(y1 - y0);
358  y0 = minY;
359  }
360  if(y1 > maxY){
361  if(y1 - y0 != 0)
362  x1 -= (y1 - maxY)*(x1 - x0)/(y1 - y0);
363  y1 = maxY;
364  }
365 
366  //Draw line
367  cv::line(
368  canvas,
369  getCVPoint(x0, y0),
370  getCVPoint(x1, y1),
371  cv::Scalar(color[2], color[1], color[0]),
372  width,
373  CV_AA
374  );
375 }
376 
378  double x,
379  double y,
380  unsigned int size,
381  const std::vector<unsigned char> &color
382 ){
383  //Clip point
384  if(x < minX)
385  return;
386  if(x > maxX)
387  return;
388  if(y < minY)
389  return;
390  if(y > maxY)
391  return;
392 
393  //Draw cirlce
394  cv::circle(
395  canvas,
396  getCVPoint(x, y),
397  size,
398  cv::Scalar(color[2], color[1], color[0]),
399  -1,
400  CV_AA
401  );
402 }
403 
404 inline void PlotCanvas::save(std::string filename) const{
405  imwrite(filename, canvas);
406 }
407 
408 }; //End namespace Plot
409 }; //End namespace TBTK
410 
411 #endif
void setLabelY(const std::string &labelY)
Definition: PlotCanvas.h:261
void clear()
Definition: PlotCanvas.h:295
const cv::Mat & getCanvas() const
Definition: PlotCanvas.h:282
void setShowColorBox(bool showColorBox)
Definition: PlotCanvas.h:265
void drawCircle(double x, double y, unsigned int size, const std::vector< unsigned char > &color)
Definition: PlotCanvas.h:377
Precompiler macros.
void setPadding(double paddingLeft, double paddingRight, double paddingBottom, double paddingTop)
Definition: PlotCanvas.h:177
void setBoundsX(double minX, double maxX)
Definition: PlotCanvas.h:189
double getMinY() const
Definition: PlotCanvas.h:249
void setBoundsY(double minY, double maxY)
Definition: PlotCanvas.h:210
Definition: PlotCanvas.h:37
void setWidth(unsigned int width)
Definition: PlotCanvas.h:169
cv::Point getCVPoint(double x, double y) const
Definition: PlotCanvas.h:286
void setBoundsColor(double minColor, double maxColor)
Definition: PlotCanvas.h:273
double getMinX() const
Definition: PlotCanvas.h:241
bool getShowColorBox() const
Definition: PlotCanvas.h:269
void setHeight(unsigned int height)
Definition: PlotCanvas.h:173
void setBounds(double minX, double maxX, double minY, double maxY)
Definition: PlotCanvas.h:231
double getMaxY() const
Definition: PlotCanvas.h:253
void setLabelX(const std::string &labelX)
Definition: PlotCanvas.h:257
double getMaxX() const
Definition: PlotCanvas.h:245
Definition: ModelFactory.h:35
void setCanvas(cv::Mat &canvas)
Definition: PlotCanvas.h:278
void setPixel(unsigned int x, unsigned int y, unsigned char red, unsigned char green, unsigned char blue)
Definition: PlotCanvas.h:308
void drawLine(double x0, double y0, double x1, double y1, const std::vector< unsigned char > &color, unsigned int width)
Definition: PlotCanvas.h:320
void save(std::string filename) const
Definition: PlotCanvas.h:404