Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / engine / transitions / clippingfunctor.cxx
blob57fcc38a207e10fe83d6845cdb08587213919a1d
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 <tools/diagnose_ex.h>
22 #include "clippingfunctor.hxx"
23 #include "transitiontools.hxx"
25 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
26 #include <basegfx/polygon/b2dpolygonclipper.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 namespace slideshow
32 namespace internal
34 ClippingFunctor::ClippingFunctor(const ParametricPolyPolygonSharedPtr& rPolygon,
35 const TransitionInfo& rTransitionInfo,
36 bool bDirectionForward,
37 bool bModeIn ) :
38 mpParametricPoly( rPolygon ),
39 maStaticTransformation(),
40 mbForwardParameterSweep( true ),
41 mbSubtractPolygon( false ),
42 mbScaleIsotrophically( rTransitionInfo.mbScaleIsotrophically ),
43 mbFlip(false)
45 ENSURE_OR_THROW( rPolygon,
46 "ClippingFunctor::ClippingFunctor(): Invalid parametric polygon" );
48 // maBackgroundRect serves as the minuent when
49 // subtracting a given clip polygon from the
50 // background. To speed up the clipper algo, avoid
51 // actual intersections of the generated
52 // poly-polygon with the minuent - i.e. choose the
53 // polygon to subtract from sufficiently large.
55 // blow up unit rect to (-1,-1),(2,2)
56 // AW: Not needed, just use range
57 // ::basegfx::B2DHomMatrix aMatrix;
58 // aMatrix.scale(3.0,3.0);
59 // aMatrix.translate(-1.0,-1.0);
60 // maBackgroundRect.transform( aMatrix );
62 // extract modification info from maTransitionInfo
65 // perform general transformations _before_ the reverse
66 // mode changes. This allows the Transition table to be
67 // filled more consistently (otherwise, when e.g. rotating
68 // a clip 90 degrees, the ReverseMethod::FlipX becomes
69 // ReverseMethod::FlipY instead)
70 if (rTransitionInfo.mnRotationAngle != 0.0 ||
71 rTransitionInfo.mnScaleX != 1.0 ||
72 rTransitionInfo.mnScaleY != 1.0)
74 maStaticTransformation.translate( -0.5, -0.5 );
75 // apply further transformations:
76 if (rTransitionInfo.mnRotationAngle != 0.0)
78 maStaticTransformation.rotate(
79 basegfx::deg2rad(rTransitionInfo.mnRotationAngle) );
81 if (rTransitionInfo.mnScaleX != 1.0 ||
82 rTransitionInfo.mnScaleY != 1.0)
84 maStaticTransformation.scale(
85 rTransitionInfo.mnScaleX,
86 rTransitionInfo.mnScaleY );
88 maStaticTransformation.translate( 0.5, 0.5 );
91 if( !bDirectionForward )
93 // Client has requested reversed
94 // direction. Apply TransitionInfo's choice
95 // for that
96 switch( rTransitionInfo.meReverseMethod )
98 default:
99 ENSURE_OR_THROW(
100 false,
101 "TransitionFactory::TransitionFactory(): Unexpected reverse method" );
102 break;
104 case TransitionInfo::ReverseMethod::Ignore:
105 break;
107 case TransitionInfo::ReverseMethod::SubtractAndInvert:
108 mbForwardParameterSweep = !mbForwardParameterSweep;
109 mbSubtractPolygon = !mbSubtractPolygon;
110 break;
112 case TransitionInfo::ReverseMethod::Rotate180:
113 maStaticTransformation = basegfx::utils::createRotateAroundPoint(0.5, 0.5, M_PI)
114 * maStaticTransformation;
115 break;
117 case TransitionInfo::ReverseMethod::FlipX:
118 maStaticTransformation = basegfx::utils::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
119 * maStaticTransformation;
120 mbFlip = true;
121 break;
123 case TransitionInfo::ReverseMethod::FlipY:
124 maStaticTransformation = basegfx::utils::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
125 * maStaticTransformation;
126 mbFlip = true;
127 break;
131 if( !bModeIn )
133 // client has requested 'out' mode. Apply
134 // TransitionInfo's method of choice
135 if( rTransitionInfo.mbOutInvertsSweep )
136 mbForwardParameterSweep = !mbForwardParameterSweep;
137 else
138 mbSubtractPolygon = !mbSubtractPolygon;
142 ::basegfx::B2DPolyPolygon ClippingFunctor::operator()( double nValue,
143 const ::basegfx::B2DSize& rTargetSize )
145 // modify clip polygon according to static
146 // transformation plus current shape size
147 ::basegfx::B2DHomMatrix aMatrix( maStaticTransformation );
149 // retrieve current clip polygon
150 ::basegfx::B2DPolyPolygon aClipPoly = (*mpParametricPoly)(
151 mbForwardParameterSweep ? nValue : 1.0 - nValue );
153 // TODO(Q4): workaround here, better be fixed in cppcanvas
154 if (aClipPoly.count() == 0)
155 aClipPoly.append( basegfx::B2DPolygon() );
157 if (mbFlip)
158 aClipPoly.flip();
160 // currently, clipper cannot cope with curves. Subdivide first
161 // AW: Should be no longer necessary; clipping tools are now bezier-safe
162 // if( aClipPoly.areControlPointsUsed() )
163 // aClipPoly = ::basegfx::utils::adaptiveSubdivideByAngle(aClipPoly);
165 if( mbSubtractPolygon )
167 // subtract given polygon from background
168 // rect. Do that before any transformations.
170 // calc maBackgroundRect \ aClipPoly
171 // =================================
173 // AW: Simplified
174 // use a range with fixed size (-1,-1),(2,2)
175 const basegfx::B2DRange aBackgroundRange(-1, -1, 2, 2);
176 const basegfx::B2DRange aClipPolyRange(aClipPoly.getB2DRange());
178 if(aBackgroundRange.isInside(aClipPolyRange))
180 // combine polygons; make the clip polygon the hole
181 aClipPoly = ::basegfx::utils::correctOrientations(aClipPoly);
182 aClipPoly.flip();
183 aClipPoly.insert(0, basegfx::utils::createPolygonFromRect(aBackgroundRange));
185 else
187 // when not completely inside aBackgroundRange clipping is needed
188 // subtract aClipPoly from aBackgroundRange
189 const basegfx::B2DPolyPolygon aBackgroundPolyPoly(basegfx::utils::createPolygonFromRect(aBackgroundRange));
190 aClipPoly = basegfx::utils::solvePolygonOperationDiff(aBackgroundPolyPoly, aClipPoly);
194 // scale polygon up to current shape size
195 if( mbScaleIsotrophically )
197 const double nScale( ::std::max( rTargetSize.getX(),
198 rTargetSize.getY() ) );
199 aMatrix.scale( nScale, nScale );
200 aMatrix.translate( -(nScale-rTargetSize.getX())/2.0,
201 -(nScale-rTargetSize.getY())/2.0 );
203 else
205 aMatrix.scale( rTargetSize.getX(),
206 rTargetSize.getY() );
209 // apply cumulative transformation to clip polygon
210 aClipPoly.transform( aMatrix );
212 return aClipPoly;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */