[postgis-tickets] r15900 - Combine multiple signum definitions (2.2)

Paul Ramsey pramsey at cleverelephant.ca
Thu Oct 5 06:21:29 PDT 2017


Author: pramsey
Date: 2017-10-05 06:21:29 -0700 (Thu, 05 Oct 2017)
New Revision: 15900

Modified:
   branches/2.2/NEWS
   branches/2.2/liblwgeom/cunit/cu_geodetic.c
   branches/2.2/liblwgeom/cunit/cu_libgeom.c
   branches/2.2/liblwgeom/liblwgeom_internal.h
   branches/2.2/liblwgeom/lwalgorithm.c
   branches/2.2/liblwgeom/lwgeodetic.c
   branches/2.2/liblwgeom/lwgeodetic.h
   branches/2.2/liblwgeom/lwspheroid.c
Log:
Combine multiple signum definitions (2.2)
(References #3878)


Modified: branches/2.2/NEWS
===================================================================
--- branches/2.2/NEWS	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/NEWS	2017-10-05 13:21:29 UTC (rev 15900)
@@ -10,6 +10,7 @@
   - #3866, Rare crash generating TWKB with large coordinate values
   - #3869, Fix build with "gold" linker
   - #3879, Division by zero in some arc cases
+  - #3878, Single defn of signum in header
 
 
 PostGIS 2.2.5

Modified: branches/2.2/liblwgeom/cunit/cu_geodetic.c
===================================================================
--- branches/2.2/liblwgeom/cunit/cu_geodetic.c	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/cunit/cu_geodetic.c	2017-10-05 13:21:29 UTC (rev 15900)
@@ -66,13 +66,6 @@
 	p->lon = rad2deg(p->lon);
 }
 
-static void test_signum(void)
-{
-	CU_ASSERT_EQUAL(signum(-5.0),-1);
-	CU_ASSERT_EQUAL(signum(5.0),1);
-}
-
-
 static void test_sphere_direction(void)
 {
 	GEOGRAPHIC_POINT s, e;
@@ -1586,7 +1579,6 @@
 	PG_ADD_TEST(suite, test_sphere_direction);
 	PG_ADD_TEST(suite, test_sphere_project);
 	PG_ADD_TEST(suite, test_lwgeom_area_sphere);
-	PG_ADD_TEST(suite, test_signum);
 	PG_ADD_TEST(suite, test_gbox_from_spherical_coordinates);
 	PG_ADD_TEST(suite, test_gserialized_get_gbox_geocentric);
 	PG_ADD_TEST(suite, test_clairaut);

Modified: branches/2.2/liblwgeom/cunit/cu_libgeom.c
===================================================================
--- branches/2.2/liblwgeom/cunit/cu_libgeom.c	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/cunit/cu_libgeom.c	2017-10-05 13:21:29 UTC (rev 15900)
@@ -1072,6 +1072,16 @@
     lwfree(s3);
 }
 
+void test_signum_macro(void);
+void test_signum_macro(void)
+{
+	CU_ASSERT_EQUAL(SIGNUM(-5.0),-1);
+	CU_ASSERT_EQUAL(SIGNUM( 5.0), 1);
+	CU_ASSERT_EQUAL(SIGNUM( 0.0), 0);
+	CU_ASSERT_EQUAL(SIGNUM(10) * 5, 5);
+	CU_ASSERT_EQUAL(SIGNUM(-10) * 5, -5);
+}
+
 /*
 ** Used by test harness to register the tests in this file.
 */
@@ -1102,4 +1112,5 @@
 	PG_ADD_TEST(suite, test_lwgeom_scale);
 	PG_ADD_TEST(suite, test_gserialized_is_empty);
     PG_ADD_TEST(suite, test_gbox_same_2d);
+	PG_ADD_TEST(suite, test_signum_macro);
 }

Modified: branches/2.2/liblwgeom/liblwgeom_internal.h
===================================================================
--- branches/2.2/liblwgeom/liblwgeom_internal.h	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/liblwgeom_internal.h	2017-10-05 13:21:29 UTC (rev 15900)
@@ -109,6 +109,14 @@
 #define SIZE_SET(varsize, size) (((varsize) & 0x00000003)|(((size) & 0x3FFFFFFF) << 2 ))
 
 /**
+* Macro that returns:
+* -1 if n < 0,
+*  1 if n > 0,
+*  0 if n == 0
+*/
+#define SIGNUM(n) (((n) > 0) - ((n) < 0))
+
+/**
 * Tolerance used to determine equality.
 */
 #define EPSILON_SQLMM 1e-8
@@ -192,11 +200,6 @@
 LWCOLLECTION* lwcollection_simplify(const LWCOLLECTION *igeom, double dist, int preserve_collapsed);
 
 /*
-* Computational geometry
-*/
-int signum(double n);
-
-/*
 * The possible ways a pair of segments can interact. Returned by lw_segment_intersects 
 */
 enum CG_SEGMENT_INTERSECTION_TYPE {

Modified: branches/2.2/liblwgeom/lwalgorithm.c
===================================================================
--- branches/2.2/liblwgeom/lwalgorithm.c	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/lwalgorithm.c	2017-10-05 13:21:29 UTC (rev 15900)
@@ -13,17 +13,6 @@
 #include "lwgeom_log.h"
 #include <ctype.h> /* for tolower */
 
-
-/**
-* Returns -1 if n < 0.0 and 1 if n > 0.0
-*/
-int signum(double n)
-{
-	if( n < 0 ) return -1;
-	if( n > 0 ) return 1;
-	return 0;
-}
-
 int
 p4d_same(const POINT4D *p1, const POINT4D *p2)
 {
@@ -61,10 +50,7 @@
 int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
 {
 	double side = ( (q->x - p1->x) * (p2->y - p1->y) - (p2->x - p1->x) * (q->y - p1->y) );
-	if ( side == 0.0 )
-		return 0;
-	else
-		return signum(side);
+	return SIGNUM(side);
 }
 
 /**

Modified: branches/2.2/liblwgeom/lwgeodetic.c
===================================================================
--- branches/2.2/liblwgeom/lwgeodetic.c	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/lwgeodetic.c	2017-10-05 13:21:29 UTC (rev 15900)
@@ -615,8 +615,8 @@
 
 int crosses_dateline(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
 {
-	double sign_s = signum(s->lon);
-	double sign_e = signum(e->lon);
+	double sign_s = SIGNUM(s->lon);
+	double sign_e = SIGNUM(e->lon);
 	double ss = fabs(s->lon);
 	double ee = fabs(e->lon);
 	if ( sign_s == sign_e )
@@ -818,7 +818,7 @@
 	}
 
 	/* Over the pole, we need normalize latitude and do this calculation in latitude */
-	if ( FP_EQUALS( slon, M_PI ) && ( signum(g.start.lon) != signum(g.end.lon) || FP_EQUALS(dlon, M_PI) ) )
+	if ( FP_EQUALS( slon, M_PI ) && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) || FP_EQUALS(dlon, M_PI) ) )
 	{
 		LWDEBUG(4, "over the pole...");
 		/* Antipodal, everything (or nothing?) is inside */
@@ -862,7 +862,7 @@
 	}
 
 	/* Dateline crossing, flip everything to the opposite hemisphere */
-	else if ( slon > M_PI && ( signum(g.start.lon) != signum(g.end.lon) ) )
+	else if ( slon > M_PI && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) ) )
 	{
 		LWDEBUG(4, "crosses dateline, flip longitudes...");
 		if ( g.start.lon > 0.0 )
@@ -967,7 +967,7 @@
 	double c_dist = sphere_distance(a, b);
 	double hca = sphere_direction(c, a, b_dist);
 	double hcb = sphere_direction(c, b, a_dist);
-	double sign = signum(hcb-hca);
+	double sign = SIGNUM(hcb-hca);
 	double ss = (a_dist + b_dist + c_dist) / 2.0;
 	double E = tan(ss/2.0)*tan((ss-a_dist)/2.0)*tan((ss-b_dist)/2.0)*tan((ss-c_dist)/2.0);
 	return 4.0 * atan(sqrt(fabs(E))) * sign;
@@ -996,7 +996,7 @@
 */
 double z_to_latitude(double z, int top)
 {
-	double sign = signum(z);
+	double sign = SIGNUM(z);
 	double tlat = acos(z);
 	LWDEBUGF(4, "inputs: z(%.8g) sign(%.8g) tlat(%.8g)", z, sign, tlat);
 	if (FP_IS_ZERO(z))

Modified: branches/2.2/liblwgeom/lwgeodetic.h
===================================================================
--- branches/2.2/liblwgeom/lwgeodetic.h	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/lwgeodetic.h	2017-10-05 13:21:29 UTC (rev 15900)
@@ -59,12 +59,7 @@
 #define deg2rad(d) (M_PI * (d) / 180.0)
 #define rad2deg(r) (180.0 * (r) / M_PI)
 
-/**
-* Ape a java function
-*/
-#define signum(a) ((a) < 0 ? -1 : ((a) > 0 ? 1 : (a)))
 
-
 /**
 * Bitmask elements for edge_intersects() return value.
 */

Modified: branches/2.2/liblwgeom/lwspheroid.c
===================================================================
--- branches/2.2/liblwgeom/lwspheroid.c	2017-10-05 13:04:27 UTC (rev 15899)
+++ branches/2.2/liblwgeom/lwspheroid.c	2017-10-05 13:21:29 UTC (rev 15900)
@@ -473,7 +473,7 @@
 	LWDEBUGF(4, "tE %.12g", tE);
 
 	ratio = (bE + tE)/tE;
-	sign = signum(B.lon - A.lon);
+	sign = SIGNUM(B.lon - A.lon);
 	return (baseArea + topArea / ratio) * sign;
 }
 
@@ -497,7 +497,7 @@
 	/* Get the raw min/max values for the latitudes */
 	ptarray_calculate_gbox_cartesian(pa, &gbox2d);
 
-	if ( signum(gbox2d.ymin) != signum(gbox2d.ymax) )
+	if ( SIGNUM(gbox2d.ymin) != SIGNUM(gbox2d.ymax) )
 		lwerror("ptarray_area_spheroid: cannot handle ptarray that crosses equator");
 
 	/* Geodetic bbox < 0.0 implies geometry is entirely in southern hemisphere */



More information about the postgis-tickets mailing list