summaryrefslogtreecommitdiffstats
path: root/drawinglayer/source/primitive2d/gridprimitive2d.cxx
blob: ddb468e9ad39f8a2985e0a3ba98018107fbbb925 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

#include <drawinglayer/primitive2d/gridprimitive2d.hxx>
#include <drawinglayer/primitive2d/pointarrayprimitive2d.hxx>
#include <drawinglayer/primitive2d/markerarrayprimitive2d.hxx>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>


using namespace com::sun::star;


namespace drawinglayer::primitive2d
{
        void GridPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
        {
            if(!(!rViewInformation.getViewport().isEmpty() && getWidth() > 0.0 && getHeight() > 0.0))
                return;

            // decompose grid matrix to get logic size
            basegfx::B2DVector aScale, aTranslate;
            double fRotate, fShearX;
            getTransform().decompose(aScale, aTranslate, fRotate, fShearX);

            // create grid matrix which transforms from scaled logic to view
            basegfx::B2DHomMatrix aRST(basegfx::utils::createShearXRotateTranslateB2DHomMatrix(
                fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
            aRST *= rViewInformation.getObjectToViewTransformation();

            // get step widths
            double fStepX(getWidth());
            double fStepY(getHeight());
            const double fMinimalStep(10.0);

            // guarantee a step width of 10.0
            if(basegfx::fTools::less(fStepX, fMinimalStep))
            {
                fStepX = fMinimalStep;
            }

            if(basegfx::fTools::less(fStepY, fMinimalStep))
            {
                fStepY = fMinimalStep;
            }

            // get relative distances in view coordinates
            double fViewStepX((rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(fStepX, 0.0)).getLength());
            double fViewStepY((rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(0.0, fStepY)).getLength());
            double fSmallStepX(1.0), fViewSmallStepX(1.0), fSmallStepY(1.0), fViewSmallStepY(1.0);
            sal_uInt32 nSmallStepsX(0), nSmallStepsY(0);

            // setup subdivisions
            if(getSubdivisionsX())
            {
                fSmallStepX = fStepX / getSubdivisionsX();
                fViewSmallStepX = fViewStepX / getSubdivisionsX();
            }

            if(getSubdivisionsY())
            {
                fSmallStepY = fStepY / getSubdivisionsY();
                fViewSmallStepY = fViewStepY / getSubdivisionsY();
            }

            // correct step width
            while(fViewStepX < getSmallestViewDistance())
            {
                fViewStepX *= 2.0;
                fStepX *= 2.0;
            }

            while(fViewStepY < getSmallestViewDistance())
            {
                fViewStepY *= 2.0;
                fStepY *= 2.0;
            }

            // correct small step width
            if(getSubdivisionsX())
            {
                while(fViewSmallStepX < getSmallestSubdivisionViewDistance())
                {
                    fViewSmallStepX *= 2.0;
                    fSmallStepX *= 2.0;
                }

                nSmallStepsX = static_cast<sal_uInt32>(fStepX / fSmallStepX);
            }

            if(getSubdivisionsY())
            {
                while(fViewSmallStepY < getSmallestSubdivisionViewDistance())
                {
                    fViewSmallStepY *= 2.0;
                    fSmallStepY *= 2.0;
                }

                nSmallStepsY = static_cast<sal_uInt32>(fStepY / fSmallStepY);
            }

            // calculate extended viewport in which grid points may lie at all
            basegfx::B2DRange aExtendedViewport;

            if(rViewInformation.getDiscreteViewport().isEmpty())
            {
                // not set, use logic size to travel over all potential grid points
                aExtendedViewport = basegfx::B2DRange(0.0, 0.0, aScale.getX(), aScale.getY());
            }
            else
            {
                // transform unit range to discrete view
                aExtendedViewport = basegfx::B2DRange(0.0, 0.0, 1.0, 1.0);
                basegfx::B2DHomMatrix aTrans(rViewInformation.getObjectToViewTransformation() * getTransform());
                aExtendedViewport.transform(aTrans);

                // intersect with visible part
                aExtendedViewport.intersect(rViewInformation.getDiscreteViewport());

                if(!aExtendedViewport.isEmpty())
                {
                    // convert back and apply scale
                    aTrans.invert();
                    aTrans.scale(aScale.getX(), aScale.getY());
                    aExtendedViewport.transform(aTrans);

                    // crop start/end in X/Y to multiples of logical step width
                    const double fHalfCrossSize((rViewInformation.getInverseObjectToViewTransformation() * basegfx::B2DVector(3.0, 0.0)).getLength());
                    const double fMinX(floor((aExtendedViewport.getMinX() - fHalfCrossSize) / fStepX) * fStepX);
                    const double fMaxX(ceil((aExtendedViewport.getMaxX() + fHalfCrossSize) / fStepX) * fStepX);
                    const double fMinY(floor((aExtendedViewport.getMinY() - fHalfCrossSize) / fStepY) * fStepY);
                    const double fMaxY(ceil((aExtendedViewport.getMaxY() + fHalfCrossSize) / fStepY) * fStepY);

                    // put to aExtendedViewport and crop on object logic size
                    aExtendedViewport = basegfx::B2DRange(
                        std::max(fMinX, 0.0),
                        std::max(fMinY, 0.0),
                        std::min(fMaxX, aScale.getX()),
                        std::min(fMaxY, aScale.getY()));
                }
            }

            if(aExtendedViewport.isEmpty())
                return;

            // prepare point vectors for point and cross markers
            std::vector< basegfx::B2DPoint > aPositionsPoint;
            std::vector< basegfx::B2DPoint > aPositionsCross;

            for(double fX(aExtendedViewport.getMinX()); fX < aExtendedViewport.getMaxX(); fX += fStepX)
            {
                const bool bXZero(basegfx::fTools::equalZero(fX));

                for(double fY(aExtendedViewport.getMinY()); fY < aExtendedViewport.getMaxY(); fY += fStepY)
                {
                    const bool bYZero(basegfx::fTools::equalZero(fY));

                    if(!bXZero && !bYZero)
                    {
                        // get discrete position and test against 3x3 area surrounding it
                        // since it's a cross
                        const double fHalfCrossSize(3.0 * 0.5);
                        const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fX, fY));
                        const basegfx::B2DRange aDiscreteRangeCross(
                            aViewPos.getX() - fHalfCrossSize, aViewPos.getY() - fHalfCrossSize,
                            aViewPos.getX() + fHalfCrossSize, aViewPos.getY() + fHalfCrossSize);

                        if(rViewInformation.getDiscreteViewport().overlaps(aDiscreteRangeCross))
                        {
                            const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
                            aPositionsCross.push_back(aLogicPos);
                        }
                    }

                    if(getSubdivisionsX() && !bYZero)
                    {
                        double fF(fX + fSmallStepX);

                        for(sal_uInt32 a(1); a < nSmallStepsX && fF < aExtendedViewport.getMaxX(); a++, fF += fSmallStepX)
                        {
                            const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fF, fY));

                            if(rViewInformation.getDiscreteViewport().isInside(aViewPos))
                            {
                                const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
                                aPositionsPoint.push_back(aLogicPos);
                            }
                        }
                    }

                    if(getSubdivisionsY() && !bXZero)
                    {
                        double fF(fY + fSmallStepY);

                        for(sal_uInt32 a(1); a < nSmallStepsY && fF < aExtendedViewport.getMaxY(); a++, fF += fSmallStepY)
                        {
                            const basegfx::B2DPoint aViewPos(aRST * basegfx::B2DPoint(fX, fF));

                            if(rViewInformation.getDiscreteViewport().isInside(aViewPos))
                            {
                                const basegfx::B2DPoint aLogicPos(rViewInformation.getInverseObjectToViewTransformation() * aViewPos);
                                aPositionsPoint.push_back(aLogicPos);
                            }
                        }
                    }
                }
            }

            // prepare return value
            const sal_uInt32 nCountPoint(aPositionsPoint.size());
            const sal_uInt32 nCountCross(aPositionsCross.size());

            // add PointArrayPrimitive2D if point markers were added
            if(nCountPoint)
            {
                rContainer.push_back(new PointArrayPrimitive2D(std::move(aPositionsPoint), getBColor()));
            }

            // add MarkerArrayPrimitive2D if cross markers were added
            if(!nCountCross)
                return;

            if(!getSubdivisionsX() && !getSubdivisionsY())
            {
                // no subdivisions, so fall back to points at grid positions, no need to
                // visualize a difference between divisions and sub-divisions
                rContainer.push_back(new PointArrayPrimitive2D(std::move(aPositionsCross), getBColor()));
            }
            else
            {
                rContainer.push_back(new MarkerArrayPrimitive2D(std::move(aPositionsCross), getCrossMarker()));
            }
        }

        GridPrimitive2D::GridPrimitive2D(
            const basegfx::B2DHomMatrix& rTransform,
            double fWidth,
            double fHeight,
            double fSmallestViewDistance,
            double fSmallestSubdivisionViewDistance,
            sal_uInt32 nSubdivisionsX,
            sal_uInt32 nSubdivisionsY,
            const basegfx::BColor& rBColor,
            const BitmapEx& rCrossMarker)
        :   maTransform(rTransform),
            mfWidth(fWidth),
            mfHeight(fHeight),
            mfSmallestViewDistance(fSmallestViewDistance),
            mfSmallestSubdivisionViewDistance(fSmallestSubdivisionViewDistance),
            mnSubdivisionsX(nSubdivisionsX),
            mnSubdivisionsY(nSubdivisionsY),
            maBColor(rBColor),
            maCrossMarker(rCrossMarker)
        {
        }

        bool GridPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
        {
            if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
            {
                const GridPrimitive2D& rCompare = static_cast<const GridPrimitive2D&>(rPrimitive);

                return (getTransform() == rCompare.getTransform()
                    && getWidth() == rCompare.getWidth()
                    && getHeight() == rCompare.getHeight()
                    && getSmallestViewDistance() == rCompare.getSmallestViewDistance()
                    && getSmallestSubdivisionViewDistance() == rCompare.getSmallestSubdivisionViewDistance()
                    && getSubdivisionsX() == rCompare.getSubdivisionsX()
                    && getSubdivisionsY() == rCompare.getSubdivisionsY()
                    && getBColor() == rCompare.getBColor()
                    && getCrossMarker() == rCompare.getCrossMarker());
            }

            return false;
        }

        basegfx::B2DRange GridPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
        {
            // get object's range
            basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
            aUnitRange.transform(getTransform());

            // intersect with visible part
            aUnitRange.intersect(rViewInformation.getViewport());

            return aUnitRange;
        }

        void GridPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
        {
            if(!getBuffered2DDecomposition().empty())
            {
                if(maLastViewport != rViewInformation.getViewport() || maLastObjectToViewTransformation != rViewInformation.getObjectToViewTransformation())
                {
                    // conditions of last local decomposition have changed, delete
                    const_cast< GridPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DContainer());
                }
            }

            if(getBuffered2DDecomposition().empty())
            {
                // remember ViewRange and ViewTransformation
                const_cast< GridPrimitive2D* >(this)->maLastObjectToViewTransformation = rViewInformation.getObjectToViewTransformation();
                const_cast< GridPrimitive2D* >(this)->maLastViewport = rViewInformation.getViewport();
            }

            // use parent implementation
            BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
        }

        // provide unique ID
        sal_uInt32 GridPrimitive2D::getPrimitive2DID() const
        {
            return PRIMITIVE2D_ID_GRIDPRIMITIVE2D;
        }

} // end of namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */