58#include <visp3/core/vpImageDraw.h>
59#include <visp3/core/vpMeterPixelConversion.h>
60#include <visp3/core/vpPoint.h>
64#ifdef ENABLE_VISP_NAMESPACE
68void DrawLine(
vpImage<Type> &canvas,
int x1,
int y1,
int x2,
int y2,
const Type &color,
unsigned int width = 1)
70 const int w =
static_cast<int>(canvas.
getWidth()) - 1;
71 const int h =
static_cast<int>(canvas.
getHeight()) - 1;
73 if (x1 < 0 || y1 < 0 || (x1 - w) > 0 || (y1 - h) > 0 || x2 < 0 || y2 < 0 || (x2 - w) > 0 || (y2 - h) > 0) {
74 if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0) || ((x1 - w) > 0 && (x2 - w) > 0) || ((y1 - h) > 0 && (y2 - h) > 0)) {
79 x1 = std::min<int>(std::max<int>(x1, 0), w);
80 x2 = std::min<int>(std::max<int>(x2, 0), w);
83 y1 = std::min<int>(std::max<int>(y1, 0), h);
84 y2 = std::min<int>(std::max<int>(y2, 0), h);
87 int x0 = (x1 * y2 - y1 * x2) / (y2 - y1);
88 int y0 = (y1 * x2 - x1 * y2) / (x2 - x1);
89 int xh = (x1 * y2 - y1 * x2 +
h * (x2 - x1)) / (y2 - y1);
90 int yw = (y1 * x2 - x1 * y2 +
w * (y2 - y1)) / (x2 - x1);
108 if ((y1 < 0 && y2 < 0) || (y1 > h && y2 > h)) {
132 const bool inverse = ((std::abs(y2 - y1) - std::abs(x2 - x1)) > 0);
143 const double dx = x2 - x1;
144 const double dy =
static_cast<double>(std::abs(y2 - y1));
146 double error = dx / 2.0f;
147 const int ystep = (y1 < y2) ? 1 : -1;
148 int y0 = y1 -
static_cast<int>(
width) / 2;
150 for (
int x = x1;
x <= x2;
x++) {
151 for (
int i = 0; i < static_cast<int>(width);
i++) {
177 unsigned int width = 1)
179 const size_t n = 8 * std::max(
static_cast<size_t>(1),
static_cast<size_t>(::pow(radius, 0.5)));
180 double px = 0, py = 0, da = 2 * M_PI / n;
181 for (
size_t i = 0;
i <= n;
i++) {
183 double cx = radius * ::cos(a) + center.
get_u();
184 double cy = radius * ::sin(a) + center.
get_v();
186 DrawLine(canvas,
static_cast<int>(cx),
static_cast<int>(cy),
static_cast<int>(px),
static_cast<int>(py), color,
194template <
class Type>
void DrawFilledRectangle(
vpImage<Type> &canvas,
vpRect rect,
const Type &color)
197 for (
int row =
static_cast<int>(rect.
getTop()); row <
static_cast<int>(rect.
getBottom()); row++) {
198 Type *dst = canvas[row];
199 for (
int col =
static_cast<int>(rect.
getLeft()); col <
static_cast<int>(rect.
getRight()); col++) {
206void DrawPolygon(
vpImage<Type> &canvas,
const std::vector<vpImagePoint> &polygon,
const Type &color,
207 unsigned int width = 1,
bool closed =
true)
210 for (
size_t i = 0;
i < polygon.size();
i++) {
211 const vpImagePoint &p1 = (
i ? polygon[
i - 1] : polygon.back()), p2 = polygon[
i];
212 DrawLine(canvas,
static_cast<int>(p1.
get_u()),
static_cast<int>(p1.
get_v()),
static_cast<int>(p2.get_u()),
213 static_cast<int>(p2.get_v()), color, width);
217 for (
size_t i = 1;
i < polygon.size();
i++) {
218 DrawLine(canvas,
static_cast<int>(polygon[i - 1].get_u()),
static_cast<int>(polygon[i - 1].get_v()),
219 static_cast<int>(polygon[i].get_u()),
static_cast<int>(polygon[i].get_v()), color, width);
225void DrawRectangle(
vpImage<Type> &canvas,
const vpRect &rect,
const Type &color,
unsigned int width = 1)
227 DrawLine(canvas,
static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getTop()),
static_cast<int>(rect.
getRight()),
228 static_cast<int>(rect.
getTop()), color, width);
229 DrawLine(canvas,
static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getTop()),
230 static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getBottom()), color, width);
231 DrawLine(canvas,
static_cast<int>(rect.
getRight()),
static_cast<int>(rect.
getBottom()),
232 static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getBottom()), color, width);
233 DrawLine(canvas,
static_cast<int>(rect.
getLeft()),
static_cast<int>(rect.
getBottom()),
234 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);
298 unsigned int w,
unsigned int h,
unsigned int thickness)
304 if ((std::fabs(a) <= std::numeric_limits<double>::epsilon()) &&
305 (std::fabs(b) <= std::numeric_limits<double>::epsilon())) {
321 drawLine(I, ip2, ip4, color, thickness);
328 drawLine(I, ip2, ip4, color, thickness);
331 drawLine(I, ip1, ip2, color, thickness);
343 unsigned char color,
unsigned int thickness)
345 DrawCircle(I, circle.
getCenter(),
static_cast<int>(circle.
getRadius()), color, thickness);
357 unsigned char color,
unsigned int thickness)
359 DrawCircle(I, center,
static_cast<int>(radius), color, thickness);
370 unsigned int thickness)
372 DrawCircle(I, circle.
getCenter(),
static_cast<int>(circle.
getRadius()),
vpRGBa(color.R, color.G, color.B), thickness);
384 unsigned int thickness)
386 DrawCircle(I, center,
static_cast<int>(radius),
vpRGBa(color.R, color.G, color.B), thickness);
398 unsigned int thickness)
402 top.set_j(ip.
get_j());
409 drawLine(I, top, bottom, color, thickness);
410 drawLine(I, left, right, color, thickness);
422 unsigned int thickness)
426 top.set_j(ip.
get_j());
433 drawLine(I, top, bottom, color, thickness);
434 drawLine(I, left, right, color, thickness);
446 unsigned char color,
unsigned int thickness)
453 bool vertical_line =
static_cast<int>(ip2_.
get_j()) ==
static_cast<int>(ip1_.
get_j());
456 std::swap(ip1_, ip2_);
460 std::swap(ip1_, ip2_);
463 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
464 double deltaj = size / length * diff_j;
465 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
466 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
467 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
470 for (
unsigned int i =
static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i();
471 i +=
static_cast<unsigned int>(2 * deltai)) {
472 double j = ip1_.
get_j();
477 for (
unsigned int j =
static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j();
478 j +=
static_cast<unsigned int>(2 * deltaj)) {
479 double i = slope * j + orig;
494 const vpColor &color,
unsigned int thickness)
501 bool vertical_line =
static_cast<int>(ip2_.
get_j()) ==
static_cast<int>(ip1_.
get_j());
504 std::swap(ip1_, ip2_);
508 std::swap(ip1_, ip2_);
511 double diff_j = vertical_line ? 1 : ip2_.
get_j() - ip1_.
get_j();
512 double deltaj = size / length * diff_j;
513 double deltai = size / length * (ip2_.
get_i() - ip1_.
get_i());
514 double slope = (ip2_.
get_i() - ip1_.
get_i()) / diff_j;
515 double orig = ip1_.
get_i() - slope * ip1_.
get_j();
518 for (
unsigned int i =
static_cast<unsigned int>(ip1_.
get_i()); i < ip2_.
get_i();
519 i +=
static_cast<unsigned int>(2 * deltai)) {
520 double j = ip1_.
get_j();
525 for (
unsigned int j =
static_cast<unsigned int>(ip1_.
get_j()); j < ip2_.
get_j();
526 j +=
static_cast<unsigned int>(2 * deltaj)) {
527 double i = slope * j + orig;
579 double coef3,
bool use_normalized_centered_moments,
unsigned char color,
580 double smallalpha,
double highalpha,
unsigned int thickness)
582 double a = 0., b = 0., e = 0.;
584 if (use_normalized_centered_moments) {
587 double n20_p = coef1;
588 double n11_p = coef2;
589 double n02_p = coef3;
590 double num = n20_p - n02_p;
591 double d = num * num + 4.0 * n11_p * n11_p;
593 if (d <= std::numeric_limits<double>::epsilon()) {
595 a = b = 2.0 * sqrt(n20_p);
598 e = atan2(2.0 * n11_p, num) / 2.0;
601 a = sqrt(2.0 * (num + d));
602 b = sqrt(2.0 * (num - d));
616 double angle = highalpha - smallalpha;
618 double t = (a - b) / (a + b);
620 double circumference = (angle / 2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
621 unsigned int nbpoints =
static_cast<unsigned int>(floor(circumference / 20));
625 double incr = angle / nbpoints;
627 double u0 = center.
get_u();
628 double v0 = center.
get_v();
629 double cose = cos(e);
630 double sine = sin(e);
632 double u = a * cos(smallalpha);
633 double v = b * sin(smallalpha);
639 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
642 for (
unsigned int i = 0; i < nbpoints; i++) {
649 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
651 drawLine(I, iP11, iP22, color, thickness);
703 bool use_normalized_centered_moments,
const vpColor &color,
double smallalpha,
704 double highalpha,
unsigned int thickness)
706 double a = 0., b = 0., e = 0.;
708 if (use_normalized_centered_moments) {
711 double n20_p = coef1;
712 double n11_p = coef2;
713 double n02_p = coef3;
714 double num = n20_p - n02_p;
715 double d = num * num + 4.0 * n11_p * n11_p;
717 if (d <= std::numeric_limits<double>::epsilon()) {
719 a = b = 2.0 * sqrt(n20_p);
722 e = atan2(2.0 * n11_p, num) / 2.0;
725 a = sqrt(2.0 * (num + d));
726 b = sqrt(2.0 * (num - d));
740 double angle = highalpha - smallalpha;
742 double t = (a - b) / (a + b);
744 double circumference = (angle / 2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
745 unsigned int nbpoints =
static_cast<unsigned int>(floor(circumference / 20));
749 double incr = angle / nbpoints;
751 double u0 = center.
get_u();
752 double v0 = center.
get_v();
753 double cose = cos(e);
754 double sine = sin(e);
756 double u = a * cos(smallalpha);
757 double v = b * sin(smallalpha);
763 iP11.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
766 for (
unsigned int i = 0; i < nbpoints; i++) {
773 iP22.
set_uv(u0 + cose * u - sine * v, v0 + sine * u + cose * v);
775 drawLine(I, iP11, iP22, color, thickness);
797 double size,
unsigned char color,
unsigned int thickness,
const vpImagePoint &offset)
814 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
817 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
820 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
869 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
872 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
875 drawArrow(I, ipo + offset, ip1 + offset, color, 4 * thickness, 2 * thickness, thickness);
888 unsigned char color,
unsigned int thickness)
890 DrawLine(I,
static_cast<int>(ip1.
get_u()),
static_cast<int>(ip1.
get_v()),
static_cast<int>(ip2.
get_u()),
891 static_cast<int>(ip2.
get_v()), color, thickness);
903 unsigned int thickness)
905 DrawLine(I,
static_cast<int>(ip1.
get_u()),
static_cast<int>(ip1.
get_v()),
static_cast<int>(ip2.
get_u()),
906 static_cast<int>(ip2.
get_v()),
vpRGBa(color.R, color.G, color.B), thickness);
917 unsigned int thickness)
943 unsigned int thickness,
bool closed)
945 DrawPolygon(I, vip, color, thickness, closed);
957 unsigned int thickness,
bool closed)
959 DrawPolygon(I, vip,
vpRGBa(color.R, color.G, color.B), thickness, closed);
975 unsigned int thickness)
978 DrawFilledRectangle(I, rectangle, color);
981 DrawRectangle(I, rectangle, color, thickness);
998 unsigned int thickness)
1001 DrawFilledRectangle(I, rectangle,
vpRGBa(color.R, color.G, color.B));
1004 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 ...
Defines a rectangle in the plane.