[postgis-tickets] [SCM] PostGIS branch main updated. 3.2.0alpha1-27-gb6cce01

git at osgeo.org git at osgeo.org
Mon Sep 27 00:08:59 PDT 2021


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "PostGIS".

The branch, main has been updated
       via  b6cce01cdaf6d31169d73871c374cae332f66992 (commit)
      from  894916e50a63091b8c7e5fb641892379eda559c7 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b6cce01cdaf6d31169d73871c374cae332f66992
Author: Sandro Santilli <strk at kbt.io>
Date:   Mon Sep 27 09:08:21 2021 +0200

    Simplify closest_point_on_segment code, add unit test

diff --git a/liblwgeom/cunit/cu_ptarray.c b/liblwgeom/cunit/cu_ptarray.c
index ee0ab76..a6299b6 100644
--- a/liblwgeom/cunit/cu_ptarray.c
+++ b/liblwgeom/cunit/cu_ptarray.c
@@ -731,6 +731,108 @@ static void test_ptarray_closest_segment_2d()
 	lwline_free(line);
 }
 
+static void test_ptarray_closest_point_on_segment(void)
+{
+	LWLINE *line;
+	double loc, dist;
+	POINT4D s0, s1, qp, cp;
+
+	s0.x = s0.y = 0; s0.z = 10; s0.m = 20;
+	s1.x = 0; s1.y = 10; s1.z = 0; s1.m = 10;
+
+	/* Closest is bottom point */
+
+	qp.x = -0.1; qp.y = 0;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 0);
+	ASSERT_DOUBLE_EQUAL(cp.z, 10);
+	ASSERT_DOUBLE_EQUAL(cp.m, 20);
+
+	qp.x = 0.1; qp.y = 0;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 0);
+	ASSERT_DOUBLE_EQUAL(cp.z, 10);
+	ASSERT_DOUBLE_EQUAL(cp.m, 20);
+
+	qp.x = 0; qp.y = -0.1;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 0);
+	ASSERT_DOUBLE_EQUAL(cp.z, 10);
+	ASSERT_DOUBLE_EQUAL(cp.m, 20);
+
+	/* Closest is top point */
+
+	qp.x = 0; qp.y = 10.1;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 10);
+	ASSERT_DOUBLE_EQUAL(cp.z, 0);
+	ASSERT_DOUBLE_EQUAL(cp.m, 10);
+
+	qp.x = 0.1; qp.y = 10;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 10);
+	ASSERT_DOUBLE_EQUAL(cp.z, 0);
+	ASSERT_DOUBLE_EQUAL(cp.m, 10);
+
+	qp.x = -0.1; qp.y = 10;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 10);
+	ASSERT_DOUBLE_EQUAL(cp.z, 0);
+	ASSERT_DOUBLE_EQUAL(cp.m, 10);
+
+	/* Closest is mid point */
+
+	qp.x = 0.1; qp.y = 5;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 5);
+	ASSERT_DOUBLE_EQUAL(cp.z, 5);
+	ASSERT_DOUBLE_EQUAL(cp.m, 15);
+
+	qp.x = -0.1; qp.y = 5;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 5);
+	ASSERT_DOUBLE_EQUAL(cp.z, 5);
+	ASSERT_DOUBLE_EQUAL(cp.m, 15);
+
+	qp.x = 0.1; qp.y = 2;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 2);
+	ASSERT_DOUBLE_EQUAL(cp.z, 8);
+	ASSERT_DOUBLE_EQUAL(cp.m, 18);
+
+	qp.x = -0.1; qp.y = 2;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 2);
+	ASSERT_DOUBLE_EQUAL(cp.z, 8);
+	ASSERT_DOUBLE_EQUAL(cp.m, 18);
+
+	qp.x = 0.1; qp.y = 8;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 8);
+	ASSERT_DOUBLE_EQUAL(cp.z, 2);
+	ASSERT_DOUBLE_EQUAL(cp.m, 12);
+
+	qp.x = -0.1; qp.y = 8;
+	closest_point_on_segment(&qp, &s0, &s1, &cp);
+	ASSERT_DOUBLE_EQUAL(cp.x, 0);
+	ASSERT_DOUBLE_EQUAL(cp.y, 8);
+	ASSERT_DOUBLE_EQUAL(cp.z, 2);
+	ASSERT_DOUBLE_EQUAL(cp.m, 12);
+
+
+}
+
 
 /*
 ** Used by the test harness to register the tests in this file.
@@ -751,4 +853,5 @@ void ptarray_suite_setup(void)
 	PG_ADD_TEST(suite, test_ptarray_scroll);
 	PG_ADD_TEST(suite, test_ptarray_closest_vertex_2d);
 	PG_ADD_TEST(suite, test_ptarray_closest_segment_2d);
+	PG_ADD_TEST(suite, test_ptarray_closest_point_on_segment);
 }
diff --git a/liblwgeom/ptarray.c b/liblwgeom/ptarray.c
index 7994d60..d903d80 100644
--- a/liblwgeom/ptarray.c
+++ b/liblwgeom/ptarray.c
@@ -1278,12 +1278,12 @@ closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, P
 	 */
 	r = ( (p->x-A->x) * (B->x-A->x) + (p->y-A->y) * (B->y-A->y) )/( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
 
-	if (r<0)
+	if (r<=0)
 	{
 		*ret = *A;
 		return;
 	}
-	if (r>1)
+	if (r>=1)
 	{
 		*ret = *B;
 		return;

-----------------------------------------------------------------------

Summary of changes:
 liblwgeom/cunit/cu_ptarray.c | 103 +++++++++++++++++++++++++++++++++++++++++++
 liblwgeom/ptarray.c          |   4 +-
 2 files changed, 105 insertions(+), 2 deletions(-)


hooks/post-receive
-- 
PostGIS


More information about the postgis-tickets mailing list