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