bump product version to 5.0.4.1
[LibreOffice.git] / slideshow / source / engine / transitions / clippingfunctor.cxx
blobc062586329a14b95739ccda6e1e22441f1334817
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <canvas/debug.hxx>
22 #include <tools/diagnose_ex.h>
23 #include "clippingfunctor.hxx"
24 #include "transitiontools.hxx"
26 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
27 #include <basegfx/polygon/b2dpolygonclipper.hxx>
28 #include <basegfx/polygon/b2dpolygontools.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 namespace slideshow
33 namespace internal
35 ClippingFunctor::ClippingFunctor(const ParametricPolyPolygonSharedPtr& rPolygon,
36 const TransitionInfo& rTransitionInfo,
37 bool bDirectionForward,
38 bool bModeIn ) :
39 mpParametricPoly( rPolygon ),
40 maStaticTransformation(),
41 mbForwardParameterSweep( true ),
42 mbSubtractPolygon( false ),
43 mbScaleIsotrophically( rTransitionInfo.mbScaleIsotrophically ),
44 mbFlip(false)
46 ENSURE_OR_THROW( rPolygon,
47 "ClippingFunctor::ClippingFunctor(): Invalid parametric polygon" );
49 // maBackgroundRect serves as the minuent when
50 // subtracting a given clip polygon from the
51 // background. To speed up the clipper algo, avoid
52 // actual intersections of the generated
53 // poly-polygon with the minuent - i.e. choose the
54 // polygon to subtract from sufficiently large.
56 // blow up unit rect to (-1,-1),(2,2)
57 // AW: Not needed, just use range
58 // ::basegfx::B2DHomMatrix aMatrix;
59 // aMatrix.scale(3.0,3.0);
60 // aMatrix.translate(-1.0,-1.0);
61 // maBackgroundRect.transform( aMatrix );
63 // extract modification info from maTransitionInfo
66 // perform general transformations _before_ the reverse
67 // mode changes. This allows the Transition table to be
68 // filled more constitently (otherwise, when e.g. rotating
69 // a clip 90 degrees, the REVERSEMETHOD_FLIP_X becomes
70 // REVERSEMETHOD_FLIP_Y instead)
71 if (rTransitionInfo.mnRotationAngle != 0.0 ||
72 rTransitionInfo.mnScaleX != 1.0 ||
73 rTransitionInfo.mnScaleY != 1.0)
75 maStaticTransformation.translate( -0.5, -0.5 );
76 // apply further transformations:
77 if (rTransitionInfo.mnRotationAngle != 0.0)
79 maStaticTransformation.rotate(
80 basegfx::deg2rad(rTransitionInfo.mnRotationAngle) );
82 if (rTransitionInfo.mnScaleX != 1.0 ||
83 rTransitionInfo.mnScaleY != 1.0)
85 maStaticTransformation.scale(
86 rTransitionInfo.mnScaleX,
87 rTransitionInfo.mnScaleY );
89 maStaticTransformation.translate( 0.5, 0.5 );
92 if( !bDirectionForward )
94 // Client has requested reversed
95 // direction. Apply TransitionInfo's choice
96 // for that
97 switch( rTransitionInfo.meReverseMethod )
99 default:
100 ENSURE_OR_THROW(
101 false,
102 "TransitionFactory::TransitionFactory(): Unexpected reverse method" );
103 break;
105 case TransitionInfo::REVERSEMETHOD_IGNORE:
106 break;
108 case TransitionInfo::REVERSEMETHOD_INVERT_SWEEP:
109 mbForwardParameterSweep = !mbForwardParameterSweep;
110 break;
112 case TransitionInfo::REVERSEMETHOD_SUBTRACT_POLYGON:
113 mbSubtractPolygon = !mbSubtractPolygon;
114 break;
116 case TransitionInfo::REVERSEMETHOD_SUBTRACT_AND_INVERT:
117 mbForwardParameterSweep = !mbForwardParameterSweep;
118 mbSubtractPolygon = !mbSubtractPolygon;
119 break;
121 case TransitionInfo::REVERSEMETHOD_ROTATE_180:
122 maStaticTransformation = basegfx::tools::createRotateAroundPoint(0.5, 0.5, M_PI)
123 * maStaticTransformation;
124 break;
126 case TransitionInfo::REVERSEMETHOD_FLIP_X:
127 maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
128 * maStaticTransformation;
129 mbFlip = true;
130 break;
132 case TransitionInfo::REVERSEMETHOD_FLIP_Y:
133 maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
134 * maStaticTransformation;
135 mbFlip = true;
136 break;
140 if( !bModeIn )
142 // client has requested 'out' mode. Apply
143 // TransitionInfo's method of choice
144 if( rTransitionInfo.mbOutInvertsSweep )
145 mbForwardParameterSweep = !mbForwardParameterSweep;
146 else
147 mbSubtractPolygon = !mbSubtractPolygon;
151 ::basegfx::B2DPolyPolygon ClippingFunctor::operator()( double nValue,
152 const ::basegfx::B2DSize& rTargetSize )
154 // modify clip polygon according to static
155 // transformation plus current shape size
156 ::basegfx::B2DHomMatrix aMatrix( maStaticTransformation );
158 // retrieve current clip polygon
159 ::basegfx::B2DPolyPolygon aClipPoly = (*mpParametricPoly)(
160 mbForwardParameterSweep ? nValue : 1.0 - nValue );
162 // TODO(Q4): workaround here, better be fixed in cppcanvas
163 if (aClipPoly.count() == 0)
164 aClipPoly.append( basegfx::B2DPolygon() );
166 if (mbFlip)
167 aClipPoly.flip();
169 // currently, clipper cannot cope with curves. Subdivide first
170 // AW: Should be no longer necessary; clipping tools are now bezier-safe
171 // if( aClipPoly.areControlPointsUsed() )
172 // aClipPoly = ::basegfx::tools::adaptiveSubdivideByAngle(aClipPoly);
174 if( mbSubtractPolygon )
176 // subtract given polygon from background
177 // rect. Do that before any transformations.
179 // calc maBackgroundRect \ aClipPoly
180 // =================================
182 // AW: Simplified
183 // use a range with fixed size (-1,-1),(2,2)
184 const basegfx::B2DRange aBackgroundRange(-1, -1, 2, 2);
185 const basegfx::B2DRange aClipPolyRange(aClipPoly.getB2DRange());
187 if(aBackgroundRange.isInside(aClipPolyRange))
189 // combine polygons; make the clip polygon the hole
190 aClipPoly = ::basegfx::tools::correctOrientations(aClipPoly);
191 aClipPoly.flip();
192 aClipPoly.insert(0, basegfx::tools::createPolygonFromRect(aBackgroundRange));
194 else
196 // when not completely inside aBackgroundRange clipping is needed
197 // subtract aClipPoly from aBackgroundRange
198 const basegfx::B2DPolyPolygon aBackgroundPolyPoly(basegfx::tools::createPolygonFromRect(aBackgroundRange));
199 aClipPoly = basegfx::tools::solvePolygonOperationDiff(aBackgroundPolyPoly, aClipPoly);
203 // scale polygon up to current shape size
204 if( mbScaleIsotrophically )
206 const double nScale( ::std::max( rTargetSize.getX(),
207 rTargetSize.getY() ) );
208 aMatrix.scale( nScale, nScale );
209 aMatrix.translate( -(nScale-rTargetSize.getX())/2.0,
210 -(nScale-rTargetSize.getY())/2.0 );
212 else
214 aMatrix.scale( rTargetSize.getX(),
215 rTargetSize.getY() );
218 // apply cumulative transformation to clip polygon
219 aClipPoly.transform( aMatrix );
221 return aClipPoly;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */