11 #include "calibration.h" 15 #define MAX_ERROR_TO_INTERSECTION 0.707106781186547524381894 16 #define MAX_FLOAT_ERROR 0.01 17 #define MAX_FLOAT_ERROR_LITTLE_NUMBERS 0.000001 26 line2D(T i_x1, T i_y1, T i_x2, T i_y2);
57 static bool isCCW(
double x0,
double y0,
double x1,
double y1,
double x2,
double y2);
58 static bool processIntersectionLines(
double x1,
double y1,
double x2,
double y2,
59 double x3,
double y3,
double x4,
double y4,
61 static bool isLinesIntersection(
double x1,
double y1,
double x2,
double y2,
62 double x3,
double y3,
double x4,
double y4,
64 static bool pointInSegment(
double x,
double y,
point2D *begin,
point2D *end);
66 static bool pointInSegmentStrict(T x, T y,
point2D *begin,
point2D *end);
70 static bool pointPreciselyInSegmentStrict(
double x,
double y,
point2D *begin,
point2D *end);
102 template <
typename T>
113 template <
typename T>
125 template <
typename T>
131 void initRectangle(T x, T y, T w, T h);
132 void initRectangleReliability(T r);
149 double rectangleGravityDistance(
Rectangle *r);
164 double rectangleIntersectRatio(
Rectangle *r);
167 double rectangleIntersectRatioStrict(
Rectangle *r);
173 double rectangleIntersectionArea(
Rectangle *r);
175 static double rectangleNIntersectionArea(
const std::vector<Rectangle *>& rectangles);
177 double rectangleArea();
179 int getPositionRelativeToCamera(
SceneModel *smodel);
181 int getPositionRelativeToCameraOld(
SceneModel *smodel);
196 void substractRectangles(std::vector<Rectangle> &toSubstract, std::deque<Rectangle> &results);
201 bool pointInRectangle(T p_x, T p_y);
223 template <
typename T>
232 void computeBoundingRectangle();
234 bool getIntersectionsWithConvexPolygon(
double val,
bool vertical,
double& v1,
double& v2);
235 bool counterclockwisePolygon();
237 bool pointInPolygon(T x, T y,
bool strict);
238 static bool pointPreciselyInConvexPolygon(
double x,
double y,
polygon2D *poly,
bool strict);
240 double polygonArea();
241 bool pointInConvexPolygon(T x, T y,
bool strict);
244 bool memmove(
int to,
int from,
int number);
245 QSharedPointer<polygon2D<int> > projectImagePolygon2D(homography_matrix H);
250 std::vector< point2D<T> > points;
255 template <
typename T>
263 QSharedPointer<polygon2D<T> > makePolygon2D();
264 QSharedPointer<polygon2D<int> > projectPolygon2D(perspective_matrix M);
267 std::vector< point3D<T> > points;
271 #define POINT_2D_X(p) ((p)->x) 272 #define POINT_2D_Y(p) ((p)->y) 275 #define POINT_3D_X(p) ((p)->x) 276 #define POINT_3D_Y(p) ((p)->y) 277 #define POINT_3D_Z(p) ((p)->z) 281 #define POLYGON_N_POINTS(pl) ((pl)->points.size()) 282 #define POLYGON_NTH_POINT(pl, n) (&(pl)->points[n]) 285 #define POLYGON_2D_BB_DONE(pl) ((pl)->bb_done) 286 #define POLYGON_2D_BB(pl) ((pl)->boundingBox) 287 #define POLYGON_2D_BB_X(pl) RECT_X(&((pl)->boundingBox)) 288 #define POLYGON_2D_BB_Y(pl) RECT_Y(&((pl)->boundingBox)) 289 #define POLYGON_2D_BB_WIDTH(pl) RECT_WIDTH(&((pl)->boundingBox)) 290 #define POLYGON_2D_BB_HEIGHT(pl) RECT_HEIGHT(&((pl)->boundingBox)) 291 #define POLYGON_2D_BB_XLEFT(pl) RECT_XLEFT(&((pl)->boundingBox)) 292 #define POLYGON_2D_BB_XRIGHT(pl) RECT_XRIGHT(&((pl)->boundingBox)) 293 #define POLYGON_2D_BB_YBOTTOM(pl) RECT_YBOTTOM(&((pl)->boundingBox)) 294 #define POLYGON_2D_BB_YTOP(pl) RECT_YTOP(&((pl)->boundingBox)) 295 #define POLYGON_2D_BB_XCENTER(pl) (POLYGON_2D_BB_XLEFT(pl) + POLYGON_2D_BB_WIDTH(pl) / 2.0) 296 #define POLYGON_2D_BB_YCENTER(pl) (POLYGON_2D_BB_YTOP(pl) + POLYGON_2D_BB_HEIGHT(pl) / 2.0) 297 #define RECT_XLEFT(_rect) ((_rect)->xleft) 298 #define RECT_X(_rect) RECT_XLEFT(_rect) 299 #define RECT_XRIGHT(_rect) ((_rect)->xright) 300 #define RECT_YTOP(_rect) ((_rect)->ytop) 301 #define RECT_Y(_rect) RECT_YTOP(_rect) 302 #define RECT_YBOTTOM(_rect) ((_rect)->ybottom) 303 #define RECT_WIDTH(_rect) ((_rect)->width) 304 #define RECT_HEIGHT(_rect) ((_rect)->height) 305 #define RECT_XCENTER(_rect) (RECT_XLEFT(_rect) + RECT_WIDTH(_rect)/2) 306 #define RECT_YCENTER(_rect) (RECT_YTOP(_rect) + RECT_HEIGHT(_rect)/2) 310 template <
typename T>
313 template <
typename T>
315 x2(i_x2), y2(i_y2) {}
316 template <
typename T>
319 template <
typename T>
322 template <
typename T>
325 template <
typename T>
328 dx = POINT_2D_X(p1) - POINT_2D_X(p2),
329 dy = POINT_2D_Y(p1) - POINT_2D_Y(p2);
330 return sqrt(dx*dx + dy*dy);
333 template <
typename T>
336 template <
typename T>
339 template <
typename T>
342 template <
typename T>
345 dx = POINT_3D_X(p1) - POINT_3D_X(p2),
346 dy = POINT_3D_Y(p1) - POINT_3D_Y(p2),
347 dz = POINT_3D_Z(p1) - POINT_3D_Z(p2);
348 return sqrt(dx*dx + dy*dy + dz*dz);
351 template <
typename T>
362 template <
typename T>
364 distance(0), type(1) {}
366 template <
typename T>
368 T i_x2, T i_y2): x1(i_x1), y1(i_y1),
370 T dx = x1 - x2, dy = y1 - y2;
371 distance = sqrt(dx + dy);
374 template <
typename T>
377 template <
typename T>
380 template <
typename T>
383 template <
typename T>
386 template <
typename T>
389 template <
typename T>
392 template <
typename T>
396 template <
typename T>
399 template <
typename T>
406 template <
typename T>
414 template <
typename T>
416 points.resize(reserve);
420 template <
typename T>
424 template <
typename T>
429 int n = points.size();
438 double xmin, xmax, ymin, ymax;
440 typename std::vector< point2D<T> >::iterator it, it_end = points.end();
442 xmin = DBL_MAX / 2.0;
447 for (it = points.begin(); it != it_end; it++) {
449 xmin = POINT_2D_X(current) < xmin ? POINT_2D_X(current) : xmin;
450 xmax = POINT_2D_X(current) > xmax ? POINT_2D_X(current) : xmax;
451 ymin = POINT_2D_Y(current) < ymin ? POINT_2D_Y(current) : ymin;
452 ymax = POINT_2D_Y(current) > ymax ? POINT_2D_Y(current) : ymax;
457 RECT_WIDTH(&b) = RECT_WIDTH(&
boundingBox) = xmax - xmin + 1;
458 RECT_HEIGHT(&b) = RECT_HEIGHT(&
boundingBox) = ymax - ymin + 1;
459 RECT_XRIGHT(&b) = RECT_XRIGHT(&
boundingBox) = xmax;
460 RECT_YBOTTOM(&b) = RECT_YBOTTOM(&
boundingBox) = ymax;
466 template <
typename T>
469 int n = points.size();
477 double xmin, xmax, ymin, ymax;
479 typename std::vector< point2D<T> >::iterator it, it_end = points.end();
481 xmin = DBL_MAX / 2.0;
486 for (it = points.begin(); it != it_end; it++) {
488 xmin = POINT_2D_X(current) < xmin ? POINT_2D_X(current) : xmin;
489 xmax = POINT_2D_X(current) > xmax ? POINT_2D_X(current) : xmax;
490 ymin = POINT_2D_Y(current) < ymin ? POINT_2D_Y(current) : ymin;
491 ymax = POINT_2D_Y(current) > ymax ? POINT_2D_Y(current) : ymax;
504 template <
typename T>
506 int i, j, n = points.size();
509 for (i = 0; i < n; i++) {
510 j = (i == n - 1 ? 0 : i + 1);
511 area += points[i].x * points[j].y - points[i].y * points[j].x;
514 return fabs(area) / 2.0;
518 template <
typename T>
521 int i, pos = 0, neg = 0;
522 int num_points = points.size();
523 double xp1, xp2, yp1, yp2;
531 for(i=0;i<num_points;i++) {
533 p2 = &points[num_points - 1 ? i + 1 : 0];
536 return (strict ? 0 : 1);
538 xp1 = POINT_2D_X(p1); yp1 = POINT_2D_Y(p1);
539 xp2 = POINT_2D_X(p2); yp2 = POINT_2D_Y(p2);
541 if (((yp1<=y) && (y<yp2)) || ((yp2<=y) && (y<yp1))) {
542 if ( x*(yp2 - yp1) + xp2*yp1 - xp1*yp2 < y*(xp2 - xp1) )
556 template <
typename T>
558 int i, pos = 0, neg = 0;
559 int num_points = POLYGON_N_POINTS(poly);
560 double xp1, xp2, yp1, yp2;
563 if(!POLYGON_2D_BB(poly).pointInRectangle(x, y))
566 p2 = POLYGON_NTH_POINT(poly, 0);
568 for(i=0;i<num_points;i++) {
570 p2 = POLYGON_NTH_POINT(poly, i<num_points - 1 ? i + 1 : 0);
573 return (strict ? 0 : 1);
575 xp1 = POINT_2D_X(p1); yp1 = POINT_2D_Y(p1);
576 xp2 = POINT_2D_X(p2); yp2 = POINT_2D_Y(p2);
578 if ( (yp1 <= y && y < yp2) || (yp2 <= y && y < yp1) ) {
579 if ( x*(yp2 - yp1) + xp2*yp1 - xp1*yp2 < y*(xp2 - xp1) )
594 template <
typename T>
606 int i, obtained_points = 0;
607 int num_points = points.size();
608 double xp1, xp2, yp1, yp2;
614 for(i=0;i<num_points;i++) {
616 p2 = &points[i < num_points - 1 ? i + 1 : 0];
618 xp1 = POINT_2D_X(p1); yp1 = POINT_2D_Y(p1);
619 xp2 = POINT_2D_X(p2); yp2 = POINT_2D_Y(p2);
623 min = xp1; max = xp2;
625 min = xp2; max = xp1;
630 if ((val >= min) && (val <= max)) {
631 if(!obtained_points) {
632 v1 = (val*(yp2 - yp1) + xp2*yp1 - xp1*yp2)/(xp2 - xp1);
635 v2 = (val*(yp2 - yp1) + xp2*yp1 - xp1*yp2)/(xp2 - xp1);
642 min = yp1; max = yp2;
644 min = yp2; max = yp1;
649 if ((val >= min) && (val <= max)) {
650 if(!obtained_points) {
651 v1 = ( val*(xp2 - xp1) - xp2*yp1 + xp1*yp2 ) / (yp2 - yp1);
654 v2 = ( val*(xp2 - xp1) - xp2*yp1 + xp1*yp2 ) / (yp2 - yp1);
661 if(obtained_points == 2)
672 template <
typename T>
676 int n_points = p1->points.size(), n_points2 = p2->points.size();
678 if(n_points != n_points2)
681 for(i=0; i<n_points; i++)
682 if( fabs(p1->points[0].x - p2->points[i].x) < allowed_point_error
683 && fabs(p1->points[0].y - p2->points[i].y) < allowed_point_error) {
691 for(i=1,j=(j+1)%n_points; i<n_points; i++, j=(j+1)%n_points)
692 if( fabs(p1->points[i].x - p2->points[j].x) >= allowed_point_error
693 || fabs(p1->points[i].y - p2->points[j].y) >= allowed_point_error)
704 template <
typename T>
715 int num_inner_pol1 = 0, num_inner_pol2 = 0;
716 int p1_points = p1->points.size(), p2_points = p2->points.size();
717 int inner_points_p1[p1_points], inner_points_p2[p2_points];
720 memset(inner_points_p1, 0, p1_points*
sizeof(
int));
721 memset(inner_points_p2, 0, p2_points*
sizeof(
int));
724 for(i=0; i<p1_points; i++) {
725 p1pol1 = POLYGON_NTH_POINT(p1, i);
728 inner_points_p1[i] = 1;
732 for(i=0; i<p2_points; i++) {
733 p1pol2 = POLYGON_NTH_POINT(p2, i);
736 inner_points_p2[i] = 1;
740 if(num_inner_pol1 == p1_points)
743 if(num_inner_pol2 == p2_points)
746 int max_points = 2*(p1_points + p2_points);
748 int j, numIntersections = 0, num_current_intersections, num_allocated = 0, add;
750 double xnear, ynear, xfar, yfar;
751 double dx, dy, d1, d2;
755 if( num_inner_pol1 == 0 && num_inner_pol2 == 0 ) {
759 p2pol1 = POLYGON_NTH_POINT(p1, 0);
761 for(i=0; i<p1_points; i++) {
763 p2pol1 = POLYGON_NTH_POINT(p1, (i + 1)%p1_points);
765 num_current_intersections = 0;
767 p2pol2 = POLYGON_NTH_POINT(p2, 0);
768 for(j=0; j<p2_points; j++) {
770 p2pol2 = POLYGON_NTH_POINT(p2, (j + 1)%p2_points);
776 allocatedResults[num_allocated++] = icurrent;
778 if(icurrent->type == 1) {
779 current_intersections[num_current_intersections ].x = icurrent->x1;
780 current_intersections[num_current_intersections++].y = icurrent->y1;
782 num_current_intersections = 0;
783 current_intersections[num_current_intersections ].x = icurrent->x1;
784 current_intersections[num_current_intersections++].y = icurrent->y1;
785 current_intersections[num_current_intersections ].x = icurrent->x2;
786 current_intersections[num_current_intersections++].y = icurrent->y2;
790 if(num_current_intersections > 2)
791 std::cout <<
"ERROR!!!: Exceded intersections capacity." << std::endl;
794 if(num_current_intersections == 0)
797 if( num_current_intersections == 2
798 && current_intersections[0].x == current_intersections[1].x
799 && current_intersections[0].y == current_intersections[1].y )
800 num_current_intersections = 1;
802 if(num_current_intersections == 1) {
803 xnear = current_intersections[0].x;
804 ynear = current_intersections[0].y;
808 if( numIntersections > 0
809 && intersection_polygon->points[numIntersections - 1].x == xnear
810 && intersection_polygon->points[numIntersections - 1].y == ynear )
813 else if( numIntersections > 1
814 && intersection_polygon->points[0].x == xnear
815 && intersection_polygon->points[0].y == ynear )
819 intersection_polygon->points[numIntersections].x = xnear;
820 intersection_polygon->points[numIntersections++].y = ynear;
823 dx = current_intersections[0].x - p1pol1->x;
824 dy = current_intersections[0].y - p1pol1->y;
826 dx = current_intersections[1].x - p1pol1->x;
827 dy = current_intersections[1].y - p1pol1->y;
830 xnear = current_intersections[0].x;
831 ynear = current_intersections[0].y;
832 xfar = current_intersections[1].x;
833 yfar = current_intersections[1].y;
835 xnear = current_intersections[1].x;
836 ynear = current_intersections[1].y;
837 xfar = current_intersections[0].x;
838 yfar = current_intersections[0].y;
841 if( numIntersections > 0
842 && ( intersection_polygon->points[numIntersections - 1].x != xnear
843 || intersection_polygon->points[numIntersections - 1].y != ynear ) ) {
844 intersection_polygon->points[numIntersections].x = xnear;
845 intersection_polygon->points[numIntersections++].y = ynear;
846 }
else if(numIntersections == 0) {
847 intersection_polygon->points[numIntersections].x = xnear;
848 intersection_polygon->points[numIntersections++].y = ynear;
852 if( numIntersections > 1
853 && ( intersection_polygon->points[0].x != xfar
854 || intersection_polygon->points[0].y != yfar ) ) {
855 intersection_polygon->points[numIntersections].x = xfar;
856 intersection_polygon->points[numIntersections++].y = yfar;
857 }
else if(numIntersections <= 1) {
858 intersection_polygon->points[numIntersections].x = xfar;
859 intersection_polygon->points[numIntersections++].y = yfar;
864 for(i=0; i<num_allocated; i++)
865 delete allocatedResults[i];
867 if(numIntersections < 3) {
868 delete intersection_polygon;
872 return intersection_polygon;
886 int from_p1, inner_index;
887 double p1_inner_min_distance[p1_points], p2_inner_min_distance[p2_points];
888 double min, max = 0.0, dist;
890 memset(p1_inner_min_distance, 0, p1_points*
sizeof(
double));
891 memset(p2_inner_min_distance, 0, p2_points*
sizeof(
double));
895 for(i=0; i<p2_points; i++) {
896 if(inner_points_p2[i]) {
898 p2pol1 = POLYGON_NTH_POINT(p1, 0);
899 for(j=0; j<p1_points; j++) {
901 p2pol1 = POLYGON_NTH_POINT(p1, (j + 1)%p1_points);
902 dist = POLYGON_NTH_POINT(p2, i)->pointSegmentDistance(p1pol1, p2pol1);
906 p2_inner_min_distance[i] = min;
916 for(i=0; i<p1_points; i++) {
917 if(inner_points_p1[i]) {
919 p2pol2 = POLYGON_NTH_POINT(p2, 0);
920 for(j=0; j<p2_points; j++) {
922 p2pol2 = POLYGON_NTH_POINT(p2, (j + 1)%p2_points);
923 dist = POLYGON_NTH_POINT(p1, i)->pointSegmentDistance(p1pol2, p2pol2);
927 p1_inner_min_distance[i] = min;
938 int n_pinner, n_pother, i_inner, i_other, in_inner = 1, first_other = 1;
939 int *inner_points_p_inner, *inner_points_p_other, differentLastToAdd = 1;
940 int inner_in_limit, other_in_limit;
954 n_pinner = p1_points;
955 n_pother = p2_points;
956 current_point = POLYGON_NTH_POINT(p1, inner_index);
957 inner_points_p_inner = inner_points_p1;
958 inner_points_p_other = inner_points_p2;
962 n_pinner = p2_points;
963 n_pother = p1_points;
964 current_point = POLYGON_NTH_POINT(p2, inner_index);
965 inner_points_p_inner = inner_points_p2;
966 inner_points_p_other = inner_points_p1;
970 intersection_polygon->points[numIntersections ].x = current_point->x;
971 intersection_polygon->points[numIntersections++].y = current_point->y;
972 i_inner = inner_index;
976 p1pol1 = POLYGON_NTH_POINT(p_inner, i_inner);
977 p2pol1 = POLYGON_NTH_POINT(p_inner, (i_inner + 1)%n_pinner);
980 if(inner_points_p_inner[(i_inner + 1)%n_pinner]) {
981 i_inner = (i_inner + 1)%n_pinner;
984 if(i_inner == inner_index) {
985 differentLastToAdd = 0;
988 current_point = POLYGON_NTH_POINT(p_inner, i_inner);
989 intersection_polygon->points[numIntersections ].x = current_point->x;
990 intersection_polygon->points[numIntersections++].y = current_point->y;
993 }
else if(first_other) {
995 p2pol2 = POLYGON_NTH_POINT(p_other, 0);
996 for(j=0; j<n_pother; j++) {
998 p2pol2 = POLYGON_NTH_POINT(p_other, (j + 1)%n_pother);
1005 allocatedResults[num_allocated++] = icurrent;
1011 p2pol2 = POLYGON_NTH_POINT(p_other, i_other);
1012 for(j=0, aux_other = i_other; j < n_pother - 1; j++, aux_other = (aux_other + 1)%n_pother) {
1014 p2pol2 = POLYGON_NTH_POINT(p_other, (aux_other + 1)%n_pother);
1019 i_other = aux_other;
1020 allocatedResults[num_allocated++] = icurrent;
1025 if(icurrent == NULL) {
1029 }
else if(icurrent->type == 1) {
1030 intersection_polygon->points[numIntersections ].x = icurrent->x1;
1031 intersection_polygon->points[numIntersections++].y = icurrent->y1;
1033 inner_in_limit = icurrent->x1 == p2pol1->x && icurrent->y1 == p2pol1->y ? 1 : 0;
1035 p1pol2 = POLYGON_NTH_POINT(p_other, i_other );
1036 p2pol2 = POLYGON_NTH_POINT(p_other, (i_other + 1)%n_pother);
1037 if(icurrent->x1 == p1pol2->x && icurrent->y1 == p1pol2->y)
1039 else if(icurrent->x1 == p2pol2->x && icurrent->y1 == p2pol2->y) {
1041 i_other = (i_other + 1)%n_pother;
1044 if(inner_in_limit) {
1053 intersection_polygon->points[numIntersections ].x = icurrent->x2;
1054 intersection_polygon->points[numIntersections++].y = icurrent->y2;
1056 i_other = (i_other + 1)%n_pother;
1058 p2pol2 = POLYGON_NTH_POINT(p_other, i_other);
1061 if(p2pol1->x == p2pol2->x && p2pol1->y == p2pol2->y)
1065 dx_inner = p2pol1->x - intersection_polygon->points[numIntersections - 1].x,
1066 dx_other = p2pol2->x - intersection_polygon->points[numIntersections - 1].x,
1067 dy_inner = p2pol1->y - intersection_polygon->points[numIntersections - 1].y,
1068 dy_other = p2pol2->y - intersection_polygon->points[numIntersections - 1].y;
1070 if(dx_inner*dx_inner + dy_inner*dy_inner < dx_other*dx_other + dy_other*dy_other)
1077 i_inner = (i_inner + 1)%n_pinner;
1079 }
else if (in_inner == 0) {
1081 p1pol2 = POLYGON_NTH_POINT(p_other, i_other);
1082 p2pol2 = POLYGON_NTH_POINT(p_other, (i_other + 1)%n_pother);
1085 if(inner_points_p_other[(i_other + 1)%n_pother]) {
1086 i_other = (i_other + 1)%n_pother;
1087 current_point = POLYGON_NTH_POINT(p_other, i_other);
1088 intersection_polygon->points[numIntersections ].x = current_point->x;
1089 intersection_polygon->points[numIntersections++].y = current_point->y;
1094 p2pol1 = POLYGON_NTH_POINT(p_inner, i_inner);
1095 for(j=0, aux_inner = i_inner; j<(n_pinner - 1); j++, aux_inner = (aux_inner + 1)%n_pinner) {
1097 p2pol1 = POLYGON_NTH_POINT(p_inner, (aux_inner + 1)%n_pinner);
1102 i_inner = aux_inner;
1103 allocatedResults[num_allocated++] = icurrent;
1108 if(icurrent == NULL) {
1110 std::cout <<
"Error of intersection not found!!" << std::endl;
1112 }
else if(icurrent->type == 1) {
1113 intersection_polygon->points[numIntersections ].x = icurrent->x1;
1114 intersection_polygon->points[numIntersections++].y = icurrent->y1;
1116 other_in_limit = icurrent->x1 == p2pol2->x && icurrent->y1 == p2pol2->y ? 1 : 0;
1118 p1pol1 = POLYGON_NTH_POINT(p_inner, i_inner );
1119 p2pol1 = POLYGON_NTH_POINT(p_inner, (i_inner + 1)%n_pinner);
1120 if(icurrent->x1 == p1pol1->x && icurrent->y1 == p1pol1->y)
1122 else if(icurrent->x1 == p2pol1->x && icurrent->y1 == p2pol1->y) {
1124 i_inner = (i_inner + 1)%n_pinner;
1127 if(other_in_limit) {
1136 intersection_polygon->points[numIntersections ].x = icurrent->x2;
1137 intersection_polygon->points[numIntersections++].y = icurrent->y2;
1139 i_inner = (i_inner + 1)%n_pinner;
1141 p2pol1 = POLYGON_NTH_POINT(p_inner, i_inner);
1144 if(p2pol1->x == p2pol2->x && p2pol1->y == p2pol2->y)
1148 dx_inner = p2pol1->x - intersection_polygon->points[numIntersections - 1].x,
1149 dx_other = p2pol2->x - intersection_polygon->points[numIntersections - 1].x,
1150 dy_inner = p2pol1->y - intersection_polygon->points[numIntersections - 1].y,
1151 dy_other = p2pol2->y - intersection_polygon->points[numIntersections - 1].y;
1153 if(dx_inner*dx_inner + dy_inner*dy_inner < dx_other*dx_other + dy_other*dy_other)
1160 i_other = (i_other + 1)%n_pother;
1165 if(inner_points_p_inner[(i_inner + 1)%n_pinner]) {
1166 i_inner = (i_inner + 1)%n_pinner;
1167 i_other = (i_other + 1)%n_pother;
1170 if(i_inner == inner_index) {
1171 differentLastToAdd = 0;
1174 current_point = POLYGON_NTH_POINT(p_inner, i_inner);
1175 intersection_polygon->points[numIntersections ].x = current_point->x;
1176 intersection_polygon->points[numIntersections++].y = current_point->y;
1180 }
else if(inner_points_p_other[(i_other + 1)%n_pother]) {
1181 i_inner = (i_inner + 1)%n_pinner;
1182 i_other = (i_other + 1)%n_pother;
1183 current_point = POLYGON_NTH_POINT(p_other, i_other);
1184 intersection_polygon->points[numIntersections ].x = current_point->x;
1185 intersection_polygon->points[numIntersections++].y = current_point->y;
1194 p1pol1 = POLYGON_NTH_POINT(p_inner, i_inner);
1195 p2pol1 = POLYGON_NTH_POINT(p_inner, (i_inner + 1)%n_pinner);
1196 p1pol2 = POLYGON_NTH_POINT(p_other, i_other);
1197 p2pol2 = POLYGON_NTH_POINT(p_other, (i_other + 1)%n_pother);
1201 if(icurrent != NULL && icurrent->type == 0) {
1203 intersection_polygon->points[numIntersections ].x = icurrent->x2;
1204 intersection_polygon->points[numIntersections++].y = icurrent->y2;
1205 i_inner = (i_inner + 1)%n_pinner;
1206 i_other = (i_other + 1)%n_pother;
1208 if(p2pol1->x == p2pol2->x && p2pol1->y == p2pol2->y)
1212 dx_inner = p2pol1->x - intersection_polygon->points[numIntersections - 1].x,
1213 dx_other = p2pol2->x - intersection_polygon->points[numIntersections - 1].x,
1214 dy_inner = p2pol1->y - intersection_polygon->points[numIntersections - 1].y,
1215 dy_other = p2pol2->y - intersection_polygon->points[numIntersections - 1].y;
1217 if(dx_inner*dx_inner + dy_inner*dy_inner < dx_other*dx_other + dy_other*dy_other)
1229 aux_index = (i_inner + 1)%n_pinner;
1230 p2aux = POLYGON_NTH_POINT(p_inner, aux_index);
1231 for(j=0; j < n_pinner - 1; j++, aux_index = (aux_index + 1)%n_pinner) {
1233 p2aux = POLYGON_NTH_POINT(p_inner, (aux_index + 1)%n_pinner);
1238 i_inner = aux_index;
1239 allocatedResults[num_allocated++] = icurrent;
1243 if(icurrent == NULL) {
1247 aux_index = (i_other + 1)%n_pother;
1248 p2aux = POLYGON_NTH_POINT(p_other, aux_index);
1249 for(j=0; j < n_pother - 1; j++, aux_index = (aux_index + 1)%n_pother) {
1251 p2aux = POLYGON_NTH_POINT(p_other, (aux_index + 1)%n_pother);
1256 i_other = aux_index;
1257 allocatedResults[num_allocated++] = icurrent;
1261 if(icurrent == NULL) {
1265 std::cout <<
"Error of intersection not found!!" << std::endl;
1268 intersection_polygon->points[numIntersections ].x = icurrent->x1;
1269 intersection_polygon->points[numIntersections++].y = icurrent->y1;
1270 i_inner = (i_inner + 1)%n_pinner;
1274 intersection_polygon->points[numIntersections ].x = icurrent->x1;
1275 intersection_polygon->points[numIntersections++].y = icurrent->y1;
1276 i_other = (i_other + 1)%n_pother;
1282 }
while (differentLastToAdd);
1284 for(i=0; i<num_allocated; i++)
1285 delete allocatedResults[i];
1287 if(numIntersections < 3) {
1288 delete intersection_polygon;
1292 return intersection_polygon;
1295 template <
typename T>
1297 if(to < 0 || from < 0) {
1298 std::cout <<
"polygon2D: memmove error: to or from lower than 0." << std::endl;
1306 if(number > points.size() - from) {
1307 std::cout <<
"polygon2D: memmove error: according to 'from' and 'number', elements to move exceding existing elements." << std::endl;
1310 for(
int i = 0; i < number; i++)
1311 points[to + i] = points[from + i];
1315 if(to + number > points.capacity())
1316 points.reserve(to + number);
1317 for(
int i = number - 1; i >= 0; i--)
1318 points[to + i] = points[from + i];
1328 template <
typename T>
1336 return rectangle1->copy();
1339 int num_inner_pol1 = 0, first_inner_pol1 = -1, num_inner_pol2 = 0, first_inner_pol2 = -1;
1343 for(i=0; i<4; i++) {
1344 p1pol1 = POLYGON_NTH_POINT(rectangle1, i);
1345 if(rectangle2->pointInConvexPolygon(p1pol1->x, p1pol1->y,
true)) {
1347 if(first_inner_pol1 < 0)
1348 first_inner_pol1 = i;
1351 p1pol2 = POLYGON_NTH_POINT(rectangle2, i);
1352 if(rectangle1->pointInConvexPolygon(p1pol2->x, p1pol2->y,
true)) {
1354 if(first_inner_pol2 < 0)
1355 first_inner_pol2 = i;
1360 if( fabs(rectangle1->points[0].x + 284.314450444592) < MAX_FLOAT_ERROR
1361 && fabs(rectangle2->points[0].x + 239.497076034818) < MAX_FLOAT_ERROR)
1362 std::cout <<
"Point found: x[0] = " << rectangle1->points[0].x << std::endl;
1363 if(num_inner_pol1 == 4)
1364 return rectangle1->copy();
1366 if(num_inner_pol2 == 4)
1367 return rectangle2->copy();
1369 Intersection<T> *intersectionsRectangle2[4][4], currentIntersections[4], allocatedResults[12], current;
1370 int j, k, l, with_segment_intersection, numIntersections, numTotalDoubles = 0, num_allocated = 0;
1371 int sequenceMap[12],
1372 originalSegmentMap[12],
1373 num_intersectionsRectangle2[4], segmentIntersectionRectangle1[4], segmentIntersectionRectangle2[4];
1375 double dpol, dx, dy, dcur1, dcur2;
1376 bool move_inner_undone =
true;
1377 polygon2D<T> *rectangle1_ext = copy_fpolygon2d_extended_size(rectangle1, 12);
1379 memset(sequenceMap, 0, 12*
sizeof(
int));
1380 memset(originalSegmentMap, 0, 12*
sizeof(
int));
1381 memset(num_intersectionsRectangle2, 0, 4*
sizeof(
int));
1382 memset(segmentIntersectionRectangle1, 0, 4*
sizeof(
int));
1383 memset(segmentIntersectionRectangle2, 0, 4*
sizeof(
int));
1385 p2pol1 = POLYGON_NTH_POINT(rectangle1, 0);
1387 for(i=0, j=1; i<4; i++) {
1389 p2pol1 = POLYGON_NTH_POINT(rectangle1, (i + 1)%4);
1391 numIntersections = 0;
1392 with_segment_intersection = 0;
1394 p2pol2 = POLYGON_NTH_POINT(rectangle2, 0);
1395 for(k=0; k<4; k++) {
1397 p2pol2 = POLYGON_NTH_POINT(rectangle2, (k + 1)%4);
1400 if( (current = get_segments_intersection(p1pol1, p2pol1, p1pol2, p2pol2)) == NULL )
1403 allocatedResults[num_allocated++] = current;
1405 if(current->type == 0) {
1408 if(++numTotalDoubles == 4) {
1409 for(l=0; l<num_allocated; l++)
1410 delete allocatedResults[l];
1411 delete rectangle1_ext;
1412 return copy_fpolygon2d(rectangle1);
1414 currentIntersections[0] = intersectionsRectangle2[k][0] = current;
1415 segmentIntersectionRectangle1[i] = 1;
1416 with_segment_intersection = segmentIntersectionRectangle2[k] = num_intersectionsRectangle2[k] = numIntersections = 1;
1420 if(!segmentIntersectionRectangle2[k])
1421 intersectionsRectangle2[k][num_intersectionsRectangle2[k]++] = current;
1422 currentIntersections[numIntersections++] = current;
1425 if(numIntersections > 0) {
1427 dx = p1pol1->x - p2pol1->x;
1428 dy = p1pol1->y - p2pol1->y;
1429 dpol = dx*dx + dy*dy;
1431 if(with_segment_intersection) {
1433 dx = currentIntersections[0]->x1 - p1pol1->x;
1434 dy = currentIntersections[0]->y1 - p1pol1->y;
1435 dcur1 = dx*dx + dy*dy;
1436 dx = currentIntersections[0]->x2 - p1pol1->x;
1437 dy = currentIntersections[0]->y2 - p1pol1->y;
1438 dcur2 = dx*dx + dy*dy;
1441 if(dcur1 > MAX_FLOAT_ERROR) {
1442 rectangle1_ext->n_points++;
1444 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1445 rectangle1_ext->points[j].x = currentIntersections[0]->x1;
1446 rectangle1_ext->points[j].y = currentIntersections[0]->y1;
1447 originalSegmentMap[j] = i;
1448 sequenceMap[j++] = 1;
1450 originalSegmentMap[j-1] = i;
1451 sequenceMap[j-1] = -1;
1453 if( fabs(dpol - dcur2) > MAX_FLOAT_ERROR ) {
1454 rectangle1_ext->n_points++;
1456 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1457 rectangle1_ext->points[j].x = currentIntersections[0]->x2;
1458 rectangle1_ext->points[j].y = currentIntersections[0]->y2;
1459 originalSegmentMap[j] = i;
1460 sequenceMap[j++] = 1;
1462 originalSegmentMap[j] = i;
1463 sequenceMap[j] = -1;
1466 if(dcur2 > MAX_FLOAT_ERROR) {
1467 rectangle1_ext->n_points++;
1469 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1470 rectangle1_ext->points[j].x = currentIntersections[0]->x2;
1471 rectangle1_ext->points[j].y = currentIntersections[0]->y2;
1472 originalSegmentMap[j] = i;
1473 sequenceMap[j++] = 1;
1475 originalSegmentMap[j-1] = i;
1476 sequenceMap[j-1] = -1;
1479 if( fabs(dpol - dcur1) > MAX_FLOAT_ERROR ) {
1480 rectangle1_ext->n_points++;
1482 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1483 rectangle1_ext->points[j].x = currentIntersections[0]->x1;
1484 rectangle1_ext->points[j].y = currentIntersections[0]->y1;
1485 originalSegmentMap[j] = i;
1486 sequenceMap[j++] = 1;
1488 originalSegmentMap[j] = i;
1489 sequenceMap[j] = -1;
1498 if(numIntersections >= 2) {
1499 for(k=0; k<numIntersections-1; k++)
1500 for(l=k+1; l<numIntersections; l++) {
1501 if( fabs(currentIntersections[k]->x1 - currentIntersections[l]->x1) < MAX_FLOAT_ERROR
1502 && fabs(currentIntersections[k]->y1 - currentIntersections[l]->y1) < MAX_FLOAT_ERROR ) {
1503 if(l + 1 < numIntersections)
1504 memmove(currentIntersections + l, currentIntersections + l + 1,
sizeof(
Intersection<T> *)*(numIntersections-l-1) );
1511 if(numIntersections == 2) {
1512 dx = currentIntersections[0]->x1 - p1pol1->x;
1513 dy = currentIntersections[0]->y1 - p1pol1->y;
1514 dcur1 = dx*dx + dy*dy;
1515 dx = currentIntersections[1]->x1 - p1pol1->x;
1516 dy = currentIntersections[1]->y1 - p1pol1->y;
1517 dcur2 = dx*dx + dy*dy;
1520 if(dcur1 > MAX_FLOAT_ERROR) {
1521 rectangle1_ext->n_points++;
1523 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1525 rectangle1_ext->points[j].x = currentIntersections[0]->x1;
1526 rectangle1_ext->points[j].y = currentIntersections[0]->y1;
1528 originalSegmentMap[j] = i;
1529 sequenceMap[j++] = 1;
1532 originalSegmentMap[j-1] = i;
1533 sequenceMap[j-1] = -1;
1535 if( fabs(dpol - dcur2) > MAX_FLOAT_ERROR ) {
1536 rectangle1_ext->n_points++;
1538 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1539 rectangle1_ext->points[j].x = currentIntersections[1]->x1;
1540 rectangle1_ext->points[j].y = currentIntersections[1]->y1;
1541 originalSegmentMap[j] = i;
1542 sequenceMap[j++] = 1;
1544 originalSegmentMap[j] = i;
1545 sequenceMap[j] = -1;
1548 if(dcur2 > MAX_FLOAT_ERROR) {
1549 rectangle1_ext->n_points++;
1551 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1552 rectangle1_ext->points[j].x = currentIntersections[1]->x1;
1553 rectangle1_ext->points[j].y = currentIntersections[1]->y1;
1554 originalSegmentMap[j] = i;
1555 sequenceMap[j++] = 1;
1557 originalSegmentMap[j-1] = i;
1558 sequenceMap[j-1] = -1;
1560 if( fabs(dpol - dcur1) > MAX_FLOAT_ERROR ) {
1561 rectangle1_ext->n_points++;
1563 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1564 rectangle1_ext->points[j].x = currentIntersections[0]->x1;
1565 rectangle1_ext->points[j].y = currentIntersections[0]->y1;
1566 originalSegmentMap[j] = i;
1567 sequenceMap[j++] = 1;
1569 originalSegmentMap[j] = i;
1570 sequenceMap[j] = -1;
1574 dx = currentIntersections[0]->x1 - p1pol1->x;
1575 dy = currentIntersections[0]->y1 - p1pol1->y;
1576 dcur1 = dx*dx + dy*dy;
1578 if(dcur1 > MAX_FLOAT_ERROR && fabs(dpol - dcur1) > MAX_FLOAT_ERROR) {
1579 rectangle1_ext->n_points++;
1581 memmove(rectangle1_ext->points + j + 1, rectangle1_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1582 rectangle1_ext->points[j].x = currentIntersections[0]->x1;
1583 rectangle1_ext->points[j].y = currentIntersections[0]->y1;
1584 originalSegmentMap[j] = i;
1585 sequenceMap[j++] = 1;
1586 }
else if( fabs(dpol - dcur1) < MAX_FLOAT_ERROR ) {
1587 originalSegmentMap[j] = i;
1588 sequenceMap[j] = -1;
1590 originalSegmentMap[j-1] = i;
1591 sequenceMap[j-1] = -1;
1598 if(move_inner_undone && first_inner_pol1 > 0 && first_inner_pol1 == i + 1) {
1599 first_inner_pol1 = j;
1600 move_inner_undone =
false;
1606 int numTotalIntersections = 0;
1607 for(i=0; i<rectangle1_ext->n_points; i++)
1608 if(sequenceMap[i] != 0)
1609 numTotalIntersections++;
1612 if( num_inner_pol1 == 0 && num_inner_pol2 == 0 ) {
1613 if(numTotalIntersections < 3) {
1614 for(l=0; l<num_allocated; l++)
1615 delete allocatedResults[l];
1616 delete rectangle1_ext;
1620 for(i=0; i<num_allocated; i++)
1621 delete allocatedResults[i];
1624 for(i=0, j=0; i<rectangle1_ext->n_points; i++)
1625 if(sequenceMap[i] != 0) {
1626 rectangle1_ext->points[j].x = rectangle1_ext->points[i].x;
1627 rectangle1_ext->points[j].y = rectangle1_ext->points[i].y;
1630 rectangle1_ext->n_points = numTotalIntersections;
1631 return rectangle1_ext;
1642 int sequenceMap2[12], originalSegmentMap2[12];
1643 polygon2D<T> *rectangle2_ext = copy_fpolygon2d_extended_size(rectangle2, 12);
1645 memset(sequenceMap2, 0, 12*
sizeof(
int));
1646 memset(originalSegmentMap2, 0, 12*
sizeof(
int));
1648 move_inner_undone =
true;
1649 p2pol2 = POLYGON_NTH_POINT(rectangle2, 0);
1651 for(i=0, j=1; i<4; i++) {
1654 p2pol2 = POLYGON_NTH_POINT(rectangle2, (i + 1)%4);
1656 if(num_intersectionsRectangle2[i] > 0) {
1658 dx = p1pol2->x - p2pol2->x;
1659 dy = p1pol2->y - p2pol2->y;
1660 dpol = dx*dx + dy*dy;
1662 if(segmentIntersectionRectangle2[i]) {
1664 dx = intersectionsRectangle2[i][0]->x1 - p1pol2->x;
1665 dy = intersectionsRectangle2[i][0]->y1 - p1pol2->y;
1666 dcur1 = dx*dx + dy*dy;
1667 dx = intersectionsRectangle2[i][0]->x2 - p1pol2->x;
1668 dy = intersectionsRectangle2[i][0]->y2 - p1pol2->y;
1669 dcur2 = dx*dx + dy*dy;
1672 if(dcur1 > MAX_FLOAT_ERROR) {
1673 rectangle2_ext->n_points++;
1675 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1676 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x1;
1677 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y1;
1678 originalSegmentMap2[j] = i;
1679 sequenceMap2[j++] = 1;
1681 originalSegmentMap2[j-1] = i;
1682 sequenceMap2[j-1] = -1;
1684 if( fabs(dpol - dcur2) >= MAX_FLOAT_ERROR ) {
1685 rectangle2_ext->n_points++;
1687 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1688 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x2;
1689 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y2;
1690 originalSegmentMap2[j] = i;
1691 sequenceMap2[j++] = 1;
1693 originalSegmentMap2[j] = i;
1694 sequenceMap2[j] = -1;
1697 if(dcur2 > MAX_FLOAT_ERROR) {
1698 rectangle2_ext->n_points++;
1700 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1701 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x2;
1702 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y2;
1703 originalSegmentMap2[j] = i;
1704 sequenceMap2[j++] = 1;
1706 originalSegmentMap2[j-1] = i;
1707 sequenceMap2[j-1] = -1;
1709 if( fabs(dpol - dcur1) > MAX_FLOAT_ERROR ) {
1710 rectangle2_ext->n_points++;
1712 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1713 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x1;
1714 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y1;
1715 originalSegmentMap2[j] = i;
1716 sequenceMap2[j++] = 1;
1718 originalSegmentMap2[j] = i;
1719 sequenceMap2[j] = -1;
1726 if(num_intersectionsRectangle2[i] >= 2) {
1727 for(k=0; k<num_intersectionsRectangle2[i]-1; k++)
1728 for(l=k+1; l<num_intersectionsRectangle2[i]; l++) {
1729 if( fabs(intersectionsRectangle2[i][k]->x1 - intersectionsRectangle2[i][l]->x1) < MAX_FLOAT_ERROR
1730 && fabs(intersectionsRectangle2[i][k]->y1 - intersectionsRectangle2[i][l]->y1) < MAX_FLOAT_ERROR ) {
1731 if(l+1 < num_intersectionsRectangle2[i])
1732 memmove(intersectionsRectangle2[i] + l, intersectionsRectangle2[i] + l + 1,
sizeof(
Intersection<T> *)*(num_intersectionsRectangle2[i]-l-1) );
1733 num_intersectionsRectangle2[i]--;
1739 if(num_intersectionsRectangle2[i] == 2) {
1740 dx = intersectionsRectangle2[i][0]->x1 - p1pol2->x;
1741 dy = intersectionsRectangle2[i][0]->y1 - p1pol2->y;
1742 dcur1 = dx*dx + dy*dy;
1743 dx = intersectionsRectangle2[i][1]->x1 - p1pol2->x;
1744 dy = intersectionsRectangle2[i][1]->y1 - p1pol2->y;
1745 dcur2 = dx*dx + dy*dy;
1748 if(dcur1 > MAX_FLOAT_ERROR) {
1749 rectangle2_ext->n_points++;
1751 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1752 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x1;
1753 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y1;
1754 originalSegmentMap2[j] = i;
1755 sequenceMap2[j++] = 1;
1757 originalSegmentMap2[j-1] = i;
1758 sequenceMap2[j-1] = -1;
1760 if( fabs(dpol - dcur2) > MAX_FLOAT_ERROR ) {
1761 rectangle2_ext->n_points++;
1763 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1764 rectangle2_ext->points[j].x = intersectionsRectangle2[i][1]->x1;
1765 rectangle2_ext->points[j].y = intersectionsRectangle2[i][1]->y1;
1766 originalSegmentMap2[j] = i;
1767 sequenceMap2[j++] = 1;
1769 originalSegmentMap2[j] = i;
1770 sequenceMap2[j] = -1;
1773 if(dcur2 > MAX_FLOAT_ERROR) {
1774 rectangle2_ext->n_points++;
1776 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1777 rectangle2_ext->points[j].x = intersectionsRectangle2[i][1]->x1;
1778 rectangle2_ext->points[j].y = intersectionsRectangle2[i][1]->y1;
1779 originalSegmentMap2[j] = i;
1780 sequenceMap2[j++] = 1;
1782 originalSegmentMap2[j-1] = i;
1783 sequenceMap2[j-1] = -1;
1786 if( fabs(dpol - dcur1) > MAX_FLOAT_ERROR ) {
1787 rectangle2_ext->n_points++;
1789 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1790 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x1;
1791 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y1;
1792 originalSegmentMap2[j] = i;
1793 sequenceMap2[j++] = 1;
1795 originalSegmentMap2[j] = i;
1796 sequenceMap2[j] = -1;
1800 dx = intersectionsRectangle2[i][0]->x1 - p1pol2->x;
1801 dy = intersectionsRectangle2[i][0]->y1 - p1pol2->y;
1802 dcur1 = dx*dx + dy*dy;
1804 if(dcur1 > MAX_FLOAT_ERROR && fabs(dpol - dcur1) > MAX_FLOAT_ERROR) {
1805 rectangle2_ext->n_points++;
1807 memmove(rectangle2_ext->points + j + 1, rectangle2_ext->points + j,
sizeof(
struct fpoint_2d)*(3-i) );
1808 rectangle2_ext->points[j].x = intersectionsRectangle2[i][0]->x1;
1809 rectangle2_ext->points[j].y = intersectionsRectangle2[i][0]->y1;
1810 originalSegmentMap2[j] = i;
1811 sequenceMap2[j++] = 1;
1812 }
else if( fabs(dcur1 - dpol) < MAX_FLOAT_ERROR ) {
1813 originalSegmentMap2[j] = i;
1814 sequenceMap2[j] = -1;
1816 originalSegmentMap2[j] = i;
1817 sequenceMap2[j-1] = -1;
1824 if(move_inner_undone && first_inner_pol2 > 0 && first_inner_pol2 == i + 1) {
1825 first_inner_pol2 = j;
1826 move_inner_undone =
false;
1841 polygon2D<T> *inner_point_rectangle, the_other_rectangle, intersection_polygon;
1842 int *innerSequenceMap, *otherSequenceMap, *innerOriginalSegmentMap, *otherOriginalSegmentMap, *innerSegmentIntersection, *otherSegmentIntersection;
1843 int first_inner_index, first_other_index, inner_index, other_index, nother, ninner, search_first_other = 1, current_inner = 1;
1846 if(num_inner_pol1 > 0) {
1847 inner_point_rectangle = rectangle1_ext;
1848 the_other_rectangle = rectangle2_ext;
1849 first_inner_index = inner_index = first_inner_pol1;
1850 innerSequenceMap = sequenceMap;
1851 otherSequenceMap = sequenceMap2;
1852 innerOriginalSegmentMap = originalSegmentMap;
1853 otherOriginalSegmentMap = originalSegmentMap2;
1854 innerSegmentIntersection = segmentIntersectionRectangle1;
1855 otherSegmentIntersection = segmentIntersectionRectangle2;
1857 inner_point_rectangle = rectangle2_ext;
1858 the_other_rectangle = rectangle1_ext;
1859 first_inner_index = inner_index = first_inner_pol2;
1860 innerSequenceMap = sequenceMap2;
1861 otherSequenceMap = sequenceMap;
1862 innerOriginalSegmentMap = originalSegmentMap2;
1863 otherOriginalSegmentMap = originalSegmentMap;
1864 innerSegmentIntersection = segmentIntersectionRectangle2;
1865 otherSegmentIntersection = segmentIntersectionRectangle1;
1868 ninner = inner_point_rectangle->n_points;
1869 nother = the_other_rectangle->n_points;
1872 intersection_polygon = copy_fpolygon2d(inner_point_rectangle);
1874 for(i=0, j=0, k=0; i<ninner || j<nother; ) {
1877 std::cout.precision(15);
1878 std::cout <<
"Rectangle 1:\t" << std::endl;
1879 for(l=0; l < rectangle1->n_points; l++)
1880 std::cout <<
"(" << rectangle1->points[l].x <<
", " << rectangle1->points[l].y <<
") ";
1881 std::cout << std::endl;
1882 std::cout <<
"Rectangle 2:\t" << std::endl;
1883 for(l=0; l < rectangle2->n_points; l++)
1884 std::cout <<
"(" << rectangle2->points[l].x <<
", " << rectangle2->points[l].y <<
") ";
1885 std::cout << std::endl;
1886 std::cout <<
"Extended Rectangle 1:\t" << std::endl;
1887 for(l=0; l < rectangle1_ext->n_points; l++)
1888 std::cout <<
"(" << rectangle1_ext->points[l].x <<
", " << rectangle1_ext->points[l].y <<
") ";
1889 std::cout << std::endl;
1890 std::cout <<
"Extended Rectangle 2:\t" << std::endl;
1891 for(l=0; l < rectangle2_ext->n_points; l++)
1892 std::cout <<
"(" << rectangle2_ext->points[l].x <<
", " << rectangle2_ext->points[l].y <<
") ";
1893 std::cout << std::endl;
1898 if( innerSequenceMap[inner_index] != 0
1899 && innerSegmentIntersection[innerOriginalSegmentMap[inner_index]] == 0
1900 && innerSegmentIntersection[innerOriginalSegmentMap[(inner_index + 3)%4]] == 0 )
1905 if(search_first_other) {
1906 intersection_polygon->points[k ].x = inner_point_rectangle->points[inner_index].x;
1907 intersection_polygon->points[k++].y = inner_point_rectangle->points[inner_index].y;
1909 if(current_inner == 0) {
1910 search_first_other = 0;
1911 for(l=0; l<nother; l++)
1912 if( fabs(inner_point_rectangle->points[inner_index].x - the_other_rectangle->points[l].x) < MAX_FLOAT_ERROR
1913 && fabs(inner_point_rectangle->points[inner_index].y - the_other_rectangle->points[l].y) < MAX_FLOAT_ERROR ) {
1914 first_other_index = other_index = l;
1917 other_index = (other_index + 1) % nother;
1922 intersection_polygon->points[k ].x = inner_point_rectangle->points[inner_index].x;
1923 intersection_polygon->points[k++].y = inner_point_rectangle->points[inner_index].y;
1925 if(current_inner == 0) {
1928 && ( fabs(inner_point_rectangle->points[inner_index].x - the_other_rectangle->points[other_index].x) >= MAX_FLOAT_ERROR
1929 || fabs(inner_point_rectangle->points[inner_index].y - the_other_rectangle->points[other_index].y) >= MAX_FLOAT_ERROR ) ) {
1930 other_index = (other_index + 1) % nother;
1936 other_index = (other_index + 1) % nother;
1941 inner_index = (inner_index + 1) % ninner;
1945 if(current_inner == 1 && inner_index == first_inner_index)
1951 if( otherSequenceMap[other_index] != 0
1952 && otherSegmentIntersection[otherOriginalSegmentMap[other_index]] == 0
1953 && otherSegmentIntersection[otherOriginalSegmentMap[(other_index + 3) % nother]] == 0 )
1956 intersection_polygon->points[k ].x = the_other_rectangle->points[other_index].x;
1957 intersection_polygon->points[k++].y = the_other_rectangle->points[other_index].y;
1959 if(current_inner == 1) {
1962 && ( fabs(inner_point_rectangle->points[inner_index].x - the_other_rectangle->points[other_index].x) >= MAX_FLOAT_ERROR
1963 || fabs(inner_point_rectangle->points[inner_index].y - the_other_rectangle->points[other_index].y) >= MAX_FLOAT_ERROR ) ) {
1964 inner_index = (inner_index + 1) % ninner;
1971 inner_index = (inner_index + 1) % ninner;
1976 other_index = (other_index + 1) % nother;
1980 if(current_inner == 0 && other_index == first_other_index)
1985 intersection_polygon->n_points = k;
1987 for(l=0; l<num_allocated; l++)
1988 delete allocatedResults[l];
1990 delete rectangle1_ext;
1991 delete rectangle2_ext;
1993 return intersection_polygon;
1999 template <
typename T>
2002 xp1L1 = POINT_2D_X(p1L1), yp1L1 = POINT_2D_Y(p1L1),
2003 xp2L1 = POINT_2D_X(p2L1), yp2L1 = POINT_2D_Y(p2L1),
2004 xp1L2 = POINT_2D_X(p1L2), yp1L2 = POINT_2D_Y(p1L2),
2005 xp2L2 = POINT_2D_X(p2L2), yp2L2 = POINT_2D_Y(p2L2),
2006 minxL1, minxL2, minyL1, minyL2, maxxL1, maxxL2, maxyL1, maxyL2;
2008 if(xp1L1 < xp2L1) { minxL1 = xp1L1; maxxL1 = xp2L1; }
2009 else { minxL1 = xp2L1; maxxL1 = xp1L1; }
2011 if(yp1L1 < yp2L1) { minyL1 = yp1L1; maxyL1 = yp2L1; }
2012 else { minyL1 = yp2L1; maxyL1 = yp1L1; }
2014 if(xp1L2 < xp2L2) { minxL2 = xp1L2; maxxL2 = xp2L2; }
2015 else { minxL2 = xp2L2; maxxL2 = xp1L2; }
2017 if(yp1L2 < yp2L2) { minyL2 = yp1L2; maxyL2 = yp2L2; }
2018 else { minyL2 = yp2L2; maxyL2 = yp1L2; }
2021 if( minxL1 > maxxL2 || maxxL1 < minxL2)
2025 if( minyL1 > maxyL2 || maxyL1 < minyL2)
2029 if( fabs(xp1L1 - xp2L1) < MAX_FLOAT_ERROR ) {
2030 if( fabs(xp1L2 - xp2L2) < MAX_FLOAT_ERROR ) {
2031 if( fabs(xp1L1 - xp1L2) < MAX_FLOAT_ERROR ) {
2034 min = minyL1 > minyL2 ? minyL1 : minyL2,
2035 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2;
2036 if ( fabs(min - max) < MAX_FLOAT_ERROR ) {
2037 result->x1 = xp1L1; result->y1 = min; result->type = 1;
2039 result->x1 = xp1L1; result->y1 = min;
2040 result->x2 = xp1L1; result->y2 = max;
2050 min = minyL1 > minyL2 ? minyL1 : minyL2,
2051 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2,
2053 y = ((xp1L1 - xp2L2)*yp1L2 - (xp1L1 - xp1L2)*yp2L2)/(xp1L2 - xp2L2);
2055 if( y >= min && max >= y ) {
2063 }
else if( fabs(xp1L2 - xp2L2) < MAX_FLOAT_ERROR ) {
2066 min = minyL1 > minyL2 ? minyL1 : minyL2,
2067 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2,
2069 y = ((xp1L2 - xp2L1)*yp1L1 - (xp1L2 - xp1L1)*yp2L1)/(xp1L1 - xp2L1);
2071 if( y >= min && max >= y ) {
2084 a1 = (yp2L1 - yp1L1)/(xp2L1 - xp1L1),
2085 a2 = (yp2L2 - yp1L2)/(xp2L2 - xp1L2),
2086 b1 = yp2L1 - a1*xp2L1,
2087 b2 = yp2L2 - a2*xp2L2;
2090 if( fabs(a1 - a2) < MAX_FLOAT_ERROR_LITTLE_NUMBERS ) {
2093 if ( fabs(b1 - b2) >= MAX_FLOAT_ERROR )
2099 min = minxL1 > minxL2 ? minxL1 : minxL2,
2100 max = maxxL1 < maxxL2 ? maxxL1 : maxxL2;
2101 if ( fabs(min - max) < MAX_FLOAT_ERROR ) {
2102 result->x1 = min; result->y1 = min*a1 + b1; result->type = 1;
2104 result->x1 = min; result->y1 = min * a1 + b1;
2105 result->x2 = max; result->y2 = max * a1 + b1;
2113 min = minxL1 > minxL2 ? minxL1 : minxL2,
2114 max = maxxL1 < maxxL2 ? maxxL1 : maxxL2,
2116 x = (b2 - b1) / den,
2117 y = (b2*a1 - b1*a2) / den;
2118 if( x >= min && max >= x ) {
2133 template <
typename T>
2136 xp1L1 = POINT_2D_X(p1L1), yp1L1 = POINT_2D_Y(p1L1),
2137 xp2L1 = POINT_2D_X(p2L1), yp2L1 = POINT_2D_Y(p2L1),
2138 xp1L2 = POINT_2D_X(p1L2), yp1L2 = POINT_2D_Y(p1L2),
2139 xp2L2 = POINT_2D_X(p2L2), yp2L2 = POINT_2D_Y(p2L2),
2140 minxL1, minxL2, minyL1, minyL2, maxxL1, maxxL2, maxyL1, maxyL2;
2142 if(xp1L1 < xp2L1) { minxL1 = xp1L1; maxxL1 = xp2L1; }
2143 else { minxL1 = xp2L1; maxxL1 = xp1L1; }
2145 if(yp1L1 < yp2L1) { minyL1 = yp1L1; maxyL1 = yp2L1; }
2146 else { minyL1 = yp2L1; maxyL1 = yp1L1; }
2148 if(xp1L2 < xp2L2) { minxL2 = xp1L2; maxxL2 = xp2L2; }
2149 else { minxL2 = xp2L2; maxxL2 = xp1L2; }
2151 if(yp1L2 < yp2L2) { minyL2 = yp1L2; maxyL2 = yp2L2; }
2152 else { minyL2 = yp2L2; maxyL2 = yp1L2; }
2155 if( minxL1 > maxxL2 || maxxL1 < minxL2)
2159 if( minyL1 > maxyL2 || maxyL1 < minyL2)
2163 if( xp1L1 == xp2L1 ) {
2164 if( xp1L2 == xp2L2 ) {
2165 if( xp1L1 == xp1L2 ) {
2168 min = minyL1 > minyL2 ? minyL1 : minyL2,
2169 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2;
2171 result->x1 = xp1L1; result->y1 = min; result->type = 1;
2173 result->x1 = xp1L1; result->y1 = min;
2174 result->x2 = xp1L1; result->y2 = max;
2184 min = minyL1 > minyL2 ? minyL1 : minyL2,
2185 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2,
2187 y = ((xp1L1 - xp2L2)*yp1L2 - (xp1L1 - xp1L2)*yp2L2)/(xp1L2 - xp2L2);
2189 if( y >= min && max >= y ) {
2197 }
else if( xp1L2 == xp2L2 ) {
2200 min = minyL1 > minyL2 ? minyL1 : minyL2,
2201 max = maxyL1 < maxyL2 ? maxyL1 : maxyL2,
2203 y = ((xp1L2 - xp2L1)*yp1L1 - (xp1L2 - xp1L1)*yp2L1)/(xp1L1 - xp2L1);
2205 if( y >= min && max >= y ) {
2217 a1 = (yp2L1 - yp1L1)/(xp2L1 - xp1L1),
2218 a2 = (yp2L2 - yp1L2)/(xp2L2 - xp1L2),
2219 b1 = yp2L1 - a1*xp2L1,
2220 b2 = yp2L2 - a2*xp2L2;
2232 min = minxL1 > minxL2 ? minxL1 : minxL2,
2233 max = maxxL1 < maxxL2 ? maxxL1 : maxxL2;
2235 result->x1 = min; result->y1 = min*a1 + b1; result->type = 1;
2237 result->x1 = min; result->y1 = min * a1 + b1;
2238 result->x2 = max; result->y2 = max * a1 + b1;
2245 min = minxL1 > minxL2 ? minxL1 : minxL2,
2246 max = maxxL1 < maxxL2 ? maxxL1 : maxxL2,
2248 x = (b2 - b1) / den,
2249 y = (b2*a1 - b1*a2) / den;
2250 if( x >= min && max >= x ) {
2262 template <
typename T>
2266 x1 = POINT_2D_X(begin),
2267 y1 = POINT_2D_Y(begin),
2268 x2 = POINT_2D_X(end),
2269 y2 = POINT_2D_Y(end),
2275 return x == x1 && y == y1 ? 1 : 0;
2279 if(y1 > y2) { min = y2; max = y1; }
2280 else { min = y1; max = y2; }
2281 return y >= min && y <= max ? 1 : 0;
2285 if(x1 > x2) { min = x2; max = x1; }
2286 else { min = x1; max = x2; }
2287 if( x < min || x > max )
2291 if(y1 > y2) { min = y2; max = y1; }
2292 else { min = y1; max = y2; }
2293 if( y < min || y > max )
2297 return y*(x2 - x1) == x*(y2 - y1) + x2*y1 - x1*y2 ? 1 : 0;
2328 template <
typename T>
2337 cross_product = (x1 - x0) * (y2 - y1) - (y1 - y0) * (x2 - x1);
2339 return (cross_product > 0) ?
true :
false;
2342 template <
typename T>
2344 int n_points = points.size();
2348 for(i = 0; i < n_points; i++) {
2349 j = n_points - i - 1;
2350 inverse->points[j].x = points[i].x;
2351 inverse->points[j].y = points[i].y;
2356 template <
typename T>
2358 int i = 0, n = points.size();
2359 QSharedPointer<polygon2D<T> > res(
new polygon2D<T>(n));
2361 for(i=0; i<n; i++) {
2362 res->points[i].x = points[i].x;
2363 res->points[i].y = points[i].y;
2366 res->computeBoundingRectangle();
2371 template <
typename T>
2373 int i = 0, n = points.size();
2377 for(i=0; i<n; i++) {
2378 SceneModel::worldToImgCoords(M, points[i].x, points[i].y, points[i].z, &X, &Y);
2379 res->points[i].x = (int)X;
2380 res->points[i].y = (int)Y;
2383 res->computeBoundingRectangle();
2388 template <
typename T>
2393 int i, check, test_ok, nbi, n = points.size();
2398 computeBoundingRectangle(bounding_rect);
2399 if (!bounding_rect.pointInRectangle(x, y))
2403 for (i = 0; i < n; i++) {
2405 end = &points[i < n - 1 ? i + 1 : 0];
2407 return (strict ? 0 : 1);
2411 length = (RECT_WIDTH(&bounding_rect) < RECT_HEIGHT(&bounding_rect) ?
2412 RECT_HEIGHT(&bounding_rect) : RECT_WIDTH(&bounding_rect));
2419 x2 = x + length * cos(angle);
2420 y2 = y + length * sin(angle);
2421 for (i = 0; i < n; i++) {
2423 end = &points[i < n - 1 ? i + 1 : 0];
2426 POINT_2D_X(begin),POINT_2D_Y(begin),
2427 POINT_2D_X(end), POINT_2D_Y(end), &inter)) )
2450 template <
typename T>
2452 int i = 0, n = points.size();
2454 QSharedPointer< polygon2D<int> > res(r);
2456 for(i=0; i<n; i++) {
2457 SceneModel::homographyToImgCoords(H, points[i].x, points[i].y, &X, &Y);
2458 res->points[i].x = (int)X;
2459 res->points[i].y = (int)Y;
2462 res->computeBoundingRectangle();
2468 template <
typename T>
2471 if(pointInSegment(x, y, begin, end))
2472 if( ( ( x >= POINT_2D_X(begin) && x <= POINT_2D_X(end) )
2473 || ( x >= POINT_2D_X(end) && x <= POINT_2D_X(begin) ) )
2474 && ( ( y >= POINT_2D_Y(end) && y <= POINT_2D_Y(begin) )
2475 || (y >= POINT_2D_Y(end) && y <= POINT_2D_Y(begin) ) ) )
2484 if(point.pointSegmentDistance(begin, end) < MAX_FLOAT_ERROR)
2489 template <
typename T>
2494 pscal = (POINT_2D_X(end) - POINT_2D_X(begin)) * (x - POINT_2D_X(begin))
2495 + (POINT_2D_Y(end) - POINT_2D_Y(begin)) * (y - POINT_2D_Y(begin));
2498 res = distance(
this, begin);
2500 pscal = (POINT_2D_X(begin) - POINT_2D_X(end)) * (x - POINT_2D_X(end))
2501 + (POINT_2D_Y(begin) - POINT_2D_Y(end)) * (y - POINT_2D_Y(end));
2503 res = distance(
this, end);
2505 res = pointLineDistance(begin, end);
2520 template <
typename T>
2522 double a = POINT_2D_Y(lpt1) - POINT_2D_Y(lpt2);
2523 double b = POINT_2D_X(lpt2) - POINT_2D_X(lpt1);
2524 double c = - (a * POINT_2D_X(lpt1) + b * POINT_2D_Y(lpt1));
2525 double s = sqrt(a * a + b * b);
2527 return fabs((a * x + b * y + c) / s);
2530 template <
typename T>
2532 double seg_vect[2], vect[2];
2536 if (((x == POINT_2D_X(begin)) && (y == POINT_2D_Y(begin))) ||
2537 ((x == POINT_2D_X(end)) && (y == POINT_2D_Y(end))))
2540 seg_vect[0] = POINT_2D_X(end) - POINT_2D_X(begin);
2541 seg_vect[1] = POINT_2D_Y(end) - POINT_2D_Y(begin);
2542 norm = sqrt((seg_vect[0] * seg_vect[0]) + (seg_vect[1] * seg_vect[1]));
2543 seg_vect[0] = seg_vect[0] / norm;
2544 seg_vect[1] = seg_vect[1] / norm;
2546 vect[0] = x - POINT_2D_X(begin);
2547 vect[1] = y - POINT_2D_Y(begin);
2548 norm = sqrt((vect[0] * vect[0]) + (vect[1] * vect[1]));
2549 vect[0] = vect[0] / norm;
2550 vect[1] = vect[1] / norm;
2552 cp = (vect[0] * seg_vect[1]) - (vect[1] * seg_vect[0]);
2553 if (fabs(cp) > 0.01)
2557 if (fabs(seg_vect[0]) >= fabs(seg_vect[1]) && fabs(seg_vect[0]) > 0.01)
2558 norm = vect[0] / seg_vect[0];
2559 if (fabs(seg_vect[1]) >= fabs(seg_vect[0]) && fabs(seg_vect[1]) > 0.01)
2560 norm = vect[1] / seg_vect[1];
2561 if ((norm >= 0.0) && (norm <= 1.0))
2572 template <
typename T>
2574 double x3,
double y3,
double x4,
double y4,
2577 if ( isCCW(x1,y1,x2,y2,x3,y3) * isCCW(x1,y1,x2,y2,x4,y4) <= 0
2578 && isCCW(x3,y3,x4,y4,x1,y1) * isCCW(x3,y3,x4,y4,x2,y2) <= 0 ) {
2580 if (processIntersectionLines(x1, y1, x2, y2, x3, y3, x4, y4, pIntersection))
2589 template <
typename T>
2591 double x3,
double y3,
double x4,
double y4,
2593 if(pIntersection == NULL) {
2594 std::cout <<
"point2D: Error: processIntersectionLines - intersection is NULL." << std::endl;
2599 if(x1==x3 && y1==y3) {
2600 POINT_2D_X(pIntersection) = x1;
2601 POINT_2D_Y(pIntersection) = y1;
2605 if(x1==x4 && y1==y4) {
2606 POINT_2D_X(pIntersection) = x1;
2607 POINT_2D_Y(pIntersection) = y1;
2611 if(x2==x3 && y2==y3) {
2612 POINT_2D_X(pIntersection) = x2;
2613 POINT_2D_Y(pIntersection) = y2;
2617 if(x2==x4 && y2==y4) {
2618 POINT_2D_X(pIntersection) = x2;
2619 POINT_2D_Y(pIntersection) = y2;
2624 if(y1==y2 && y2==y3 && y3==y4) {
2625 double max1 = std::max(x1,x2);
2626 double min1 = std::min(x1,x2);
2627 double max2 = std::max(x3,x4);
2628 double min2 = std::min(x3,x4);
2631 if(max1 < min2 || max2 < min1)
2635 if(max1 > min2 || max2 > min1) {
2637 if(max2 > max1 && min2 > min1) {
2639 POINT_2D_X(pIntersection) = (min2 + max1)/2.0;
2640 POINT_2D_Y(pIntersection) = y3;
2644 if(max1 > max2 && min1 > min2) {
2646 POINT_2D_X(pIntersection) = (min1 + max2)/2.0;
2647 POINT_2D_Y(pIntersection) = y3;
2652 if (min2 > min1 && max2 < max1) {
2654 POINT_2D_X(pIntersection) = (min2 + max2)/2.0;
2655 POINT_2D_Y(pIntersection) = y3;
2659 if (min2 < min1 && max2 > max1) {
2661 POINT_2D_X(pIntersection) = (min1 + max1)/2.0;
2662 POINT_2D_Y(pIntersection) = y3;
2669 if(x1==x2 && x2==x3 && x3==x4) {
2670 double max1 = std::max(y1,y2);
2671 double min1 = std::min(y1,y2);
2672 double max2 = std::max(y3,y4);
2673 double min2 = std::min(y3,y4);
2676 if (max1 < min2 || max2 < min1)
2680 if(max1 > min2 || max2 > min1) {
2682 if(max2 > max1 && min2 > min1) {
2684 POINT_2D_Y(pIntersection) = (min2 + max1)/2.0;
2685 POINT_2D_X(pIntersection) = x3;
2689 if(max1 > max2 && min1 > min2) {
2691 POINT_2D_Y(pIntersection) = (min1 + max2)/2.0;
2692 POINT_2D_X(pIntersection) = x3;
2697 if(min2 > min1 && max2 < max1) {
2699 POINT_2D_Y(pIntersection) = (min2 + max2)/2.0;
2700 POINT_2D_X(pIntersection) = x3;
2704 if(min2 < min1 && max2 > max1) {
2706 POINT_2D_Y(pIntersection) = (min1 + max1)/2.0;
2707 POINT_2D_X(pIntersection) = x3;
2715 POINT_2D_X(pIntersection) = 0;
2716 POINT_2D_Y(pIntersection) = 0;
2719 double x12 = x2 - x1;
2720 double y12 = y2 - y1;
2723 double x34 = x4 - x3;
2724 double y34 = y4 - y3;
2726 double x31 = x1 - x3;
2727 double y31 = y1 - y3;
2730 double alpha = x31 * y34 - y31 * x34;
2731 double delta = y12 * x34 - x12 * y34;
2734 if (fabs(delta) < 1e-6) {
2735 std::cout <<
"Error: point2D: processIntersectionsLines - delta is NULL" << std::endl;
2741 POINT_2D_X(pIntersection) = x1 + alpha*x12;
2742 POINT_2D_Y(pIntersection) = y1 + alpha*y12;
2754 template <
typename T>
2756 return isCCW( POINT_2D_X(pPt0), POINT_2D_Y(pPt0),
2757 POINT_2D_X(pPt1), POINT_2D_Y(pPt1),
2758 POINT_2D_X(pPt2), POINT_2D_Y(pPt2) );
2761 template <
typename T>
2762 bool point2D<T>::isCCW(
double x0,
double y0,
double x1,
double y1,
double x2,
double y2) {
2763 double dx1 = x1 - x0;
2764 double dy1 = y1 - y0;
2765 double dx2 = x2 - x0;
2766 double dy2 = y2 - y0;
2768 if ((dx1*dy2) > (dy1*dx2))
2770 if ((dx1*dy2) < (dy1*dx2))
2772 if ((dx1*dx2) < 0 || (dy1*dy2) < 0)
2774 if ( (dx1*dx1+dy1*dy1) < (dx2*dx2+dy2*dy2))
2780 template <
typename T>
2783 template <
typename T>
2790 template <
typename T>
2792 points.reserve(reserve);
2795 template <
typename T>
2798 template <
typename T>
2800 initRectangle(1,1,1,1);
2803 template <
typename T>
2805 initRectangle(x, y, w, h);
2808 template <
typename T>
2811 template <
typename T>
2814 xright = (w > 0 ? x + w - 1 : x);
2816 ybottom = (h > 0 ? y + h - 1 : y);
2821 template <
typename T>
2823 xleft = xright = ytop = ybottom = width = height = r;
2826 template <
typename T>
2833 template <
typename T>
2836 if(r1 == NULL && r2 == NULL)
2839 return r2->copyRectangle();
2841 return r1->copyRectangle();
2845 merge->xleft = std::min(r1->xleft, r2->xleft);
2846 merge->xright = std::max(r2->xright, r2->xright);
2847 merge->ytop = std::min(r1->ytop, r2->ytop);
2848 merge->ybottom = std::max(r1->ybottom, r2->ybottom);
2849 merge->width = merge->xright - merge->xleft + 1;
2850 merge->height = merge->ybottom - merge->ytop + 1;
2856 template <
typename T>
2859 res.xleft = std::min(r1.xleft, r2.xleft);
2860 res.xright = std::max(r2.xright, r2.xright);
2861 res.ytop = std::min(r1.ytop, r2.ytop);
2862 res.ybottom = std::max(r1.ybottom, r2.ybottom);
2863 res.width = res.xright - res.xleft + 1;
2864 res.height = res.ybottom - res.ytop + 1;
2869 template <
typename T>
2871 return mergeRectangles(
this, r);
2874 template <
typename T>
2876 this->xleft = std::min(this->xleft, r.xleft);
2877 this->xright = std::max(this->xright, r.xright);
2878 this->ytop = std::min(this->ytop, r.ytop);
2879 this->ybottom = std::max(this->ybottom, r.ybottom);
2880 this->width = this->xright - this->xleft + 1;
2881 this->height = this->ybottom - this->ytop + 1;
2885 template <
typename T>
2890 xleft = std::min(xleft, r->xleft);
2891 xright = std::max(xright, r->xright);
2892 ytop = std::min(ytop, r->ytop);
2893 ybottom = std::max(ybottom, r->ybottom);
2894 width = xright - xleft + 1;
2895 height = ybottom - ytop + 1;
2898 template <
typename T>
2900 xleft = std::min(xleft, r.xleft);
2901 xright = std::max(xright, r.xright);
2902 ytop = std::min(ytop, r.ytop);
2903 ybottom = std::max(ybottom, r.ybottom);
2904 width = xright - xleft + 1;
2905 height = ybottom - ytop + 1;
2910 template <
typename T>
2912 if(r1->xright < r2->xleft)
2913 return r2->xleft - r1->xright;
2914 else if(r2->xright < r1->xleft)
2915 return r1->xleft - r2->xright;
2920 template <
typename T>
2922 if(r1->ybottom < r2->ytop)
2923 return r2->ytop - r1->ybottom;
2924 else if(r2->ybottom < r1->ytop)
2925 return r1->ytop - r2->ybottom;
2930 template <
typename T>
2934 if((inter = rectanglesIntersection(r1, r2)) != NULL) {
2939 double dy = 0.0, dx = 0.0;
2942 if(r1->xright <= r2->xleft) {
2943 dx = r2->xleft - r1->xright;
2944 }
else if(r2->xright <= r1->xleft) {
2945 dx = r1->xleft - r2->xright;
2948 if(r1->ybottom <= r2->ytop) {
2949 dy = r2->ytop - r1->ybottom;
2950 }
else if(r2->ybottom <= r1->ytop) {
2951 dy = r1->ytop - r2->ybottom;
2959 return sqrt(dx*dx + dy*dy);
2963 template <
typename T>
2965 return rectangleDistance(
this, r);
2968 template <
typename T>
2970 if( r1 == NULL || r2 == NULL )
2974 X1 = r1->xleft + r1->width / 2.0,
2975 X2 = r2->xleft + r2->width / 2.0,
2976 Y1 = r1->ytop + r1->height / 2.0,
2977 Y2 = r2->ytop + r2->height / 2.0;
2980 return fabs(Y1 - Y2);
2982 return fabs(X1 - X2);
2984 double dX = X1 - X2, dY = Y1 - Y2;
2986 return sqrt(dX*dX + dY*dY);
2989 template <
typename T>
2991 return rectangleGravityDistance(
this, r);
2994 template <
typename T>
2996 if(r1 == NULL || r2 == NULL)
3000 x_left = std::max(r1->xleft, r2->xleft),
3001 x_right = std::min(r1->xright, r2->xright),
3002 y_top = std::max(r1->ytop, r2->ytop),
3003 y_bottom = std::min(r1->ybottom, r2->ybottom);
3005 if(x_left < x_right && y_top < y_bottom)
3006 return new Rectangle<T>(x_left, y_top, x_right - x_left + 1, y_bottom - y_top + 1);
3011 template <
typename T>
3013 if(r1 == NULL || r2 == NULL || inter == NULL)
3017 x_left = std::max(r1->xleft, r2->xleft),
3018 x_right = std::min(r1->xright, r2->xright),
3019 y_top = std::max(r1->ytop, r2->ytop),
3020 y_bottom = std::min(r1->ybottom, r2->ybottom);
3022 if(x_left < x_right && y_top < y_bottom) {
3023 inter->initRectangle(x_left, y_top, x_right - x_left + 1, y_bottom - y_top + 1);
3030 template <
typename T>
3033 x_left = std::max(r1.xleft, r2.xleft),
3034 x_right = std::min(r1.xright, r2.xright),
3035 y_top = std::max(r1.ytop, r2.ytop),
3036 y_bottom = std::min(r1.ybottom, r2.ybottom);
3038 if(x_left < x_right && y_top < y_bottom) {
3039 inter.initRectangle(x_left, y_top, x_right - x_left + 1, y_bottom - y_top + 1);
3047 template <
typename T>
3049 return rectanglesIntersection(
this, r);
3052 template <
typename T>
3054 if(r1 == NULL && r2 == NULL)
3057 return r2->copyRectangle();
3059 return r1->copyRectangle();
3061 x_left = std::min(r1->xleft, r2->xleft),
3062 x_right = std::max(r1->xright, r2->xright),
3063 y_top = std::min(r1->ytop, r2->ytop),
3064 y_bottom = std::max(r1->ybottom, r2->ybottom);
3066 return new Rectangle<T>(x_left, y_top, x_right - x_left + 1, y_bottom - y_top + 1);
3071 template <
typename T>
3073 return rectanglesUnion(
this, r);
3076 template <
typename T>
3079 if(r1 == NULL || r2 == NULL)
3087 a1 = r1->width*r1->height,
3088 a2 = r2->width*r2->height,
3089 ai = inter->width*inter->height, ratio;
3092 ratio = (a2 - ai)/a2;
3094 ratio = (a1 - ai)/a1;
3102 template <
typename T>
3104 return rectangleIntersectRatioStrict(
this, r);
3107 template <
typename T>
3109 if(r1 == NULL || r2 == NULL)
3116 double a1 = r1->width*r1->height, r;
3117 r = inter->width * inter->height / a1;
3124 template <
typename T>
3126 return rectangleIntersectRatio(
this, r);
3130 template <
typename T>
3132 return r1->xleft >= r2->xleft && r1->xright >= r2->xright
3133 && r1->ytop >= r2->ytop && r1->ybottom <= r2->ybottom;
3136 template <
typename T>
3138 return rectangleInRectangle(
this, r);
3141 template <
typename T>
3143 if(r1 == NULL || r2 == NULL)
3150 double a = inter->width*inter->height;
3157 template <
typename T>
3159 return rectangleIntersectionArea(
this, r);
3162 template <
typename T>
3165 if(rectangles.empty())
3168 typename std::vector<Rectangle<T> *>::iterator r_iter = rectangles.begin(),
3169 r_end = rectangles.end();
3171 xLeft = (*r_iter)->xleft,
3172 xRight = (*r_iter)->xright,
3173 yTop = (*r_iter)->ytop,
3174 yBottom = (*r_iter)->ybottom;
3177 for (; r_iter != r_end; r_iter++) {
3178 xLeft = std::max(xLeft, (*r_iter)->xleft);
3179 xRight = std::min(xRight, (*r_iter)->xright);
3180 yTop = std::max(yTop, (*r_iter)->ytop);
3181 yBottom = std::min(yBottom, (*r_iter)->ybottom);
3184 if( xLeft <= xRight && yTop <= yBottom )
3185 return (xRight - xLeft + 1)*(yBottom - yTop + 1);
3192 template <
typename T>
3194 return width*height;
3197 template <
typename T>
3200 double x, y, X1, Y1, X2, Y2;
3203 X1 = xleft; Y1 = ybottom;
3204 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), X1, Y1, 0.0, &x, &y);
3205 SceneModel::worldToImgCoords(SM_CALIB_MATRIX(smodel), x, y, smodel->m_OneMeterRepresentation, &X2, &Y2);
3212 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), X1, Y1, 0.0, &x, &y);
3213 SceneModel::worldToImgCoords(SM_CALIB_MATRIX(smodel), x, y, smodel->m_OneMeterRepresentation, &X2, &Y2);
3222 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), X1, Y1, 0.0, &x, &y);
3223 SceneModel::worldToImgCoords(SM_CALIB_MATRIX(smodel), x, y, smodel->m_OneMeterRepresentation, &X2, &Y2);
3230 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), X1, Y1, 0.0, &x, &y);
3231 SceneModel::worldToImgCoords(SM_CALIB_MATRIX(smodel), x, y, smodel->m_OneMeterRepresentation, &X2, &Y2);
3244 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), X1, Y1, 0.0, &x, &y);
3245 SceneModel::worldToImgCoords(SM_CALIB_MATRIX(smodel), x, y, smodel->m_OneMeterRepresentation, &X2, &Y2);
3252 template <
typename T>
3256 int LeftToRight, FrontToBack;
3260 else if(xleft > x2d)
3272 return LeftToRight + 3*FrontToBack;
3275 template <
typename T>
3279 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), (xleft + xright) / 2.0, ybottom, 0, &x, &y);
3280 else if(position < 6)
3281 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), (xleft + xright) / 2.0, (ybottom + ytop) / 2.0, 0, &x, &y);
3283 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), (xleft + xright) / 2.0, ytop / 2.0, 0, &x, &y);
3292 template <
typename T>
3295 SceneModel::imgToWorldCoordsGivenHeight(SM_CALIB_MATRIX(smodel), (xleft + xright) / 2.0, ybottom, 0.0, &x, &y);
3302 template <
typename T>
3304 if( RECT_YBOTTOM(r1) <= RECT_YBOTTOM(r2) && RECT_YTOP(r1) >= RECT_YTOP(r2)
3305 && RECT_XRIGHT(r1) <= RECT_XRIGHT(r2) && RECT_XLEFT(r1) >= RECT_XLEFT(r2) )
3310 template <
typename T>
3312 if( RECT_YBOTTOM(&r) <= RECT_YBOTTOM(
this) && RECT_YTOP(&r) >= RECT_YTOP(
this)
3313 && RECT_XRIGHT(&r) <= RECT_XRIGHT(
this) && RECT_XLEFT(&r) >= RECT_XLEFT(
this) )
3319 template <
typename T>
3323 if(!rectanglesIntersection(r1, r2, &intersection))
3325 return intersection.rectangleArea();
3328 template <
typename T>
3332 if(!rectanglesIntersection(r1, r2, intersection))
3334 return intersection.rectangleArea();
3338 template <
typename T>
3340 return xleft <= p_x && p_x <= xright && ytop <= p_y && p_y <= ybottom;
3344 template <
typename T>
3348 if( RECT_XLEFT(r1) >= RECT_XRIGHT(r2) || RECT_XRIGHT(r1) <= RECT_XLEFT(r2) )
3352 if( RECT_YBOTTOM(r1) <= RECT_YTOP(r2) || RECT_YTOP(r1) >= RECT_YBOTTOM(r2) )
3358 template <
typename T>
3362 if( RECT_XLEFT(
this) >= RECT_XRIGHT(&r) || RECT_XRIGHT(
this) <= RECT_XLEFT(&r) )
3366 if( RECT_YBOTTOM(
this) <= RECT_YTOP(&r) || RECT_YTOP(
this) >= RECT_YBOTTOM(&r) )
3388 template <
typename T>
3391 typedef typename std::deque< Rectangle<T> >::iterator rec_deq_iter_type;
3393 std::deque< Rectangle<T> > local_results;
3395 local_results.push_back(*
this);
3397 int size = toSubstract.size();
3401 rec_deq_iter_type it;
3406 for(i=0; i<size; i++) {
3408 for(it = local_results.begin(); it != local_results.end(); ) {
3411 if(!r.thereIsIntersection(s)) {
3418 local_results.push_back(
Rectangle<T>(r.xleft, r.ytop, r.width, s.ytop - r.ytop + 1));
3420 yt = std::max(r.ytop, s.ytop);
3421 yb = std::min(r.ybottom, s.ybottom);
3423 if(r.xleft < s.xleft)
3424 local_results.push_back(
Rectangle<T>(r.xleft, yt, s.xleft - r.xleft + 1, yb - yt + 1));
3427 if(s.xright < r.xright)
3428 local_results.push_back(
Rectangle<T>(s.xright, yt, r.xright - s.xright + 1, yb - yt + 1));
3431 if(s.ybottom < r.ybottom)
3432 local_results.push_back(
Rectangle<T>(r.xleft, s.ybottom, r.width, r.ybottom - s.ybottom + 1));
3435 it = local_results.erase(it);
3439 if(local_results.empty())
3443 if(local_results.empty())
3446 rec_deq_iter_type lit, lend = local_results.end();
3447 for(lit = local_results.begin(); lit != lend; lit++)
3448 results.push_back(*lit);
3452 template <
typename T>
3454 if(r.xleft != this->xleft)
3456 if(r.xright != this->xright)
3458 if(r.ytop != this->ytop)
3460 if(r.ybottom != this->ybottom)
Definition: geometric.h:103
Definition: geometric.h:114
Definition: geometric.h:20
Definition: geometric.h:36
Definition: calibration.h:51
Definition: calibration.h:26
Definition: calibration.h:23
Definition: gtstructures.h:32
Definition: geometric.h:23
Definition: geometric.h:256
Definition: geometric.h:224