1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
24 #include <o3tl/temporary.hxx>
25 #include <osl/diagnose.h>
26 #include <basegfx/matrix/b2dhommatrix.hxx>
27 #include <basegfx/point/b2dpoint.hxx>
28 #include <basegfx/polygon/b2dpolygon.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 #include "snakewipe.hxx"
31 #include "transitiontools.hxx"
34 namespace slideshow::internal
{
36 SnakeWipe::SnakeWipe( sal_Int32 nElements
, bool diagonal
, bool flipOnYAxis
)
37 : m_sqrtElements( static_cast<sal_Int32
>(
38 sqrt( static_cast<double>(nElements
) ) ) ),
39 m_elementEdge( 1.0 / m_sqrtElements
),
41 m_flipOnYAxis(flipOnYAxis
)
45 ::basegfx::B2DPolyPolygon
SnakeWipe::calcSnake( double t
) const
47 ::basegfx::B2DPolyPolygon res
;
48 const double area
= t
* m_sqrtElements
* m_sqrtElements
;
49 const sal_Int32 line_
= static_cast<sal_Int32
>(area
) / m_sqrtElements
;
50 const double line
= ::basegfx::pruneScaleValue(
51 static_cast<double>(line_
) / m_sqrtElements
);
52 const double col
= ::basegfx::pruneScaleValue(
53 (area
- (line_
* m_sqrtElements
)) / m_sqrtElements
);
55 if (! ::basegfx::fTools::equalZero( line
)) {
56 ::basegfx::B2DPolygon poly
;
57 poly
.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
58 poly
.append( ::basegfx::B2DPoint( 0.0, line
) );
59 poly
.append( ::basegfx::B2DPoint( 1.0, line
) );
60 poly
.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
64 if (! ::basegfx::fTools::equalZero( col
))
67 if ((line_
& 1) == 1) {
68 // odd line: => right to left
71 ::basegfx::B2DPolygon poly
;
72 poly
.append( ::basegfx::B2DPoint( offset
, line
) );
73 poly
.append( ::basegfx::B2DPoint( offset
,
74 line
+ m_elementEdge
) );
75 poly
.append( ::basegfx::B2DPoint( offset
+ col
,
76 line
+ m_elementEdge
) );
77 poly
.append( ::basegfx::B2DPoint( offset
+ col
, line
) );
85 ::basegfx::B2DPolyPolygon
SnakeWipe::calcHalfDiagonalSnake(
86 double t
, bool in
) const
88 ::basegfx::B2DPolyPolygon res
;
91 const double sqrtArea2
= sqrt( t
* m_sqrtElements
* m_sqrtElements
);
92 const double edge
= ::basegfx::pruneScaleValue(
93 std::trunc(sqrtArea2
) /
96 ::basegfx::B2DPolygon poly
;
97 if (! ::basegfx::fTools::equalZero( edge
)) {
98 poly
.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
99 poly
.append( ::basegfx::B2DPoint( 0.0, edge
) );
100 poly
.append( ::basegfx::B2DPoint( edge
, 0.0 ) );
101 poly
.setClosed(true);
104 const double a
= M_SQRT1_2
/ m_sqrtElements
;
105 const double d
= std::modf(sqrtArea2
, &o3tl::temporary(double()));
106 const double len
= t
* M_SQRT2
* d
;
107 const double height
= ::basegfx::pruneScaleValue( M_SQRT1_2
/ m_sqrtElements
);
109 poly
.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
110 poly
.append( ::basegfx::B2DPoint( 0.0, height
) );
111 poly
.append( ::basegfx::B2DPoint( len
+ a
, height
) );
112 poly
.append( ::basegfx::B2DPoint( len
+ a
, 0.0 ) );
113 poly
.setClosed(true);
114 ::basegfx::B2DHomMatrix aTransform
;
116 if ((static_cast<sal_Int32
>(sqrtArea2
) & 1) == 1)
119 aTransform
= basegfx::utils::createRotateB2DHomMatrix(M_PI_2
+ M_PI_4
);
120 aTransform
.translate(edge
+ m_elementEdge
, 0.0);
124 aTransform
= basegfx::utils::createTranslateB2DHomMatrix(-a
, 0.0);
125 aTransform
.rotate( -M_PI_4
);
126 aTransform
.translate( 0.0, edge
);
129 poly
.transform( aTransform
);
134 const double sqrtArea2
= sqrt( t
* m_sqrtElements
* m_sqrtElements
);
135 const double edge
= ::basegfx::pruneScaleValue(
136 std::trunc(sqrtArea2
) /
139 ::basegfx::B2DPolygon poly
;
140 if (! ::basegfx::fTools::equalZero( edge
)) {
141 poly
.append( ::basegfx::B2DPoint( 0.0, 1.0 ) );
142 poly
.append( ::basegfx::B2DPoint( edge
, 1.0 ) );
143 poly
.append( ::basegfx::B2DPoint( 1.0, edge
) );
144 poly
.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
145 poly
.setClosed(true);
148 const double a
= M_SQRT1_2
/ m_sqrtElements
;
149 const double d
= std::modf(sqrtArea2
, &o3tl::temporary(double()));
150 const double len
= (1.0 - t
) * M_SQRT2
* d
;
151 const double height
= ::basegfx::pruneScaleValue( M_SQRT1_2
/ m_sqrtElements
);
153 poly
.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
154 poly
.append( ::basegfx::B2DPoint( 0.0, height
) );
155 poly
.append( ::basegfx::B2DPoint( len
+ a
, height
) );
156 poly
.append( ::basegfx::B2DPoint( len
+ a
, 0.0 ) );
157 poly
.setClosed(true);
158 ::basegfx::B2DHomMatrix aTransform
;
160 if ((static_cast<sal_Int32
>(sqrtArea2
) & 1) == 1)
163 aTransform
= basegfx::utils::createTranslateB2DHomMatrix(0.0, -height
);
164 aTransform
.rotate( M_PI_2
+ M_PI_4
);
165 aTransform
.translate( 1.0, edge
);
169 aTransform
= basegfx::utils::createRotateB2DHomMatrix(-M_PI_4
);
170 aTransform
.translate( edge
, 1.0 );
172 poly
.transform( aTransform
);
179 ::basegfx::B2DPolyPolygon
SnakeWipe::operator () ( double t
)
181 ::basegfx::B2DPolyPolygon res
;
185 res
.append( calcHalfDiagonalSnake( 1.0, true ) );
186 res
.append( calcHalfDiagonalSnake( 2.0 * (t
- 0.5), false ) );
189 res
.append( calcHalfDiagonalSnake( 2.0 * t
, true ) );
195 return flipOnYAxis(res
);
199 ::basegfx::B2DPolyPolygon
ParallelSnakesWipe::operator () ( double t
)
201 ::basegfx::B2DPolyPolygon res
;
204 OSL_ASSERT( m_opposite
);
205 ::basegfx::B2DPolyPolygon
half(
206 calcHalfDiagonalSnake( t
, false /* out */ ) );
207 // flip on x axis and rotate 90 degrees:
208 basegfx::B2DHomMatrix
aTransform(basegfx::utils::createScaleB2DHomMatrix(1.0, -1.0));
209 aTransform
.translate( -0.5, 0.5 );
210 aTransform
.rotate( M_PI_2
);
211 aTransform
.translate( 0.5, 0.5 );
212 half
.transform( aTransform
);
216 // rotate 180 degrees:
217 aTransform
= basegfx::utils::createTranslateB2DHomMatrix(-0.5, -0.5);
218 aTransform
.rotate( M_PI
);
219 aTransform
.translate( 0.5, 0.5 );
220 half
.transform( aTransform
);
225 ::basegfx::B2DPolyPolygon
half( calcSnake( t
/ 2.0 ) );
226 // rotate 90 degrees:
227 basegfx::B2DHomMatrix
aTransform(basegfx::utils::createTranslateB2DHomMatrix(-0.5, -0.5));
228 aTransform
.rotate( M_PI_2
);
229 aTransform
.translate( 0.5, 0.5 );
230 half
.transform( aTransform
);
231 res
.append( flipOnYAxis(half
) );
233 res
.append(flipOnXAxis(half
));
239 return flipOnYAxis(res
);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */