1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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
),
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 ) );
61 if (! ::basegfx::fTools::equalZero( col
))
64 if ((line_
& 1) == 1) {
65 // odd line: => right to left
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
) );
82 ::basegfx::B2DPolyPolygon
SnakeWipe::calcHalfDiagonalSnake(
83 double t
, bool in
) const
85 ::basegfx::B2DPolyPolygon res
;
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
) ) /
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 ) );
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
);
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)
116 aTransform
= basegfx::utils::createRotateB2DHomMatrix(M_PI_2
+ M_PI_4
);
117 aTransform
.translate(edge
+ m_elementEdge
, 0.0);
121 aTransform
= basegfx::utils::createTranslateB2DHomMatrix(-a
, 0.0);
122 aTransform
.rotate( -M_PI_4
);
123 aTransform
.translate( 0.0, edge
);
126 poly
.transform( aTransform
);
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
) ) /
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);
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
);
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)
160 aTransform
= basegfx::utils::createTranslateB2DHomMatrix(0.0, -height
);
161 aTransform
.rotate( M_PI_2
+ M_PI_4
);
162 aTransform
.translate( 1.0, edge
);
166 aTransform
= basegfx::utils::createRotateB2DHomMatrix(-M_PI_4
);
167 aTransform
.translate( edge
, 1.0 );
169 poly
.transform( aTransform
);
176 ::basegfx::B2DPolyPolygon
SnakeWipe::operator () ( double t
)
178 ::basegfx::B2DPolyPolygon res
;
182 res
.append( calcHalfDiagonalSnake( 1.0, true ) );
183 res
.append( calcHalfDiagonalSnake( 2.0 * (t
- 0.5), false ) );
186 res
.append( calcHalfDiagonalSnake( 2.0 * t
, true ) );
191 return m_flipOnYAxis
? flipOnYAxis(res
) : res
;
194 ::basegfx::B2DPolyPolygon
ParallelSnakesWipe::operator () ( double t
)
196 ::basegfx::B2DPolyPolygon res
;
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
);
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
);
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: */