60 #include <visp3/core/vpImageDraw.h> 61 #include <visp3/core/vpPoint.h> 62 #include <visp3/core/vpMeterPixelConversion.h> 66 template<
class Type>
void DrawLine(
vpImage<Type> & canvas,
int x1,
int y1,
int x2,
int y2,
const Type & color,
unsigned int width = 1)
68 const int w =
static_cast<int>(canvas.
getWidth()) - 1;
69 const int h =
static_cast<int>(canvas.
getHeight()) - 1;
71 if (x1 < 0 || y1 < 0 || (x1 - w) > 0 || (y1 - h) > 0 || x2 < 0 || y2 < 0 || (x2 - w) > 0 || (y2 - h) > 0) {
72 if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0) || ((x1 - w) > 0 && (x2 - w) > 0) || ((y1 - h) > 0 && (y2 -h) > 0)) {
77 x1 = std::min<int>(std::max<int>(x1, 0), w);
78 x2 = std::min<int>(std::max<int>(x2, 0), w);
79 }
else if (x1 == x2) {
80 y1 = std::min<int>(std::max<int>(y1, 0), h);
81 y2 = std::min<int>(std::max<int>(y2, 0), h);
83 int x0 = (x1*y2 - y1*x2) / (y2 - y1);
84 int y0 = (y1*x2 - x1*y2) / (x2 - x1);
85 int xh = (x1*y2 - y1*x2 + h*(x2 - x1)) / (y2 - y1);
86 int yw = (y1*x2 - x1*y2 + w*(y2 - y1)) / (x2 - x1);
104 if ((y1 < 0 && y2 < 0) || (y1 > h && y2 > h)) {
128 const bool inverse = ((std::abs(y2 - y1) - std::abs(x2 - x1)) > 0);
139 const double dx = x2 - x1;
140 const double dy =
static_cast<double>(std::abs(y2 - y1));
142 double error = dx / 2.0f;
143 const int ystep = (y1 < y2) ? 1 : -1;
144 int y0 = y1 -
static_cast<int>(width) / 2;
146 for (
int x = x1; x <= x2; x++) {
147 for (
int i = 0; i < static_cast<int>(width); i++) {
152 canvas[x][y] = color;
156 canvas[y][x] = color;
171 template<
class Type>
void DrawCircle(
vpImage<Type> & canvas,
const vpImagePoint & center,
int radius,
const Type & color,
unsigned int width = 1)
173 const size_t n = 8 * std::max(static_cast<size_t>(1), static_cast<size_t>(::pow(radius, 0.5)));
174 double px = 0, py = 0, da = 2 * M_PI / n;
175 for (
size_t i = 0; i <= n; i++) {
177 double cx = radius*::cos(a) + center.
get_u();
178 double cy = radius*::sin(a) + center.
get_v();
180 DrawLine(canvas, static_cast<int>(cx), static_cast<int>(cy), static_cast<int>(px), static_cast<int>(py), color, width);
187 template<
class Type>
void DrawFilledRectangle(
vpImage<Type> & canvas,
vpRect rect,
const Type & color)
190 for (
int row = static_cast<int>(rect.
getTop()); row < static_cast<int>(rect.
getBottom()); row++) {
191 Type * dst = canvas[row];
192 for (
int col = static_cast<int>(rect.
getLeft()); col < static_cast<int>(rect.
getRight()); col++) {
198 template<
class Type>
void DrawPolygon(
vpImage<Type> & canvas,
const std::vector<vpImagePoint> & polygon,
199 const Type & color,
unsigned int width = 1,
bool closed =
true)
202 for (
size_t i = 0; i < polygon.size(); i++) {
203 const vpImagePoint & p1 = (i ? polygon[i - 1] : polygon.back()), p2 = polygon[i];
204 DrawLine(canvas, static_cast<int>(p1.
get_u()), static_cast<int>(p1.
get_v()),
205 static_cast<int>(p2.get_u()), static_cast<int>(p2.get_v()), color, width);
209 for (
size_t i = 1; i < polygon.size(); i++) {
210 DrawLine(canvas, static_cast<int>(polygon[i-1].get_u()), static_cast<int>(polygon[i-1].get_v()),
211 static_cast<int>(polygon[i].get_u()), static_cast<int>(polygon[i].get_v()), color, width);
216 template<
class Type>
void DrawRectangle(
vpImage<Type> & canvas,
const vpRect & rect,
const Type & color,
unsigned int width = 1)
218 DrawLine(canvas, static_cast<int>(rect.
getLeft()), static_cast<int>(rect.
getTop()),
219 static_cast<int>(rect.
getRight()), static_cast<int>(rect.
getTop()), color, width);
220 DrawLine(canvas, static_cast<int>(rect.
getRight()), static_cast<int>(rect.
getTop()),
221 static_cast<int>(rect.
getRight()), static_cast<int>(rect.
getBottom()), color, width);
222 DrawLine(canvas, static_cast<int>(rect.
getRight()), static_cast<int>(rect.
getBottom()),
223 static_cast<int>(rect.
getLeft()), static_cast<int>(rect.
getBottom()), color, width);
224 DrawLine(canvas, static_cast<int>(rect.
getLeft()), static_cast<int>(rect.
getBottom()),
225 static_cast<int>(rect.
getLeft()), static_cast<int>(rect.
getTop()), color, width);
238 unsigned char color,
unsigned int w,
unsigned int h,
unsigned int thickness)
244 if ((std::fabs(a) <= std::numeric_limits<double>::epsilon()) &&
245 (std::fabs(b) <= std::numeric_limits<double>::epsilon())) {
260 drawLine(I, ip2, ip4, color, thickness);
267 drawLine(I, ip2, ip4, color, thickness);
270 drawLine(I, ip1, ip2, color, thickness);
283 const vpColor &color,
unsigned int w,
unsigned int h,
unsigned int thickness)
289 if ((std::fabs(a) <= std::numeric_limits<double>::epsilon()) &&
290 (std::fabs(b) <= std::numeric_limits<double>::epsilon())) {
305 drawLine(I, ip2, ip4, color, thickness);
312 drawLine(I, ip2, ip4, color, thickness);
315 drawLine(I, ip1, ip2, color, thickness);
328 unsigned int radius,
unsigned char color,
unsigned int thickness)
330 DrawCircle(I, center, static_cast<int>(radius), color, thickness);
342 unsigned int radius,
const vpColor &color,
unsigned int thickness)
344 DrawCircle(I, center, static_cast<int>(radius),
vpRGBa(color.
R, color.
G, color.
B), thickness);
356 unsigned char color,
unsigned int thickness)
367 drawLine(I, top, bottom, color, thickness);
368 drawLine(I, left, right, color, thickness);
380 const vpColor &color,
unsigned int thickness)
391 drawLine(I, top, bottom, color, thickness);
392 drawLine(I, left, right, color, thickness);
403 unsigned char color,
unsigned int thickness)
410 bool vertical_line =
static_cast<int>(ip2_.
get_j()) == static_cast<int>(ip1_.
get_j());
413 std::swap(ip1_, ip2_);
416 std::swap(ip1_, ip2_);
419 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
420 double deltaj = size / length * diff_j;
421 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
422 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
423 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
426 for (
unsigned int i = static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i(); i +=
static_cast<unsigned int>(2 * deltai)) {
427 double j = ip1_.
get_j();
431 for (
unsigned int j = static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j(); j +=
static_cast<unsigned int>(2 * deltaj)) {
432 double i = slope * j + orig;
452 bool vertical_line =
static_cast<int>(ip2_.
get_j()) == static_cast<int>(ip1_.
get_j());
455 std::swap(ip1_, ip2_);
458 std::swap(ip1_, ip2_);
461 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
462 double deltaj = size / length * diff_j;
463 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
464 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
465 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
468 for (
unsigned int i = static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i(); i +=
static_cast<unsigned int>(2 * deltai)) {
469 double j = ip1_.
get_j();
473 for (
unsigned int j = static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j(); j +=
static_cast<unsigned int>(2 * deltaj)) {
474 double i = slope * j + orig;
517 double coef2,
double coef3,
bool use_normalized_centered_moments,
unsigned char color,
518 double smallalpha,
double highalpha,
unsigned int thickness)
520 double a = 0., b = 0., e = 0.;
522 if (use_normalized_centered_moments) {
525 double n20_p = coef1;
526 double n11_p = coef2;
527 double n02_p = coef3;
528 double num = n20_p - n02_p;
529 double d = num * num + 4.0 * n11_p * n11_p;
531 if (d <= std::numeric_limits<double>::epsilon()) {
533 a = b = 2.0*sqrt(n20_p);
536 e = atan2(2.0*n11_p, num)/2.0;
539 a = sqrt(2.0*(num + d));
540 b = sqrt(2.0*(num - d));
553 double angle = highalpha - smallalpha;
555 double t = (a - b) / (a + b);
557 double circumference = (angle/2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
558 unsigned int nbpoints = (
unsigned int)(floor(circumference / 20));
562 double incr = angle / nbpoints;
564 double u0 = center.
get_u();
565 double v0 = center.
get_v();
566 double cose = cos(e);
567 double sine = sin(e);
569 double u = a * cos(smallalpha);
570 double v = b * sin(smallalpha);
576 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
579 for (
unsigned int i = 0; i < nbpoints; i++) {
586 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
588 drawLine(I, iP11, iP22, color, thickness);
631 double coef2,
double coef3,
bool use_normalized_centered_moments,
const vpColor &color,
632 double smallalpha,
double highalpha,
unsigned int thickness)
634 double a = 0., b = 0., e = 0.;
636 if (use_normalized_centered_moments) {
639 double n20_p = coef1;
640 double n11_p = coef2;
641 double n02_p = coef3;
642 double num = n20_p - n02_p;
643 double d = num * num + 4.0 * n11_p * n11_p;
645 if (d <= std::numeric_limits<double>::epsilon()) {
647 a = b = 2.0*sqrt(n20_p);
650 e = atan2(2.0*n11_p, num)/2.0;
653 a = sqrt(2.0*(num + d));
654 b = sqrt(2.0*(num - d));
667 double angle = highalpha - smallalpha;
669 double t = (a - b) / (a + b);
671 double circumference = (angle/2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
672 unsigned int nbpoints = (
unsigned int)(floor(circumference / 20));
676 double incr = angle / nbpoints;
678 double u0 = center.
get_u();
679 double v0 = center.
get_v();
680 double cose = cos(e);
681 double sine = sin(e);
683 double u = a * cos(smallalpha);
684 double v = b * sin(smallalpha);
690 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
693 for (
unsigned int i = 0; i < nbpoints; i++) {
700 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
702 drawLine(I, iP11, iP22, color, thickness);
724 double size,
unsigned char color,
unsigned int thickness,
const vpImagePoint &offset)
741 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
744 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
747 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
795 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
798 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
801 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
813 unsigned char color,
unsigned int thickness)
815 DrawLine(I, static_cast<int>(ip1.
get_u()), static_cast<int>(ip1.
get_v()),
816 static_cast<int>(ip2.
get_u()), static_cast<int>(ip2.
get_v()), color,
829 DrawLine(I, static_cast<int>(ip1.
get_u()), static_cast<int>(ip1.
get_v()),
830 static_cast<int>(ip2.
get_u()), static_cast<int>(ip2.
get_v()),
vpRGBa(color.
R, color.
G, color.
B),
867 unsigned char color,
unsigned int thickness,
bool closed)
869 DrawPolygon(I, vip, color, thickness, closed);
881 const vpColor &color,
unsigned int thickness,
bool closed)
883 DrawPolygon(I, vip,
vpRGBa(color.
R, color.
G, color.
B), thickness, closed);
901 DrawFilledRectangle(I, rectangle, color);
903 DrawRectangle(I, rectangle, color, thickness);
922 DrawFilledRectangle(I, rectangle,
vpRGBa(color.
R, color.
G, color.
B));
924 DrawRectangle(I, rectangle,
vpRGBa(color.
R, color.
G, color.
B), thickness);
unsigned int getWidth() const
static void drawRectangle(vpImage< unsigned char > &I, const vpRect &rectangle, unsigned char color, bool fill=false, unsigned int thickness=1)
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned char B
Blue component.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
static void drawCircle(vpImage< unsigned char > &I, const vpImagePoint ¢er, unsigned int radius, unsigned char color, unsigned int thickness=1)
void set_uv(double u, double v)
static void drawFrame(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, unsigned char color, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
Class to define RGB colors available for display functionnalities.
static void drawEllipse(vpImage< unsigned char > &I, const vpImagePoint ¢er, double coef1, double coef2, double coef3, bool use_normalized_centered_moments, unsigned char color, double smallalpha=0, double highalpha=2 *M_PI, unsigned int thickness=1)
static void drawArrow(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static const vpColor none
void track(const vpHomogeneousMatrix &cMo)
static void drawDottedLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
unsigned char G
Green component.
static const vpColor green
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static double sqr(double x)
static void drawPoint(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned char color, unsigned int thickness=1)
Generic class defining intrinsic camera parameters.
static void drawCross(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, unsigned char color, unsigned int thickness=1)
unsigned char R
Red component.
static void drawPolygon(vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, unsigned char color, unsigned int thickness=1, bool closed=true)
unsigned int getHeight() const
Defines a rectangle in the plane.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
Definition of the vpImage class member functions.
static const vpColor blue