Jul 15, 1994 - the running time of a containment algorithm in terms of m, n, r, and k. For our .... Avnaim and Boissonnat, yet they are similar in structure.
Multiple Containment Methods Karen Daniels, Zhenyu Li, Victor Milenkovic
y
Aiken Computation Laboratory, Harvard University, Cambridge, MA 02138. July 15, 1994
Abstract We present three dierent methods for nding solutions to the 2D translation-only containment problem: nd translations for k polygons that place them inside a given polygonal container without overlap. Both the container and the polygons to be placed in it may be nonconvex. First, we provide several exact algorithms that improve results for k = 2 or k = 3. In particular, we give an algorithm for three convex polygons and a nonconvex container with running time in O(m3 n log mn), where n is the number of vertices in the container, and m is the sum of the vertices of the k polygons. This is an improvement of a factor of n2 over previous algorithms. Second, we give an approximation algorithm for k nonconvex polygons and a nonconvex container based on restriction and subdivision of the con guration space. Third, we develop a MIP (mixed integer programming) model for k nonconvex polygons and a nonconvex container.
This paper is available from the Center for Research in Computing Technology, Division of Applied Sciences, Harvard University as technical report TR-12-94. y This research was funded by the Textile/Clothing Technology Corporation from funds awarded to them by the Alfred P. Sloan Foundation and by NSF grants CCR-91-157993 and CCR-90-09272.
1 Introduction Our research into containment problems grew out of the need to solve an NP-hard layout problem in the apparel industry. This layout problem is known as the marker-making problem, and involves laying out two-dimensional polygonal apparel pattern pieces on cloth inside a rectanglar sheet of stock material of xed width and minimum length. In cases in which a piece can be easily classi ed as either \large" or \small", one promising approach is to rst identify and pack all the large pieces. Regions of unused cloth between the large pieces form a set of polygonal \containers" into which the smaller pieces (trim) can then be placed. To solve this (still NP-hard) trim placement problem we transform it into an \easier" NP-hard matching problem as follows. We determine, for each container, all the groups of trim pieces which can t into that container. Selecting the collection of groups, one per container, which uses the most trim pieces (preferably all of them) is a purely combinatorial matching problem. This matching problem can be solved by heuristics, approximation algorithms, or mixed integer programming. Deciding which groups of trim pieces can t into each container requires solving many containment problems. We consider here only the problem of translating several polygons to mutually nonoverlapping positions inside a polygonal container. Discrete rotations, such as multiples of 90 degrees, can be handled by iterating over the valid angles.
1.1 Notation Throughout the paper, we use the following notation:
C , a polygonal \container" with n vertices; P , 1 i k, a polygon with m vertices to be placed in the container; V , 1 i k, the set of translations of P that places it in the interior of C (V is also referred i
i
i
i
i
to as the valid area); U , 1 i < j k, the set of displacements from P to P such that they do not overlap each other (U = ?U ) (U is also referred to as the set of valid displacements). ij
i
ij
ji
j
ij
A simultaneous placement of the P inside C is a set of translations t , 1 i k, such that t 2 V ; 1 i k; and t ? t 2 U ; 1 i < j k: We de ne the kN-N problem to be that of nding a simultaneous placement of k nonconvex polygons into a nonconvex container. Similarly, the kC-N problem is that of nding a simultaneous placement of k convex polygons into a nonconvex container. We de ne the (r; k)N-N problem to be that of nding all subsets of size k out of a set of r nonconvex polygons such that the r polygons can be simultaneously placed into a nonconvex container. We similarly de ne the (r; k)C-N problem. We de ne m = max1 m . We assume that m is always less than n. In general, we express the running time of a containment algorithm in terms of m, n, r, and k. For our applications: 4 m 100, 100 n 300, 10 r 500, and 1 k 10. i
i
i
i
ik
j
i
1
i
ij
Note that we only seek a single \yes-no" answer to each containment problem; we do not require the set of all possible solutions. We need a fast solution, since we must solve the containment problem many times while setting up the matching model. We also observe that many of our containers can hold just a few pieces (no more than three), so we focus on algorithms which place up to three pieces. Also, many of our trim pieces are nearly convex, so algorithms which place convex pieces into nonconvex containers are of particular interest. A practical application of the matching idea requires at least a fast solution to (r; 3)C-N. Figure 1 shows a typical containment example for three nearly convex pieces in a nonconvex \gap" in a clothing (pants) marker. 96
67
102
22
85
11
18
37
60
46
(a) Before placing pieces 50, 67, and 85 in gap between panels 22,23, 9, 6.
(b)
37
103
53 52
76
21
10 35
6 18
11
53 52
21 64 40
9 50 85 67
74
22
76
31
43 101
23
6
10 103
102
83 94 49
74
50
35
96
9
23 83 94 49
31
43 101
64 40
60
46
Pieces successfully placed.
Figure 1: 3C-N Example
1.2 Related Work We know of at least one industrial solution to multiple containment, but it is used in an interactive setting where speed is less critical than in our application. That solution appears to be based on inner and outer approximations, and the running time increases with the tightness of the t. For arbitrary k, translational multiple containment is NP-complete. Thus, the only known exact algorithms for kN-N or kC-N have running time which is exponential in k. Published algorithms for translational multiple containment for small values of k are not fast enough for our purposes because they tend to depend on a high power of n. Fortune [6] gives a solution to 1C-N by computing the Minkowski sum (see Section 1.3) using a generalized Voronoi diagram. Avnaim and Boissonnat [2, 3] use the Minkowski sum and convex decomposition to solve 1N-N and 2N-N, and Avnaim gives an algorithm for 3N-N. Devillers [4] gives faster algorithms for 2C-N and 3C-N. These running times are summarized in the following table.
k kC-N kN-N 1 O(mn log mn) [6] O(m2n2 log mn) [2, 3] 2 O(m2n2 log m) [4] O(m4n4 log mn) [2, 3] 3 O(m3n3 log m) [4] O(m14n6 log mn) [3] Avnaim and Boissonnat also give a solution to the 3N-P problem, three nonconvex polygons in a parallelogram container, using time in O(m60 log m3) [2, 3]. 2
Even though the 3N-P algorithm seems of very high degree in comparison to the others, it is actually more practical than some of the other lower degree algorithms. The 3N-N algorithm, in particular, iterates over all choices of edges from V1, V2, V3, U12, U13, and U23. Its running time is therefore proportional to the product of the sizes of these sets, even in the best case. On the other hand, the m60 factor for 3N-P arises from the worst case bound on the sizes of the outputs of several consecutive Minkowski sums and polygon unions/intersections. At each step the algorithm uses an output sensitive operation which takes advantage of the fact that the worst case is usually a gross overestimate. In theory, for polygons P and Q, jP [ Qj; jP \ Qj 2 O(jP j jQj), but, in practice, jP [ Qj; jP \ Qj jP j + jQj. On the one hand, new vertices are created by edge-edge intersections, but on the other hand, many vertices are discarded when the union or intersection boundary is computed. Let O(s) be the maximum size of a polygon generated by the algorithm. The 3N-P algorithm has running time in O(s log s). The 3N-N algorithm has running time in O(s5 log s). Unfortunately, Avnaim shows that there is no formula for a solution to 3N-N purely based on polygon union, intersection, and Minkowski sums [3]. Some sort of iteration over the edges is required and therefore the running time is in (s2) in some \practical" sense. Note that such a lower bound cannot be proved rigorously because one could always create an algorithm that deliberately created a huge maximum polygon and thus had running time in O(s).
1.3 The Minkowski Sum The Minkowski sum [7] of two point-sets (of R2 in the case of this paper) is de ned A B = fa + b j a 2 A; b 2 B g: Suppose jAj = m and jB j = n, then the following is known about A B . Type Size Time to Construct Both A and B are convex: O(m + n)[7] O(m + n)[7] Either A or B is convex: O(mn)[5] O(mn log mn)[5] Both A and B are star-shaped polygons: O(mn(mn))[12] O((mn) log mn)[8][12] Both A and B are general polygons: O(m2n2)[10][9] O(m2n2 log mn)[9][1] For a point-set A, let A denote the set complement of A and de ne ?A = f?a j a 2 Ag. For a vector t, de ne A + t = fa + t j a 2 Ag and A ? t = fa ? t j a 2 Ag. Section 1.1 gave the various parts of the containment problem: C ; P , 1 i k; V , 1 i k; and U , 1 i < j k. Expressed in terms of set operations and Minkowski sums, V = C ?P ; 1 i k; and U = P ?P ; 1 i < j k: These will be the values of the symbols unless otherwise stated. i
i
ij
i
i
ij
i
j
1.4 Overview of New Results Because this work is central to our trim placement task and we anticipate using it for many types of clothing, our research has gone in many directions. We present three dierent methods for nding solutions to translation-only containment problems: 3
1. exact algorithms for 2C-N, (r; 2)C-N, 2N-N, 3C-N, and 3N-N; 2. an approximation algorithm for kN-N based on restriction and subdivision; 3. a MIP (mixed integer programming) model for kN-N.1 Each approach has its strengths and weaknesses. It is likely that all three will have uses in practice (see Section 5). Our paper is divided into four parts, one for each algorithm and a fourth to discuss implementation and comparisons. Below we summarize the key features of each approach.
1.4.1 Exact Algorithm The running times for our new exact algorithms are summarized in the following table.
k kC-N kN-N 6 2 2 O(mn log mn) O(m n log mn) 3 O(m3n log mn) O(m16n4 log mn) The algorithm for 2C-N yields an algorithm for (r; 2)C-N with running time in O(r2m log mn) for r n, which often holds for our applications. The algorithms for 2N-N and 3N-N are similar in outline to those of Avnaim and Boissonnat, but with the emphasis placed on minimizing the power of n. The sum of the degrees of m and n remains the same. In our applications, m is generally about an order of magnitude smaller than n. Devillers, in fact, treats m as a constant when comparing his results to Avnaim and Boissonnat. As mentioned above, the focus of our eorts has been on 2C-N and 3C-N, and for these our running times have smaller powers of n and no larger powers ofm than Devillers. Our solution for (r; 2)C-N amortizes the cost that is proportional to n over the 2 2C-N queries necessary to solve the problem. For our solution of 2C-N and 3C-N, we introduce the notion of iterating over separating line orientations for pairs of convex polygons. We create data stuctures for rapid computation of the maximum component of a point-set in a particular direction. Finally, our solution for 3C-N relies on a proof that the set of solutions to 1C-N has a number of components in O(n), which we have not seen previously in the literature. r
1.4.2 Approximation Algorithm Our approximation algorithm for kN-N is based on a set of restriction operations on the valid areas (V 's) and the valid displacements (U 's). These restriction operations are based on Avnaim and Boissonnat's solutions to 2N-N and 3N-P (parallelogram container)[2], and our 2N-N algorithm. Their 3N-P algorithm depends on a Helly property of the valid areas for parallelogram containers: for any simultaneous translation of V1, V2, and V3 such that they intersect in pairs, there is a point common to all three. If the Helly property is violated for the valid areas belonging to a 3N-N problem, our approximation algorithm applies restrictions. If repeated restrictions fail to shrink the i
ij
We solve this model using the MINTO mixed integer optimizing package, developed by Savelsbergh and Nemhauser at Georgia Institute of Technology. 1
4
sets suciently to become Helly-satisfying, the algorithm subdivides a valid area and recurses. We formulate this algorithm for kN-N. In a sense, the approximation algorithm substitutes subdivision for the iteration used in the exact 3N-N solutions. In practice, much iteration is required before hitting the right combination of edges, but subdivision is rarely required. Even when subdivision is unnecessary, the running times of the approximation algorithm are in O(m28n24 log m3n) for 3C-N and in O(m60n40 log m3n) for 3N-N. These seem much worse than the iteration-based methods, but in terms of the size s of the largest polygon generated, the running times are both in O(s log s) as opposed to O(s5) for the iterationbased algorithms. The tests we have run indicate that the running times of the approximation algorithm are much faster than the best times we would estimate for exact solutions to 3N-N.
1.4.3 MIP Model Our MIP (mixed integer programming) model for kN-N is a generalization of our LP (linear programming) model for compaction [11, 12]. A feasible solution to the MIP model gives a nonoverlapping placement of any number of nonconvex polygons in a nonconvex container. The running times for the MIP model are not as good as for the approximation algorithm, but they are practical for k 3. Our motivation for considering this approach is two-fold. First, we feel that it is important to compare the utility of operations research methods vs. \pure" computational geometry methods. Within the operations research community, we have seen MIP-based methods proposed as solutions to textile layout problems, but we are not aware of any actual implementations. Second, the speed of our LP-based compaction suggested that a containment method based on it might be of practical value. Speci cally, we observed that, although compaction is harder than layout (it is PSPACE vs. NP-hard [11]), and we have shown that any motion planning-based compaction algorithm has an exponential lower time bound [11], nevertheless, compaction is very ecient in practice.
2 Exact Algorithms This section gives exact algorithms for a number of containment problems.
2.1 Exact Algorithms for 2N-N and 3N-N Our algorithms for 2N-N and 3N-N have running times with smaller powers of n than those of Avnaim and Boissonnat, yet they are similar in structure. To solve 2N-N we use the test (V1 U12) \ V2 6= ; instead of (?V1 V2) \ U12 6= ;, but then apply a similar convex decomposition. Our solution to 3N-N is also similar in form except that the dominant case iterates over the edges of U12, U13, U23, V1, and V2 (or V3).
2N-N
Avnaim and Boissonnat [2] observe that the 2N-N problem has a solution if and only if,
U12 \ (?V1 V2) 6= ;; 5
that is, if there is a valid displacement that is also a dierence between valid translations in V2 and V1. The set ?V1 V2 can be computed using time in O(m8n8 log mn); however, Avnaim and Boissonnat reduce this time to O(m4n4 log mn) by decomposing V1 into convex components. We observe that it is more ecient to determine,
V1 \ (V2 U21) 6= ;: The set V2 U21 can be computed using time in O(m12n4 log mn). However, a convex decomposition of V2 very similar to the one used by Avnaim and Boissonnat reduces the time to O(m6n2 log mn) for testing the emptiness of the intersection with V1.
3N-N
Avnaim's algorithm for 3N-N [3] is broken into several cases based on the types of contact among the pieces and the container. Two of these cases involve either O(m2n2) or O(m4) calls to an algorithm for 2N-N. Substituting our algorithm for 2N-N changes the running time to O(m8n4) for this part of the algorithm. The remaining case involves six single contacts: a single contact between each pair of pieces and a single contact between each piece and the container. We solve this case by iterating over the O(m14n2) ways of selecting a single edge from each of U12, U13, U23, and V1. The choice of edges in U12, U13, U23 implies a connected one-dimensional family of vectors u12(s) 2 U12, u13(s) 2 U13, u23(s) 2 U23, parameterized by s. Let e be the edge selected in V1. It is sucient to determine a value of s such that e \ (@V2 ? u12(s)) and e \ (@V3 ? u13(s)) have a common point, where @V2 and @V3 are the boundaries of V2 and V3. The edge e can be parameterized by t, and thus the set of intersections e \ (@V2 ? u12(s)) can be expressed as a set I2 of O(m2n2) line segments in (s; t)-space. Set I3 is similarly de ned. Finding a valid placement is equivalent to nding a point in I2 \ I3, which can be done using time in O(m2n2 log mn). Total time for solving 3N-N is therefore in O(m16n4 log mn).
2.2 Exact Algorithms for 2C-N and (
C-N Problem
r; 2)
Devillers gives an algorithm for the 2C-N problem with running time in O(m2n2 log m). We present here two new algorithms with running times in O(mn log mn). One of these also yields an algorithm for the (r; 2)C-N problem with running time in O(r2m log mn) (assuming r n). In comparison, Devillers' 2C-N algorithm would require time in O(r2m2n2 log m) to solve the (r; 2)C-N problem. Our rst algorithm is simply an application of our 2N-N algorithm to 2C-N. Our second algorithm is based on the following lemma.
Lemma 2.1 Let P1 and P2 be convex polygons and let t1 and t2 be translations such that P1 + t1 and P2 + t2 do not overlap (t2 ? t1 2 U12). Then there exists a line L parallel to an edge of P1 ?P2 that separates P1 + t1 from P2 + t2.
Proof: This follows directly from the fact that P1 ?P2 is convex and that U12 = P1 ?P2. Consider the following algorithm, called EFFICIENT-2C-N. 6
For each vector w normal to an edge e of P1 ?P2: 1a. compute v1, the vertex of V1 that minimizes v w, v 2 V1; 1b. compute v2, the vertex of V2 that maximizes v w, v 2 V2; 2a. determine a 2 P1 which maximizes p w, p 2 P1; 2b. determine b 2 P2 which minimizes p w, p 2 P2; 3. if (v1 + a) w (v2 + b) w, then return the solution t1 = v1, t2 = v2. Otherwise, report no solution exists.
Lemma 2.2 EFFICIENT-2C-N solves 2C-N using time in O(m log mn) plus the amount of time required to compute the convex hulls of V1 and V2 , which is in O(mn log mn).
Proof: Since jP1 ?P2j = m1 + m2 2 O(m), the outer loop runs m times. Step 1a can be solved
using a binary search on the edges of the convex hull of V1, and similarly for Step 1b. Both convex hulls have size in O(mn). Naively, steps 2a and 2b take time in O(log m), although pointers to a and b can be stored with e when P1 ?P2 is computed without aecting the running time. Step 3 takes constant time and uses the fact that L(w; d) : (x; y) w = d is a separating line for any d satisfying (v1 + a) w d (v2 + b) w.
Corollary 2.3 For r n (which is often true for our applications),
solves the (r; 2)C-N problem using time in O(r m log mn). 2
r
2
applications of EFFICIENT-2C-N
For our applications, m 100 and n 300, so this is practical. It is also numerically stable.
2.3 Exact Algorithms for 3C-N and (
C-N Problem
r; 3)
Devillers solves the 3C-N problem by iterating the translation of P1 over the vertices of V1 and then solving the 2C-N problem for P2 and P3 for each translation of P1. Since jV1j 2 O(mn) and the running time of his 2C-N algorithm is in O(m2n2 log m), his 3C-N algorithm uses time in O(m3n3 log m). We rst observe that if one used EFFICIENT-2C-N to solve the 2C-N problems, the resulting running time would be in O(m2n2 log mn). Unfortunately, it is necessary to recompute V2 , V3 and their convex hulls for each iteration, and therefore one would not achieve a running time proportional to n log n. To achieve this goal, it is necessary to modify EFFICIENT-2C-N to handle three convex polygons. We rst present an algorithm QUARTIC-3C-N which has running time in O(m4n log mn). We then break up the 3C-N problem into cases based on the maximum number of contacts among the simultaneously placed polygons. We give an algorithm for the three-contact case with running time in O(m2n log mn), and we give algorithms for the two, one, and zero-contact cases with running times in O(m3n log mn). Taken together, this gives an algorithm CUBIC-3C-N with running time in O(m3n log mn). Unfortunately, we have not discovered how to amortize this cost over multiple queries, and so the running time for our solution to the (r; 3)C-N problem is in O(k3m3n log mn). 7
2.3.1 QUARTIC-3C-N Just as EFFICIENT-2C-N iterates over the orientations of separating lines for two convex polygons, QUARTIC-3C-N iterates over the orientations of the separating lines for each pair out of the three convex polygons. QUARTIC-3C-N must iterate over a number in O(m3) of triples of orientations. It suces to show how to solve the 3C-N problem for a particular triple of orientations using time in O(mn log mn). We rst describe a function FARTHEST-VALUE which acts on a polygon V and two vectors w and (d). For a given value of d, w and returns a piece-wise linear function f 0
V;w;w 0
max v w : and (d) is the maximum w -component of the points of V in the half-plane H (w; d) : In other words, f (d) is monotonically decreasing since increasing d diminishes the (x; y) w d. Note that f subset of V in the half-plane H (w; d).
f
V;w;w 0
(d) =
0
v2V
vwd
0
V;w;w 0
V;w;w 0
Lemma 2.4 FARTHEST-VALUE(V , w, w ) has size in O(jV j) and can be computing using time in O(jV j log jV j). 0
Proof: First, create a data structure that can answer the following query using time in O(log jV j):
given d, determine the maximum w -component of a vertex of V in the half-plane H (w; d). To do this, sort the vertices of V by their w-component and then put them into a balanced tree. At the root of each subtree store the maximum w -component of the vertices in that subtree. The values can be stored using a post-order traversal, and thus it requires total time in O(jV j log jV j) to create this data structure. To answer a query for a particular d, perform a standard search-tree lookup with that value of d. Take the maximum w -component stored in right subtrees of the nodes traversed by the search. Next, compute FARTHEST-VALUE(V , w, w ) by sweeping the line L(w; d) from left to right (d = ?1 to d = 1). Keep track of the edge e of V which intersects L farthest in the direction of w (if w is perpendicular to L, arbitrarily choose one of the two directions along L). There are jV j vertex events at which the sweepline passes through a vertex of V . Between every pair of consecutive vertex events, the maximizing edge e does not change, although the w -component of e \ L does. There is possibly one extra event at which the w -component of e \ L becomes equal to the maximum w -component of a vertex of jV j to the right of L. Therefore, there are at most 2jV j events as the line sweeps from left to right. Each event can be treated using time in O(log jV j). This completes the proof of the lemma. 0
0
0
0
0
0
0
0
0
Using FARTHEST-VALUE, we can now give an algorithm for solving 3C-N for a particular choice of separating line orientations. Suppose we have chosen orientation w for the normal to the line separating P from P , 1 i; j 3 (w is chosen to be an outward pointing normal to P ?P , hence w = ?w ). Let a be the point of P with maximum w -component. For translations t and t , P + t and P + t will have a separating line only if (t + a ) w (t + a ) w . Any d satisfying (t + a ) w d (t + a ) w will yield a separating line L(w ; d ) for P + t and P + t . ij
i
j
ji
j
ij
i
i
ij
j
i
ij
j
i
ij
ij
i
ij
j
ij
i
ij
j
ji
j
ij
j
8
i
ij
ij
j
ji
ij
ij
ij
i
i
Running the following algorithm for each triple w12; w13; w23 of separating line orientations gives us the complete algorithm QUARTIC-3C-N. First determine the set D23 of (d12; d13) such that there is a placement of P2 and P3 inside H (w12; d12) \ C and H (w13; d13) \ C , respectively, with a separating line with normal w23. This is equivalent to the condition,
f3
V ;w13 ;w23
(d13 ? a31 w13) ? f
V2 ;w12 ;w23
(d12 ? a21 w12) (a32 ? a23) w23:
Next, transform the coordinates of V1 to (d12; d13)-space:
v ! ((v + a12) w12; (v + a13) w13): If the transformed V1 and the set D23 have any point in common, then this point corresponds to a translation of P1 that allows a simultaneous placement of P2 and P3 such that there are separating lines with the chosen orientations.
Lemma 2.5 QUARTIC-3C-N has running time in O(m4n log mn). Proof: It suces to verify that the algorithm described in the previous two paragraphs has running
time in O(mn log mn). The set D23 is bounded above by the curve,
f 3 13 32 (d13 ? a31 w13) ? f 2 12 23 (d12 ? a21 w12) = (a32 ? a23) w23: in (d12; d13)-space. Since f 2 12 23 (d12 ? a21 w12) and f 3 13 23 (d13 ? a31 w13) are monotonically decreasing functions of d12 and d13, respectively, this curve can be computed in time in O(jV2j + jV2j) using the outputs of FARTHEST-VALUE(V2, w12, w23) and FARTHEST-VALUE(V3, w13, w23), respectively. Total time is in O(mn log mn). Testing the transformed V1 and the set D23 for an intersection can be done in the same running time. V ;w
V ;w
;w
V ;w
;w
V ;w
;w
;w
2.3.2 CUBIC-3C-N It is easy to speed up QUARTIC-3C-N to detect solutions for which P1 + t1, P2 + t2 and P3 + t3 have three pairwise contacts. We simply observe that the number of potential separating line triples is in O(m). As P1, P2 , and P3 \orbit" around each other in mutual contact, there is an event when two out of the three have a vertex-vertex contact, and the number of such events is in O(m). Hence all three-contact solutions can be found using time in O(m mn log mn) = O(m2n log mn). For the two-contact case, we show that the number of triples is in O(m2); details are given below. For the zero and one-contact cases, it is necessary to consider (m3) triples, however, we eliminate a factor of m in the nal running time by showing that the number of components in the valid area of the non-contacting piece is in O(n), not O(mn).
Two-Contact Case:
Here we show how to reduce the number of triples in the two-contact case from the naive result of O(m3) to O(m2). Suppose in the solution P1 contacts P2 along edge e12 (of either P1 or P2) and similarly P1 contacts P3 along edge e13. Imagine sliding P2 and P3 towards each other while 9
maintaining contact with P1 (this is a hypothetical operation, not part of an algorithm). Either P2 comes in contact with P3 or the contacts reach endpoints of segments e12 and e13. If the two polygons come in contact, then there is a triple of separating line orientations for the three-contact problem which also works for the two-contact problem: simply choose the separating line for the hypothetically altered positions of P2 and P3 and this line will also work for the unaltered twocontact solution. If P2 and P3 reach the ends of the edges, then there is a triple of separating line orientations corresponding to a vertex-vertex contact between P1 and P2 and between P1 and P3. This set of orientations will work for the unaltered two-contact solution. Hence, we can generate all relevant triples using the following algorithm: for each placement of P1, P2, P3 with exactly two vertex-vertex contacts, generate a triple of separating line orientations. Each vertex-vertex contact corresponds to a vertex of a Minkowski sum P ?P , hence the number of vertex-vertex contacts is in O(m) and the number of pairs of contacts is in O(m2). The running time of QUARTIC-3C-N using this set of triples is in O(m3n log mn). i
j
One and Zero Contact Cases:
The previous section gave two cases of a O(m3n log mn) time CUBIC-3C-N algorithm. The other two cases involve instances of 3C-N for which there are solutions but one of the three pieces is always separated from the other two, which may or may not be in contact. WLOG, assume it is P1 which cannot be put in contact with P2 and P3. We can replace each V1 by V1, which contains a single point from each connected component of V1 , and give the following algorithm: for each triple of separating line orientations and for each v1 2 V1, check if
f2
V ;w12 ;w32
((v1 + a12 ? a21) w12) + f
V3 ;w13 ;w23
((v1 + a13 ? a31) w13) (a32 ? a23) w23:
We can compute the values of f 2 12 32 ((v1 + a12 ? a21) w12) and f 3 13 23 ((v1 + a12 ? a21) w12) by lookups in the balanced trees built on top of calls to FARTHEST-VALUE(V2, w12, w32) and FARTHEST-VALUE(V3, w13, w23). These lookups take time in O(log mn). Since only two orientation vectors are involved in constructing each tree, the total cost of contructing all the trees is in O(m2 mn log mn). Hence the total cost is in O(m3jV1j log mn). Lemma 2.6 (see below) shows that this cost is in O(m3n log mn). This completes all the cases of CUBIC-3C-N. V ;w
V ;w
;w
;w
Lemma 2.6 Let P be convex with m vertices and let C be an arbitrary polygon with n vertices. Let V = C ?P be the set of translations that place P inside C . Then the number of connected
components of V is in O(n).
Proof: We prove a somewhat stronger condition. Let CC(C; P ) be the number of connected
components of the set of translations of P that place it inside C . Let T(C; P ) be the number of edges and vertices of C that are touched by some translated copy of P inside C (call these touchable edges and vertices). We claim CC(C; P ) T(C; P ) ? 2. First we note that if T(C; P ) 2, then CC(C; P ) = 0 (and T(C; P ) = 0). Otherwise, if V 6= ; and there were two or fewer contacts then there would be a direction in which to translate P \to in nity" without leaving C . This is the base case of an induction on T(C; P ). Now suppose we choose a connected component V V . Consider V P which is the set of points on or in the interior of C which are touched by a copy of P translated by some vector in V . As P slides around the boundary of V P it has either single or double contacts with the boundary of 0
0
0
0
10
C . The double contact positions will be isolated points, corresponding to vertices of V . The single contact points will be chains along the boundary of V P . For each position t of P inside V P for which is has two distinct contact points a and b with C , 1 i k, we will generate a subset C C as follows. (These is and ks are local to this proof.) Take the portion of the boundary of C from a to b that does not include any point of V P except a and b . Add to this the portion of the boundary of P + t that joins a to b and lies in the interior of V P . Finally, slightly dimple this chain of P + t so that P + t is no longer a valid placement of P inside C . We claim it is possible to prove that the resulting sets generate, disjointly, all the other components of V . We further claim that each C can share at most two touchable edges or vertices in common with those touching the boundary of V P and these are the only touchable edges or vertices they can have with each other. (Note, C and C are not necessarily disjoint.) For each C , let n be the number of touchable edges and vertices not in common with V P . If n = 0, then we know that C does not generate a connected component and we ignore it. Otherwise, 0
0
0
i
i
i
i
i
i
0
i
i
i
0
i
i
i
i
i
i
0
i
i
j
0
i
i
i
CC(C ) T(C ) ? 2 n + 2 ? 2 = n : i
i
CC(C ) = 1 +
i
X k
i
CC(C ) = 1 +
X k
n: i
i
=1
=1
i
i
On the other hand, V P must touch at least three vertices or edges or C , 0
T(C ) 3 +
X k
n: i
=1
i
Hence CC(C ) T(C ) ? 2.
3 Approximation Algorithm Here we describe an approximate subdivision algorithm for kN-N. Avnaim and Boissonnat [2] solved 3N-P problem for the case when C is a parallelogram. That work relied on their solution of the double containment problem [2]. We extend their parallelogram approach to solve the general case via a process of restriction followed by subdivision. Recall the de nitions of V , 1 i k, and U , 1 i < j k, and their initial values (Section 1.1). We de ne and apply certain restrictions to V or U , replacing them by a subset. After each restriction, V will remain a superset of the set of valid translations of P that will allow the other k ? 1 polygons to be placed inside P without overlapping (each other and the container). Similarly, U will remain a superset of the set of valid displacement of P and P that will allow the other k ? 2 pieces to be placed without overlapping each other. The overall structure of the approximation algorithm is to apply restrictions until no more signi cant \shrinking" is possible. If no solution to the kN-N problem is found in these restricted sets, the algorithm branches by subdividing one of the V and recursing on each half while the other V , j 6= i remain the same. i
ij
i
ij
i
i
ij
i
i
j
j
11
3.1 Restrictions Lemma 3.1 The following are valid restrictions: 1. U U \ (?V V ) for i = 6 j; 2. V V \ (V U ) for i = 6 j; 3. U U \ (U U ) for h = 6 i =6 j . ij
ij
i
i
i
ij
j
j
ji
ij
ih
hj
Proof: Restriction 1 states that a valid displacement from i to j must be a valid translation for
j minus a valid translation for i. Restriction 2 states that a valid translation for i must be a valid translation for j plus a valid displacement from j to i. Restriction 3 states that a valid displacement from i to j must be a valid displacement from i to h plus a valid displacement from h to j .
Corollary 3.2 The following are valid restrictions: \ 2a. V V \ V U ; i
i
j
=i
ji
j6
U \
3a. U
ij
\
ij
U U . ih
=i;j
hj
h6
De ne restriction functions of the form RESTRICT-X where X is 1, 2, 3, 2a, or 3a. The function call RESTRICT-3a(U12 ) applies restriction 3a to the set U12 with respect to the current values of the V 's and the U 's. In our notation, Avnaim and Boissonnat's observation is that if one applies RESTRICT-1(U ), 1 i < j 3, in parallel, followed by RESTRICT-3a(U ), 1 i < j 3, in parallel, then each element of U corresponds to a solution to 3N-N if V1 , V2 and V3 satisfy the Helly property described in Section 1.4.2. Our observation is that the Helly property is very likely to be satis ed. We apply restrictions and, if necessary, subdivisions until it is satis ed or until some restriction yields an empty set. We use an approximate test for a solution: t lies within of V and t ? t lies with of U . This assures that the subdivision terminates when a subdivided valid area has diameter less than . In practice, this termination condition is rarely necessary. Pseudo-code for the algorithm is given below. In the approximation algorithm, V , 1 i k, and U , 1 i < j k are global variables set initially to the values given in Section 1.1. The function PUSH-CONTEXT() saves the current values of the variables. The function POP-CONTEXT() restores the values to those saved by the matching PUSH-CONTEXT(). If at any point a valid placement is found, execution terminates with success. The value is chosen such that 0 < < 1. We use = 0:9. We set our equal to 0.01 inch. i
ij
ij
ij
ij
i
i
i
ij
TOP-LEVEL()
do
j
RESTRICT-1(U ), 1 i < j k if any U = ; return FAIL ij
ij
12
i
ij
do
RESTRICT-3a(U ), 1 i < j k if any U = ; return FAIL TEST-FOR-SOLUTION() while some U is diminished in area by a factor of RESTRICT-2a(V ), 1 i k if any V = ; return FAIL while (some V is diminished in area by a factor of and all V consist of a single connected component) SUBDIVIDE() return FAIL ij
ij
ij
i
i
i
i
SUBDIVIDE() select V with multiple components if no V has multiple components select V with longest diameter d if d < return FAIL cut V into two components midway along diameter a
i
a
a
PUSH-CONTEXT() for each component V of selected V V V TOP-LEVEL() POP-CONTEXT() return FAIL
a
a
In our implementation, diameters are only measured parallel to the coordinate axes. Finally, here is an algorithm that tests if a solution is easily found. TEST-FOR-SOLUTION() PUSH-CONTEXT() while all V and U are non-empty and a U has non-empty area select U of smallest (non-zero) area select u 2 U U fu g RESTRICT-2(V , U ) RESTRICT-2(V , U ) RESTRICT-3a(U ), 1 i < j k if all U 's and all V 's are non-empty RESTRICT-2a(V ) if V is non-empty return SUCCESS POP-CONTEXT() return FAIL i
ij
ij
ab
ab
ab
ab
ab
a
ab
b
ab
ij
ij
i
a
a
13
4 MIP Model Our MIP model for multiple containment is a generalization of the LP model used in our compaction algorithm [11, 12]. The compaction algorithm takes a set of positions of non-overlapping P 's already placed in the container, and then simulates the motion of the P 's under \forces". The simulation of motion is formulated as an LP problem. We build two types of non-overlapping constraints. The rst type keeps pieces within the container, and the second prohibits overlap among pieces. A piece P remains within the container if its position t is within its valid area V . For the compaction problem, the container is rectangular, so V is rectangular. This yields constraints which simply impose upper and lower bounds on t . The second type of constraint prevents overlap among pieces. For two pieces P and P with position variables t and t , P does not overlap P if t ? t is in U . However, U can be highly nonconvex, so we build these constraints using a locality heuristic: the algorithm selects a large convex subset of U according to the current value of t ? t , which is in U . It then restricts the value of each t ? t to the convex subset of U decided by the locality heuristic. The restriction on t ? t is expressed by a set of linear constraints based on the convex subset. Collectively, the linear constraints for adjacent pairs of polygons trace out a convex subspace of the solution space. The objective of compaction is to optimize an energy function which is a linear functional of the t 's. The solution of this LP problem gives a set of non-overlapping positions that improves the objective of compaction. If necessary, we iterate when t ? t moves into a suciently dierent region of U , so that the convex region generated by the locality heuristic is dierent. To formulate the multiple containment problem, we must consider the same two types of nonoverlapping constraints. The nature of the rst type of constraint is dierent for multiple containment because the container need not be rectangular. To accomodate this, we currently use a recursive procedure to decompose each V into convex regions. The procedure uses a line which passes through an edge of a re ex vertex to split the polygon into two subpolygons and recurse on the subpolygons. Let C be a convex region of V . We associate a Boolean variable B with C , and build, for each 1 h l, a set of constraints incorporating B . If the program chooses to set P B = 1, then t lies within region C of V . The additional constraint =1 B = 1 implies that exactly one region of V is chosen. For the second type of constraint in the multiple containment problem, we consider all the convex regions of U generated by the locality heuristic for the LP model. These regions form a cover of U . We follow the same constraint building procedure for each convex region of U as we did above for each convex region of V . Again, exactly one region, associated with a value of 1 for a Boolean variable, will be chosen by the program. In this case, t ? t lies within the chosen region. We solve the multiple containment problem by replacing the objective function of the LP compaction model with a constant objective function, using the two sets of constraints described above, and solving the resulting mixed integer program. No iteration is necessary; any feasible solution is a valid placement for the set of pieces within the container. We summarize this approach below. i
i
i
i
i
i
i
i
i
j
j
i
ij
j
j
i
ij
i
j
ij
ij
j
ij
j
i
i
i
j
i
ij
i
h
i
h
h
l
h
i
h
i
h
h
i
ij
ij
ij
i
j
14
i
h
A position-based multiple containment algorithm using MIP formulation. Assign a positional variable p for polygon i (1 i k); i
L := the list of all pairs of polygons; S := ;; /* S is the set of linear constraints generated so far */ foreach polygon P do Compute V ; Find a convex covering C1, C2, : : :, C of V ; Assign a Boolean variable B for C (1 h l); S0 := the MIP constraints generated from C (1 h l); S := S [ S0; Add the constraint P =1 B = 1; i
i
l
h
i
h
h
l
end foreach pair of polygons (P ; P ) in L do h
h
i
j
Compute U ; Find a convex covering D1 , D2 , : : :, D of U ; Assign a Boolean variable E for D (1 h l); S1 := the MIP constraints generated from D (1 h l); S := S [ S1; Add the constraint P =1 E = 1; ij
l
h
ij
h
h
end
l
h
h
Set up a mixed integer program maximize (constant objective function) subject to S ; Obtain the set of new positions p (1 i k) by solving the mixed integer program; Place polygon P at p (1 i k); i
i
i
15
5 Implementation We present a set of tables which give running times for our three containment algorithms (exact, approximation, and MIP) on a variety of examples. We then compare the performance of the three algorithms. All running times are given in seconds on a 28 MIPS SPARCstation2. The setup time to calculate valid area polygons is not included in the running time. Table entries for which placement was determined to be infeasible are identi ed by a *. The examples given below test containment of up to four polygons. Most of our examples use apparel markers, since that is our current application. The majority of our marker examples are pants markers, which involve placing nearly convex pieces into nonconvex containers. A few of the marker examples are from outside the pants domain, and illustrate placement of nonconvex pieces into a nonconvex container. In addition to our marker examples, we developed a separate set of \arti cial" examples involving a variety of convex and nonconvex containers.
5.1 MIP Model In the table below, size represents number of vertices, P is a polygon, and C is the container. The total number of variables includes the number of Boolean variables. Time is the elapsed time, in seconds, required by the MINTO3 package to solve the MIP model. i
Type 3C-N 3C-N 3C-N* 3C-N 4C-N 4C-N
P
k
=1
i
jP j jCj #Constraints # Boolean Variables Total #Variables Time
45 30 38 41 46 58
i
328 216 216 162 162 100
135 126 147 144 225 288
49 50 59 36 75 100
76 77 86 63 110 135
16 98 222 12 64 57
Table 1: MIP Containment Results for Marker Examples
2
Inc
SPARCstation is a registered trademark of SPARC International, Inc., licensed exclusively to Sun Microsystems,
The MINTO mixed integer optimizing package was developed by Savelsbergh and Nemhauser at Georgia Institute of Technology. 3
16
5.2 Approximation Algorithm Type Time 3N-C 1 3C-N* 2 3C-C 1 3C-C 1 3C-C 1 3C-C 0 3C-C* 0 Table 2: Approximation Results for Arti cial Examples Type Time 3C-N 3 3C-N 1 3C-N 2 3C-N 2 3C-N 1 3C-N 4 3C-N 2 3C-N 3 3C-N 2 3C-N 1 3C-N 2 3C-N 2 3C-N 2 3N-N 5 3C-N 2 4C-N 26 4C-N* 3 4C-N 19 4N-N 111
Type Time 3C-N 1 3C-N 1 3C-N 3 3C-N 2 3C-N 1 3C-N 1 3C-N 2 3C-N 4 3C-N 1 3C-N 2 3C-N 2 3C-N 3 3C-N 2 3N-N 5 4C-N 30 4C-N 43 4C-N 5 4C-N 192 4N-N 111
Table 3: Approximation Results for Marker Examples
17
Implementation Note: Implementing the approximation algorithm requires the use of non-regularized set operations. Here we present two examples of 3N-N placements achieved for some of our examples using the approximation algorithm. The tightness of the layouts is actually typical of the algorithms for all three of our approaches. The gures given here are \before" and \after" screen dumps from our Automatic Marker Generation Testbed. Note: The pieces to be placed are above the marker in the \palette", whose scale is dierent from that of the marker.
Figure 2: 3N-N Example 1 before placing pieces
Figure 3: 3N-N Example 1 after placing pieces
18
Figure 4: 3N-N Example 2 before placing pieces
Figure 5: 3N-N Example 2 after placing pieces
19
5.3 Comparisons 5.3.1 Comparison of MIP and Approximation Running Times Type MIP time Approximation time 3C-N 37 3 3C-N 8 2 3C-N 23 2 3C-N 27 1 3C-N 19 2 3C-N 46 3 3C-N 82 2 3N-N > 600 5 4C-N 41 30 4C-N 26 10 4C-N 229 43 4C-N 87 5 4C-N 5 19 4C-N > 1500 192 4C-N* > 8338 3 Table 4: MINTO vs. Approximation for Marker Examples
5.3.2 Comparison of Exact and Approximation Running Times Because these algorithms are so fast, timing comparisons are done based on 100 calls to each algorithm. Type Exact time Approximation time 2C-N 4 15 2C-N 8 35 Table 5: Exact vs. Approximation for Marker Examples (100 calls each)
5.3.3 Conclusions For (r; 2)C-N, our exact algorithm outperforms our approximation algorithm. For 3C-N and 3NN problems in which a placement is feasible, both our approximation and MIP algorithms are practical, although the approximation method is signi cantly faster than the MIP. For infeasible placements, the MIP approach is currently too slow for our applications. However, our restriction process enables the approximation algorithm to often detect infeasibility very quickly. It is possible 20
Type Exact time Approximation time 2C-C 3 19 2C-N 3 22 Table 6: Exact vs. Approximation for Arti cial Examples (100 calls each) that integrating some restriction into our MIP model will enable it to detect infeasibility much faster.
References [1] P. K. Agarwal, M. Sharir, and S. Toledo. Applications of parametric searching in geometric optimization. In Proc. 3rd ACM-SIAM Sympos. Discrete Algorithms, pages 72{82, 1992. [2] F. Avnaim and J. Boissonnat. Simultaneous Containment of Several Polygons. In Proceedings of the 3rd ACM Symposium on Computational Geometry, pages 242{250, 1987. [3] Francis Avnaim. Placement et deplacement de formes rigides ou articulees. PhD thesis, Universite de Franche-Comte, France, 1989. [4] Olivier Devillers. Simultaneous containment of several polygons: Analysis of the contact con gurations. Technical Report 1179, INRIA, 1990. [5] D. Dobkin, J. Hershberger, D. Kirkpatrik, and S. Suri. Implicitly searching convolutions and computing depth of collision. In Proceedings of 2nd SIGAL, pages 165{180, 1990. [6] S. Fortune. A Fast Algorithm for Polygon Containment by Translation. In Proceedings of the 12th Colloquium on Automata, Languages, and Programming, pages 189{198. Springer-Verlag, 1985. [7] L. Guibas, L. Ramshaw, and J. Stol . A Kinetic Framework for Computational Geometry. In IEEE 24th Annual Symposium on Foundations of Computer Science, 1983. [8] J. Hershberger. Finding the upper envelope of n line segments in o(n log n) time. Inform. Process. Lett., 33:169{174, 1989. [9] A. Kaul, M.A. O'Connor, and V. Srinivasan. Computing Minkowski Sums of Regular Polygons. In Proceedings of the Third Canadian Conference on Computational Geometry, Vancouver, British Columbia, 1991. [10] K. Kedem and M. Sharir. An ecient motion planning algorithm for a convex rigid polygonal object in 2-dimensional polygonal space. Discrete Comput. Geom., 5:43{75, 1990. [11] Z. Li and V. Milenkovic. A compaction algorithm for non-convex polygons and its application. In Proc. 9th Annu. ACM Sympos. Comput. Geom., pages 153{162, 1993. 21
[12] Z. Li and V. Milenkovic. Compaction and seperation algorithms for non-convex polygons and their applications. Technical Report TR-13-93, Center for Research in Computing Technology, 1993.
22