-1

Background

I have described an algorithm for generating random points inside an arbitrary shape (such as a circle, polygon, or an arbitrary closed curve) contained within a box. It involves checking whether the box is outside or partially or fully inside the shape, and then—

  • Generating a uniform random point inside the box if the box is inside the shape,
  • rejecting the box and starting over if the box is outside the shape, and
  • subdividing the box, choosing a random sub-box, and repeating this process for that sub-box otherwise.

This algorithm uses a function called InShape that determines whether a shape covers an axis-aligned bounding box. It takes such a bounding box as input and returns—

  • YES if the box is entirely inside the shape;
  • NO if the box is entirely outside the shape; and
  • MAYBE if the box is partly inside and partly outside the shape.

Now, take a particular implementation of InShape that has certain knowledge about a particular shape. Assume the following:

  • The shape is closed, has nonzero finite volume, and has a boundary of measure zero.
  • The InShape implementation can determine only pointwise whether a point is either outside the shape, or on or inside the shape.
  • The InShape implementation has access to arbitrary-precision arithmetic, as well as interval arithmetic using arbitrary-precision rational numbers. See my library, for example.
  • Other than this, it doesn't matter how the shape is described -- it could be described as a sequence of line and/or curve segments describing the shape's outline; as a signed distance function; as an inequality; as a union or intersection of multiple shapes; etc.

The InShape implementation is given an axis-aligned bounding box as input. The goal is to correctly classify the box just by evaluating the shape pointwise.

Under certain conditions, this is trivial to do. For example, if the shape is enclosed by a 1x1 rectangle, the point (0, 0) is on the shape, and every horizontal or vertical line crosses the shape (inside the rectangle) at most once (think of one quarter of a circle centered at the origin), then the box can be correctly classified just by checking the point's corners. The algorithm (Algorithm 1) is thus to return—

  • YES if all the box's vertices are on or inside the shape;
  • NO if none of the box's vertices are on or inside the shape; and
  • MAYBE in any other case.

More generally, I believe Algorithm 1 will work if—

  • the shape is enclosed by a hypercube $[0, 1]\times[0, 1]\times...\times[0, 1]$,
  • the point $(0, 0, ..., 0)$ is on or inside the shape, and
  • every open axis-aligned line segment that begins in one face of the hypercube and ends in another face crosses the shape at most once (an example is one quarter of a circular disk whose center is at $(0, 0)$),

Or if—

  • the shape is enclosed by a $2$-dimensional rectangle $[0, 1]\times[0, 1]$,
  • the line segment $((0, 0), (1, 1))$ is entirely on or inside the shape,
  • the shape is convex and symmetric about that line segment, and
  • the box being tested arose out of a recursive subdivision of the 2-dimensional rectangle into smaller boxes with half the size, $\frac{1}{4}$ the size, etc.

However, for more general convex shapes (which are the shapes that I care about most), this is not so easy. For example, if the shape is convex and the point $(0, 0)$ is on the shape, the correct algorithm to classify the shape (Algorithm 2) is to return—

  • YES if all the box's vertices are on or inside the shape;
  • NO if none of the box's vertices are on or inside the shape and if the shape's boundary does not intersect the box's boundary; and
  • MAYBE in any other case.

This is not so easy because checking whether a box intersects a shape might not be robust especially if the shape is described by an inequality (such as $x^2 + y^2 - 1 <= 0$). Under certain cases, the algorithm might miss an intersection even though it's present. But at least when the shape is convex and when InShape uses interval arithmetic and builds one interval for each dimension of the box (here, $[x, x+\epsilon]$ and $[y, y+\epsilon]$), and evaluates the inequality only once with the intervals, InShape can still get robust results. In this algorithm (Algorithm 3), InShape returns—

  • YES if the result's upper bound is less than 0;
  • NO if the result's lower bound is greater than 0; and
  • MAYBE in any other case.

Questions

Thus my questions are:

  1. What are necessary and/or sufficient conditions (such as convexity or regularity conditions, or other requirements on the shape) that allow Algorithm 1 to work correctly? Are the sufficient conditions I gave above for this algorithm correct? If so, can they be relaxed?

  2. What are necessary and/or sufficient conditions that allow Algorithm 2 to work correctly, if the InShape method can only evaluate the shape point-by-point? In particular, how can Algorithm 2 robustly check for intersections as required to determine whether to return NO or MAYBE?

  3. What are other conditions that allow InShape to correctly classify whether a box is outside or on or inside a shape when InShape can only evaluate the shape point-by-point, or when InShape proceeds as in Algorithm 3?

  4. Is it possible (or what additional conditions make it possible) to correctly classify a bounding box as NO or MAYBE, using only pointwise evaluation of the shape, if—

    • the shape is enclosed by a hypercube $[0, 1]\times[0, 1]\times...\times[0, 1]$,
    • the point $(0, 0, ..., 0)$ is on or inside the shape,
    • the shape is convex, and
    • the box being tested arose out of a recursive subdivision of the hypercube into smaller boxes with half the size, $\frac{1}{4}$ the size, etc.?

    (Note that in this case, classifying a bounding box as YES is trivial; just check its four corners. On the other hand, I know that it's not enough to classify the box as NO or MAYBE this way.)

Note

An answer suggested the "Separating Axis Theorem for Polygons". But I don't see how the separating axis theorem can be applied to my questions given above. (Notably, the algorithm would have to determine whether a separating axis exists in order to apply the theorem, which seems more complicated than checking the corners of the bounding box, especially if the shape is described as a signed distance function, an implicit equation, etc.) In addition, the theorem doesn't seem to address the issue of whether to return MAYBE or YES, given that the algorithm shouldn't return NO.

Thus, if the separating axis theorem really related to this question, then the answer should give further detail on how the separating axis theorem answers the question of finding an algorithm to classify whether a box is in a shape given only pointwise access to the shape.

Examples

Take the following shapes, all of which are convex and equal 0 at the origin:

  • $v^2 - (u/v)^{1.4-1}*exp(-(u/v)) \le 0$ - Ratio-of-uniforms shape for the gamma(1.4) distribution
  • $v^2 - exp(-(u/v)^2/2) \le 0$ - Ratio-of-uniforms shape for the normal distribution
  • $v^2 - (u/v)^2*exp(-(u/v)^2/2) \le 0$ - Ratio-of-uniforms shape for the Maxwell distribution

All three shapes don't work under Algorithm 1, but they appear to give correct results under Algorithm 3, even without the intersection checks required by Algorithm 2.

Peter O.
  • 931
  • Maybe I misunderstood something but if shape is the triangle with vertices $(0,0), (\frac{1}{2},1),(1,\frac{1}{2})$, it seems to satisfy your first "sufficient" condition. Yet, if the algorithm is applied to the box with vertices ${\frac{1}{3},1}\times{0,1}$, the algorithm would say NO, while the correct answer should be MAYBE. – Peter Košinár Nov 05 '20 at 14:13
  • @PeterKošinár: The box you give has zero volume. I mean to apply the algorithm only to shapes and boxes with nonzero volume. – Peter O. Nov 05 '20 at 17:08
  • Since we're dealing with 2D objects in my example, the volume of the box would be its area and the rectangle I suggested has sides of length $\frac{2}{3}$ and $1$ so its "volume" should be $\frac{2}{3}$, shouldn't it? – Peter Košinár Nov 05 '20 at 18:05
  • @PeterKošinár: But if I understand correctly, the box with vertices (1/3,1) and (0, 1) does not have an area of 2/3, but rather is a degenerate box with an area of 0 since its y-coordinates are the same. In any case I have just clarified the first sufficient condition in this question. – Peter O. Nov 05 '20 at 18:19
  • Ah, my notation might have been confusing (it was a cartesian product of two sets, expressing all four vertices of the box). It is the axis-aligned rectangle whose one vertex is $(\frac{1}{3},0)$ and the opposite one is at $(1,1)$. – Peter Košinár Nov 05 '20 at 18:54
  • @PeterKošinár: Now it's clearer to me. In that case, the triangle you describe doesn't meet the first sufficient condition in this post, as it's possible to draw an axis aligned segment from (0, 1/3) to (1, 1/3) that crosses the triangle twice. – Peter O. Nov 05 '20 at 19:05
  • 1
    Cross-posted: https://stackoverflow.com/q/64728693/781723, https://math.stackexchange.com/q/3882545/14578. Please do not post the same question on multiple sites. – D.W. Dec 12 '21 at 21:17

1 Answers1

0

You may want to check the Separating Axis Theorem for $n$-dimensions. There is an answer by @Jens Renders here which may be what you are looking for.

See: Proof of Separating Axis Theorem for polygons

vvg
  • 3,311
  • I don't see how this theorem can be applied to my question. (Notably, the algorithm would have to determine whether a separating axis exists in order to apply the theorem, which seems more complicated than checking the corners of the bounding box, especially if the shape is described as a signed distance function, an implicit equation, etc.) In addition, the theorem doesn't seem to address the issue of whether to return MAYBE or YES, given that the algorithm shouldn't return NO. – Peter O. Nov 05 '20 at 17:47