/* On the left */
q.x = -2.0;
q.y = 1.5;
- rv = lw_segment_side(p1, p2, q);
+ rv = lw_segment_side(&p1, &p2, &q);
//printf("left %g\n",rv);
CU_ASSERT(rv < 0.0);
/* On the right */
q.x = 2.0;
- rv = lw_segment_side(p1, p2, q);
+ rv = lw_segment_side(&p1, &p2, &q);
//printf("right %g\n",rv);
CU_ASSERT(rv > 0.0);
/* On the line */
q.x = 0.0;
- rv = lw_segment_side(p1, p2, q);
+ rv = lw_segment_side(&p1, &p2, &q);
//printf("on line %g\n",rv);
CU_ASSERT_EQUAL(rv, 0.0);
q1.y = 0.5;
q2.x = 0.5;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_RIGHT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_RIGHT );
/* Q: Horizontal line crossing right to left */
q1.x = 0.5;
q1.y = 0.5;
q2.x = -0.5;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_LEFT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_LEFT );
/* Q: Horizontal line not crossing right to left */
q1.x = 0.5;
q1.y = 1.5;
q2.x = -0.5;
q2.y = 1.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal line crossing at second vertex right to left */
q1.x = 0.5;
q1.y = 1.0;
q2.x = -0.5;
q2.y = 1.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal line crossing at first vertex right to left */
q1.x = 0.5;
q1.y = 0.0;
q2.x = -0.5;
q2.y = 0.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_LEFT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_LEFT );
/* Q: Diagonal line with large range crossing at first vertex right to left */
q1.x = 0.5;
q1.y = 10.0;
q2.x = -0.5;
q2.y = -10.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_LEFT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_LEFT );
/* Q: Diagonal line with large range crossing at second vertex right to left */
q1.x = 0.5;
q1.y = 11.0;
q2.x = -0.5;
q2.y = -9.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal touching from left at second vertex*/
q1.x = -0.5;
q1.y = 0.5;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal touching from right at first vertex */
q1.x = 0.0;
q1.y = 0.5;
q2.x = 0.5;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_RIGHT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_RIGHT );
/* Q: Horizontal touching from left and far below on second vertex */
q1.x = -0.5;
q1.y = -10.5;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal touching from right and far above on second vertex */
q1.x = 0.5;
q1.y = 10.5;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Co-linear from top */
q1.x = 0.0;
q1.y = 10.0;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_COLINEAR );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_COLINEAR );
/* Q: Co-linear from bottom */
q1.x = 0.0;
q1.y = -10.0;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_COLINEAR );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_COLINEAR );
/* Q: Co-linear contained */
q1.x = 0.0;
q1.y = 0.4;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_COLINEAR );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_COLINEAR );
/* Q: Horizontal touching at end point from left */
q1.x = -0.5;
q1.y = 1.0;
q2.x = 0.0;
q2.y = 1.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_NO_INTERSECTION );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_NO_INTERSECTION );
/* Q: Horizontal touching at end point from right */
q1.x = 0.0;
q1.y = 1.0;
q2.x = 0.0;
q2.y = 0.5;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_COLINEAR );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_COLINEAR );
/* Q: Horizontal touching at start point from left */
q1.x = 0.0;
q1.y = 0.0;
q2.x = -0.5;
q2.y = 0.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_LEFT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_LEFT );
/* Q: Horizontal touching at start point from right */
q1.x = 0.0;
q1.y = 0.0;
q2.x = 0.5;
q2.y = 0.0;
- CU_ASSERT( lw_segment_intersects(p1, p2, q1, q2) == SEG_CROSS_RIGHT );
+ CU_ASSERT( lw_segment_intersects(&p1, &p2, &q1, &q2) == SEG_CROSS_RIGHT );
}
}
if (
- (NULL == CU_add_test(pSuite, "test_mindistance2d_tolerance()", test_mindistance2d_tolerance))
+ (NULL == CU_add_test(pSuite, "test_mindistance2d_tolerance()", test_mindistance2d_tolerance)) ||
+ (NULL == CU_add_test(pSuite, "test_rect_tree_contains_point()", test_rect_tree_contains_point)) ||
+ (NULL == CU_add_test(pSuite, "test_rect_tree_intersects_tree()", test_rect_tree_intersects_tree))
)
{
CU_cleanup_registry();
}
+void test_rect_tree_contains_point(void)
+{
+ LWPOLY *poly;
+ POINT2D p;
+ RECT_NODE* tree;
+ int result;
+ int boundary = 0;
+
+ /* square */
+ poly = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", PARSER_CHECK_NONE);
+ tree = rect_tree_new(poly->rings[0]);
+
+ /* inside square */
+ boundary = 0;
+ p.x = 0.5;
+ p.y = 0.5;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_NOT_EQUAL(result, 0);
+
+ /* outside square */
+ boundary = 0;
+ p.x = 1.5;
+ p.y = 0.5;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(result, 0);
+
+ rect_tree_free(tree);
+ lwpoly_free(poly);
+
+ /* ziggy zaggy horizontal saw tooth polygon */
+ poly = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 1 3, 2 0, 3 3, 4 0, 4 5, 0 5, 0 0))", PARSER_CHECK_NONE);
+ tree = rect_tree_new(poly->rings[0]);
+
+ /* not in, left side */
+ boundary = 0;
+ p.x = -0.5;
+ p.y = 0.5;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(result, 0);
+
+ /* not in, right side */
+ boundary = 0;
+ p.x = 3.0;
+ p.y = 1.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(result, 0);
+
+ /* inside */
+ boundary = 0;
+ p.x = 2.0;
+ p.y = 1.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_NOT_EQUAL(result, 0);
+
+ /* on left border */
+ boundary = 0;
+ p.x = 0.0;
+ p.y = 1.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on right border */
+ boundary = 0;
+ p.x = 4.0;
+ p.y = 0.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on tooth concave */
+ boundary = 0;
+ p.x = 3.0;
+ p.y = 3.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on tooth convex */
+ boundary = 0;
+ p.x = 2.0;
+ p.y = 0.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ rect_tree_free(tree);
+ lwpoly_free(poly);
+
+ /* ziggy zaggy vertical saw tooth polygon */
+ poly = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ tree = rect_tree_new(poly->rings[0]);
+
+ /* not in, left side */
+ boundary = 0;
+ p.x = -0.5;
+ p.y = 3.5;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(result, 0);
+
+ /* not in, right side */
+ boundary = 0;
+ p.x = 6.0;
+ p.y = 2.2;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(result, 0);
+ /* inside */
+ boundary = 0;
+ p.x = 3.0;
+ p.y = 2.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_NOT_EQUAL(result, 0);
+
+ /* on bottom border */
+ boundary = 0;
+ p.x = 1.0;
+ p.y = 0.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on top border */
+ boundary = 0;
+ p.x = 3.0;
+ p.y = 6.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on tooth concave */
+ boundary = 0;
+ p.x = 3.0;
+ p.y = 1.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on tooth convex */
+ boundary = 0;
+ p.x = 0.0;
+ p.y = 2.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ /* on tooth convex */
+ boundary = 0;
+ p.x = 0.0;
+ p.y = 6.0;
+ result = rect_tree_contains_point(tree, &p, &boundary);
+ CU_ASSERT_EQUAL(boundary, 1);
+
+ rect_tree_free(tree);
+ lwpoly_free(poly);
+
+}
+
+void test_rect_tree_intersects_tree(void)
+{
+ LWPOLY *poly1, *poly2;
+ RECT_NODE *tree1, *tree2;
+ int result;
+
+ poly1 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ poly2 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ tree1 = rect_tree_new(poly1->rings[0]);
+ tree2 = rect_tree_new(poly2->rings[0]);
+ result = rect_tree_intersects_tree(tree1, tree2);
+ CU_ASSERT_EQUAL(result, LW_TRUE);
+ lwpoly_free(poly1);
+ lwpoly_free(poly2);
+ rect_tree_free(tree1);
+ rect_tree_free(tree2);
+
+ poly1 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ poly2 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0.3 0.7, 0.3 0.8, 0.4 0.8, 0.4 0.7, 0.3 0.7))", PARSER_CHECK_NONE);
+ tree1 = rect_tree_new(poly1->rings[0]);
+ tree2 = rect_tree_new(poly2->rings[0]);
+ result = rect_tree_intersects_tree(tree1, tree2);
+ CU_ASSERT_EQUAL(result, LW_FALSE);
+ lwpoly_free(poly1);
+ lwpoly_free(poly2);
+ rect_tree_free(tree1);
+ rect_tree_free(tree2);
+
+ poly1 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ poly2 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0.3 0.7, 0.3 0.8, 0.4 0.8, 1.3 0.3, 0.3 0.7))", PARSER_CHECK_NONE);
+ tree1 = rect_tree_new(poly1->rings[0]);
+ tree2 = rect_tree_new(poly2->rings[0]);
+ result = rect_tree_intersects_tree(tree1, tree2);
+ CU_ASSERT_EQUAL(result, LW_TRUE);
+ lwpoly_free(poly1);
+ lwpoly_free(poly2);
+ rect_tree_free(tree1);
+ rect_tree_free(tree2);
+/*
+ poly1 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((0 0, 3 1, 0 2, 3 3, 0 4, 3 5, 0 6, 5 6, 5 0, 0 0))", PARSER_CHECK_NONE);
+ poly2 = (LWPOLY*)lwgeom_from_ewkt("POLYGON((-1 5, 0 5, 0 7, -1 7, -1 5))", PARSER_CHECK_NONE);
+ tree1 = rect_tree_new(poly1->rings[0]);
+ tree2 = rect_tree_new(poly2->rings[0]);
+ result = rect_tree_intersects_tree(tree1, tree2);
+ CU_ASSERT_EQUAL(result, LW_TRUE);
+ lwpoly_free(poly1);
+ lwpoly_free(poly2);
+ rect_tree_free(tree1);
+ rect_tree_free(tree2);
+*/
+
+}