1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX
25 #define _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX
26 
27 #include <basegfx/polygon/b2dpolypolygon.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace basegfx
32 {
33 	namespace tools
34 	{
35 		// Solve all crossovers in a polyPolygon. This re-layouts all contained polygons so that the
36 		// result will contain only non-cutting polygons. For that reason, points will be added at
37 		// crossover and touch points and the single Polygons may be re-combined. The orientations
38 		// of the contained polygons in not changed but used as topological information.
39 		// Self crossovers of the contained sub-polygons are implicitely handled, but to not lose
40 		// the topological information, it may be necessary to remove self-intersections of the
41 		// contained sub-polygons in a preparing step and to explicitely correct their orientations.
42 		B2DPolyPolygon solveCrossovers(const B2DPolyPolygon& rCandidate);
43 
44 		// Version for single polygons. This is for solving self-intersections. Result will be free of
45 		// crossovers. When result contains multiple polygons, it may be necessary to rearrange their
46 		// orientations since holes may have been created (use correctOrientations eventually).
47 		B2DPolyPolygon solveCrossovers(const B2DPolygon& rCandidate);
48 
49 		// Neutral polygons will be stripped. Neutral polygons are ones who's orientation is
50 		// neutral, so normally they have no volume -> just closed paths. A polygon with the same
51 		// positive and negative oriented volume is also neutral, so this may not be wanted. It is
52 		// safe to call with crossover-free polygons, though (that's where it's mostly used).
53 		B2DPolyPolygon stripNeutralPolygons(const B2DPolyPolygon& rCandidate);
54 
55 		// Remove not necessary polygons. Works only correct with crossover-free polygons. For each
56 		// polygon, the depth for the PolyPolygon is calculated. The orientation is used to identify holes.
57 		// Start value for holes is -1, for polygons it's zero. Ech time a polygon is contained in another one,
58 		// it's depth is increased when inside a polygon, decreased when inside a hole. The result is a depth
59 		// which e.g. is -1 for holes outside everything, 1 for a polygon covered by another polygon and zero
60 		// for e.g. holes in a polygon or polygons outside everythig else.
61 		// In the 2nd step, all polygons with depth other than zero are removed. If bKeepAboveZero is used,
62 		// all polygons < 1 are removed. The bKeepAboveZero mode is useful for clipping, e.g. just append
63 		// one polygon to another and use this mode -> only parts where two polygons overlapped will be kept.
64 		// In combination with correct orientation of the input orientations and the SolveCrossover calls this
65 		// can be combined for logical polygon operations or polygon clipping.
66 		B2DPolyPolygon stripDispensablePolygons(const B2DPolyPolygon& rCandidate, bool bKeepAboveZero = false);
67 
68         // geometrically convert PolyPolygons which are proposed to use nonzero fill rule
69         // to a representation where evenodd paint will give the same result. To do this
70         // all intersections and self-intersections get solved (the polygons will be rearranged
71         // if needed). Then all polygons which are inside another one with the same orientation
72         // get deleted
73         B2DPolyPolygon createNonzeroConform(const B2DPolyPolygon& rCandidate);
74 
75         // For convenience: The four basic operations OR, XOR, AND and DIFF for
76         // two PolyPolygons. These are combinations of the above methods. To not be forced
77         // to do evtl. already done preparations twice, You have to do the operations Yourself.
78         //
79         // A source preparation consists of preparing it to be seen as XOR-Rule PolyPolygon,
80         // so it is freed of intersections, self-intersections and the orientations are corrected.
81         // Important is that it will define the same areas as before, but is intersection-free.
82         // As an example think about a single polygon looping in itself and having holes. To
83         // topologically correctly handle this, it is necessary to remove all intersections and
84         // to correct the orientations. The orientation of the isolated holes e.g. will be negative.
85         // Topologically it is necessary to prepare each polygon which is seen as entity. It is
86         // not sufficient just to concatenate them and prepare the result, this may be topologically
87         // different since the simple concatenation will be seen as XOR. To work correctly, You
88         // may need to OR those polygons.
89 
90         // Preparations: solve self-intersections and intersections, remove neutral
91         // parts and correct orientations.
92         B2DPolyPolygon prepareForPolygonOperation(const B2DPolygon& rCandidate);
93         B2DPolyPolygon prepareForPolygonOperation(const B2DPolyPolygon& rCandidate);
94 
95         // OR: Return all areas where CandidateA or CandidateB exist
96         B2DPolyPolygon solvePolygonOperationOr(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB);
97 
98         // XOR: Return all areas where CandidateA or CandidateB exist, but not both
99         B2DPolyPolygon solvePolygonOperationXor(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB);
100 
101         // AND: Return all areas where CandidateA and CandidateB exist
102         B2DPolyPolygon solvePolygonOperationAnd(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB);
103 
104         // DIFF: Return all areas where CandidateA is not covered by CandidateB (cut B out of A)
105         B2DPolyPolygon solvePolygonOperationDiff(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB);
106 
107 		/** merge all single PolyPolygons to a single, OR-ed PolyPolygon
108 
109 			@param rInput
110 			The source PolyPolygons
111 
112 			@return A single PolyPolygon containing the Or-merged result
113 		*/
114 		B2DPolyPolygon mergeToSinglePolyPolygon(const B2DPolyPolygonVector& rInput);
115 
116 	} // end of namespace tools
117 } // end of namespace basegfx
118 
119 //////////////////////////////////////////////////////////////////////////////
120 
121 
122 #endif /* _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX */
123