1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
28cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "svx/polypolygoneditor.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace sdr {
33cdf0e10cSrcweir 
PolyPolygonEditor(const basegfx::B2DPolyPolygon & rPolyPolygon,bool bClosed)34cdf0e10cSrcweir PolyPolygonEditor::PolyPolygonEditor( const basegfx::B2DPolyPolygon& rPolyPolygon, bool bClosed )
35cdf0e10cSrcweir : maPolyPolygon( rPolyPolygon )
36cdf0e10cSrcweir , mbIsClosed( bClosed )
37cdf0e10cSrcweir {
38cdf0e10cSrcweir }
39cdf0e10cSrcweir 
DeletePoints(const std::set<sal_uInt16> & rAbsPoints)40cdf0e10cSrcweir bool PolyPolygonEditor::DeletePoints( const std::set< sal_uInt16 >& rAbsPoints )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	bool bPolyPolyChanged = false;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
45cdf0e10cSrcweir 	for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); aIter++ )
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		sal_uInt32 nPoly, nPnt;
48cdf0e10cSrcweir 		if( GetRelativePolyPoint(maPolyPolygon,(*aIter), nPoly, nPnt) )
49cdf0e10cSrcweir 		{
50cdf0e10cSrcweir 			// remove point
51cdf0e10cSrcweir 			basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPoly));
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 			aCandidate.remove(nPnt);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 			if( ( mbIsClosed && aCandidate.count() < 3L) || (aCandidate.count() < 2L) )
56cdf0e10cSrcweir 			{
57cdf0e10cSrcweir 				maPolyPolygon.remove(nPoly);
58cdf0e10cSrcweir 			}
59cdf0e10cSrcweir 			else
60cdf0e10cSrcweir 			{
61cdf0e10cSrcweir 				maPolyPolygon.setB2DPolygon(nPoly, aCandidate);
62cdf0e10cSrcweir 			}
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 			bPolyPolyChanged = true;
65cdf0e10cSrcweir 		}
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	return bPolyPolyChanged;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
SetSegmentsKind(SdrPathSegmentKind eKind,const std::set<sal_uInt16> & rAbsPoints)71cdf0e10cSrcweir bool PolyPolygonEditor::SetSegmentsKind(SdrPathSegmentKind eKind, const std::set< sal_uInt16 >& rAbsPoints )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	bool bPolyPolyChanged = false;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
76cdf0e10cSrcweir 	for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); aIter++ )
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir 		sal_uInt32 nPolyNum, nPntNum;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			// do change at aNewPolyPolygon. Take a look at edge.
83cdf0e10cSrcweir 			basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
84cdf0e10cSrcweir 			bool bCandidateChanged(false);
85cdf0e10cSrcweir 			const sal_uInt32 nCount(aCandidate.count());
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 			if(nCount && (nPntNum + 1 < nCount || aCandidate.isClosed()))
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				// it's a valid edge, check control point usage
90cdf0e10cSrcweir 				const sal_uInt32 nNextIndex((nPntNum + 1) % nCount);
91cdf0e10cSrcweir 				const bool bContolUsed(aCandidate.areControlPointsUsed()
92cdf0e10cSrcweir 					&& (aCandidate.isNextControlPointUsed(nPntNum) || aCandidate.isPrevControlPointUsed(nNextIndex)));
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 				if(bContolUsed)
95cdf0e10cSrcweir 				{
96cdf0e10cSrcweir 					if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_LINE == eKind)
97cdf0e10cSrcweir 					{
98cdf0e10cSrcweir 						// remove control
99cdf0e10cSrcweir 						aCandidate.resetNextControlPoint(nPntNum);
100cdf0e10cSrcweir 						aCandidate.resetPrevControlPoint(nNextIndex);
101cdf0e10cSrcweir 						bCandidateChanged = true;
102cdf0e10cSrcweir 					}
103cdf0e10cSrcweir 				}
104cdf0e10cSrcweir 				else
105cdf0e10cSrcweir 				{
106cdf0e10cSrcweir 					if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_CURVE == eKind)
107cdf0e10cSrcweir 					{
108cdf0e10cSrcweir 						// add control
109cdf0e10cSrcweir 						const basegfx::B2DPoint aStart(aCandidate.getB2DPoint(nPntNum));
110cdf0e10cSrcweir 						const basegfx::B2DPoint aEnd(aCandidate.getB2DPoint(nNextIndex));
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 						aCandidate.setNextControlPoint(nPntNum, interpolate(aStart, aEnd, (1.0 / 3.0)));
113cdf0e10cSrcweir 						aCandidate.setPrevControlPoint(nNextIndex, interpolate(aStart, aEnd, (2.0 / 3.0)));
114cdf0e10cSrcweir 						bCandidateChanged = true;
115cdf0e10cSrcweir 					}
116cdf0e10cSrcweir 				}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 				if(bCandidateChanged)
119cdf0e10cSrcweir 				{
120cdf0e10cSrcweir 					maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
121cdf0e10cSrcweir 					bPolyPolyChanged = true;
122cdf0e10cSrcweir 				}
123cdf0e10cSrcweir 			}
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	return bPolyPolyChanged;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
SetPointsSmooth(basegfx::B2VectorContinuity eFlags,const std::set<sal_uInt16> & rAbsPoints)130cdf0e10cSrcweir bool PolyPolygonEditor::SetPointsSmooth( basegfx::B2VectorContinuity eFlags, const std::set< sal_uInt16 >& rAbsPoints)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	bool bPolyPolygonChanged(false);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
135cdf0e10cSrcweir 	for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); aIter++ )
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		sal_uInt32 nPolyNum, nPntNum;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 		if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
140cdf0e10cSrcweir 		{
141cdf0e10cSrcweir 			// do change at aNewPolyPolygon...
142cdf0e10cSrcweir 			basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 			// set continuity in point, make sure there is a curve
145cdf0e10cSrcweir 			bool bPolygonChanged(false);
146cdf0e10cSrcweir 			bPolygonChanged = basegfx::tools::expandToCurveInPoint(aCandidate, nPntNum);
147cdf0e10cSrcweir 			bPolygonChanged |= basegfx::tools::setContinuityInPoint(aCandidate, nPntNum, eFlags);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 			if(bPolygonChanged)
150cdf0e10cSrcweir 			{
151cdf0e10cSrcweir 				maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
152cdf0e10cSrcweir 				bPolyPolygonChanged = true;
153cdf0e10cSrcweir 			}
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	return bPolyPolygonChanged;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
GetRelativePolyPoint(const basegfx::B2DPolyPolygon & rPoly,sal_uInt32 nAbsPnt,sal_uInt32 & rPolyNum,sal_uInt32 & rPointNum)160cdf0e10cSrcweir bool PolyPolygonEditor::GetRelativePolyPoint( const basegfx::B2DPolyPolygon& rPoly, sal_uInt32 nAbsPnt, sal_uInt32& rPolyNum, sal_uInt32& rPointNum )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir 	const sal_uInt32 nPolyCount(rPoly.count());
163cdf0e10cSrcweir 	sal_uInt32 nPolyNum(0L);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	while(nPolyNum < nPolyCount)
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir 		const sal_uInt32 nPointCount(rPoly.getB2DPolygon(nPolyNum).count());
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		if(nAbsPnt < nPointCount)
170cdf0e10cSrcweir 		{
171cdf0e10cSrcweir 			rPolyNum = nPolyNum;
172cdf0e10cSrcweir 			rPointNum = nAbsPnt;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 			return true;
175cdf0e10cSrcweir 		}
176cdf0e10cSrcweir 		else
177cdf0e10cSrcweir 		{
178cdf0e10cSrcweir 			nPolyNum++;
179cdf0e10cSrcweir 			nAbsPnt -= nPointCount;
180cdf0e10cSrcweir 		}
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	return false;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir } // end of namespace sdr
187