tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / slideshow / source / engine / transitions / snakewipe.cxx
blob7196989abd395fc758fea53a503706eb35608416
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 .
20 #include <sal/config.h>
22 #include <cmath>
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 ),
40 m_diagonal(diagonal),
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 ) );
61 poly.setClosed(true);
62 res.append(poly);
64 if (! ::basegfx::fTools::equalZero( col ))
66 double offset = 0.0;
67 if ((line_ & 1) == 1) {
68 // odd line: => right to left
69 offset = (1.0 - col);
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 ) );
78 poly.setClosed(true);
79 res.append(poly);
82 return res;
85 ::basegfx::B2DPolyPolygon SnakeWipe::calcHalfDiagonalSnake(
86 double t, bool in ) const
88 ::basegfx::B2DPolyPolygon res;
90 if (in) {
91 const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
92 const double edge = ::basegfx::pruneScaleValue(
93 std::trunc(sqrtArea2) /
94 m_sqrtElements );
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);
102 res.append(poly);
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 );
108 poly.clear();
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)
118 // odd line
119 aTransform = basegfx::utils::createRotateB2DHomMatrix(M_PI_2 + M_PI_4);
120 aTransform.translate(edge + m_elementEdge, 0.0);
122 else
124 aTransform = basegfx::utils::createTranslateB2DHomMatrix(-a, 0.0);
125 aTransform.rotate( -M_PI_4 );
126 aTransform.translate( 0.0, edge );
129 poly.transform( aTransform );
130 res.append(poly);
132 else // out
134 const double sqrtArea2 = sqrt( t * m_sqrtElements * m_sqrtElements );
135 const double edge = ::basegfx::pruneScaleValue(
136 std::trunc(sqrtArea2) /
137 m_sqrtElements );
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);
146 res.append(poly);
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 );
152 poly.clear();
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)
162 // odd line
163 aTransform = basegfx::utils::createTranslateB2DHomMatrix(0.0, -height);
164 aTransform.rotate( M_PI_2 + M_PI_4 );
165 aTransform.translate( 1.0, edge );
167 else
169 aTransform = basegfx::utils::createRotateB2DHomMatrix(-M_PI_4);
170 aTransform.translate( edge, 1.0 );
172 poly.transform( aTransform );
173 res.append(poly);
176 return res;
179 ::basegfx::B2DPolyPolygon SnakeWipe::operator () ( double t )
181 ::basegfx::B2DPolyPolygon res;
182 if (m_diagonal)
184 if (t >= 0.5) {
185 res.append( calcHalfDiagonalSnake( 1.0, true ) );
186 res.append( calcHalfDiagonalSnake( 2.0 * (t - 0.5), false ) );
188 else
189 res.append( calcHalfDiagonalSnake( 2.0 * t, true ) );
191 else
192 res = calcSnake(t);
194 if (m_flipOnYAxis)
195 return flipOnYAxis(res);
196 return res;
199 ::basegfx::B2DPolyPolygon ParallelSnakesWipe::operator () ( double t )
201 ::basegfx::B2DPolyPolygon res;
202 if (m_diagonal)
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 );
213 half.flip();
214 res.append( half );
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 );
221 res.append( half );
223 else
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) );
232 if (m_opposite)
233 res.append(flipOnXAxis(half));
234 else
235 res.append(half);
238 if (m_flipOnYAxis)
239 return flipOnYAxis(res);
240 return res;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */