1*464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*464702f4SAndrew Rist  * distributed with this work for additional information
6*464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*464702f4SAndrew Rist  *
11*464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*464702f4SAndrew Rist  *
13*464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*464702f4SAndrew Rist  * software distributed under the License is distributed on an
15*464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*464702f4SAndrew Rist  * specific language governing permissions and limitations
18*464702f4SAndrew Rist  * under the License.
19*464702f4SAndrew Rist  *
20*464702f4SAndrew Rist  *************************************************************/
21*464702f4SAndrew Rist 
22*464702f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <drawinglayer/primitive2d/helplineprimitive2d.hxx>
28cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
29cdf0e10cSrcweir #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygonclipper.hxx>
31cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
32cdf0e10cSrcweir #include <drawinglayer/geometry/viewinformation2d.hxx>
33cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace com::sun::star;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace drawinglayer
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	namespace primitive2d
44cdf0e10cSrcweir 	{
create2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const45cdf0e10cSrcweir 		Primitive2DSequence HelplinePrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
46cdf0e10cSrcweir 		{
47cdf0e10cSrcweir 			std::vector< BasePrimitive2D* > aTempPrimitiveTarget;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 			if(!rViewInformation.getViewport().isEmpty() && !getDirection().equalZero())
50cdf0e10cSrcweir 			{
51cdf0e10cSrcweir 				// position to view coordinates, DashLen and DashLen in logic
52cdf0e10cSrcweir 				const basegfx::B2DPoint aViewPosition(rViewInformation.getObjectToViewTransformation() * getPosition());
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 				switch(getStyle())
55cdf0e10cSrcweir 				{
56cdf0e10cSrcweir 					default : // HELPLINESTYLE2D_POINT
57cdf0e10cSrcweir 					{
58cdf0e10cSrcweir 						const double fViewFixValue(15.0);
59cdf0e10cSrcweir 						basegfx::B2DVector aNormalizedDirection(getDirection());
60cdf0e10cSrcweir 						aNormalizedDirection.normalize();
61cdf0e10cSrcweir 						aNormalizedDirection *= fViewFixValue;
62cdf0e10cSrcweir 						const basegfx::B2DPoint aStartA(aViewPosition - aNormalizedDirection);
63cdf0e10cSrcweir 						const basegfx::B2DPoint aEndA(aViewPosition + aNormalizedDirection);
64cdf0e10cSrcweir 						basegfx::B2DPolygon aLineA;
65cdf0e10cSrcweir 						aLineA.append(aStartA);
66cdf0e10cSrcweir 						aLineA.append(aEndA);
67cdf0e10cSrcweir 						aLineA.transform(rViewInformation.getInverseObjectToViewTransformation());
68cdf0e10cSrcweir 						PolygonMarkerPrimitive2D* pNewA = new PolygonMarkerPrimitive2D(aLineA, getRGBColA(), getRGBColB(), getDiscreteDashLength());
69cdf0e10cSrcweir 						aTempPrimitiveTarget.push_back(pNewA);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 						const basegfx::B2DVector aPerpendicularNormalizedDirection(basegfx::getPerpendicular(aNormalizedDirection));
72cdf0e10cSrcweir 						const basegfx::B2DPoint aStartB(aViewPosition - aPerpendicularNormalizedDirection);
73cdf0e10cSrcweir 						const basegfx::B2DPoint aEndB(aViewPosition + aPerpendicularNormalizedDirection);
74cdf0e10cSrcweir 						basegfx::B2DPolygon aLineB;
75cdf0e10cSrcweir 						aLineB.append(aStartB);
76cdf0e10cSrcweir 						aLineB.append(aEndB);
77cdf0e10cSrcweir 						aLineB.transform(rViewInformation.getInverseObjectToViewTransformation());
78cdf0e10cSrcweir 						PolygonMarkerPrimitive2D* pNewB = new PolygonMarkerPrimitive2D(aLineB, getRGBColA(), getRGBColB(), getDiscreteDashLength());
79cdf0e10cSrcweir 						aTempPrimitiveTarget.push_back(pNewB);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 						break;
82cdf0e10cSrcweir 					}
83cdf0e10cSrcweir 					case HELPLINESTYLE2D_LINE :
84cdf0e10cSrcweir 					{
85cdf0e10cSrcweir 						basegfx::B2DPolygon aLine;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 						if(basegfx::areParallel(getDirection(), basegfx::B2DVector(1.0, 0.0)))
88cdf0e10cSrcweir 						{
89cdf0e10cSrcweir 							// parallel to X-Axis, get cuts with Y-Axes
90cdf0e10cSrcweir 							const double fCutA((rViewInformation.getDiscreteViewport().getMinX() - aViewPosition.getX()) / getDirection().getX());
91cdf0e10cSrcweir 							const double fCutB((rViewInformation.getDiscreteViewport().getMaxX() - aViewPosition.getX()) / getDirection().getX());
92cdf0e10cSrcweir 							const basegfx::B2DPoint aPosA(aViewPosition + (fCutA * getDirection()));
93cdf0e10cSrcweir 							const basegfx::B2DPoint aPosB(aViewPosition + (fCutB * getDirection()));
94cdf0e10cSrcweir 							const bool bBothLeft(aPosA.getX() < rViewInformation.getDiscreteViewport().getMinX() && aPosB.getX() < rViewInformation.getDiscreteViewport().getMinX());
95cdf0e10cSrcweir 							const bool bBothRight(aPosA.getX() > rViewInformation.getDiscreteViewport().getMaxX() && aPosB.getX() < rViewInformation.getDiscreteViewport().getMaxX());
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 							if(!bBothLeft && !bBothRight)
98cdf0e10cSrcweir 							{
99cdf0e10cSrcweir 								aLine.append(aPosA);
100cdf0e10cSrcweir 								aLine.append(aPosB);
101cdf0e10cSrcweir 							}
102cdf0e10cSrcweir 						}
103cdf0e10cSrcweir 						else
104cdf0e10cSrcweir 						{
105cdf0e10cSrcweir 							// get cuts with X-Axes
106cdf0e10cSrcweir 							const double fCutA((rViewInformation.getDiscreteViewport().getMinY() - aViewPosition.getY()) / getDirection().getY());
107cdf0e10cSrcweir 							const double fCutB((rViewInformation.getDiscreteViewport().getMaxY() - aViewPosition.getY()) / getDirection().getY());
108cdf0e10cSrcweir 							const basegfx::B2DPoint aPosA(aViewPosition + (fCutA * getDirection()));
109cdf0e10cSrcweir 							const basegfx::B2DPoint aPosB(aViewPosition + (fCutB * getDirection()));
110cdf0e10cSrcweir 							const bool bBothAbove(aPosA.getY() < rViewInformation.getDiscreteViewport().getMinY() && aPosB.getY() < rViewInformation.getDiscreteViewport().getMinY());
111cdf0e10cSrcweir 							const bool bBothBelow(aPosA.getY() > rViewInformation.getDiscreteViewport().getMaxY() && aPosB.getY() < rViewInformation.getDiscreteViewport().getMaxY());
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 							if(!bBothAbove && !bBothBelow)
114cdf0e10cSrcweir 							{
115cdf0e10cSrcweir 								aLine.append(aPosA);
116cdf0e10cSrcweir 								aLine.append(aPosB);
117cdf0e10cSrcweir 							}
118cdf0e10cSrcweir 						}
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 						if(aLine.count())
121cdf0e10cSrcweir 						{
122cdf0e10cSrcweir 							// clip against visible area
123cdf0e10cSrcweir 							const basegfx::B2DPolyPolygon aResult(basegfx::tools::clipPolygonOnRange(aLine, rViewInformation.getDiscreteViewport(), true, true));
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 							for(sal_uInt32 a(0L); a < aResult.count(); a++)
126cdf0e10cSrcweir 							{
127cdf0e10cSrcweir 								basegfx::B2DPolygon aPart(aResult.getB2DPolygon(a));
128cdf0e10cSrcweir 								aPart.transform(rViewInformation.getInverseObjectToViewTransformation());
129cdf0e10cSrcweir 								PolygonMarkerPrimitive2D* pNew = new PolygonMarkerPrimitive2D(aPart, getRGBColA(), getRGBColB(), getDiscreteDashLength());
130cdf0e10cSrcweir 								aTempPrimitiveTarget.push_back(pNew);
131cdf0e10cSrcweir 							}
132cdf0e10cSrcweir 						}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 						break;
135cdf0e10cSrcweir 					}
136cdf0e10cSrcweir 				}
137cdf0e10cSrcweir 			}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 			// prepare return value
140cdf0e10cSrcweir 			Primitive2DSequence aRetval(aTempPrimitiveTarget.size());
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < aTempPrimitiveTarget.size(); a++)
143cdf0e10cSrcweir 			{
144cdf0e10cSrcweir 				const Primitive2DReference xRef(aTempPrimitiveTarget[a]);
145cdf0e10cSrcweir 				aRetval[a] = xRef;
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			return aRetval;
149cdf0e10cSrcweir 		}
150cdf0e10cSrcweir 
HelplinePrimitive2D(const basegfx::B2DPoint & rPosition,const basegfx::B2DVector & rDirection,HelplineStyle2D eStyle,const basegfx::BColor & rRGBColA,const basegfx::BColor & rRGBColB,double fDiscreteDashLength)151cdf0e10cSrcweir 		HelplinePrimitive2D::HelplinePrimitive2D(
152cdf0e10cSrcweir 			const basegfx::B2DPoint& rPosition,
153cdf0e10cSrcweir 			const basegfx::B2DVector& rDirection,
154cdf0e10cSrcweir 			HelplineStyle2D eStyle,
155cdf0e10cSrcweir 			const basegfx::BColor& rRGBColA,
156cdf0e10cSrcweir 			const basegfx::BColor& rRGBColB,
157cdf0e10cSrcweir 			double fDiscreteDashLength)
158cdf0e10cSrcweir 		:	BufferedDecompositionPrimitive2D(),
159cdf0e10cSrcweir 			maPosition(rPosition),
160cdf0e10cSrcweir 			maDirection(rDirection),
161cdf0e10cSrcweir 			meStyle(eStyle),
162cdf0e10cSrcweir 			maRGBColA(rRGBColA),
163cdf0e10cSrcweir 			maRGBColB(rRGBColB),
164cdf0e10cSrcweir 			mfDiscreteDashLength(fDiscreteDashLength),
165cdf0e10cSrcweir 			maLastObjectToViewTransformation(),
166cdf0e10cSrcweir 			maLastViewport()
167cdf0e10cSrcweir 		{
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 
operator ==(const BasePrimitive2D & rPrimitive) const170cdf0e10cSrcweir 		bool HelplinePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
171cdf0e10cSrcweir 		{
172cdf0e10cSrcweir 			if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
173cdf0e10cSrcweir 			{
174cdf0e10cSrcweir 				const HelplinePrimitive2D& rCompare = (HelplinePrimitive2D&)rPrimitive;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 				return (getPosition() == rCompare.getPosition()
177cdf0e10cSrcweir 					&& getDirection() == rCompare.getDirection()
178cdf0e10cSrcweir 					&& getStyle() == rCompare.getStyle()
179cdf0e10cSrcweir 					&& getRGBColA() == rCompare.getRGBColA()
180cdf0e10cSrcweir 					&& getRGBColB() == rCompare.getRGBColB()
181cdf0e10cSrcweir 					&& getDiscreteDashLength() == rCompare.getDiscreteDashLength());
182cdf0e10cSrcweir 			}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			return false;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const187cdf0e10cSrcweir 		Primitive2DSequence HelplinePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
188cdf0e10cSrcweir 		{
189cdf0e10cSrcweir 			::osl::MutexGuard aGuard( m_aMutex );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			if(getBuffered2DDecomposition().hasElements())
192cdf0e10cSrcweir 			{
193cdf0e10cSrcweir 				if(maLastViewport != rViewInformation.getViewport() || maLastObjectToViewTransformation != rViewInformation.getObjectToViewTransformation())
194cdf0e10cSrcweir 				{
195cdf0e10cSrcweir 					// conditions of last local decomposition have changed, delete
196cdf0e10cSrcweir 					const_cast< HelplinePrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
197cdf0e10cSrcweir 				}
198cdf0e10cSrcweir 			}
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 			if(!getBuffered2DDecomposition().hasElements())
201cdf0e10cSrcweir 			{
202cdf0e10cSrcweir 				// remember ViewRange and ViewTransformation
203cdf0e10cSrcweir 				const_cast< HelplinePrimitive2D* >(this)->maLastObjectToViewTransformation = rViewInformation.getObjectToViewTransformation();
204cdf0e10cSrcweir 				const_cast< HelplinePrimitive2D* >(this)->maLastViewport = rViewInformation.getViewport();
205cdf0e10cSrcweir 			}
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 			// use parent implementation
208cdf0e10cSrcweir 			return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 		// provide unique ID
212cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(HelplinePrimitive2D, PRIMITIVE2D_ID_HELPLINEPRIMITIVE2D)
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	} // end of namespace primitive2d
215cdf0e10cSrcweir } // end of namespace drawinglayer
216cdf0e10cSrcweir 
217cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
218cdf0e10cSrcweir // eof
219