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::COLOR_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::IMREAD_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::LINE_AA );
111  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, cv::LINE_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::LINE_AA );
115  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, cv::LINE_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::LINE_AA );
131  cv::putText ( background_, txt, w2m ( p0 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, cv::LINE_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::LINE_AA );
135  cv::putText ( background_, txt, w2m ( p1 ).cv(), cv::FONT_HERSHEY_PLAIN, 0.6, gray, 1, cv::LINE_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 }
tuw::Figure::blue_dark
static const cv::Scalar blue_dark
Definition: figure.h:192
tuw::Figure::orange
static const cv::Scalar orange
Definition: figure.h:193
tuw::WorldScopedMaps::height
int height() const
Definition: world_scoped_maps.cpp:102
tuw::Point2D::cv
const cv::Point_< double > & cv() const
Definition: point2d.cpp:135
tuw::Figure::grid_scale_y_
double grid_scale_y_
Definition: figure.h:23
tuw::Figure::white
static const cv::Scalar white
Definition: figure.h:200
tuw::Point2D::y
const double & y() const
Definition: point2d.cpp:49
tuw::Figure::niceDirtyPink
static const cv::Scalar niceDirtyPink
Definition: figure.h:215
tuw::WorldScopedMaps::line
void line(T &map, const Point2D &p0, const Point2D &p1, const cv::Scalar &color, int thickness=1, int lineType=cv::LINE_AA) const
Definition: world_scoped_maps.h:94
tuw::Figure::niceGreen
static const cv::Scalar niceGreen
Definition: figure.h:208
tuw::WorldScopedMaps::min_y
double min_y() const
Definition: world_scoped_maps.cpp:93
tuw::WorldScopedMaps::min_x
double min_x() const
Definition: world_scoped_maps.cpp:84
tuw::Figure::niceLime
static const cv::Scalar niceLime
Definition: figure.h:214
tuw::WorldScopedMaps::scale_y
double scale_y() const
Definition: world_scoped_maps.cpp:96
tuw::Figure::niceGreyPurple
static const cv::Scalar niceGreyPurple
Definition: figure.h:211
tuw::Figure::niceGreenBlue
static const cv::Scalar niceGreenBlue
Definition: figure.h:205
tuw::WorldScopedMaps::max_x
double max_x() const
Definition: world_scoped_maps.cpp:81
tuw::Figure::drawBackground
void drawBackground()
dimension of the drawn grid, if -1 no grid will be drawn
Definition: figure.cpp:87
tuw::Figure::niceBlue
static const cv::Scalar niceBlue
Definition: figure.h:202
tuw::Figure::label_format_y_
std::string label_format_y_
label format string
Definition: figure.h:19
figure.h
tuw::Figure::grid_scale_x_
double grid_scale_x_
if empty no file will be used
Definition: figure.h:23
tuw::Figure::niceGrey
static const cv::Scalar niceGrey
Definition: figure.h:209
tuw::Figure::niceRed
static const cv::Scalar niceRed
Definition: figure.h:206
tuw::Figure::background_filename_
std::string background_filename_
background data, grid or image
Definition: figure.h:22
tuw::Figure::title_
std::string title_
Definition: figure.h:17
tuw::Figure::clear
void clear()
Definition: figure.cpp:138
tuw::Figure::gray
static const cv::Scalar gray
Definition: figure.h:198
tuw::Figure::niceMagenta
static const cv::Scalar niceMagenta
Definition: figure.h:204
tuw::Figure::title
const std::string title() const
Definition: figure.cpp:156
test_point2d.p
p
Definition: test_point2d.py:20
tuw::Figure::Figure
Figure(const std::string &title)
draws the background image
Definition: figure.cpp:42
tuw::Figure::label_format_x_
std::string label_format_x_
window name
Definition: figure.h:18
tuw::Figure::putText
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::LINE_AA, bool bottomLeftOrigin=false)
Definition: figure.cpp:176
tuw::Figure::symbol
void symbol(cv::Mat &view, const Point2D &p, const cv::Scalar &color)
Definition: figure.cpp:150
tuw::Figure::yellow
static const cv::Scalar yellow
Definition: figure.h:194
tuw::Point2D::x
const double & x() const
Definition: point2d.cpp:35
tuw::Figure::cyan
static const cv::Scalar cyan
Definition: figure.h:195
tuw::Figure::appendToView
void appendToView(const cv::Mat &_mat, const cv::Scalar &_colMin, const cv::Scalar &_colMax, u_int8_t _truncateLayerVal=0)
Definition: figure.cpp:180
tuw::Figure::green_bright
static const cv::Scalar green_bright
Definition: figure.h:187
tuw::Pose2D
Definition: pose2d.h:17
tuw::Figure::line
void line(const Point2D &p0, const Point2D &p1, const cv::Scalar &color, int thickness=1, int lineType=cv::LINE_AA)
Definition: figure.cpp:143
tuw::Figure::blue
static const cv::Scalar blue
Definition: figure.h:190
tuw::Figure::niceGreenWashed
static const cv::Scalar niceGreenWashed
Definition: figure.h:212
tuw::WorldScopedMaps::width
int width() const
Definition: world_scoped_maps.cpp:99
tuw::Figure::background_
cv::Mat background_
canvas
Definition: figure.h:21
tuw::Figure::blue_bright
static const cv::Scalar blue_bright
Definition: figure.h:191
tuw::Figure::niceGreyLight
static const cv::Scalar niceGreyLight
Definition: figure.h:210
tuw
Definition: command.h:8
tuw::WorldScopedMaps::scale_x
double scale_x() const
Definition: world_scoped_maps.cpp:87
tuw::Figure::niceMustard
static const cv::Scalar niceMustard
Definition: figure.h:203
tuw::Figure::view
const cv::Mat & view() const
Definition: figure.cpp:55
tuw::WorldScopedMaps::max_y
double max_y() const
Definition: world_scoped_maps.cpp:90
tuw::Figure::black
static const cv::Scalar black
Definition: figure.h:199
tuw::Point2D
Definition: point2d.h:19
tuw::Figure::niceGreyDark
static const cv::Scalar niceGreyDark
Definition: figure.h:213
tuw::WorldScopedMaps::w2m
Point2D w2m(const Point2D &src) const
Definition: world_scoped_maps.cpp:61
tuw::Figure::niceRedDark
static const cv::Scalar niceRedDark
Definition: figure.h:207
tuw::Figure::backgroundFileName
const std::string & backgroundFileName() const
Definition: figure.cpp:52
tuw::Figure::setView
void setView(const cv::Mat &view)
Definition: figure.cpp:67
tuw::Figure::green_dark
static const cv::Scalar green_dark
Definition: figure.h:188
tuw::Point2D::set
Point2D & set(double x, double y)
Definition: point2d.cpp:17
tuw::WorldScopedMaps::initialized
bool initialized()
Definition: world_scoped_maps.cpp:20
tuw::Figure::background
const cv::Mat & background() const
Definition: figure.cpp:61
cv
Definition: point2d.h:208
tuw::Figure::view_
cv::Mat view_
label format string
Definition: figure.h:20
tuw::Figure::green
static const cv::Scalar green
color to use with the drawing functions
Definition: figure.h:186
tuw::Figure::setLabel
void setLabel(const std::string &label_x=std::string("x=%f"), const std::string &label_y=std::string("y=%f"))
Definition: figure.cpp:49
tuw::Figure::gray_bright
static const cv::Scalar gray_bright
Definition: figure.h:197
tuw::Figure::red
static const cv::Scalar red
Definition: figure.h:189
tuw::Figure::circle
void circle(const Point2D &p, int radius, const cv::Scalar &color, int thickness=1, int lineType=cv::LINE_AA)
Definition: figure.cpp:160
tuw::Figure::magenta
static const cv::Scalar magenta
Definition: figure.h:196
tuw::WorldScopedMaps::init
void init()
initializes the transformation matrices
Definition: world_scoped_maps.cpp:24
tuw::WorldScopedMaps::circle
void circle(T &map, const Point2D &p, int radius, const cv::Scalar &color, int thickness=1, int lineType=cv::LINE_AA) const
Definition: world_scoped_maps.h:107


tuw_geometry
Author(s): Markus Bader
autogenerated on Sun Feb 26 2023 03:25:40