xref: /trunk/main/drawinglayer/source/primitive2d/gridprimitive2d.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <drawinglayer/primitive2d/gridprimitive2d.hxx>
32*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
33*cdf0e10cSrcweir #include <drawinglayer/primitive2d/pointarrayprimitive2d.hxx>
34*cdf0e10cSrcweir #include <drawinglayer/primitive2d/markerarrayprimitive2d.hxx>
35*cdf0e10cSrcweir #include <drawinglayer/geometry/viewinformation2d.hxx>
36*cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
37*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace com::sun::star;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace drawinglayer
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     namespace primitive2d
48*cdf0e10cSrcweir     {
49*cdf0e10cSrcweir         Primitive2DSequence GridPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
50*cdf0e10cSrcweir         {
51*cdf0e10cSrcweir             Primitive2DSequence aRetval;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir             if(!rViewInformation.getViewport().isEmpty() && getWidth() > 0.0 && getHeight() > 0.0)
54*cdf0e10cSrcweir             {
55*cdf0e10cSrcweir                 // decompose grid matrix to get logic size
56*cdf0e10cSrcweir                 basegfx::B2DVector aScale, aTranslate;
57*cdf0e10cSrcweir                 double fRotate, fShearX;
58*cdf0e10cSrcweir                 getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir                 // create grid matrix which transforms from scaled logic to view
61*cdf0e10cSrcweir                 basegfx::B2DHomMatrix aRST(basegfx::tools::createShearXRotateTranslateB2DHomMatrix(
62*cdf0e10cSrcweir                     fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
63*cdf0e10cSrcweir                 aRST *= rViewInformation.getObjectToViewTransformation();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir                 // get step widths
66*cdf0e10cSrcweir                 double fStepX(getWidth());
67*cdf0e10cSrcweir                 double fStepY(getHeight());
68*cdf0e10cSrcweir                 const double fMinimalStep(10.0);
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir                 // guarantee a step width of 10.0
71*cdf0e10cSrcweir                 if(basegfx::fTools::less(fStepX, fMinimalStep))
72*cdf0e10cSrcweir                 {
73*cdf0e10cSrcweir                     fStepX = fMinimalStep;
74*cdf0e10cSrcweir                 }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir                 if(basegfx::fTools::less(fStepY, fMinimalStep))
77*cdf0e10cSrcweir                 {
78*cdf0e10cSrcweir                     fStepY = fMinimalStep;
79*cdf0e10cSrcweir                 }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir                 // get relative distances in view coordinates
82*cdf0e10cSrcweir                 double fViewStepX((rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(fStepX, 0.0)).getLength());
83*cdf0e10cSrcweir                 double fViewStepY((rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(0.0, fStepY)).getLength());
84*cdf0e10cSrcweir                 double fSmallStepX(1.0), fViewSmallStepX(1.0), fSmallStepY(1.0), fViewSmallStepY(1.0);
85*cdf0e10cSrcweir                 sal_uInt32 nSmallStepsX(0L), nSmallStepsY(0L);
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir                 // setup subdivisions
88*cdf0e10cSrcweir                 if(getSubdivisionsX())
89*cdf0e10cSrcweir                 {
90*cdf0e10cSrcweir                     fSmallStepX = fStepX / getSubdivisionsX();
91*cdf0e10cSrcweir                     fViewSmallStepX = fViewStepX / getSubdivisionsX();
92*cdf0e10cSrcweir                 }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir                 if(getSubdivisionsY())
95*cdf0e10cSrcweir                 {
96*cdf0e10cSrcweir                     fSmallStepY = fStepY / getSubdivisionsY();
97*cdf0e10cSrcweir                     fViewSmallStepY = fViewStepY / getSubdivisionsY();
98*cdf0e10cSrcweir                 }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir                 // correct step width
101*cdf0e10cSrcweir                 while(fViewStepX < getSmallestViewDistance())
102*cdf0e10cSrcweir                 {
103*cdf0e10cSrcweir                     fViewStepX *= 2.0;
104*cdf0e10cSrcweir                     fStepX *= 2.0;
105*cdf0e10cSrcweir                 }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir                 while(fViewStepY < getSmallestViewDistance())
108*cdf0e10cSrcweir                 {
109*cdf0e10cSrcweir                     fViewStepY *= 2.0;
110*cdf0e10cSrcweir                     fStepY *= 2.0;
111*cdf0e10cSrcweir                 }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir                 // correct small step width
114*cdf0e10cSrcweir                 if(getSubdivisionsX())
115*cdf0e10cSrcweir                 {
116*cdf0e10cSrcweir                     while(fViewSmallStepX < getSmallestSubdivisionViewDistance())
117*cdf0e10cSrcweir                     {
118*cdf0e10cSrcweir                         fViewSmallStepX *= 2.0;
119*cdf0e10cSrcweir                         fSmallStepX *= 2.0;
120*cdf0e10cSrcweir                     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir                     nSmallStepsX = (sal_uInt32)(fStepX / fSmallStepX);
123*cdf0e10cSrcweir                 }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir                 if(getSubdivisionsY())
126*cdf0e10cSrcweir                 {
127*cdf0e10cSrcweir                     while(fViewSmallStepY < getSmallestSubdivisionViewDistance())
128*cdf0e10cSrcweir                     {
129*cdf0e10cSrcweir                         fViewSmallStepY *= 2.0;
130*cdf0e10cSrcweir                         fSmallStepY *= 2.0;
131*cdf0e10cSrcweir                     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir                     nSmallStepsY = (sal_uInt32)(fStepY / fSmallStepY);
134*cdf0e10cSrcweir                 }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir                 // prepare point vectors for point and cross markers
137*cdf0e10cSrcweir                 std::vector< basegfx::B2DPoint > aPositionsPoint;
138*cdf0e10cSrcweir                 std::vector< basegfx::B2DPoint > aPositionsCross;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir                 for(double fX(0.0); fX < aScale.getX(); fX += fStepX)
141*cdf0e10cSrcweir                 {
142*cdf0e10cSrcweir                     const bool bXZero(basegfx::fTools::equalZero(fX));
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir                     for(double fY(0.0); fY < aScale.getY(); fY += fStepY)
145*cdf0e10cSrcweir                     {
146*cdf0e10cSrcweir                         const bool bYZero(basegfx::fTools::equalZero(fY));
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir                         if(!bXZero && !bYZero)
149*cdf0e10cSrcweir                         {
150*cdf0e10cSrcweir                             // get discrete position and test against 3x3 area surrounding it
151*cdf0e10cSrcweir                             // since it's a cross
152*cdf0e10cSrcweir                             const double fHalfCrossSize(3.0 * 0.5);
153*cdf0e10cSrcweir                             const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fX, fY));
154*cdf0e10cSrcweir                             const basegfx::B2DRange aDiscreteRangeCross(
155*cdf0e10cSrcweir                                 aViewPos.getX() - fHalfCrossSize, aViewPos.getY() - fHalfCrossSize,
156*cdf0e10cSrcweir                                 aViewPos.getX() + fHalfCrossSize, aViewPos.getY() + fHalfCrossSize);
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir                             if(rViewInformation.getDiscreteViewport().overlaps(aDiscreteRangeCross))
159*cdf0e10cSrcweir                             {
160*cdf0e10cSrcweir                                 const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
161*cdf0e10cSrcweir                                 aPositionsCross.push_back(aLogicPos);
162*cdf0e10cSrcweir                             }
163*cdf0e10cSrcweir                         }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir                         if(getSubdivisionsX() && !bYZero)
166*cdf0e10cSrcweir                         {
167*cdf0e10cSrcweir                             double fF(fX + fSmallStepX);
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir                             for(sal_uInt32 a(1L); a < nSmallStepsX && fF < aScale.getX(); a++, fF += fSmallStepX)
170*cdf0e10cSrcweir                             {
171*cdf0e10cSrcweir                                 const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fF, fY));
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir                                 if(rViewInformation.getDiscreteViewport().isInside(aViewPos))
174*cdf0e10cSrcweir                                 {
175*cdf0e10cSrcweir                                     const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
176*cdf0e10cSrcweir                                     aPositionsPoint.push_back(aLogicPos);
177*cdf0e10cSrcweir                                 }
178*cdf0e10cSrcweir                             }
179*cdf0e10cSrcweir                         }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir                         if(getSubdivisionsY() && !bXZero)
182*cdf0e10cSrcweir                         {
183*cdf0e10cSrcweir                             double fF(fY + fSmallStepY);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir                             for(sal_uInt32 a(1L); a < nSmallStepsY && fF < aScale.getY(); a++, fF += fSmallStepY)
186*cdf0e10cSrcweir                             {
187*cdf0e10cSrcweir                                 const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fX, fF));
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir                                 if(rViewInformation.getDiscreteViewport().isInside(aViewPos))
190*cdf0e10cSrcweir                                 {
191*cdf0e10cSrcweir                                     const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
192*cdf0e10cSrcweir                                     aPositionsPoint.push_back(aLogicPos);
193*cdf0e10cSrcweir                                 }
194*cdf0e10cSrcweir                             }
195*cdf0e10cSrcweir                         }
196*cdf0e10cSrcweir                     }
197*cdf0e10cSrcweir                 }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir                 // prepare return value
200*cdf0e10cSrcweir                 const sal_uInt32 nCountPoint(aPositionsPoint.size());
201*cdf0e10cSrcweir                 const sal_uInt32 nCountCross(aPositionsCross.size());
202*cdf0e10cSrcweir                 const sal_uInt32 nRetvalCount((nCountPoint ? 1 : 0) + (nCountCross ? 1 : 0));
203*cdf0e10cSrcweir                 sal_uInt32 nInsertCounter(0);
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir                 aRetval.realloc(nRetvalCount);
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir                 // add PointArrayPrimitive2D if point markers were added
208*cdf0e10cSrcweir                 if(nCountPoint)
209*cdf0e10cSrcweir                 {
210*cdf0e10cSrcweir                     aRetval[nInsertCounter++] = Primitive2DReference(new PointArrayPrimitive2D(aPositionsPoint, getBColor()));
211*cdf0e10cSrcweir                 }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir                 // add MarkerArrayPrimitive2D if cross markers were added
214*cdf0e10cSrcweir                 if(nCountCross)
215*cdf0e10cSrcweir                 {
216*cdf0e10cSrcweir                     if(!getSubdivisionsX() && !getSubdivisionsY())
217*cdf0e10cSrcweir                     {
218*cdf0e10cSrcweir                         // no subdivisions, so fall back to points at grid positions, no need to
219*cdf0e10cSrcweir                         // visualize a difference between divisions and sub-divisions
220*cdf0e10cSrcweir                         aRetval[nInsertCounter++] = Primitive2DReference(new PointArrayPrimitive2D(aPositionsCross, getBColor()));
221*cdf0e10cSrcweir                     }
222*cdf0e10cSrcweir                     else
223*cdf0e10cSrcweir                     {
224*cdf0e10cSrcweir                         aRetval[nInsertCounter++] = Primitive2DReference(new MarkerArrayPrimitive2D(aPositionsCross, getCrossMarker()));
225*cdf0e10cSrcweir                     }
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir             return aRetval;
230*cdf0e10cSrcweir         }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         GridPrimitive2D::GridPrimitive2D(
233*cdf0e10cSrcweir             const basegfx::B2DHomMatrix& rTransform,
234*cdf0e10cSrcweir             double fWidth,
235*cdf0e10cSrcweir             double fHeight,
236*cdf0e10cSrcweir             double fSmallestViewDistance,
237*cdf0e10cSrcweir             double fSmallestSubdivisionViewDistance,
238*cdf0e10cSrcweir             sal_uInt32 nSubdivisionsX,
239*cdf0e10cSrcweir             sal_uInt32 nSubdivisionsY,
240*cdf0e10cSrcweir             const basegfx::BColor& rBColor,
241*cdf0e10cSrcweir             const BitmapEx& rCrossMarker)
242*cdf0e10cSrcweir         :   BufferedDecompositionPrimitive2D(),
243*cdf0e10cSrcweir             maTransform(rTransform),
244*cdf0e10cSrcweir             mfWidth(fWidth),
245*cdf0e10cSrcweir             mfHeight(fHeight),
246*cdf0e10cSrcweir             mfSmallestViewDistance(fSmallestViewDistance),
247*cdf0e10cSrcweir             mfSmallestSubdivisionViewDistance(fSmallestSubdivisionViewDistance),
248*cdf0e10cSrcweir             mnSubdivisionsX(nSubdivisionsX),
249*cdf0e10cSrcweir             mnSubdivisionsY(nSubdivisionsY),
250*cdf0e10cSrcweir             maBColor(rBColor),
251*cdf0e10cSrcweir             maCrossMarker(rCrossMarker),
252*cdf0e10cSrcweir             maLastObjectToViewTransformation(),
253*cdf0e10cSrcweir             maLastViewport()
254*cdf0e10cSrcweir         {
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         bool GridPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
258*cdf0e10cSrcweir         {
259*cdf0e10cSrcweir             if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 const GridPrimitive2D& rCompare = (GridPrimitive2D&)rPrimitive;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir                 return (getTransform() == rCompare.getTransform()
264*cdf0e10cSrcweir                     && getWidth() == rCompare.getWidth()
265*cdf0e10cSrcweir                     && getHeight() == rCompare.getHeight()
266*cdf0e10cSrcweir                     && getSmallestViewDistance() == rCompare.getSmallestViewDistance()
267*cdf0e10cSrcweir                     && getSmallestSubdivisionViewDistance() == rCompare.getSmallestSubdivisionViewDistance()
268*cdf0e10cSrcweir                     && getSubdivisionsX() == rCompare.getSubdivisionsX()
269*cdf0e10cSrcweir                     && getSubdivisionsY() == rCompare.getSubdivisionsY()
270*cdf0e10cSrcweir                     && getBColor() == rCompare.getBColor()
271*cdf0e10cSrcweir                     && getCrossMarker() == rCompare.getCrossMarker());
272*cdf0e10cSrcweir             }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir             return false;
275*cdf0e10cSrcweir         }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir         basegfx::B2DRange GridPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
278*cdf0e10cSrcweir         {
279*cdf0e10cSrcweir             // get object's range
280*cdf0e10cSrcweir             basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
281*cdf0e10cSrcweir             aUnitRange.transform(getTransform());
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir             // intersect with visible part
284*cdf0e10cSrcweir             aUnitRange.intersect(rViewInformation.getViewport());
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir             return aUnitRange;
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir         Primitive2DSequence GridPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( m_aMutex );
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir             if(getBuffered2DDecomposition().hasElements())
294*cdf0e10cSrcweir             {
295*cdf0e10cSrcweir                 if(maLastViewport != rViewInformation.getViewport() || maLastObjectToViewTransformation != rViewInformation.getObjectToViewTransformation())
296*cdf0e10cSrcweir                 {
297*cdf0e10cSrcweir                     // conditions of last local decomposition have changed, delete
298*cdf0e10cSrcweir                     const_cast< GridPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
299*cdf0e10cSrcweir                 }
300*cdf0e10cSrcweir             }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir             if(!getBuffered2DDecomposition().hasElements())
303*cdf0e10cSrcweir             {
304*cdf0e10cSrcweir                 // remember ViewRange and ViewTransformation
305*cdf0e10cSrcweir                 const_cast< GridPrimitive2D* >(this)->maLastObjectToViewTransformation = rViewInformation.getObjectToViewTransformation();
306*cdf0e10cSrcweir                 const_cast< GridPrimitive2D* >(this)->maLastViewport = rViewInformation.getViewport();
307*cdf0e10cSrcweir             }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir             // use parent implementation
310*cdf0e10cSrcweir             return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
311*cdf0e10cSrcweir         }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir         // provide unique ID
314*cdf0e10cSrcweir         ImplPrimitrive2DIDBlock(GridPrimitive2D, PRIMITIVE2D_ID_GRIDPRIMITIVE2D)
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir     } // end of namespace primitive2d
317*cdf0e10cSrcweir } // end of namespace drawinglayer
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
320*cdf0e10cSrcweir // eof
321