summaryrefslogtreecommitdiff
path: root/basegfx/inc/basegfx/polygon/b2dpolypolygoncutter.hxx
blob: c118076d82fef11cb76f7a6db6c90678aa78c497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*************************************************************************
 *
 *  $RCSfile: b2dpolypolygoncutter.hxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: aw $ $Date: 2003-11-10 11:45:48 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX
#define _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX

#ifndef _SAL_TYPES_H_
#include <sal/types.h>
#endif

#ifndef _BGFX_POINT_B2DPOINT_HXX
#include <basegfx/point/b2dpoint.hxx>
#endif

#ifndef _BGFX_VECTOR_B2DVECTOR_HXX
#include <basegfx/vector/b2dvector.hxx>
#endif

#ifndef _BGFX_RANGE_B2DRANGE_HXX
#include <basegfx/range/b2drange.hxx>
#endif

#ifndef _BGFX_POLYGON_B2DPOLYPOLYGON_HXX
#include <basegfx/polygon/b2dpolypolygon.hxx>
#endif

#include <vector>
#include <algorithm>

//////////////////////////////////////////////////////////////////////////////
// B2DPolygonNode class
//
// This class is a pure tooling class used to hold a polygon in double linked
// list form for faster manipulation. It is used from the polygon clipper.

namespace basegfx
{
    namespace polygon
    {
        class B2DPolygonNode
        {
            ::basegfx::point::B2DPoint              maPosition;
            B2DPolygonNode*                         mpPrevious;
            B2DPolygonNode*                         mpNext;

            B2DPolygonNode*                         mpListPrevious;
            B2DPolygonNode*                         mpListNext;

        public:
            B2DPolygonNode(const ::basegfx::point::B2DPoint& rPosition, B2DPolygonNode* pPrevious);
            ~B2DPolygonNode();

            B2DPolygonNode* getPrevious() const { return mpPrevious; }
            B2DPolygonNode* getNext() const { return mpNext; }
            const ::basegfx::point::B2DPoint& getPosition() const { return maPosition; }

            void calcMinMaxX(double& fMaxAX, double& fMinAX) const;
            void calcMinMaxY(double& fMaxAY, double& fMinAY) const;

            void swapPreviousNext() { B2DPolygonNode* pZwi = mpPrevious; mpPrevious = mpNext; mpNext = pZwi; }
            void swapNextPointers(B2DPolygonNode* pCand);

            void addToList(B2DPolygonNode*& rpList);
            void remFromList(B2DPolygonNode*& rpList);

            sal_Bool getOrientation() const;
            void swapOrientation();
            ::basegfx::range::B2DRange getRange() const;

            // isInside tests for B2dPoint and other B2DPolygonNode Polygon. On border is not inside as long as
            // not sal_True is given in bWithBorder flag.
            sal_Bool isInside(const ::basegfx::point::B2DPoint& rPnt, sal_Bool bWithBorder = sal_False) const;
            sal_Bool isPolygonInside(B2DPolygonNode* pPoly, sal_Bool bWithBorder = sal_False) const;
        };

        // a type definition to have a vector of pointers to B2DPolygonNodes
        typedef ::std::vector< B2DPolygonNode* > B2DPolygonNodeVector;

    } // end of namespace polygon
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////
// B2DSimpleCut class

namespace basegfx
{
    namespace polygon
    {
        class B2DSimpleCut
        {
            B2DPolygonNode*                         mpLeft;
            B2DPolygonNode*                         mpRight;

            // bitfield
            unsigned                                mbCorrectOrientation : 1;
            unsigned                                mbOrientation : 1;

        public:
            B2DSimpleCut(B2DPolygonNode* pL, B2DPolygonNode* pR, sal_Bool bCoOr = sal_False, sal_Bool bOr = sal_True)
            :   mpLeft(pL),
                mpRight(pR),
                mbCorrectOrientation(bCoOr),
                mbOrientation(bOr)
            {
            }

            void solve();
            B2DPolygonNode* getLeft() const { return mpLeft; }
            B2DPolygonNode* getRight() const { return mpRight; }

            sal_Bool isSameCut(B2DPolygonNode* pA, B2DPolygonNode* pB) const
            {
                return ((pA == mpLeft && pB == mpRight) || (pB == mpLeft && pA == mpRight));
            }
        };

        // a type definition to have a vector of pointers to B2DSimpleCuts
        typedef ::std::vector< B2DSimpleCut* > B2DSimpleCutVector;

    } // end of namespace polygon
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////
// B2DClipExtraPolygonInfo class

namespace basegfx
{
    namespace polygon
    {
        class B2DClipExtraPolygonInfo
        {
            ::basegfx::range::B2DRange              maRange;
            sal_Int32                               mnDepth;

            // bitfield
            unsigned                                mbOrientation : 1;

        public:
            B2DClipExtraPolygonInfo() {}

            void init(B2DPolygonNode* pNew);
            const ::basegfx::range::B2DRange& getRange() const { return maRange; }
            sal_Bool getOrientation() const { return mbOrientation; }

            sal_Int32 getDepth() const { return mnDepth; }
            void changeDepth(sal_Bool bOrientation);
        };
    } // end of namespace polygon
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////
// class B2DPolyPolygonCutter

namespace basegfx
{
    namespace polygon
    {
        class B2DPolyPolygonCutter
        {
            // list of polys
            B2DPolygonNodeVector                    maPolygonList;
            B2DPolyPolygon                          maNotClosedPolygons;

            // help routines
            sal_Bool isSamePos(const ::basegfx::point::B2DPoint& rPntA, const ::basegfx::point::B2DPoint& rPntB)
            {
                return rPntA.equal(rPntB);
            }

            B2DSimpleCut* getExistingCut(B2DSimpleCutVector& rTmpCuts, B2DPolygonNode* pA, B2DPolygonNode* pB);
            B2DPolygonNode* extractNextPolygon(B2DPolygonNode*& rpList);
            sal_Bool isCrossover(B2DPolygonNode* pA, B2DPolygonNode* pB);
            sal_Bool isCrossover(B2DSimpleCut* pEnter, B2DSimpleCut* pLeave);

            sal_Bool isNextSamePos(B2DPolygonNode* pA, B2DPolygonNode* pB)
            {
                return isSamePos(pA->getNext()->getPosition(), pB->getNext()->getPosition());
            }

            sal_Bool isPrevSamePos(B2DPolygonNode* pA, B2DPolygonNode* pB)
            {
                return isSamePos(pA->getPrevious()->getPosition(), pB->getPrevious()->getPosition());
            }

            void addAllNodes(B2DPolygonNode* pPolygon, B2DPolygonNode*& rpList);
            B2DPolygonNode* createNewPolygon(const B2DPolygon& rPolygon);
            void deletePolygon(B2DPolygonNode* pCand);
            void polysToList(B2DPolygonNode*& rpList);
            void listToPolys(B2DPolygonNode*& rpList);

            sal_Bool doRangesIntersect(const ::basegfx::range::B2DRange& rRange1, const ::basegfx::range::B2DRange& rRange2) const
            {
                return rRange1.overlaps(rRange2);
            }

            sal_Bool doRangesInclude(const ::basegfx::range::B2DRange& rRange1, const ::basegfx::range::B2DRange& rRange2) const
            {
                return rRange1.isInside(rRange2);
            }

            void solveAllCuts(B2DSimpleCutVector& rCuts);

        public:
            B2DPolyPolygonCutter() {}
            ~B2DPolyPolygonCutter();

            // put/get poly
            void addPolyPolygon(const B2DPolyPolygon& rPolyPolygon, sal_Bool bForceOrientation = sal_False);
            void getPolyPolygon(B2DPolyPolygon& rPolyPolygon);

            // transformations
            void removeSelfIntersections();
            void removeDoubleIntersections();

            // remove included
            void removeIncludedPolygons(sal_Bool bUseOr = sal_True);
        };
    } // end of namespace polygon
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////


#endif //   _BGFX_POLYGON_B2DPOLYPOLYGONCUTTER_HXX