[PATCH] Add overlaps geometric operators that ignore point overlaps - Mailing list pgsql-hackers

From Ankit Kumar Pandey
Subject [PATCH] Add overlaps geometric operators that ignore point overlaps
Date
Msg-id f7772fc2-1833-f3ab-b955-0b47bc0954a2@gmail.com
Whole thread Raw
Responses Re: [PATCH] Add overlaps geometric operators that ignore point overlaps  (Kyotaro Horiguchi <horikyota.ntt@gmail.com>)
List pgsql-hackers
Hi,

This is patch for todo item: Add overlaps geometric operators that 
ignore point overlaps

Issue:

SELECT circle '((0,0), 1)' && circle '((2,0),1) returns True

Expectation: In above case, both figures touch other but do not overlap 
(i.e. touching != overlap). Hence, it should return false.

Cause:

Less than or equal check between distance of center and sum of radius

Datum
circle_overlap(PG_FUNCTION_ARGS)
{
     CIRCLE       *circle1 = PG_GETARG_CIRCLE_P(0);
     CIRCLE       *circle2 = PG_GETARG_CIRCLE_P(1);

     PG_RETURN_BOOL(FPle(point_dt(&circle1->center, &circle2->center),
                         float8_pl(circle1->radius, circle2->radius)));
}

Possible fix:

# Don't check for <= , just < would suffice.

Datum
circle_overlap(PG_FUNCTION_ARGS)
{
     CIRCLE       *circle1 = PG_GETARG_CIRCLE_P(0);
     CIRCLE       *circle2 = PG_GETARG_CIRCLE_P(1);

     PG_RETURN_BOOL(FPlt(point_dt(&circle1->center, &circle2->center),
                         float8_pl(circle1->radius, circle2->radius)));
}

same for boxes as well.

Results:

Before:

select box '((0,0),(1,1))' && box '((0,1), (1,2))';
  ?column?
----------
  t
(1 row)

With patch:

select box '((0,1),(1,1))' && box '((1,1), (1,2))';
  ?column?
----------
  f
(1 row)

Bring box slightly ( > EPSILON) inside the other box

select box '((0,0),(1,1.0001))' && box '((0,1), (1,2))';
  ?column?
----------
  t
(1 row)

similar for circle.


Now, as per as discussion 
(https://www.postgresql.org/message-id/20100322175532.GG26428%40fetter.org) 
and corresponding change in docs, 
https://www.postgresql.org/docs/15/functions-geometry.html, it mentions

`Do these objects overlap? (One point in common makes this true.) `. 
Does this means current behavior is correct? Or do we still need the 
proposed change (if so, with proper updates in docs)?

If current behavior is correct, this todo item might need some update 
(unless I missed anything) otherwise any suggestion is welcomed.

Also, I did some search around this and there is general sense of 
differentiation between overlap and touch of geometric figures. I am not 
able to find any function which can determine if two geometric figures 
touch each

other at a point (and if there is real use case of this).

In any case, patch attached for a reference. Any feedback is welcomed.


-- 
Regards,
Ankit Kumar Pandey

Attachment

pgsql-hackers by date:

Previous
From: Ankit Kumar Pandey
Date:
Subject: Re: Request for removal of BUG #5705 from todo items as no repro
Next
From: Jeff Davis
Date:
Subject: Re: New strategies for freezing, advancing relfrozenxid early