cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / slideshow / source / engine / transitions / clippingfunctor.cxx
blob78c7e71029b7bdcda5b470b09e959326d221bf8c
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 <comphelper/diagnose_ex.hxx>
22 #include "clippingfunctor.hxx"
24 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
25 #include <basegfx/polygon/b2dpolypolygontools.hxx>
26 #include <basegfx/polygon/b2dpolygontools.hxx>
27 #include <basegfx/matrix/b2dhommatrixtools.hxx>
29 namespace slideshow::internal
31 ClippingFunctor::ClippingFunctor(const ParametricPolyPolygonSharedPtr& rPolygon,
32 const TransitionInfo& rTransitionInfo,
33 bool bDirectionForward,
34 bool bModeIn ) :
35 mpParametricPoly( rPolygon ),
36 maStaticTransformation(),
37 mbForwardParameterSweep( true ),
38 mbSubtractPolygon( false ),
39 mbScaleIsotrophically( rTransitionInfo.mbScaleIsotrophically ),
40 mbFlip(false)
42 ENSURE_OR_THROW( rPolygon,
43 "ClippingFunctor::ClippingFunctor(): Invalid parametric polygon" );
45 // maBackgroundRect serves as the minuent when
46 // subtracting a given clip polygon from the
47 // background. To speed up the clipper algo, avoid
48 // actual intersections of the generated
49 // poly-polygon with the minuent - i.e. choose the
50 // polygon to subtract from sufficiently large.
52 // blow up unit rect to (-1,-1),(2,2)
53 // AW: Not needed, just use range
54 // ::basegfx::B2DHomMatrix aMatrix;
55 // aMatrix.scale(3.0,3.0);
56 // aMatrix.translate(-1.0,-1.0);
57 // maBackgroundRect.transform( aMatrix );
59 // extract modification info from maTransitionInfo
62 // perform general transformations _before_ the reverse
63 // mode changes. This allows the Transition table to be
64 // filled more consistently (otherwise, when e.g. rotating
65 // a clip 90 degrees, the ReverseMethod::FlipX becomes
66 // ReverseMethod::FlipY instead)
67 if (rTransitionInfo.mnRotationAngle != 0.0 ||
68 rTransitionInfo.mnScaleX != 1.0 ||
69 rTransitionInfo.mnScaleY != 1.0)
71 maStaticTransformation.translate( -0.5, -0.5 );
72 // apply further transformations:
73 if (rTransitionInfo.mnRotationAngle != 0.0)
75 maStaticTransformation.rotate(
76 basegfx::deg2rad(rTransitionInfo.mnRotationAngle) );
78 if (rTransitionInfo.mnScaleX != 1.0 ||
79 rTransitionInfo.mnScaleY != 1.0)
81 maStaticTransformation.scale(
82 rTransitionInfo.mnScaleX,
83 rTransitionInfo.mnScaleY );
85 maStaticTransformation.translate( 0.5, 0.5 );
88 if( !bDirectionForward )
90 // Client has requested reversed
91 // direction. Apply TransitionInfo's choice
92 // for that
93 switch( rTransitionInfo.meReverseMethod )
95 default:
96 ENSURE_OR_THROW(
97 false,
98 "TransitionFactory::TransitionFactory(): Unexpected reverse method" );
99 break;
101 case TransitionInfo::ReverseMethod::Ignore:
102 break;
104 case TransitionInfo::ReverseMethod::SubtractAndInvert:
105 mbForwardParameterSweep = !mbForwardParameterSweep;
106 mbSubtractPolygon = !mbSubtractPolygon;
107 break;
109 case TransitionInfo::ReverseMethod::Rotate180:
110 maStaticTransformation = basegfx::utils::createRotateAroundPoint(0.5, 0.5, M_PI)
111 * maStaticTransformation;
112 break;
114 case TransitionInfo::ReverseMethod::FlipX:
115 maStaticTransformation = basegfx::utils::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
116 * maStaticTransformation;
117 mbFlip = true;
118 break;
120 case TransitionInfo::ReverseMethod::FlipY:
121 maStaticTransformation = basegfx::utils::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
122 * maStaticTransformation;
123 mbFlip = true;
124 break;
128 if( !bModeIn )
130 // client has requested 'out' mode. Apply
131 // TransitionInfo's method of choice
132 if( rTransitionInfo.mbOutInvertsSweep )
133 mbForwardParameterSweep = !mbForwardParameterSweep;
134 else
135 mbSubtractPolygon = !mbSubtractPolygon;
139 ::basegfx::B2DPolyPolygon ClippingFunctor::operator()( double nValue,
140 const ::basegfx::B2DSize& rTargetSize )
142 // modify clip polygon according to static
143 // transformation plus current shape size
144 ::basegfx::B2DHomMatrix aMatrix( maStaticTransformation );
146 // retrieve current clip polygon
147 ::basegfx::B2DPolyPolygon aClipPoly = (*mpParametricPoly)(
148 mbForwardParameterSweep ? nValue : 1.0 - nValue );
150 // TODO(Q4): workaround here, better be fixed in cppcanvas
151 if (aClipPoly.count() == 0)
152 aClipPoly.append( basegfx::B2DPolygon() );
154 if (mbFlip)
155 aClipPoly.flip();
157 if( mbSubtractPolygon )
159 // subtract given polygon from background
160 // rect. Do that before any transformations.
162 // calc maBackgroundRect \ aClipPoly
163 // =================================
165 // AW: Simplified
166 // use a range with fixed size (-1,-1),(2,2)
167 const basegfx::B2DRange aBackgroundRange(-1, -1, 2, 2);
168 const basegfx::B2DRange aClipPolyRange(aClipPoly.getB2DRange());
170 if(aBackgroundRange.isInside(aClipPolyRange))
172 // combine polygons; make the clip polygon the hole
173 aClipPoly = ::basegfx::utils::correctOrientations(aClipPoly);
174 aClipPoly.flip();
175 aClipPoly.insert(0, basegfx::utils::createPolygonFromRect(aBackgroundRange));
177 else
179 // when not completely inside aBackgroundRange clipping is needed
180 // subtract aClipPoly from aBackgroundRange
181 const basegfx::B2DPolyPolygon aBackgroundPolyPoly(basegfx::utils::createPolygonFromRect(aBackgroundRange));
182 aClipPoly = basegfx::utils::solvePolygonOperationDiff(aBackgroundPolyPoly, aClipPoly);
186 // scale polygon up to current shape size
187 if( mbScaleIsotrophically )
189 const double nScale( ::std::max( rTargetSize.getWidth(),
190 rTargetSize.getHeight() ) );
191 aMatrix.scale( nScale, nScale );
192 aMatrix.translate( -(nScale - rTargetSize.getWidth())/2.0,
193 -(nScale - rTargetSize.getHeight())/2.0 );
195 else
197 aMatrix.scale( rTargetSize.getWidth(),
198 rTargetSize.getHeight() );
201 // apply cumulative transformation to clip polygon
202 aClipPoly.transform( aMatrix );
204 return aClipPoly;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */