60 #include <visp3/core/vpImageDraw.h>
61 #include <visp3/core/vpMeterPixelConversion.h>
62 #include <visp3/core/vpPoint.h>
66 #ifdef ENABLE_VISP_NAMESPACE
70 void DrawLine(
vpImage<Type> &canvas,
int x1,
int y1,
int x2,
int y2,
const Type &color,
unsigned int width = 1)
72 const int w =
static_cast<int>(canvas.
getWidth()) - 1;
73 const int h =
static_cast<int>(canvas.
getHeight()) - 1;
75 if (x1 < 0 || y1 < 0 || (x1 - w) > 0 || (y1 - h) > 0 || x2 < 0 || y2 < 0 || (x2 - w) > 0 || (y2 - h) > 0) {
76 if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0) || ((x1 - w) > 0 && (x2 - w) > 0) || ((y1 - h) > 0 && (y2 - h) > 0)) {
81 x1 = std::min<int>(std::max<int>(x1, 0), w);
82 x2 = std::min<int>(std::max<int>(x2, 0), w);
85 y1 = std::min<int>(std::max<int>(y1, 0), h);
86 y2 = std::min<int>(std::max<int>(y2, 0), h);
89 int x0 = (x1 * y2 - y1 * x2) / (y2 - y1);
90 int y0 = (y1 * x2 - x1 * y2) / (x2 - x1);
91 int xh = (x1 * y2 - y1 * x2 + h * (x2 - x1)) / (y2 - y1);
92 int yw = (y1 * x2 - x1 * y2 + w * (y2 - y1)) / (x2 - x1);
110 if ((y1 < 0 && y2 < 0) || (y1 > h && y2 > h)) {
134 const bool inverse = ((std::abs(y2 - y1) - std::abs(x2 - x1)) > 0);
145 const double dx = x2 - x1;
146 const double dy =
static_cast<double>(std::abs(y2 - y1));
148 double error = dx / 2.0f;
149 const int ystep = (y1 < y2) ? 1 : -1;
150 int y0 = y1 -
static_cast<int>(width) / 2;
152 for (
int x = x1; x <= x2; x++) {
153 for (
int i = 0; i < static_cast<int>(width); i++) {
158 canvas[x][y] = color;
163 canvas[y][x] = color;
177 template <
class Type>
179 unsigned int width = 1)
181 const size_t n = 8 * std::max(
static_cast<size_t>(1),
static_cast<size_t>(::pow(radius, 0.5)));
182 double px = 0, py = 0, da = 2 * M_PI / n;
183 for (
size_t i = 0; i <= n; i++) {
185 double cx = radius * ::cos(a) + center.
get_u();
186 double cy = radius * ::sin(a) + center.
get_v();
188 DrawLine(canvas,
static_cast<int>(cx),
static_cast<int>(cy),
static_cast<int>(px),
static_cast<int>(py), color,
196 template <
class Type>
void DrawFilledRectangle(
vpImage<Type> &canvas,
vpRect rect,
const Type &color)
199 for (
int row =
static_cast<int>(rect.
getTop()); row <
static_cast<int>(rect.
getBottom()); row++) {
200 Type *dst = canvas[row];
201 for (
int col =
static_cast<int>(rect.
getLeft()); col <
static_cast<int>(rect.
getRight()); col++) {
207 template <
class Type>
208 void DrawPolygon(
vpImage<Type> &canvas,
const std::vector<vpImagePoint> &polygon,
const Type &color,
209 unsigned int width = 1,
bool closed =
true)
212 for (
size_t i = 0; i < polygon.size(); i++) {
213 const vpImagePoint &p1 = (i ? polygon[i - 1] : polygon.back()), p2 = polygon[i];
214 DrawLine(canvas,
static_cast<int>(p1.
get_u()),
static_cast<int>(p1.
get_v()),
static_cast<int>(p2.get_u()),
215 static_cast<int>(p2.get_v()), color, width);
219 for (
size_t i = 1; i < polygon.size(); i++) {
220 DrawLine(canvas,
static_cast<int>(polygon[i - 1].get_u()),
static_cast<int>(polygon[i - 1].get_v()),
221 static_cast<int>(polygon[i].get_u()),
static_cast<int>(polygon[i].get_v()), color, width);
226 template <
class Type>
227 void DrawRectangle(
vpImage<Type> &canvas,
const vpRect &rect,
const Type &color,
unsigned int width = 1)
229 DrawLine(canvas,
static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getTop()),
static_cast<int>(rect.
getRight()),
230 static_cast<int>(rect.
getTop()), color, width);
231 DrawLine(canvas,
static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getTop()),
232 static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getBottom()), color, width);
233 DrawLine(canvas,
static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getBottom()),
234 static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getBottom()), color, width);
235 DrawLine(canvas,
static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getBottom()),
236 static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getTop()), color, width);
250 unsigned char color,
unsigned int w,
unsigned int h,
unsigned int thickness)
256 if ((std::fabs(a) <= std::numeric_limits<double>::epsilon()) &&
257 (std::fabs(b) <= std::numeric_limits<double>::epsilon())) {
273 drawLine(I, ip2, ip4, color, thickness);
280 drawLine(I, ip2, ip4, color, thickness);
283 drawLine(I, ip1, ip2, color, thickness);
296 unsigned int w,
unsigned int h,
unsigned int thickness)
302 if ((std::fabs(a) <= std::numeric_limits<double>::epsilon()) &&
303 (std::fabs(b) <= std::numeric_limits<double>::epsilon())) {
319 drawLine(I, ip2, ip4, color, thickness);
326 drawLine(I, ip2, ip4, color, thickness);
329 drawLine(I, ip1, ip2, color, thickness);
341 unsigned char color,
unsigned int thickness)
343 DrawCircle(I, circle.
getCenter(),
static_cast<int>(circle.
getRadius()), color, thickness);
355 unsigned char color,
unsigned int thickness)
357 DrawCircle(I, center,
static_cast<int>(radius), color, thickness);
368 unsigned int thickness)
382 unsigned int thickness)
384 DrawCircle(I, center,
static_cast<int>(radius),
vpRGBa(color.
R, color.
G, color.
B), thickness);
396 unsigned int thickness)
407 drawLine(I, top, bottom, color, thickness);
408 drawLine(I, left, right, color, thickness);
420 unsigned int thickness)
431 drawLine(I, top, bottom, color, thickness);
432 drawLine(I, left, right, color, thickness);
443 unsigned char color,
unsigned int thickness)
450 bool vertical_line =
static_cast<int>(ip2_.
get_j()) ==
static_cast<int>(ip1_.
get_j());
453 std::swap(ip1_, ip2_);
457 std::swap(ip1_, ip2_);
460 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
461 double deltaj = size / length * diff_j;
462 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
463 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
464 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
467 for (
unsigned int i =
static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i();
468 i +=
static_cast<unsigned int>(2 * deltai)) {
469 double j = ip1_.
get_j();
474 for (
unsigned int j =
static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j();
475 j +=
static_cast<unsigned int>(2 * deltaj)) {
476 double i = slope * j + orig;
490 const vpColor &color,
unsigned int thickness)
497 bool vertical_line =
static_cast<int>(ip2_.
get_j()) ==
static_cast<int>(ip1_.
get_j());
500 std::swap(ip1_, ip2_);
504 std::swap(ip1_, ip2_);
507 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
508 double deltaj = size / length * diff_j;
509 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
510 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
511 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
514 for (
unsigned int i =
static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i();
515 i +=
static_cast<unsigned int>(2 * deltai)) {
516 double j = ip1_.
get_j();
521 for (
unsigned int j =
static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j();
522 j +=
static_cast<unsigned int>(2 * deltaj)) {
523 double i = slope * j + orig;
566 double coef3,
bool use_normalized_centered_moments,
unsigned char color,
567 double smallalpha,
double highalpha,
unsigned int thickness)
569 double a = 0., b = 0., e = 0.;
571 if (use_normalized_centered_moments) {
574 double n20_p = coef1;
575 double n11_p = coef2;
576 double n02_p = coef3;
577 double num = n20_p - n02_p;
578 double d = num * num + 4.0 * n11_p * n11_p;
580 if (d <= std::numeric_limits<double>::epsilon()) {
582 a = b = 2.0 * sqrt(n20_p);
585 e = atan2(2.0 * n11_p, num) / 2.0;
588 a = sqrt(2.0 * (num + d));
589 b = sqrt(2.0 * (num - d));
603 double angle = highalpha - smallalpha;
605 double t = (a - b) / (a + b);
607 double circumference = (angle / 2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
608 unsigned int nbpoints = (
unsigned int)(floor(circumference / 20));
612 double incr = angle / nbpoints;
614 double u0 = center.
get_u();
615 double v0 = center.
get_v();
616 double cose = cos(e);
617 double sine = sin(e);
619 double u = a * cos(smallalpha);
620 double v = b * sin(smallalpha);
626 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
629 for (
unsigned int i = 0; i < nbpoints; i++) {
636 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
638 drawLine(I, iP11, iP22, color, thickness);
681 bool use_normalized_centered_moments,
const vpColor &color,
double smallalpha,
682 double highalpha,
unsigned int thickness)
684 double a = 0., b = 0., e = 0.;
686 if (use_normalized_centered_moments) {
689 double n20_p = coef1;
690 double n11_p = coef2;
691 double n02_p = coef3;
692 double num = n20_p - n02_p;
693 double d = num * num + 4.0 * n11_p * n11_p;
695 if (d <= std::numeric_limits<double>::epsilon()) {
697 a = b = 2.0 * sqrt(n20_p);
700 e = atan2(2.0 * n11_p, num) / 2.0;
703 a = sqrt(2.0 * (num + d));
704 b = sqrt(2.0 * (num - d));
718 double angle = highalpha - smallalpha;
720 double t = (a - b) / (a + b);
722 double circumference = (angle / 2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
723 unsigned int nbpoints = (
unsigned int)(floor(circumference / 20));
727 double incr = angle / nbpoints;
729 double u0 = center.
get_u();
730 double v0 = center.
get_v();
731 double cose = cos(e);
732 double sine = sin(e);
734 double u = a * cos(smallalpha);
735 double v = b * sin(smallalpha);
741 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
744 for (
unsigned int i = 0; i < nbpoints; i++) {
751 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
753 drawLine(I, iP11, iP22, color, thickness);
775 double size,
unsigned char color,
unsigned int thickness,
const vpImagePoint &offset)
792 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);
847 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
850 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
853 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
865 unsigned char color,
unsigned int thickness)
867 DrawLine(I,
static_cast<int>(ip1.
get_u()),
static_cast<int>(ip1.
get_v()),
static_cast<int>(ip2.
get_u()),
868 static_cast<int>(ip2.
get_v()), color, thickness);
879 unsigned int thickness)
881 DrawLine(I,
static_cast<int>(ip1.
get_u()),
static_cast<int>(ip1.
get_v()),
static_cast<int>(ip2.
get_u()),
882 static_cast<int>(ip2.
get_v()),
vpRGBa(color.
R, color.
G, color.
B), thickness);
893 unsigned int thickness)
919 unsigned int thickness,
bool closed)
921 DrawPolygon(I, vip, color, thickness, closed);
933 unsigned int thickness,
bool closed)
935 DrawPolygon(I, vip,
vpRGBa(color.
R, color.
G, color.
B), thickness, closed);
951 unsigned int thickness)
954 DrawFilledRectangle(I, rectangle, color);
957 DrawRectangle(I, rectangle, color, thickness);
974 unsigned int thickness)
977 DrawFilledRectangle(I, rectangle,
vpRGBa(color.
R, color.
G, color.
B));
980 DrawRectangle(I, rectangle,
vpRGBa(color.
R, color.
G, color.
B), thickness);
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
static const vpColor none
static const vpColor blue
static const vpColor green
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D circle in an image.
vpImagePoint getCenter() const
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 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 drawRectangle(vpImage< unsigned char > &I, const vpRect &rectangle, unsigned char color, bool fill=false, unsigned int thickness=1)
static void drawCircle(vpImage< unsigned char > &I, const vpImageCircle &circle, unsigned char color, unsigned int thickness=1)
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
static void drawPoint(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned char color, unsigned int thickness=1)
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))
static void drawPolygon(vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, unsigned char color, unsigned int thickness=1, bool closed=true)
static void drawCross(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, unsigned char color, unsigned int thickness=1)
static void drawDottedLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
void set_uv(double u, double v)
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getHeight() const
static double sqr(double x)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
Defines a rectangle in the plane.