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:
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?
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?
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?
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.