1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <drawinglayer/primitive2d/gridprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/pointarrayprimitive2d.hxx>
22 #include <drawinglayer/primitive2d/markerarrayprimitive2d.hxx>
23 #include <drawinglayer/geometry/viewinformation2d.hxx>
24 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
25 #include <basegfx/matrix/b2dhommatrixtools.hxx>
29 using namespace com::sun::star
;
32 namespace drawinglayer::primitive2d
34 void GridPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
, const geometry::ViewInformation2D
& rViewInformation
) const
36 if(!(!rViewInformation
.getViewport().isEmpty() && getWidth() > 0.0 && getHeight() > 0.0))
39 // decompose grid matrix to get logic size
40 basegfx::B2DVector aScale
, aTranslate
;
41 double fRotate
, fShearX
;
42 getTransform().decompose(aScale
, aTranslate
, fRotate
, fShearX
);
44 // create grid matrix which transforms from scaled logic to view
45 basegfx::B2DHomMatrix
aRST(basegfx::utils::createShearXRotateTranslateB2DHomMatrix(
46 fShearX
, fRotate
, aTranslate
.getX(), aTranslate
.getY()));
47 aRST
*= rViewInformation
.getObjectToViewTransformation();
50 double fStepX(getWidth());
51 double fStepY(getHeight());
52 const double fMinimalStep(10.0);
54 // guarantee a step width of 10.0
55 if(basegfx::fTools::less(fStepX
, fMinimalStep
))
57 fStepX
= fMinimalStep
;
60 if(basegfx::fTools::less(fStepY
, fMinimalStep
))
62 fStepY
= fMinimalStep
;
65 // get relative distances in view coordinates
66 double fViewStepX((rViewInformation
.getObjectToViewTransformation() * basegfx::B2DVector(fStepX
, 0.0)).getLength());
67 double fViewStepY((rViewInformation
.getObjectToViewTransformation() * basegfx::B2DVector(0.0, fStepY
)).getLength());
68 double fSmallStepX(1.0), fViewSmallStepX(1.0), fSmallStepY(1.0), fViewSmallStepY(1.0);
69 sal_uInt32
nSmallStepsX(0), nSmallStepsY(0);
72 if(getSubdivisionsX())
74 fSmallStepX
= fStepX
/ getSubdivisionsX();
75 fViewSmallStepX
= fViewStepX
/ getSubdivisionsX();
78 if(getSubdivisionsY())
80 fSmallStepY
= fStepY
/ getSubdivisionsY();
81 fViewSmallStepY
= fViewStepY
/ getSubdivisionsY();
85 while(fViewStepX
< getSmallestViewDistance())
91 while(fViewStepY
< getSmallestViewDistance())
97 // correct small step width
98 if(getSubdivisionsX())
100 while(fViewSmallStepX
< getSmallestSubdivisionViewDistance())
102 fViewSmallStepX
*= 2.0;
106 nSmallStepsX
= static_cast<sal_uInt32
>(fStepX
/ fSmallStepX
);
109 if(getSubdivisionsY())
111 while(fViewSmallStepY
< getSmallestSubdivisionViewDistance())
113 fViewSmallStepY
*= 2.0;
117 nSmallStepsY
= static_cast<sal_uInt32
>(fStepY
/ fSmallStepY
);
120 // calculate extended viewport in which grid points may lie at all
121 basegfx::B2DRange aExtendedViewport
;
123 if(rViewInformation
.getDiscreteViewport().isEmpty())
125 // not set, use logic size to travel over all potential grid points
126 aExtendedViewport
= basegfx::B2DRange(0.0, 0.0, aScale
.getX(), aScale
.getY());
130 // transform unit range to discrete view
131 aExtendedViewport
= basegfx::B2DRange(0.0, 0.0, 1.0, 1.0);
132 basegfx::B2DHomMatrix
aTrans(rViewInformation
.getObjectToViewTransformation() * getTransform());
133 aExtendedViewport
.transform(aTrans
);
135 // intersect with visible part
136 aExtendedViewport
.intersect(rViewInformation
.getDiscreteViewport());
138 if(!aExtendedViewport
.isEmpty())
140 // convert back and apply scale
142 aTrans
.scale(aScale
.getX(), aScale
.getY());
143 aExtendedViewport
.transform(aTrans
);
145 // crop start/end in X/Y to multiples of logical step width
146 const double fHalfCrossSize((rViewInformation
.getInverseObjectToViewTransformation() * basegfx::B2DVector(3.0, 0.0)).getLength());
147 const double fMinX(floor((aExtendedViewport
.getMinX() - fHalfCrossSize
) / fStepX
) * fStepX
);
148 const double fMaxX(ceil((aExtendedViewport
.getMaxX() + fHalfCrossSize
) / fStepX
) * fStepX
);
149 const double fMinY(floor((aExtendedViewport
.getMinY() - fHalfCrossSize
) / fStepY
) * fStepY
);
150 const double fMaxY(ceil((aExtendedViewport
.getMaxY() + fHalfCrossSize
) / fStepY
) * fStepY
);
152 // put to aExtendedViewport and crop on object logic size
153 aExtendedViewport
= basegfx::B2DRange(
154 std::max(fMinX
, 0.0),
155 std::max(fMinY
, 0.0),
156 std::min(fMaxX
, aScale
.getX()),
157 std::min(fMaxY
, aScale
.getY()));
161 if(aExtendedViewport
.isEmpty())
164 // prepare point vectors for point and cross markers
165 std::vector
< basegfx::B2DPoint
> aPositionsPoint
;
166 std::vector
< basegfx::B2DPoint
> aPositionsCross
;
168 for(double fX(aExtendedViewport
.getMinX()); fX
< aExtendedViewport
.getMaxX(); fX
+= fStepX
)
170 const bool bXZero(basegfx::fTools::equalZero(fX
));
172 for(double fY(aExtendedViewport
.getMinY()); fY
< aExtendedViewport
.getMaxY(); fY
+= fStepY
)
174 const bool bYZero(basegfx::fTools::equalZero(fY
));
176 if(!bXZero
&& !bYZero
)
178 // get discrete position and test against 3x3 area surrounding it
179 // since it's a cross
180 const double fHalfCrossSize(3.0 * 0.5);
181 const basegfx::B2DPoint
aViewPos(aRST
* basegfx::B2DPoint(fX
, fY
));
182 const basegfx::B2DRange
aDiscreteRangeCross(
183 aViewPos
.getX() - fHalfCrossSize
, aViewPos
.getY() - fHalfCrossSize
,
184 aViewPos
.getX() + fHalfCrossSize
, aViewPos
.getY() + fHalfCrossSize
);
186 if(rViewInformation
.getDiscreteViewport().overlaps(aDiscreteRangeCross
))
188 const basegfx::B2DPoint
aLogicPos(rViewInformation
.getInverseObjectToViewTransformation() * aViewPos
);
189 aPositionsCross
.push_back(aLogicPos
);
193 if(getSubdivisionsX() && !bYZero
)
195 double fF(fX
+ fSmallStepX
);
197 for(sal_uInt32
a(1); a
< nSmallStepsX
&& fF
< aExtendedViewport
.getMaxX(); a
++, fF
+= fSmallStepX
)
199 const basegfx::B2DPoint
aViewPos(aRST
* basegfx::B2DPoint(fF
, fY
));
201 if(rViewInformation
.getDiscreteViewport().isInside(aViewPos
))
203 const basegfx::B2DPoint
aLogicPos(rViewInformation
.getInverseObjectToViewTransformation() * aViewPos
);
204 aPositionsPoint
.push_back(aLogicPos
);
209 if(getSubdivisionsY() && !bXZero
)
211 double fF(fY
+ fSmallStepY
);
213 for(sal_uInt32
a(1); a
< nSmallStepsY
&& fF
< aExtendedViewport
.getMaxY(); a
++, fF
+= fSmallStepY
)
215 const basegfx::B2DPoint
aViewPos(aRST
* basegfx::B2DPoint(fX
, fF
));
217 if(rViewInformation
.getDiscreteViewport().isInside(aViewPos
))
219 const basegfx::B2DPoint
aLogicPos(rViewInformation
.getInverseObjectToViewTransformation() * aViewPos
);
220 aPositionsPoint
.push_back(aLogicPos
);
227 // prepare return value
228 const sal_uInt32
nCountPoint(aPositionsPoint
.size());
229 const sal_uInt32
nCountCross(aPositionsCross
.size());
231 // add PointArrayPrimitive2D if point markers were added
234 rContainer
.push_back(new PointArrayPrimitive2D(std::move(aPositionsPoint
), getBColor()));
237 // add MarkerArrayPrimitive2D if cross markers were added
241 if(!getSubdivisionsX() && !getSubdivisionsY())
243 // no subdivisions, so fall back to points at grid positions, no need to
244 // visualize a difference between divisions and sub-divisions
245 rContainer
.push_back(new PointArrayPrimitive2D(std::move(aPositionsCross
), getBColor()));
249 rContainer
.push_back(new MarkerArrayPrimitive2D(std::move(aPositionsCross
), getCrossMarker()));
253 GridPrimitive2D::GridPrimitive2D(
254 basegfx::B2DHomMatrix aTransform
,
257 double fSmallestViewDistance
,
258 double fSmallestSubdivisionViewDistance
,
259 sal_uInt32 nSubdivisionsX
,
260 sal_uInt32 nSubdivisionsY
,
261 const basegfx::BColor
& rBColor
,
262 const BitmapEx
& rCrossMarker
)
263 : maTransform(std::move(aTransform
)),
266 mfSmallestViewDistance(fSmallestViewDistance
),
267 mfSmallestSubdivisionViewDistance(fSmallestSubdivisionViewDistance
),
268 mnSubdivisionsX(nSubdivisionsX
),
269 mnSubdivisionsY(nSubdivisionsY
),
271 maCrossMarker(rCrossMarker
)
275 bool GridPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
277 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
279 const GridPrimitive2D
& rCompare
= static_cast<const GridPrimitive2D
&>(rPrimitive
);
281 return (getTransform() == rCompare
.getTransform()
282 && getWidth() == rCompare
.getWidth()
283 && getHeight() == rCompare
.getHeight()
284 && getSmallestViewDistance() == rCompare
.getSmallestViewDistance()
285 && getSmallestSubdivisionViewDistance() == rCompare
.getSmallestSubdivisionViewDistance()
286 && getSubdivisionsX() == rCompare
.getSubdivisionsX()
287 && getSubdivisionsY() == rCompare
.getSubdivisionsY()
288 && getBColor() == rCompare
.getBColor()
289 && getCrossMarker() == rCompare
.getCrossMarker());
295 basegfx::B2DRange
GridPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& rViewInformation
) const
297 // get object's range
298 basegfx::B2DRange
aUnitRange(0.0, 0.0, 1.0, 1.0);
299 aUnitRange
.transform(getTransform());
301 // intersect with visible part
302 aUnitRange
.intersect(rViewInformation
.getViewport());
307 void GridPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor
& rVisitor
, const geometry::ViewInformation2D
& rViewInformation
) const
309 if(!getBuffered2DDecomposition().empty())
311 if(maLastViewport
!= rViewInformation
.getViewport() || maLastObjectToViewTransformation
!= rViewInformation
.getObjectToViewTransformation())
313 // conditions of last local decomposition have changed, delete
314 const_cast< GridPrimitive2D
* >(this)->setBuffered2DDecomposition(Primitive2DContainer());
318 if(getBuffered2DDecomposition().empty())
320 // remember ViewRange and ViewTransformation
321 const_cast< GridPrimitive2D
* >(this)->maLastObjectToViewTransformation
= rViewInformation
.getObjectToViewTransformation();
322 const_cast< GridPrimitive2D
* >(this)->maLastViewport
= rViewInformation
.getViewport();
325 // use parent implementation
326 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor
, rViewInformation
);
330 sal_uInt32
GridPrimitive2D::getPrimitive2DID() const
332 return PRIMITIVE2D_ID_GRIDPRIMITIVE2D
;
335 } // end of namespace
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */