figure.cpp
Go to the documentation of this file.
1 #include <cfloat>
2 #include <tgmath.h>
3 #include <boost/lexical_cast.hpp>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv2/imgproc/imgproc.hpp>
6 #include "tuw_geometry/figure.h"
7 
8 
9 using namespace tuw;
10 
11 const cv::Scalar Figure::green ( 0, 255, 0 );
12 const cv::Scalar Figure::green_bright ( 51, 255, 51 );
13 const cv::Scalar Figure::green_dark ( 0, 102, 0 );
14 const cv::Scalar Figure::red ( 0, 0, 255 );
15 const cv::Scalar Figure::blue ( 255, 0, 0 );
16 const cv::Scalar Figure::blue_bright ( 255, 51, 51 );
17 const cv::Scalar Figure::blue_dark ( 139, 0, 0 );
18 const cv::Scalar Figure::orange ( 0, 128, 255 );
19 const cv::Scalar Figure::yellow ( 0, 255, 255 );
20 const cv::Scalar Figure::cyan ( 255, 255, 0 );
21 const cv::Scalar Figure::magenta ( 255, 0, 255 );
22 const cv::Scalar Figure::gray_bright ( 224, 224, 224 );
23 const cv::Scalar Figure::gray ( 128, 128, 128 );
24 const cv::Scalar Figure::black ( 0, 0, 0 );
25 const cv::Scalar Figure::white ( 255, 255, 255 );
26 
27 const cv::Scalar Figure::niceBlue (155, 100, 59);
28 const cv::Scalar Figure::niceMustard ( 28, 174, 184);
29 const cv::Scalar Figure::niceMagenta (147, 32, 220);
30 const cv::Scalar Figure::niceGreenBlue (181, 196, 36);
31 const cv::Scalar Figure::niceRed ( 82, 77, 204);
32 const cv::Scalar Figure::niceRedDark ( 52, 48, 158);
33 const cv::Scalar Figure::niceGreen ( 55, 142, 84);
34 const cv::Scalar Figure::niceGrey (132, 109, 106);
35 const cv::Scalar Figure::niceGreyLight (179, 165, 153);
36 const cv::Scalar Figure::niceGreyPurple (155, 135, 149);
37 const cv::Scalar Figure::niceGreenWashed (151, 166, 125);
38 const cv::Scalar Figure::niceGreyDark ( 85, 79, 74);
39 const cv::Scalar Figure::niceLime ( 23, 176, 154);
40 const cv::Scalar Figure::niceDirtyPink ( 78, 0, 120);
41 
42 Figure::Figure ( const std::string &title )
43  : title_ ( title )
44  , label_format_x_ ( "x=%f" )
45  , label_format_y_ ( "y=%f" ) {
46 
47 }
48 
49 void Figure::setLabel ( const std::string &label_format_x, const std::string &label_format_y ) {
50  label_format_x_ = label_format_x, label_format_y_ = label_format_y;
51 }
52 const std::string& Figure::backgroundFileName() const {
53  return background_filename_;
54 }
55 const cv::Mat& Figure::view() const {
56  return view_;
57 }
58 cv::Mat& Figure::view() {
59  return view_;
60 }
61 const cv::Mat& Figure::background() const {
62  return background_;
63 }
64 cv::Mat& Figure::background() {
65  return background_;
66 }
67 void Figure::setView ( const cv::Mat& view ) {
68  if ( view.empty() ) return;
69  view_.create ( view.cols, view.rows, CV_8UC3 );
70  int depth = view.depth(), cn = view.channels();
71  int type = CV_MAKETYPE ( depth, cn );
72  if ( type == CV_8UC3 ) {
73  view.copyTo ( view_ );
74  } else if ( ( view.channels() == 1 ) && ( view.depth() == CV_8U ) ) {
75  cv::cvtColor ( view, view_, CV_GRAY2BGR );
76  }
77 }
78 
79 void Figure::init ( int width_pixel, int height_pixel, double min_x, double max_x, double min_y, double max_y, double rotation, double grid_scale_y, double grid_scale_x, const std::string &background_image ) {
80  WorldScopedMaps::init(width_pixel, height_pixel, min_x, max_x, min_y, max_y, rotation);
81  grid_scale_x_ = grid_scale_x, grid_scale_y_ = grid_scale_y;
82  background_filename_ = background_image;
84  clear();
85 }
86 
88 
89  background_.create ( height(), width(), CV_8UC3 );
90  if ( !background_filename_.empty() ) {
91  cv::Mat image = cv::imread ( background_filename_, CV_LOAD_IMAGE_COLOR );
92  cv::resize ( image, background_, cv::Size ( background_.cols, background_.rows ), cv::INTER_AREA );
93  } else {
94  background_.setTo ( 0xFF );
95  }
96  if ( ( grid_scale_y_ > 0 ) && ( grid_scale_x_ > 0 ) ) {
97  char txt[0xFF];
98  Point2D p0, p1, pm0, pm1;
101  for ( p0.y() = min_y; p0.y() <= max_y; p0.y() +=grid_scale_y_ ) {
102  p0.x() = round ( max_x() /grid_scale_x_ ) * grid_scale_x_;
103  p1.y() = p0.y();
104  p1.x() = round ( min_x() /grid_scale_x_ ) * grid_scale_x_;
105  if ( fabs ( p0.y() ) > FLT_MIN ) WorldScopedMaps::line ( background_, p0, p1, gray_bright, 1, 8 );
106  else WorldScopedMaps::line ( background_, p0, p1, gray, 1, 8 );
107  }
108  p0.set ( 0, max_y - grid_scale_y_/2.0 );
109  sprintf ( txt, label_format_y_.c_str(), max_y );
110  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, white, 3, CV_AA );
111  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, CV_AA );
112  p1.set ( 0, min_y + grid_scale_y_/2.0 );
113  sprintf ( txt, label_format_y_.c_str(), min_y );
114  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, white, 3, CV_AA );
115  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, CV_AA );
116 
119  for ( p0.x() = min_x; p0.x() <= max_x; p0.x() +=grid_scale_x_ ) {
120  p0.y() = round ( WorldScopedMaps::max_y() /grid_scale_y_ ) * grid_scale_y_;
121  p1.x() = p0.x();
122  p1.y() = round ( WorldScopedMaps::min_y() /grid_scale_y_ ) * grid_scale_y_;
123  pm0 = w2m ( p0 );
124  pm1 = w2m ( p1 );
125  if ( fabs ( p0.x() ) > FLT_MIN ) WorldScopedMaps::line ( background_, p0, p1, gray_bright, 1, 8 );
126  else WorldScopedMaps::line ( background_, p0, p1, gray, 1, 8 );
127  }
128  p0.set ( max_x - grid_scale_x_/2.0, 0 );
129  sprintf ( txt, label_format_x_.c_str(), max_x );
130  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, white, 3, CV_AA );
131  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, CV_AA );
132  p1.set ( min_x + grid_scale_x_/2.0, 0 );
133  sprintf ( txt, label_format_x_.c_str(), min_x );
134  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, white, 3, CV_AA );
135  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, CV_AA );
136  }
137 }
138 void Figure::clear () {
139  view_.create ( background_.cols, background_.rows, CV_8UC3 );
140  background_.copyTo ( view_ );
141 }
142 
143 void Figure::line ( const Point2D &p0, const Point2D &p1, const cv::Scalar &color, int thickness, int lineType ) {
144  WorldScopedMaps::line ( view_, p0, p1, color, thickness, lineType );
145 }
146 
147 void Figure::symbol ( const Point2D &p, const cv::Scalar &color ) {
148  symbol ( view_, p, color );
149 }
150 void Figure::symbol ( cv::Mat &view, const Point2D &p, const cv::Scalar &color ) {
151  cv::Point pi = w2m ( p ).cv();
152  if ( ( pi.x < 0 ) || ( pi.x >= view.cols ) || ( pi.y < 0 ) || ( pi.y >= view.rows ) ) return;
153  cv::Vec3b &pixel = view.at<cv::Vec3b> ( pi );
154  pixel[0] = color[0], pixel[1] = color[1], pixel[2] = color[2];
155 }
156 const std::string Figure::title() const {
157  return title_;
158 }
159 
160 void Figure::circle ( const Point2D &p, int radius, const cv::Scalar &color, int thickness, int lineType ) {
161  WorldScopedMaps::circle ( view_, p, radius, color, thickness, lineType );
162 }
163 
164 void Figure::symbol ( const Pose2D &p, double radius, const cv::Scalar &color, int thickness, int lineType ) {
165  symbol ( view_, p, radius, color, thickness, lineType );
166 }
167 
168 void Figure::symbol ( cv::Mat &view, const Pose2D &p, double radius, const cv::Scalar &color, int thickness, int lineType ) {
169  circle ( p.position(), radius * (scale_x() + scale_y())/2., color, thickness, lineType );
170  line ( p.position(), p.point_ahead ( radius ), color, thickness, lineType );
171 
172 }
173 void Figure::putText ( const std::string& text, const Point2D &p, int fontFace, double fontScale, cv::Scalar color, int thickness, int lineType, bool bottomLeftOrigin ) {
174  putText ( view_, text, p, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin );
175 }
176 void Figure::putText ( cv::Mat &view, const std::string& text, const Point2D &p, int fontFace, double fontScale, cv::Scalar color, int thickness, int lineType, bool bottomLeftOrigin ) {
177  cv::putText ( view, text, w2m ( p ).cv(), fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin );
178 }
179 
180 void Figure::appendToView ( const cv::Mat& _mat, const cv::Scalar& _colMin, const cv::Scalar& _colMax, u_int8_t _truncateLayerVal ) {
181  if( view().empty() || _mat.empty() || !initialized() ) { return; }
182 
183  CV_Assert ( _mat.depth() == CV_8U );
184  int channels = _mat.channels();
185  int nRows = _mat.rows;
186  int nCols = _mat.cols * channels;
187 
188  uint8_t const* p_s; cv::Vec3b* p_d;
189  for(int i = 0; i < nRows; ++i) {
190  for (p_s = _mat.ptr<const uint8_t>(i), p_d =view().ptr<cv::Vec3b>(i); p_s != _mat.ptr<uint8_t>(i+1); p_d++, p_s++){
191  if ( (*p_d == cv::Vec3b(255,255,255) ) && (*p_s < 255 - _truncateLayerVal ) ) {
192  double scale = *p_s / (255. - (double)_truncateLayerVal);
193  *p_d = cv::Vec3b( _colMin[0] + scale * (_colMax[0] - _colMin[0]),
194  _colMin[1] + scale * (_colMax[1] - _colMin[1]),
195  _colMin[2] + scale * (_colMax[2] - _colMin[2]) );
196  }
197  }
198  }
199 }
void circle(T &map, const Point2D &p, int radius, const cv::Scalar &color, int thickness=1, int lineType=CV_AA) const
const double & y() const
Definition: point2d.cpp:49
static const cv::Scalar green_bright
Definition: figure.h:188
static const cv::Scalar niceGreyLight
Definition: figure.h:211
double grid_scale_x_
if empty no file will be used
Definition: figure.h:24
static const cv::Scalar niceRedDark
Definition: figure.h:208
static const cv::Scalar black
Definition: figure.h:200
void setView(const cv::Mat &view)
Definition: figure.cpp:67
static const cv::Scalar gray_bright
Definition: figure.h:198
static const cv::Scalar niceGreyDark
Definition: figure.h:214
cv::Mat background_
canvas
Definition: figure.h:22
static const cv::Scalar blue
Definition: figure.h:191
void init()
initializes the transformation matrices
cv::Mat view_
label format string
Definition: figure.h:21
Point2D point_ahead(double d=1.) const
Definition: pose2d.cpp:54
void line(T &map, const Point2D &p0, const Point2D &p1, const cv::Scalar &color, int thickness=1, int lineType=CV_AA) const
void symbol(cv::Mat &view, const Point2D &p, const cv::Scalar &color)
Definition: figure.cpp:150
static const cv::Scalar niceBlue
Definition: figure.h:203
void appendToView(const cv::Mat &_mat, const cv::Scalar &_colMin, const cv::Scalar &_colMax, u_int8_t _truncateLayerVal=0)
Definition: figure.cpp:180
const std::string & backgroundFileName() const
Definition: figure.cpp:52
Point2D w2m(const Point2D &src) const
std::string background_filename_
background data, grid or image
Definition: figure.h:23
Definition: point2d.h:208
static const cv::Scalar green_dark
Definition: figure.h:189
const cv::Mat & background() const
Definition: figure.cpp:61
static const cv::Scalar orange
Definition: figure.h:194
const Point2D & position() const
Definition: pose2d.cpp:47
static const cv::Scalar niceGrey
Definition: figure.h:210
void circle(const Point2D &p, int radius, const cv::Scalar &color, int thickness=1, int lineType=CV_AA)
Definition: figure.cpp:160
static const cv::Scalar blue_dark
Definition: figure.h:193
Definition: command.h:8
static const cv::Scalar white
Definition: figure.h:201
void line(const Point2D &p0, const Point2D &p1, const cv::Scalar &color, int thickness=1, int lineType=CV_AA)
Definition: figure.cpp:143
void clear()
Definition: figure.cpp:138
Figure(const std::string &title)
draws the background image
Definition: figure.cpp:42
void putText(cv::Mat &view, const std::string &text, const Point2D &p, int fontFace=cv::FONT_HERSHEY_PLAIN, double fontScale=0.6, cv::Scalar color=cv::Scalar(128, 0, 0), int thickness=1, int lineType=CV_AA, bool bottomLeftOrigin=false)
Definition: figure.cpp:176
static const cv::Scalar red
Definition: figure.h:190
static const cv::Scalar green
color to use with the drawing functions
Definition: figure.h:187
void setLabel(const std::string &label_x=std::string("x=%f"), const std::string &label_y=std::string("y=%f"))
Definition: figure.cpp:49
static const cv::Scalar niceRed
Definition: figure.h:207
static const cv::Scalar niceLime
Definition: figure.h:215
static const cv::Scalar gray
Definition: figure.h:199
static const cv::Scalar niceDirtyPink
Definition: figure.h:216
std::string label_format_x_
window name
Definition: figure.h:19
static const cv::Scalar niceMagenta
Definition: figure.h:205
const double & x() const
Definition: point2d.cpp:35
static const cv::Scalar niceGreyPurple
Definition: figure.h:212
static const cv::Scalar niceGreen
Definition: figure.h:209
static const cv::Scalar niceGreenBlue
Definition: figure.h:206
std::string title_
Definition: figure.h:18
static const cv::Scalar yellow
Definition: figure.h:195
std::string label_format_y_
label format string
Definition: figure.h:20
static const cv::Scalar niceMustard
Definition: figure.h:204
Point2D & set(double x, double y)
Definition: point2d.cpp:17
static const cv::Scalar blue_bright
Definition: figure.h:192
void drawBackground()
dimension of the drawn grid, if -1 no grid will be drawn
Definition: figure.cpp:87
static const cv::Scalar magenta
Definition: figure.h:197
const std::string title() const
Definition: figure.cpp:156
static const cv::Scalar niceGreenWashed
Definition: figure.h:213
const cv::Point_< double > & cv() const
Definition: point2d.cpp:135
const cv::Mat & view() const
Definition: figure.cpp:55
double grid_scale_y_
Definition: figure.h:24
static const cv::Scalar cyan
Definition: figure.h:196


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:08