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