build fix
[LibreOffice.git] / slideshow / source / engine / transitions / clippingfunctor.cxx
blobc2e8fac9f135877510dfd1a08e9ed3aab8b2c4ba
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_FLIP_X becomes
69 // REVERSEMETHOD_FLIP_Y 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_INVERT_SWEEP:
108 mbForwardParameterSweep = !mbForwardParameterSweep;
109 break;
111 case TransitionInfo::REVERSEMETHOD_SUBTRACT_POLYGON:
112 mbSubtractPolygon = !mbSubtractPolygon;
113 break;
115 case TransitionInfo::REVERSEMETHOD_SUBTRACT_AND_INVERT:
116 mbForwardParameterSweep = !mbForwardParameterSweep;
117 mbSubtractPolygon = !mbSubtractPolygon;
118 break;
120 case TransitionInfo::REVERSEMETHOD_ROTATE_180:
121 maStaticTransformation = basegfx::tools::createRotateAroundPoint(0.5, 0.5, M_PI)
122 * maStaticTransformation;
123 break;
125 case TransitionInfo::REVERSEMETHOD_FLIP_X:
126 maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
127 * maStaticTransformation;
128 mbFlip = true;
129 break;
131 case TransitionInfo::REVERSEMETHOD_FLIP_Y:
132 maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
133 * maStaticTransformation;
134 mbFlip = true;
135 break;
139 if( !bModeIn )
141 // client has requested 'out' mode. Apply
142 // TransitionInfo's method of choice
143 if( rTransitionInfo.mbOutInvertsSweep )
144 mbForwardParameterSweep = !mbForwardParameterSweep;
145 else
146 mbSubtractPolygon = !mbSubtractPolygon;
150 ::basegfx::B2DPolyPolygon ClippingFunctor::operator()( double nValue,
151 const ::basegfx::B2DSize& rTargetSize )
153 // modify clip polygon according to static
154 // transformation plus current shape size
155 ::basegfx::B2DHomMatrix aMatrix( maStaticTransformation );
157 // retrieve current clip polygon
158 ::basegfx::B2DPolyPolygon aClipPoly = (*mpParametricPoly)(
159 mbForwardParameterSweep ? nValue : 1.0 - nValue );
161 // TODO(Q4): workaround here, better be fixed in cppcanvas
162 if (aClipPoly.count() == 0)
163 aClipPoly.append( basegfx::B2DPolygon() );
165 if (mbFlip)
166 aClipPoly.flip();
168 // currently, clipper cannot cope with curves. Subdivide first
169 // AW: Should be no longer necessary; clipping tools are now bezier-safe
170 // if( aClipPoly.areControlPointsUsed() )
171 // aClipPoly = ::basegfx::tools::adaptiveSubdivideByAngle(aClipPoly);
173 if( mbSubtractPolygon )
175 // subtract given polygon from background
176 // rect. Do that before any transformations.
178 // calc maBackgroundRect \ aClipPoly
179 // =================================
181 // AW: Simplified
182 // use a range with fixed size (-1,-1),(2,2)
183 const basegfx::B2DRange aBackgroundRange(-1, -1, 2, 2);
184 const basegfx::B2DRange aClipPolyRange(aClipPoly.getB2DRange());
186 if(aBackgroundRange.isInside(aClipPolyRange))
188 // combine polygons; make the clip polygon the hole
189 aClipPoly = ::basegfx::tools::correctOrientations(aClipPoly);
190 aClipPoly.flip();
191 aClipPoly.insert(0, basegfx::tools::createPolygonFromRect(aBackgroundRange));
193 else
195 // when not completely inside aBackgroundRange clipping is needed
196 // subtract aClipPoly from aBackgroundRange
197 const basegfx::B2DPolyPolygon aBackgroundPolyPoly(basegfx::tools::createPolygonFromRect(aBackgroundRange));
198 aClipPoly = basegfx::tools::solvePolygonOperationDiff(aBackgroundPolyPoly, aClipPoly);
202 // scale polygon up to current shape size
203 if( mbScaleIsotrophically )
205 const double nScale( ::std::max( rTargetSize.getX(),
206 rTargetSize.getY() ) );
207 aMatrix.scale( nScale, nScale );
208 aMatrix.translate( -(nScale-rTargetSize.getX())/2.0,
209 -(nScale-rTargetSize.getY())/2.0 );
211 else
213 aMatrix.scale( rTargetSize.getX(),
214 rTargetSize.getY() );
217 // apply cumulative transformation to clip polygon
218 aClipPoly.transform( aMatrix );
220 return aClipPoly;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */