Skip to content

Commit 41c0326

Browse files
committed
Add spoly_is_convex
1 parent 5228df6 commit 41c0326

9 files changed

+140
-0
lines changed

Diff for: doc/functions.sgm

+27
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,33 @@
618618
</programlisting>
619619
</example>
620620
</sect3>
621+
622+
<sect3 id="funcs.spoly.isconvex">
623+
<title>
624+
Spoly is convex
625+
</title>
626+
<para>
627+
Returns true if the specified spherical polygon is convex.
628+
Returns false otherwise.
629+
</para>
630+
<funcsynopsis>
631+
<funcprototype>
632+
<funcdef><function>spoly_is_convex</function></funcdef>
633+
<paramdef>spoly <parameter>polygon</parameter></paramdef>
634+
</funcprototype>
635+
</funcsynopsis>
636+
<example>
637+
<title>Check if polygon is convex</title>
638+
<programlisting>
639+
<![CDATA[sql> SELECT spoly_is_convex( spoly '{(0,0),(1,0),(1,1),(1,2)}' );]]>
640+
<![CDATA[ spoly_is_convex]]>
641+
<![CDATA[-----------------]]>
642+
<![CDATA[ t]]>
643+
<![CDATA[ (1 row)]]>
644+
</programlisting>
645+
</example>
646+
</sect3>
647+
621648
</sect2>
622649

623650
<sect2 id="funcs.sbox">

Diff for: expected/init_test.out.in

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ psql:pg_sphere.test.sql:8622: NOTICE: argument type pointkey is only a shell
3333
psql:pg_sphere.test.sql:8628: NOTICE: argument type pointkey is only a shell
3434
psql:pg_sphere.test.sql:8634: NOTICE: argument type pointkey is only a shell
3535
psql:pg_sphere.test.sql:8640: NOTICE: argument type pointkey is only a shell
36+

Diff for: expected/init_test_healpix.out.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
psql:pg_sphere.test.sql:9207: NOTICE: return type smoc is only a shell
22
psql:pg_sphere.test.sql:9213: NOTICE: argument type smoc is only a shell
3+

Diff for: expected/poly.out

+25
Original file line numberDiff line numberDiff line change
@@ -1799,3 +1799,28 @@ SELECT spoly_as_array( spoly '{(0,0),(1,0),(1,1)}' );
17991799
{"(0 , 0)","(1 , 0)","(1 , 1)"}
18001800
(1 row)
18011801

1802+
-- spoly is convex
1803+
SELECT spoly_is_convex(spoly'{(53d 45m 35.0s, 37d 6m 30.0s), (52d 21m 36.0s, 41d 36m 7.0s), (54d 14m 18.0s, 45d 1m 35.0s), (51d 23m 3.0s, 45d 22m 49.0s), (51d 2m 12.0s, 41d 52m 1.0s), (50d 41m 47.0s, 38d 22m 0s) }');
1804+
spoly_is_convex
1805+
-----------------
1806+
f
1807+
(1 row)
1808+
1809+
SELECT spoly_is_convex(spoly'{(12d,32d),(34d,12d),(59d,21d),(69d,21d)}');
1810+
spoly_is_convex
1811+
-----------------
1812+
f
1813+
(1 row)
1814+
1815+
SELECT spoly_is_convex(spoly'{(12d,32d),(34d,12d),(59d,21d),(34d,40d)}');
1816+
spoly_is_convex
1817+
-----------------
1818+
t
1819+
(1 row)
1820+
1821+
SELECT spoly_is_convex(NULL);
1822+
spoly_is_convex
1823+
-----------------
1824+
f
1825+
(1 row)
1826+

Diff for: pgs_polygon.sql.in

+14
Original file line numberDiff line numberDiff line change
@@ -973,3 +973,17 @@ CREATE AGGREGATE spoly (
973973
stype = spoly,
974974
finalfunc = spoly_add_points_fin_aggr
975975
);
976+
977+
978+
--
979+
-- polygon is convex
980+
--
981+
982+
CREATE FUNCTION spoly_is_convex(spoly)
983+
RETURNS BOOL
984+
AS 'MODULE_PATHNAME', 'spherepoly_is_convex'
985+
LANGUAGE 'c'
986+
IMMUTABLE PARALLEL SAFE;
987+
988+
COMMENT ON FUNCTION spoly_is_convex(spoly) IS
989+
'true if spherical polygon is convex';

Diff for: sql/poly.sql

+6
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,9 @@ SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 1 );
616616
SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 2 );
617617
SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 3 );
618618
SELECT spoly_as_array( spoly '{(0,0),(1,0),(1,1)}' );
619+
620+
-- spoly is convex
621+
SELECT spoly_is_convex(spoly'{(53d 45m 35.0s, 37d 6m 30.0s), (52d 21m 36.0s, 41d 36m 7.0s), (54d 14m 18.0s, 45d 1m 35.0s), (51d 23m 3.0s, 45d 22m 49.0s), (51d 2m 12.0s, 41d 52m 1.0s), (50d 41m 47.0s, 38d 22m 0s) }');
622+
SELECT spoly_is_convex(spoly'{(12d,32d),(34d,12d),(59d,21d),(69d,21d)}');
623+
SELECT spoly_is_convex(spoly'{(12d,32d),(34d,12d),(59d,21d),(34d,40d)}');
624+
SELECT spoly_is_convex(NULL);

Diff for: src/polygon.c

+52
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PG_FUNCTION_INFO_V1(spheretrans_poly);
5959
PG_FUNCTION_INFO_V1(spheretrans_poly_inverse);
6060
PG_FUNCTION_INFO_V1(spherepoly_add_point);
6161
PG_FUNCTION_INFO_V1(spherepoly_add_points_finalize);
62+
PG_FUNCTION_INFO_V1(spherepoly_is_convex);
6263

6364

6465
/*
@@ -1531,3 +1532,54 @@ spherepoly_add_points_finalize(PG_FUNCTION_ARGS)
15311532
}
15321533
PG_RETURN_POINTER(poly);
15331534
}
1535+
1536+
1537+
Datum
1538+
spherepoly_is_convex(PG_FUNCTION_ARGS)
1539+
{
1540+
Vector3D u,
1541+
v,
1542+
vsu,
1543+
wsv,
1544+
crs;
1545+
int32 i;
1546+
float8 cur = 0.0,
1547+
prev = 0.0;
1548+
SPOLY *poly = (SPOLY *) PG_GETARG_POINTER(0);
1549+
1550+
if (poly == NULL)
1551+
{
1552+
PG_RETURN_BOOL(false);
1553+
}
1554+
1555+
poly = PG_GETARG_SPOLY(0);
1556+
if (poly->npts == 3)
1557+
{
1558+
PG_RETURN_BOOL(true);
1559+
}
1560+
1561+
for (i = 0; i < poly->npts; i++)
1562+
{
1563+
const int j = (i - 1 + poly->npts) % poly->npts;
1564+
const int k = (i + 1) % poly->npts;
1565+
1566+
spoint_vector3d(&u, &poly->p[i]);
1567+
spoint_vector3d(&v, &poly->p[j]);
1568+
spoint_vector3d(&vsu, &poly->p[j]);
1569+
spoint_vector3d(&wsv, &poly->p[k]);
1570+
1571+
vector3d_addwithscalar(&vsu, -1, &u);
1572+
vector3d_addwithscalar(&wsv, -1, &v);
1573+
1574+
vector3d_cross(&crs, &vsu, &wsv);
1575+
1576+
cur = vector3d_scalar(&crs, &v);
1577+
if (cur * prev < 0)
1578+
{
1579+
PG_RETURN_BOOL(false);
1580+
}
1581+
prev = cur;
1582+
}
1583+
1584+
PG_RETURN_BOOL(true);
1585+
}

Diff for: src/polygon.h

+5
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,9 @@ Datum spherepoly_add_points_finalize(PG_FUNCTION_ARGS);
358358
*/
359359
Datum spherepoly_get_array(PG_FUNCTION_ARGS);
360360

361+
/*
362+
* Checks whether a polygon is convex
363+
*/
364+
Datum spherepoly_is_convex(PG_FUNCTION_ARGS);
365+
361366
#endif

Diff for: upgrade_scripts/pg_sphere--1.2.3--1.3.0.sql.in

+9
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,12 @@ CREATE FUNCTION spoly_as_array(spoly)
5454

5555
COMMENT ON FUNCTION spoly_as_array(spoly) IS
5656
'returns spoly as array of points';
57+
58+
CREATE FUNCTION spoly_is_convex(spoly)
59+
RETURNS BOOL
60+
AS 'MODULE_PATHNAME', 'spherepoly_is_convex'
61+
LANGUAGE 'c'
62+
IMMUTABLE PARALLEL SAFE;
63+
64+
COMMENT ON FUNCTION spoly_is_convex(spoly) IS
65+
'true if spherical polygon is convex';

0 commit comments

Comments
 (0)