Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f855597

Browse files
df7cbmsdemlei
authored andcommitted
Add smoc rounding function that converts smocs to lower order
All touching and overlapping bigger pixels are included in the result.
1 parent 33807c7 commit f855597

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

expected/moc.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ SELECT intersection(moc) FROM (VALUES ('0/1-4'::smoc), ('0/2-5'), (NULL)) sub(mo
589589
0/2-4
590590
(1 row)
591591

592+
SELECT smoc_round(6, '7/1,3,5,9');
593+
smoc_round
594+
------------
595+
6/0-2
596+
(1 row)
597+
598+
SELECT smoc_round(5, '7/1,3,5,9');
599+
smoc_round
600+
------------
601+
5/0
602+
(1 row)
603+
592604
WITH mocs(x) AS (VALUES ('0/'::smoc), ('0/1'), ('0/2'), ('0/4'), ('0/1,3'), ('0/1-3'), ('0/2-4'))
593605
SELECT a.x AS a, b.x AS b,
594606
a.x = b.x AS "=", a.x <> b.x AS "<>",

expected/moc1.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
112112
Buffers: shared hit=3
113113
(3 rows)
114114

115+
SELECT smoc_round(4, coverage) FROM moc1;
116+
smoc_round
117+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
118+
0/3,8-9 1/2-3,5,7,9,11,20,29,31,40,42,44-45 2/5-7,25-27,32-33,35,41-43,64-66,68-69,74-75,77-79,84-86,89-92,96-97,101,103-104,106,109-111,115,121-123,164-166,172,184-186,188 3/0-1,10-11,13-15,17-19,64,66,69,71,77-79,97-99,136-137,139,161-163,268-270,280,295,306-307,348-350,353-355,372,376,383,393-396,398,409,411,420,422,433,435,448,455,459,481-483,668-670,696,701,703,748-750,756-757,766-767 4/8-10,12,16-18,30-31,38-39,49-51,67,260,262,268,281,283,288,290,301,303,305-307,387,552-553,555,640,642-643,1084-1085,1124-1126,1136-1137,1150-1153,1166-1167,1177-1179,1218-1219,1221-1223,1404-1406,1408-1409,1411,1492,1496,1508,1512,1525-1527,1529-1531,1568,1570-1571,1588,1590,1596,1598,1600,1602,1613,1615,1712,1714,1725,1727,1729,1731,1737-1739,1796-1798,1800-1802,1815,1819,1824,1831,1835,1921-1923,2768-2770,2781-2783,2788,2792,2794-2795,2803,2809,2811,3004,3032,3040-3042,3054-3055,3059,3062-3063
119+
(1 row)
120+

moc.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ PG_FUNCTION_INFO_V1(smoc_superset_spoint);
3232
PG_FUNCTION_INFO_V1(smoc_not_superset_spoint);
3333
PG_FUNCTION_INFO_V1(smoc_union);
3434
PG_FUNCTION_INFO_V1(smoc_intersection);
35+
PG_FUNCTION_INFO_V1(smoc_round);
3536
PG_FUNCTION_INFO_V1(smoc_disc);
3637
PG_FUNCTION_INFO_V1(smoc_scircle);
3738
PG_FUNCTION_INFO_V1(smoc_spoly);
@@ -913,6 +914,27 @@ smoc_intersection(PG_FUNCTION_ARGS)
913914
PG_RETURN_POINTER(moc_ret);
914915
}
915916

917+
Datum
918+
smoc_round(PG_FUNCTION_ARGS)
919+
{
920+
int order = PG_GETARG_INT32(0);
921+
Smoc* moc_a = (Smoc *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
922+
Smoc* moc_ret;
923+
void* moc_in_context = create_moc_in_context(moc_error_out);
924+
int32 moc_size;
925+
926+
check_order(order);
927+
928+
moc_round(moc_in_context, order, moc_a, VARSIZE(moc_a) - VARHDRSZ, moc_error_out);
929+
930+
moc_size = VARHDRSZ + get_moc_size(moc_in_context, moc_error_out);
931+
/* palloc() will leak the moc_in_context if it fails :-/ */
932+
moc_ret = (Smoc*) palloc0(moc_size);
933+
SET_VARSIZE(moc_ret, moc_size);
934+
create_moc_release_context(moc_in_context, moc_ret, moc_error_out);
935+
PG_RETURN_POINTER(moc_ret);
936+
}
937+
916938
Datum
917939
smoc_disc(PG_FUNCTION_ARGS)
918940
{

pgs_moc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Datum spoint_not_subset_smoc_c(SPoint*, Datum);
8484

8585
Datum smoc_union(PG_FUNCTION_ARGS);
8686
Datum smoc_intersection(PG_FUNCTION_ARGS);
87+
Datum smoc_round(PG_FUNCTION_ARGS);
8788
Datum smoc_disc(PG_FUNCTION_ARGS);
8889
Datum smoc_scircle(PG_FUNCTION_ARGS);
8990
Datum smoc_spoly(PG_FUNCTION_ARGS);

pgs_moc_ops.sql.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ COMMENT ON AGGREGATE intersection (smoc) IS 'smoc intersection aggregate';
240240

241241
-- smoc constructors
242242

243+
CREATE FUNCTION smoc_round ("order" int, smoc)
244+
RETURNS smoc
245+
AS 'MODULE_PATHNAME'
246+
LANGUAGE C
247+
PARALLEL SAFE
248+
IMMUTABLE
249+
STRICT;
250+
251+
COMMENT ON FUNCTION smoc_round ("order" int, smoc) IS 'reduce order of smoc';
252+
243253
CREATE FUNCTION smoc_disc ("order" int, lng double precision, lat double precision, radius double precision)
244254
RETURNS smoc
245255
AS 'MODULE_PATHNAME'

pgs_process_moc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ void
146146
moc_intersection(void* moc_in_context, Smoc* moc_a, int32 moc_a_end, Smoc* moc_b, int32 moc_b_end,
147147
pgs_error_handler error_out);
148148

149+
void
150+
moc_round(void* moc_in_context, int order, Smoc* moc_a, int32 moc_a_end,
151+
pgs_error_handler error_out);
152+
149153
void
150154
moc_disc(void* moc_in_context, int order, double theta, double phi, double radius,
151155
pgs_error_handler error_out);

process_moc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,32 @@ moc_intersection(void* moc_in_context, Smoc* moc_a, int32 moc_a_end, Smoc* moc_b
921921
PGS_CATCH
922922
}
923923

924+
void
925+
moc_round(void* moc_in_context, int order, Smoc* moc_a, int32 moc_a_end,
926+
pgs_error_handler error_out)
927+
{
928+
moc_input* p = static_cast<moc_input*>(moc_in_context);
929+
PGS_TRY
930+
moc_input & m = *p;
931+
932+
for (int32 a = moc_a->data_begin; a < moc_a_end; a += MOC_INTERVAL_SIZE)
933+
{
934+
// page bumps
935+
int32 mod = (a + MOC_INTERVAL_SIZE) % PG_TOAST_PAGE_FRAGMENT;
936+
if (mod > 0 && mod < MOC_INTERVAL_SIZE)
937+
a += MOC_INTERVAL_SIZE - mod;
938+
moc_interval & x = *interval_ptr(moc_a, a);
939+
940+
int shift = 2 * (HEALPIX_MAX_ORDER - order);
941+
hpint64 first = (x.first >> shift) << shift; // set low bits to zero
942+
hpint64 low_bits_one = (1L << shift) - 1;
943+
hpint64 second = ((x.second + low_bits_one) >> shift) << shift; // round low bits up
944+
945+
add_to_map(m.input_map, first, second);
946+
}
947+
PGS_CATCH
948+
}
949+
924950
void
925951
moc_disc(void* moc_in_context, int order, double theta, double phi, double radius,
926952
pgs_error_handler error_out)

sql/moc.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ SELECT '0/1'::smoc & '1/3,5,7,9' AS intersection;
133133
SELECT '1/9,11,13,15'::smoc & '0/1,2' AS intersection;
134134
SELECT intersection(moc) FROM (VALUES ('0/1-4'::smoc), ('0/2-5'), (NULL)) sub(moc);
135135

136+
SELECT smoc_round(6, '7/1,3,5,9');
137+
SELECT smoc_round(5, '7/1,3,5,9');
138+
136139
WITH mocs(x) AS (VALUES ('0/'::smoc), ('0/1'), ('0/2'), ('0/4'), ('0/1,3'), ('0/1-3'), ('0/2-4'))
137140
SELECT a.x AS a, b.x AS b,
138141
a.x = b.x AS "=", a.x <> b.x AS "<>",

sql/moc1.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
3636
SELECT coverage FROM moc1 WHERE coverage @> '0/0-11';
3737
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
3838
SELECT coverage FROM moc1 WHERE coverage <@ '0/0-11';
39+
40+
SELECT smoc_round(4, coverage) FROM moc1;

0 commit comments

Comments
 (0)