summaryrefslogtreecommitdiff
path: root/drawinglayer/source/primitive2d/sceneprimitive2d.cxx
blob: 06370532313726150c9b1e982cdc060ef5d552f8 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: sceneprimitive2d.cxx,v $
 *
 *  $Revision: 1.8 $
 *
 *  last change: $Author: aw $ $Date: 2008-02-07 13:41:58 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SCENEPRIMITIVE2D_HXX
#include <drawinglayer/primitive2d/sceneprimitive2d.hxx>
#endif

#ifndef _BGFX_TOOLS_CANVASTOOLS_HXX
#include <basegfx/tools/canvastools.hxx>
#endif

#ifndef _BGFX_POLYGON_B2DPOLYGONTOOLS_HXX
#include <basegfx/polygon/b2dpolygontools.hxx>
#endif

#ifndef _BGFX_POLYGON_B2DPOLYGON_HXX
#include <basegfx/polygon/b2dpolygon.hxx>
#endif

#ifndef _BGFX_POLYPOLYGON_B2DPOLYGONCLIPPER_HXX
#include <basegfx/polygon/b2dpolygonclipper.hxx>
#endif

#ifndef _BGFX_POLYPOLYGON_B2DPOLYGONTOOLS_HXX
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#endif

#ifndef _BGFX_MATRIX_B2DHOMMATRIX_HXX
#include <basegfx/matrix/b2dhommatrix.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_BITMAPPRIMITIVE2D_HXX
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PROCESSOR3D_DEFAULTPROCESSOR3D_HXX
#include <drawinglayer/processor3d/defaultprocessor3d.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PROCESSOR3D_SHADOW3DEXTRACTOR_HXX
#include <drawinglayer/processor3d/shadow3dextractor.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PROCESSOR3D_LABEL3DEXTRACTOR_HXX
#include <drawinglayer/processor3d/label3dextractor.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_GEOMETRY_VIEWINFORMATION2D_HXX
#include <drawinglayer/geometry/viewinformation2d.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_PRIMITIVETYPES2D_HXX
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#endif

#ifndef INCLUDED_SVTOOLS_OPTIONSDRAWINGLAYER_HXX
#include <svtools/optionsdrawinglayer.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PROCESSOR3D_GEOMETRY2DEXTRACTOR_HXX
#include <drawinglayer/processor3d/geometry2dextractor.hxx>
#endif

//////////////////////////////////////////////////////////////////////////////

using namespace com::sun::star;

//////////////////////////////////////////////////////////////////////////////

namespace drawinglayer
{
    namespace primitive2d
    {
        Primitive2DSequence ScenePrimitive2D::createLocalDecomposition(const geometry::ViewInformation2D& rViewInformation) const
        {
            Primitive2DSequence aRetval;

            // create 2D shadows from contained 3D primitives. This creates the shadow primitives on demand and tells if
            // there are some or not. Do this at start, the shadow might still be visible even when the scene is not
            if(impGetShadow3D(rViewInformation))
            {
                // test visibility
                const basegfx::B2DRange aShadow2DRange(getB2DRangeFromPrimitive2DSequence(maShadowPrimitives, rViewInformation));
                const basegfx::B2DRange aViewRange(rViewInformation.getViewport());

                if(aViewRange.isEmpty() || aShadow2DRange.overlaps(aViewRange))
                {
                    // add extracted 2d shadows (before 3d scene creations itself)
                    aRetval = maShadowPrimitives;
                }
            }

            // get geometry of output range
            basegfx::B2DPolygon aLogicOutline(basegfx::tools::createPolygonFromRect(basegfx::B2DRange(0.0, 0.0, 1.0, 1.0)));
            aLogicOutline.transform(getObjectTransformation());

            // clip it against ViewRange
            const basegfx::B2DPolyPolygon aLogicClippedOutline(basegfx::tools::clipPolygonOnRange(aLogicOutline, rViewInformation.getViewport(), true, false));
            const basegfx::B2DRange aLogicVisiblePart(basegfx::tools::getRange(aLogicClippedOutline));

            if(!aLogicVisiblePart.isEmpty())
            {
                // transform back to unity
                basegfx::B2DHomMatrix aInverseTransform(getObjectTransformation());
                basegfx::B2DPolyPolygon aUnitClippedOutline(aLogicClippedOutline);
                aInverseTransform.invert();
                aUnitClippedOutline.transform(aInverseTransform);

                // get logical size by decomposing
                basegfx::B2DVector aScale, aTranslate;
                double fRotate, fShearX;
                getObjectTransformation().decompose(aScale, aTranslate, fRotate, fShearX);

                // get logical sizes and generate visible part in unit coordinates
                double fLogicSizeX(aScale.getX());
                double fLogicSizeY(aScale.getY());
                const basegfx::B2DRange aUnitVisiblePart(basegfx::tools::getRange(aUnitClippedOutline));

                // test if view size (pixel) maybe too big and limit it
                const double fViewSizeX(fLogicSizeX * (rViewInformation.getViewTransformation() * basegfx::B2DVector(aUnitVisiblePart.getWidth(), 0.0)).getLength());
                const double fViewSizeY(fLogicSizeY * (rViewInformation.getViewTransformation() * basegfx::B2DVector(0.0, aUnitVisiblePart.getHeight())).getLength());
                const double fViewVisibleArea(fViewSizeX * fViewSizeY);
                const SvtOptionsDrawinglayer aDrawinglayerOpt;
                const double fMaximumVisibleArea(aDrawinglayerOpt.GetQuadratic3DRenderLimit());
                double fReduceFactor(1.0);

                if(fViewVisibleArea > fMaximumVisibleArea)
                {
                    fReduceFactor = sqrt(fMaximumVisibleArea / fViewVisibleArea);
                    fLogicSizeX *= fReduceFactor;
                    fLogicSizeY *= fReduceFactor;
                }

                // use default 3D primitive processor to create BitmapEx for aUnitVisiblePart and process
                processor3d::DefaultProcessor3D aProcessor3D(
                    rViewInformation,
                    getTransformation3D(),
                    getSdrSceneAttribute(),
                    getSdrLightingAttribute(),
                    fLogicSizeX,
                    fLogicSizeY,
                    aUnitVisiblePart);

                aProcessor3D.processNonTransparent(getChildren3D());
                aProcessor3D.processTransparent(getChildren3D());

                const BitmapEx aNewBitmap(aProcessor3D.getBitmapEx());
                const Size aBitmapSizePixel(aNewBitmap.GetSizePixel());

                if(aBitmapSizePixel.getWidth() && aBitmapSizePixel.getHeight())
                {
                    // correct reduce-factor free logic, view-unit-aligned coordinates for the created bitmap
                    // without translation
                    basegfx::B2DPoint aViewTopLeft(aUnitVisiblePart.getMinX() * aScale.getX(), aUnitVisiblePart.getMinY() * aScale.getY());
                    aViewTopLeft *= rViewInformation.getViewTransformation();
                    aViewTopLeft.setX(floor(aViewTopLeft.getX()));
                    aViewTopLeft.setY(floor(aViewTopLeft.getY()));
                    const basegfx::B2DPoint aLogicTopLeft(rViewInformation.getInverseViewTransformation() * aViewTopLeft);

                    // same for size, do not forget to correct by fReduceFactor
                    const basegfx::B2DVector aViewSize(aBitmapSizePixel.getWidth() / fReduceFactor, aBitmapSizePixel.getHeight() / fReduceFactor);
                    const basegfx::B2DVector aLogicSize(rViewInformation.getInverseViewTransformation() * aViewSize);

                    // create 2d transform from it. Target is to always have a output-pixel-aligned
                    // logic position and size wich corresponds to the bitmap size (when no reduceFactor)
                    basegfx::B2DHomMatrix aNew2DTransform;
                    aNew2DTransform.scale(aLogicSize.getX(), aLogicSize.getY());
                    aNew2DTransform.translate(aLogicTopLeft.getX(), aLogicTopLeft.getY());
                    aNew2DTransform.shearX(fShearX);
                    aNew2DTransform.rotate(fRotate);
                    aNew2DTransform.translate(aTranslate.getX(), aTranslate.getY());

                    // create bitmap primitive and add
                    BitmapPrimitive2D* pNewTextBitmap = new BitmapPrimitive2D(aNewBitmap, aNew2DTransform);
                    const Primitive2DReference xRef(pNewTextBitmap);
                    appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, xRef);

#ifdef DBG_UTIL
                    {
                        // check if pixel size and transformation is correct
                        if(basegfx::fTools::equal(fReduceFactor, 1.0))
                        {
                            if(basegfx::fTools::equalZero(fShearX) && basegfx::fTools::equalZero(fRotate))
                            {
                                basegfx::B2DVector aScaleB, aTranslateB;
                                double fRotateB, fShearXB;

                                pNewTextBitmap->getTransform().decompose(aScaleB, aTranslateB, fRotateB, fShearXB);
                                basegfx::B2DVector aViewSizeB(basegfx::absolute(aScaleB));
                                aViewSizeB *= rViewInformation.getViewTransformation();
                                const Size aSourceSizePixel(pNewTextBitmap->getBitmapEx().GetSizePixel());
                                const bool bXEqual(aSourceSizePixel.getWidth() == basegfx::fround(aViewSizeB.getX()));
                                const bool bYEqual(aSourceSizePixel.getHeight() == basegfx::fround(aViewSizeB.getY()));
                                OSL_ENSURE(bXEqual && bYEqual, "3D renderer produced non-pixel-aligned graphic (!)");
                            }
                        }
                    }
#endif
                }
            }

            // create 2D labels from contained 3D label primitives. This creates the label primitives on demand and tells if
            // there are some or not. Do this at end, the labels might still be visible even when the scene is not
            if(impGetLabel3D(rViewInformation))
            {
                // test visibility
                const basegfx::B2DRange aLabel2DRange(getB2DRangeFromPrimitive2DSequence(maLabelPrimitives, rViewInformation));
                const basegfx::B2DRange aViewRange(rViewInformation.getViewport());

                if(aViewRange.isEmpty() || aLabel2DRange.overlaps(aViewRange))
                {
                    // add extracted 2d labels (after 3d scene creations)
                    appendPrimitive2DSequenceToPrimitive2DSequence(aRetval, maLabelPrimitives);
                }
            }

            return aRetval;
        }

        Primitive2DSequence ScenePrimitive2D::getGeometry2D(const geometry::ViewInformation2D& rViewInformation) const
        {
            Primitive2DSequence aRetval;

            // create 2D shadows from contained 3D primitives
            if(impGetShadow3D(rViewInformation))
            {
                // add extracted 2d shadows (before 3d scene creations itself)
                aRetval = maShadowPrimitives;
            }

            // create 2D projected geometry from 3D geometry
            if(getChildren3D().hasElements())
            {
                // create 2D geometry extraction processor
                processor3d::Geometry2DExtractingProcessor aGeometryProcessor(
                    rViewInformation.getViewTime(),
                    getObjectTransformation(),
                    getTransformation3D().getWorldToView());

                // process local primitives
                aGeometryProcessor.process(getChildren3D());

                // fetch result and append
                Primitive2DSequence a2DExtractedPrimitives(aGeometryProcessor.getPrimitive2DSequence());
                appendPrimitive2DSequenceToPrimitive2DSequence(aRetval, a2DExtractedPrimitives);
            }

            // create 2D labels from contained 3D label primitives
            if(impGetLabel3D(rViewInformation))
            {
                // add extracted 2d labels (after 3d scene creations)
                appendPrimitive2DSequenceToPrimitive2DSequence(aRetval, maLabelPrimitives);
            }

            return aRetval;
        }

        ScenePrimitive2D::ScenePrimitive2D(
            const primitive3d::Primitive3DSequence& rxChildren3D,
            const attribute::SdrSceneAttribute& rSdrSceneAttribute,
            const attribute::SdrLightingAttribute& rSdrLightingAttribute,
            const basegfx::B2DHomMatrix& rObjectTransformation,
            const geometry::Transformation3D& rTransformation3D)
        :   BasePrimitive2D(),
            mxChildren3D(rxChildren3D),
            maSdrSceneAttribute(rSdrSceneAttribute),
            maSdrLightingAttribute(rSdrLightingAttribute),
            maObjectTransformation(rObjectTransformation),
            maTransformation3D(rTransformation3D),
            mbShadow3DChecked(false),
            mbLabel3DChecked(false),
            maLastViewTransformation(),
            maLastViewport()
        {
        }

        bool ScenePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
        {
            if(BasePrimitive2D::operator==(rPrimitive))
            {
                const ScenePrimitive2D& rCompare = (ScenePrimitive2D&)rPrimitive;

                return (primitive3d::arePrimitive3DSequencesEqual(getChildren3D(), rCompare.getChildren3D())
                    && getSdrSceneAttribute() == rCompare.getSdrSceneAttribute()
                    && getSdrLightingAttribute() == rCompare.getSdrLightingAttribute()
                    && getObjectTransformation() == rCompare.getObjectTransformation()
                    && getTransformation3D() == rCompare.getTransformation3D());
            }

            return false;
        }

        basegfx::B2DRange ScenePrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
        {
            // call parent. Do not calculate using unit range since the decomposition may extend the range
            // by single pixels to level out the boundaries of the created bitmap primitives. So, use
            // the default mechanism to use the range of the sub-primitives.
            basegfx::B2DRange aRetval(BasePrimitive2D::getB2DRange(rViewInformation));

            // expand by evtl. existing shadow primitives
            if(impGetShadow3D(rViewInformation))
            {
                const basegfx::B2DRange aShadow2DRange(getB2DRangeFromPrimitive2DSequence(maShadowPrimitives, rViewInformation));

                if(!aShadow2DRange.isEmpty())
                {
                    aRetval.expand(aShadow2DRange);
                }
            }

            // expand by evtl. existing label primitives
            if(impGetLabel3D(rViewInformation))
            {
                const basegfx::B2DRange aLabel2DRange(getB2DRangeFromPrimitive2DSequence(maLabelPrimitives, rViewInformation));

                if(!aLabel2DRange.isEmpty())
                {
                    aRetval.expand(aLabel2DRange);
                }
            }

            return aRetval;
        }

        Primitive2DSequence ScenePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
        {
            ::osl::MutexGuard aGuard( m_aMutex );

            // check if mpLocalDecomposition can be reused. Basic decision is based on the
            // comparison of old and new viewTransformations, if they are equal reuse is possible anyways.
            bool bNeedNewDecomposition(false);

            if(getLocalDecomposition().hasElements())
            {
                if(maLastViewport != rViewInformation.getViewport() || maLastViewTransformation != rViewInformation.getViewTransformation())
                {
                    bNeedNewDecomposition = true;
                }
            }

            if(bNeedNewDecomposition)
            {
                // here is space to find out if only the visible view area has changed (scroll)
                // and thus the object decomposition may be reused. For that, the logic visible part
                // and the view sizes need to be the same
                basegfx::B2DPolygon aWorldOutline(basegfx::tools::createPolygonFromRect(basegfx::B2DRange(0.0, 0.0, 1.0, 1.0)));
                aWorldOutline.transform(getObjectTransformation());

                // calculate last clipped visible part
                const basegfx::B2DPolyPolygon aLastLogicClippedOutline(basegfx::tools::clipPolygonOnRange(aWorldOutline, maLastViewport, true, false));
                const basegfx::B2DRange aLastLogicVisiblePart(basegfx::tools::getRange(aLastLogicClippedOutline));

                // calculate new clipped visible part
                const basegfx::B2DPolyPolygon aNewLogicClippedOutline(basegfx::tools::clipPolygonOnRange(aWorldOutline, rViewInformation.getViewport(), true, false));
                const basegfx::B2DRange aNewLogicVisiblePart(basegfx::tools::getRange(aNewLogicClippedOutline));

                if(aLastLogicVisiblePart.equal(aNewLogicVisiblePart))
                {
                    // logic visible part is the same, check for same view size to test for zooming
                    basegfx::B2DRange aLastViewVisiblePart(aLastLogicVisiblePart);
                    aLastViewVisiblePart.transform(maLastViewTransformation);
                    const basegfx::B2DVector aLastViewSize(aLastViewVisiblePart.getRange());

                    basegfx::B2DRange aNewViewVisiblePart(aNewLogicVisiblePart);
                    aNewViewVisiblePart.transform(rViewInformation.getViewTransformation());
                    const basegfx::B2DVector aNewViewSize(aNewViewVisiblePart.getRange());

                    if(aLastViewSize.equal(aNewViewSize))
                    {
                        // view size is the same, reuse possible
                        bNeedNewDecomposition = false;
                    }
                }
            }

            if(bNeedNewDecomposition)
            {
                // conditions of last local decomposition have changed, delete
                const_cast< ScenePrimitive2D* >(this)->setLocalDecomposition(Primitive2DSequence());
            }

            if(!getLocalDecomposition().hasElements())
            {
                // remember ViewRange and ViewTransformation
                const_cast< ScenePrimitive2D* >(this)->maLastViewTransformation = rViewInformation.getViewTransformation();
                const_cast< ScenePrimitive2D* >(this)->maLastViewport = rViewInformation.getViewport();
            }

            // use parent implementation
            return BasePrimitive2D::get2DDecomposition(rViewInformation);
        }

        bool ScenePrimitive2D::impGetShadow3D(const geometry::ViewInformation2D& rViewInformation) const
        {
            osl::MutexGuard aGuard( m_aMutex );

            // create on demand
            if(!mbShadow3DChecked && getChildren3D().hasElements())
            {
                // create shadow extraction processor
                const basegfx::B3DHomMatrix aWorldToEye(getTransformation3D().getOrientation() * getTransformation3D().getTransformation());
                const basegfx::B3DHomMatrix aEyeToView(getTransformation3D().getDeviceToView() * getTransformation3D().getProjection());

                processor3d::Shadow3DExtractingProcessor aShadowProcessor(
                    rViewInformation.getViewTime(),
                    getObjectTransformation(),
                    aWorldToEye,
                    aEyeToView,
                    getSdrLightingAttribute(),
                    getChildren3D(),
                    getSdrSceneAttribute().getShadowSlant());

                // process local primitives
                aShadowProcessor.process(getChildren3D());

                // fetch result and set checked flag
                const_cast< ScenePrimitive2D* >(this)->maShadowPrimitives = aShadowProcessor.getPrimitive2DSequence();
                const_cast< ScenePrimitive2D* >(this)->mbShadow3DChecked = true;
            }

            // return if there are shadow primitives
            return maShadowPrimitives.hasElements();
        }

        bool ScenePrimitive2D::impGetLabel3D(const geometry::ViewInformation2D& rViewInformation) const
        {
            osl::MutexGuard aGuard( m_aMutex );

            // create on demand
            if(!mbLabel3DChecked && getChildren3D().hasElements())
            {
                // create label extraction processor
                processor3d::Label3DExtractingProcessor aLabelProcessor(
                    rViewInformation.getViewTime(),
                    getObjectTransformation(),
                    getTransformation3D().getWorldToView());

                // process local primitives
                aLabelProcessor.process(getChildren3D());

                // fetch result and set checked flag
                const_cast< ScenePrimitive2D* >(this)->maLabelPrimitives = aLabelProcessor.getPrimitive2DSequence();
                const_cast< ScenePrimitive2D* >(this)->mbLabel3DChecked = true;
            }

            // return if there are label primitives
            return maLabelPrimitives.hasElements();
        }

        // provide unique ID
        ImplPrimitrive2DIDBlock(ScenePrimitive2D, PRIMITIVE2D_ID_SCENEPRIMITIVE2D)

    } // end of namespace primitive2d
} // end of namespace drawinglayer

//////////////////////////////////////////////////////////////////////////////
// eof