1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
33*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
34*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
35*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
36*cdf0e10cSrcweir #include "snakewipe.hxx"
37*cdf0e10cSrcweir #include "transitiontools.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace slideshow {
41*cdf0e10cSrcweir namespace internal {
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir SnakeWipe::SnakeWipe( sal_Int32 nElements, bool diagonal, bool flipOnYAxis )
44*cdf0e10cSrcweir     : m_sqrtElements( static_cast<sal_Int32>(
45*cdf0e10cSrcweir                           sqrt( static_cast<double>(nElements) ) ) ),
46*cdf0e10cSrcweir       m_elementEdge( 1.0 / m_sqrtElements ),
47*cdf0e10cSrcweir       m_diagonal(diagonal),
48*cdf0e10cSrcweir       m_flipOnYAxis(flipOnYAxis)
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::calcSnake( double t ) const
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
55*cdf0e10cSrcweir     const double area = (t * m_sqrtElements * m_sqrtElements);
56*cdf0e10cSrcweir     const sal_Int32 line_ = (static_cast<sal_Int32>(area) / m_sqrtElements);
57*cdf0e10cSrcweir     const double line = ::basegfx::pruneScaleValue(
58*cdf0e10cSrcweir         static_cast<double>(line_) / m_sqrtElements );
59*cdf0e10cSrcweir     const double col = ::basegfx::pruneScaleValue(
60*cdf0e10cSrcweir         (area - (line_ * m_sqrtElements)) / m_sqrtElements );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     if (! ::basegfx::fTools::equalZero( line )) {
63*cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
64*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
65*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, line ) );
66*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 1.0, line ) );
67*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
68*cdf0e10cSrcweir         poly.setClosed(true);
69*cdf0e10cSrcweir         res.append(poly);
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir     if (! ::basegfx::fTools::equalZero( col ))
72*cdf0e10cSrcweir     {
73*cdf0e10cSrcweir         double offset = 0.0;
74*cdf0e10cSrcweir         if ((line_ & 1) == 1) {
75*cdf0e10cSrcweir             // odd line: => right to left
76*cdf0e10cSrcweir             offset = (1.0 - col);
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
79*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset, line ) );
80*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset,
81*cdf0e10cSrcweir                                           line + m_elementEdge ) );
82*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset + col,
83*cdf0e10cSrcweir                                           line + m_elementEdge ) );
84*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( offset + col, line ) );
85*cdf0e10cSrcweir         poly.setClosed(true);
86*cdf0e10cSrcweir         res.append(poly);
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     return res;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::calcHalfDiagonalSnake(
93*cdf0e10cSrcweir     double t, bool in ) const
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     if (in) {
98*cdf0e10cSrcweir         const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
99*cdf0e10cSrcweir         const double edge = ::basegfx::pruneScaleValue(
100*cdf0e10cSrcweir             static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
101*cdf0e10cSrcweir             m_sqrtElements );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
104*cdf0e10cSrcweir         if (! ::basegfx::fTools::equalZero( edge )) {
105*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
106*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, edge ) );
107*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( edge, 0.0 ) );
108*cdf0e10cSrcweir             poly.setClosed(true);
109*cdf0e10cSrcweir             res.append(poly);
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir         const double a = (M_SQRT1_2 / m_sqrtElements);
112*cdf0e10cSrcweir         const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
113*cdf0e10cSrcweir         const double len = (t * M_SQRT2 * d);
114*cdf0e10cSrcweir         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
115*cdf0e10cSrcweir         poly.clear();
116*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
117*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, height ) );
118*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, height ) );
119*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
120*cdf0e10cSrcweir         poly.setClosed(true);
121*cdf0e10cSrcweir         ::basegfx::B2DHomMatrix aTransform;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             // odd line
126*cdf0e10cSrcweir             aTransform = basegfx::tools::createRotateB2DHomMatrix(M_PI_2 + M_PI_4);
127*cdf0e10cSrcweir             aTransform.translate(edge + m_elementEdge, 0.0);
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir         else
130*cdf0e10cSrcweir         {
131*cdf0e10cSrcweir             aTransform = basegfx::tools::createTranslateB2DHomMatrix(-a, 0.0);
132*cdf0e10cSrcweir             aTransform.rotate( -M_PI_4 );
133*cdf0e10cSrcweir             aTransform.translate( 0.0, edge );
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         poly.transform( aTransform );
137*cdf0e10cSrcweir         res.append(poly);
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir     else // out
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
142*cdf0e10cSrcweir         const double edge = ::basegfx::pruneScaleValue(
143*cdf0e10cSrcweir             static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
144*cdf0e10cSrcweir             m_sqrtElements );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         ::basegfx::B2DPolygon poly;
147*cdf0e10cSrcweir         if (! ::basegfx::fTools::equalZero( edge )) {
148*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 0.0, 1.0 ) );
149*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( edge, 1.0 ) );
150*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 1.0, edge ) );
151*cdf0e10cSrcweir             poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
152*cdf0e10cSrcweir             poly.setClosed(true);
153*cdf0e10cSrcweir             res.append(poly);
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         const double a = (M_SQRT1_2 / m_sqrtElements);
156*cdf0e10cSrcweir         const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
157*cdf0e10cSrcweir         const double len = ((1.0 - t) * M_SQRT2 * d);
158*cdf0e10cSrcweir         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
159*cdf0e10cSrcweir         poly.clear();
160*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
161*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( 0.0, height ) );
162*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, height ) );
163*cdf0e10cSrcweir         poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
164*cdf0e10cSrcweir         poly.setClosed(true);
165*cdf0e10cSrcweir         ::basegfx::B2DHomMatrix aTransform;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
168*cdf0e10cSrcweir         {
169*cdf0e10cSrcweir             // odd line
170*cdf0e10cSrcweir             aTransform = basegfx::tools::createTranslateB2DHomMatrix(0.0, -height);
171*cdf0e10cSrcweir             aTransform.rotate( M_PI_2 + M_PI_4 );
172*cdf0e10cSrcweir             aTransform.translate( 1.0, edge );
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir         else
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             aTransform = basegfx::tools::createRotateB2DHomMatrix(-M_PI_4);
177*cdf0e10cSrcweir             aTransform.translate( edge, 1.0 );
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir         poly.transform( aTransform );
180*cdf0e10cSrcweir         res.append(poly);
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     return res;
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir ::basegfx::B2DPolyPolygon SnakeWipe::operator () ( double t )
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
189*cdf0e10cSrcweir     if (m_diagonal)
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         if (t >= 0.5) {
192*cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 1.0, true ) );
193*cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 2.0 * (t - 0.5), false ) );
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         else
196*cdf0e10cSrcweir             res.append( calcHalfDiagonalSnake( 2.0 * t, true ) );
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir     else
199*cdf0e10cSrcweir         res = calcSnake(t);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     return m_flipOnYAxis ? flipOnYAxis(res) : res;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir ::basegfx::B2DPolyPolygon ParallelSnakesWipe::operator () ( double t )
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon res;
207*cdf0e10cSrcweir     if (m_diagonal)
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         OSL_ASSERT( m_opposite );
210*cdf0e10cSrcweir         ::basegfx::B2DPolyPolygon half(
211*cdf0e10cSrcweir             calcHalfDiagonalSnake( t, false /* out */ ) );
212*cdf0e10cSrcweir         // flip on x axis and rotate 90 degrees:
213*cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createScaleB2DHomMatrix(1.0, -1.0));
214*cdf0e10cSrcweir         aTransform.translate( -0.5, 0.5 );
215*cdf0e10cSrcweir         aTransform.rotate( M_PI_2 );
216*cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
217*cdf0e10cSrcweir         half.transform( aTransform );
218*cdf0e10cSrcweir         half.flip();
219*cdf0e10cSrcweir         res.append( half );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         // rotate 180 degrees:
222*cdf0e10cSrcweir         aTransform = basegfx::tools::createTranslateB2DHomMatrix(-0.5, -0.5);
223*cdf0e10cSrcweir         aTransform.rotate( M_PI );
224*cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
225*cdf0e10cSrcweir         half.transform( aTransform );
226*cdf0e10cSrcweir         res.append( half );
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir     else
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         ::basegfx::B2DPolyPolygon half( calcSnake( t / 2.0 ) );
231*cdf0e10cSrcweir         // rotate 90 degrees:
232*cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createTranslateB2DHomMatrix(-0.5, -0.5));
233*cdf0e10cSrcweir         aTransform.rotate( M_PI_2 );
234*cdf0e10cSrcweir         aTransform.translate( 0.5, 0.5 );
235*cdf0e10cSrcweir         half.transform( aTransform );
236*cdf0e10cSrcweir         res.append( flipOnYAxis(half) );
237*cdf0e10cSrcweir         res.append( m_opposite ? flipOnXAxis(half) : half );
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     return m_flipOnYAxis ? flipOnYAxis(res) : res;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir }
245