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