Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / engine / transitions / snakewipe.cxx
blobdd15d1238deb2cc23f682abb63f55cf854096063
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 <osl/diagnose.h>
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <basegfx/point/b2dpoint.hxx>
24 #include <basegfx/polygon/b2dpolygon.hxx>
25 #include <basegfx/matrix/b2dhommatrixtools.hxx>
26 #include "snakewipe.hxx"
27 #include "transitiontools.hxx"
30 namespace slideshow {
31 namespace internal {
33 SnakeWipe::SnakeWipe( sal_Int32 nElements, bool diagonal, bool flipOnYAxis )
34 : m_sqrtElements( static_cast<sal_Int32>(
35 sqrt( static_cast<double>(nElements) ) ) ),
36 m_elementEdge( 1.0 / m_sqrtElements ),
37 m_diagonal(diagonal),
38 m_flipOnYAxis(flipOnYAxis)
42 ::basegfx::B2DPolyPolygon SnakeWipe::calcSnake( double t ) const
44 ::basegfx::B2DPolyPolygon res;
45 const double area = (t * m_sqrtElements * m_sqrtElements);
46 const sal_Int32 line_ = (static_cast<sal_Int32>(area) / m_sqrtElements);
47 const double line = ::basegfx::pruneScaleValue(
48 static_cast<double>(line_) / m_sqrtElements );
49 const double col = ::basegfx::pruneScaleValue(
50 (area - (line_ * m_sqrtElements)) / m_sqrtElements );
52 if (! ::basegfx::fTools::equalZero( line )) {
53 ::basegfx::B2DPolygon poly;
54 poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
55 poly.append( ::basegfx::B2DPoint( 0.0, line ) );
56 poly.append( ::basegfx::B2DPoint( 1.0, line ) );
57 poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
58 poly.setClosed(true);
59 res.append(poly);
61 if (! ::basegfx::fTools::equalZero( col ))
63 double offset = 0.0;
64 if ((line_ & 1) == 1) {
65 // odd line: => right to left
66 offset = (1.0 - col);
68 ::basegfx::B2DPolygon poly;
69 poly.append( ::basegfx::B2DPoint( offset, line ) );
70 poly.append( ::basegfx::B2DPoint( offset,
71 line + m_elementEdge ) );
72 poly.append( ::basegfx::B2DPoint( offset + col,
73 line + m_elementEdge ) );
74 poly.append( ::basegfx::B2DPoint( offset + col, line ) );
75 poly.setClosed(true);
76 res.append(poly);
79 return res;
82 ::basegfx::B2DPolyPolygon SnakeWipe::calcHalfDiagonalSnake(
83 double t, bool in ) const
85 ::basegfx::B2DPolyPolygon res;
87 if (in) {
88 const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
89 const double edge = ::basegfx::pruneScaleValue(
90 static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
91 m_sqrtElements );
93 ::basegfx::B2DPolygon poly;
94 if (! ::basegfx::fTools::equalZero( edge )) {
95 poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
96 poly.append( ::basegfx::B2DPoint( 0.0, edge ) );
97 poly.append( ::basegfx::B2DPoint( edge, 0.0 ) );
98 poly.setClosed(true);
99 res.append(poly);
101 const double a = (M_SQRT1_2 / m_sqrtElements);
102 const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
103 const double len = (t * M_SQRT2 * d);
104 const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
105 poly.clear();
106 poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
107 poly.append( ::basegfx::B2DPoint( 0.0, height ) );
108 poly.append( ::basegfx::B2DPoint( len + a, height ) );
109 poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
110 poly.setClosed(true);
111 ::basegfx::B2DHomMatrix aTransform;
113 if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
115 // odd line
116 aTransform = basegfx::utils::createRotateB2DHomMatrix(M_PI_2 + M_PI_4);
117 aTransform.translate(edge + m_elementEdge, 0.0);
119 else
121 aTransform = basegfx::utils::createTranslateB2DHomMatrix(-a, 0.0);
122 aTransform.rotate( -M_PI_4 );
123 aTransform.translate( 0.0, edge );
126 poly.transform( aTransform );
127 res.append(poly);
129 else // out
131 const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
132 const double edge = ::basegfx::pruneScaleValue(
133 static_cast<double>( static_cast<sal_Int32>(sqrtArea2) ) /
134 m_sqrtElements );
136 ::basegfx::B2DPolygon poly;
137 if (! ::basegfx::fTools::equalZero( edge )) {
138 poly.append( ::basegfx::B2DPoint( 0.0, 1.0 ) );
139 poly.append( ::basegfx::B2DPoint( edge, 1.0 ) );
140 poly.append( ::basegfx::B2DPoint( 1.0, edge ) );
141 poly.append( ::basegfx::B2DPoint( 1.0, 0.0 ) );
142 poly.setClosed(true);
143 res.append(poly);
145 const double a = (M_SQRT1_2 / m_sqrtElements);
146 const double d = (sqrtArea2 - static_cast<sal_Int32>(sqrtArea2));
147 const double len = ((1.0 - t) * M_SQRT2 * d);
148 const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
149 poly.clear();
150 poly.append( ::basegfx::B2DPoint( 0.0, 0.0 ) );
151 poly.append( ::basegfx::B2DPoint( 0.0, height ) );
152 poly.append( ::basegfx::B2DPoint( len + a, height ) );
153 poly.append( ::basegfx::B2DPoint( len + a, 0.0 ) );
154 poly.setClosed(true);
155 ::basegfx::B2DHomMatrix aTransform;
157 if ((static_cast<sal_Int32>(sqrtArea2) & 1) == 1)
159 // odd line
160 aTransform = basegfx::utils::createTranslateB2DHomMatrix(0.0, -height);
161 aTransform.rotate( M_PI_2 + M_PI_4 );
162 aTransform.translate( 1.0, edge );
164 else
166 aTransform = basegfx::utils::createRotateB2DHomMatrix(-M_PI_4);
167 aTransform.translate( edge, 1.0 );
169 poly.transform( aTransform );
170 res.append(poly);
173 return res;
176 ::basegfx::B2DPolyPolygon SnakeWipe::operator () ( double t )
178 ::basegfx::B2DPolyPolygon res;
179 if (m_diagonal)
181 if (t >= 0.5) {
182 res.append( calcHalfDiagonalSnake( 1.0, true ) );
183 res.append( calcHalfDiagonalSnake( 2.0 * (t - 0.5), false ) );
185 else
186 res.append( calcHalfDiagonalSnake( 2.0 * t, true ) );
188 else
189 res = calcSnake(t);
191 return m_flipOnYAxis ? flipOnYAxis(res) : res;
194 ::basegfx::B2DPolyPolygon ParallelSnakesWipe::operator () ( double t )
196 ::basegfx::B2DPolyPolygon res;
197 if (m_diagonal)
199 OSL_ASSERT( m_opposite );
200 ::basegfx::B2DPolyPolygon half(
201 calcHalfDiagonalSnake( t, false /* out */ ) );
202 // flip on x axis and rotate 90 degrees:
203 basegfx::B2DHomMatrix aTransform(basegfx::utils::createScaleB2DHomMatrix(1.0, -1.0));
204 aTransform.translate( -0.5, 0.5 );
205 aTransform.rotate( M_PI_2 );
206 aTransform.translate( 0.5, 0.5 );
207 half.transform( aTransform );
208 half.flip();
209 res.append( half );
211 // rotate 180 degrees:
212 aTransform = basegfx::utils::createTranslateB2DHomMatrix(-0.5, -0.5);
213 aTransform.rotate( M_PI );
214 aTransform.translate( 0.5, 0.5 );
215 half.transform( aTransform );
216 res.append( half );
218 else
220 ::basegfx::B2DPolyPolygon half( calcSnake( t / 2.0 ) );
221 // rotate 90 degrees:
222 basegfx::B2DHomMatrix aTransform(basegfx::utils::createTranslateB2DHomMatrix(-0.5, -0.5));
223 aTransform.rotate( M_PI_2 );
224 aTransform.translate( 0.5, 0.5 );
225 half.transform( aTransform );
226 res.append( flipOnYAxis(half) );
227 res.append( m_opposite ? flipOnXAxis(half) : half );
230 return m_flipOnYAxis ? flipOnYAxis(res) : res;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */