1*70f497fbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*70f497fbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*70f497fbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*70f497fbSAndrew Rist  * distributed with this work for additional information
6*70f497fbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*70f497fbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*70f497fbSAndrew Rist  * "License"); you may not use this file except in compliance
9*70f497fbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*70f497fbSAndrew Rist  *
11*70f497fbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*70f497fbSAndrew Rist  *
13*70f497fbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*70f497fbSAndrew Rist  * software distributed under the License is distributed on an
15*70f497fbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*70f497fbSAndrew Rist  * KIND, either express or implied.  See the License for the
17*70f497fbSAndrew Rist  * specific language governing permissions and limitations
18*70f497fbSAndrew Rist  * under the License.
19*70f497fbSAndrew Rist  *
20*70f497fbSAndrew Rist  *************************************************************/
21*70f497fbSAndrew Rist 
22*70f497fbSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <canvas/debug.hxx>
28cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
29cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
31cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
32cdf0e10cSrcweir #include "snakewipe.hxx"
33cdf0e10cSrcweir #include "transitiontools.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace slideshow {
37cdf0e10cSrcweir namespace internal {
38cdf0e10cSrcweir 
SnakeWipe(sal_Int32 nElements,bool diagonal,bool flipOnYAxis)39cdf0e10cSrcweir SnakeWipe::SnakeWipe( sal_Int32 nElements, bool diagonal, bool flipOnYAxis )
40cdf0e10cSrcweir     : m_sqrtElements( static_cast<sal_Int32>(
41cdf0e10cSrcweir                           sqrt( static_cast<double>(nElements) ) ) ),
42cdf0e10cSrcweir       m_elementEdge( 1.0 / m_sqrtElements ),
43cdf0e10cSrcweir       m_diagonal(diagonal),
44cdf0e10cSrcweir       m_flipOnYAxis(flipOnYAxis)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
calcSnake(double t) const48cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::calcSnake( double t ) const
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
51cdf0e10cSrcweir     const double area = (t * m_sqrtElements * m_sqrtElements);
52cdf0e10cSrcweir     const sal_Int32 line_ = (static_cast<sal_Int32>(area) / m_sqrtElements);
53cdf0e10cSrcweir     const double line = ::basegfx::pruneScaleValue(
54cdf0e10cSrcweir         static_cast<double>(line_) / m_sqrtElements );
55cdf0e10cSrcweir     const double col = ::basegfx::pruneScaleValue(
56cdf0e10cSrcweir         (area - (line_ * m_sqrtElements)) / m_sqrtElements );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     if (! ::basegfx::fTools::equalZero( line )) {
59cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
60cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
61cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, line ) );
62cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 1.0, line ) );
63cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
64cdf0e10cSrcweir         poly.setClosed(true);
65cdf0e10cSrcweir         res.append(poly);
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir     if (! ::basegfx::fTools::equalZero( col ))
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         double offset = 0.0;
70cdf0e10cSrcweir         if ((line_ & 1) == 1) {
71cdf0e10cSrcweir             // odd line: => right to left
72cdf0e10cSrcweir             offset = (1.0 - col);
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
75cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset, line ) );
76cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset,
77cdf0e10cSrcweir                                           line + m_elementEdge ) );
78cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset + col,
79cdf0e10cSrcweir                                           line + m_elementEdge ) );
80cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset + col, line ) );
81cdf0e10cSrcweir         poly.setClosed(true);
82cdf0e10cSrcweir         res.append(poly);
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     return res;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
calcHalfDiagonalSnake(double t,bool in) const88cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::calcHalfDiagonalSnake(
89cdf0e10cSrcweir     double t, bool in ) const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     if (in) {
94cdf0e10cSrcweir         const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
95cdf0e10cSrcweir         const double edge = ::basegfx::pruneScaleValue(
96cdf0e10cSrcweir             static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
97cdf0e10cSrcweir             m_sqrtElements );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
100cdf0e10cSrcweir         if (! ::basegfx::fTools::equalZero( edge )) {
101cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
102cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, edge ) );
103cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( edge, 0.0 ) );
104cdf0e10cSrcweir             poly.setClosed(true);
105cdf0e10cSrcweir             res.append(poly);
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         const double a = (M_SQRT1_2 / m_sqrtElements);
108cdf0e10cSrcweir         const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
109cdf0e10cSrcweir         const double len = (t * M_SQRT2 * d);
110cdf0e10cSrcweir         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
111cdf0e10cSrcweir         poly.clear();
112cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
113cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, height ) );
114cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, height ) );
115cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
116cdf0e10cSrcweir         poly.setClosed(true);
117cdf0e10cSrcweir         ::basegfx::B2DHomMatrix aTransform;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             // odd line
122cdf0e10cSrcweir             aTransform = basegfx::tools::createRotateB2DHomMatrix(M_PI_2 + M_PI_4);
123cdf0e10cSrcweir             aTransform.translate(edge + m_elementEdge, 0.0);
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir         else
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             aTransform = basegfx::tools::createTranslateB2DHomMatrix(-a, 0.0);
128cdf0e10cSrcweir             aTransform.rotate( -M_PI_4 );
129cdf0e10cSrcweir             aTransform.translate( 0.0, edge );
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         poly.transform( aTransform );
133cdf0e10cSrcweir         res.append(poly);
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir     else // out
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
138cdf0e10cSrcweir         const double edge = ::basegfx::pruneScaleValue(
139cdf0e10cSrcweir             static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
140cdf0e10cSrcweir             m_sqrtElements );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
143cdf0e10cSrcweir         if (! ::basegfx::fTools::equalZero( edge )) {
144cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, 1.0 ) );
145cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( edge, 1.0 ) );
146cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 1.0, edge ) );
147cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
148cdf0e10cSrcweir             poly.setClosed(true);
149cdf0e10cSrcweir             res.append(poly);
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         const double a = (M_SQRT1_2 / m_sqrtElements);
152cdf0e10cSrcweir         const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
153cdf0e10cSrcweir         const double len = ((1.0 - t) * M_SQRT2 * d);
154cdf0e10cSrcweir         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
155cdf0e10cSrcweir         poly.clear();
156cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
157cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, height ) );
158cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, height ) );
159cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
160cdf0e10cSrcweir         poly.setClosed(true);
161cdf0e10cSrcweir         ::basegfx::B2DHomMatrix aTransform;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             // odd line
166cdf0e10cSrcweir             aTransform = basegfx::tools::createTranslateB2DHomMatrix(0.0, -height);
167cdf0e10cSrcweir             aTransform.rotate( M_PI_2 + M_PI_4 );
168cdf0e10cSrcweir             aTransform.translate( 1.0, edge );
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir         else
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             aTransform = basegfx::tools::createRotateB2DHomMatrix(-M_PI_4);
173cdf0e10cSrcweir             aTransform.translate( edge, 1.0 );
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir         poly.transform( aTransform );
176cdf0e10cSrcweir         res.append(poly);
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     return res;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
operator ()(double t)182cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::operator () ( double t )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
185cdf0e10cSrcweir     if (m_diagonal)
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         if (t >= 0.5) {
188cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 1.0, true ) );
189cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 2.0 * (t - 0.5), false ) );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         else
192cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 2.0 * t, true ) );
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir     else
195cdf0e10cSrcweir         res = calcSnake(t);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     return m_flipOnYAxis ? flipOnYAxis(res) : res;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
operator ()(double t)200cdf0e10cSrcweir ::basegfx::B2DPolyPolygon ParallelSnakesWipe::operator () ( double t )
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
203cdf0e10cSrcweir     if (m_diagonal)
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         OSL_ASSERT( m_opposite );
206cdf0e10cSrcweir         ::basegfx::B2DPolyPolygon half(
207cdf0e10cSrcweir             calcHalfDiagonalSnake( t, false /* out */ ) );
208cdf0e10cSrcweir         // flip on x axis and rotate 90 degrees:
209cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createScaleB2DHomMatrix(1.0, -1.0));
210cdf0e10cSrcweir         aTransform.translate( -0.5, 0.5 );
211cdf0e10cSrcweir         aTransform.rotate( M_PI_2 );
212cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
213cdf0e10cSrcweir         half.transform( aTransform );
214cdf0e10cSrcweir         half.flip();
215cdf0e10cSrcweir         res.append( half );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         // rotate 180 degrees:
218cdf0e10cSrcweir         aTransform = basegfx::tools::createTranslateB2DHomMatrix(-0.5, -0.5);
219cdf0e10cSrcweir         aTransform.rotate( M_PI );
220cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
221cdf0e10cSrcweir         half.transform( aTransform );
222cdf0e10cSrcweir         res.append( half );
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir     else
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         ::basegfx::B2DPolyPolygon half( calcSnake( t / 2.0 ) );
227cdf0e10cSrcweir         // rotate 90 degrees:
228cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createTranslateB2DHomMatrix(-0.5, -0.5));
229cdf0e10cSrcweir         aTransform.rotate( M_PI_2 );
230cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
231cdf0e10cSrcweir         half.transform( aTransform );
232cdf0e10cSrcweir         res.append( flipOnYAxis(half) );
233cdf0e10cSrcweir         res.append( m_opposite ? flipOnXAxis(half) : half );
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     return m_flipOnYAxis ? flipOnYAxis(res) : res;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir }
240cdf0e10cSrcweir }
241