xref: /AOO41X/main/basegfx/source/tools/tools.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_basegfx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "basegfx/tools/tools.hxx"
32*cdf0e10cSrcweir #include "basegfx/range/b2drange.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <algorithm>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace basegfx
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir     namespace tools
40*cdf0e10cSrcweir     {
41*cdf0e10cSrcweir         namespace
42*cdf0e10cSrcweir         {
43*cdf0e10cSrcweir             inline double distance( const double&					nX,
44*cdf0e10cSrcweir                                     const double&					nY,
45*cdf0e10cSrcweir                                     const ::basegfx::B2DVector& 	rNormal,
46*cdf0e10cSrcweir                                     const double&					nC )
47*cdf0e10cSrcweir             {
48*cdf0e10cSrcweir                 return nX*rNormal.getX() + nY*rNormal.getY() - nC;
49*cdf0e10cSrcweir             }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir             void moveLineOutsideRect( ::basegfx::B2DPoint& 			io_rStart,
52*cdf0e10cSrcweir                                       ::basegfx::B2DPoint& 			io_rEnd,
53*cdf0e10cSrcweir                                       const ::basegfx::B2DVector&	rMoveDirection,
54*cdf0e10cSrcweir                                       const ::basegfx::B2DRange&	rFitTarget		)
55*cdf0e10cSrcweir             {
56*cdf0e10cSrcweir                 // calc c for normal line form equation n x - c = 0
57*cdf0e10cSrcweir                 const double nC( rMoveDirection.scalar( io_rStart ) );
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir                 // calc maximum orthogonal distance for all four bound
60*cdf0e10cSrcweir                 // rect corners to the line
61*cdf0e10cSrcweir                 const double nMaxDistance( ::std::max(
62*cdf0e10cSrcweir                                                0.0,
63*cdf0e10cSrcweir                                                ::std::max(
64*cdf0e10cSrcweir                                                    distance(rFitTarget.getMinX(),
65*cdf0e10cSrcweir                                                             rFitTarget.getMinY(),
66*cdf0e10cSrcweir                                                             rMoveDirection,
67*cdf0e10cSrcweir                                                             nC),
68*cdf0e10cSrcweir                                                    ::std::max(
69*cdf0e10cSrcweir                                                        distance(rFitTarget.getMinX(),
70*cdf0e10cSrcweir                                                                 rFitTarget.getMaxY(),
71*cdf0e10cSrcweir                                                                 rMoveDirection,
72*cdf0e10cSrcweir                                                                 nC),
73*cdf0e10cSrcweir                                                        ::std::max(
74*cdf0e10cSrcweir                                                            distance(rFitTarget.getMaxX(),
75*cdf0e10cSrcweir                                                                     rFitTarget.getMinY(),
76*cdf0e10cSrcweir                                                                     rMoveDirection,
77*cdf0e10cSrcweir                                                                     nC),
78*cdf0e10cSrcweir                                                            distance(rFitTarget.getMaxX(),
79*cdf0e10cSrcweir                                                                     rFitTarget.getMaxY(),
80*cdf0e10cSrcweir                                                                     rMoveDirection,
81*cdf0e10cSrcweir                                                                     nC) ) ) ) ) );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir                 // now move line points, such that the bound rect
84*cdf0e10cSrcweir                 // points are all either 'on' or on the negative side
85*cdf0e10cSrcweir                 // of the half-plane
86*cdf0e10cSrcweir                 io_rStart += nMaxDistance*rMoveDirection;
87*cdf0e10cSrcweir                 io_rEnd   += nMaxDistance*rMoveDirection;
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         void infiniteLineFromParallelogram( ::basegfx::B2DPoint& 		io_rLeftTop,
92*cdf0e10cSrcweir                                             ::basegfx::B2DPoint& 		io_rLeftBottom,
93*cdf0e10cSrcweir                                             ::basegfx::B2DPoint& 		io_rRightTop,
94*cdf0e10cSrcweir                                             ::basegfx::B2DPoint& 		io_rRightBottom,
95*cdf0e10cSrcweir                                             const ::basegfx::B2DRange&	rFitTarget	)
96*cdf0e10cSrcweir         {
97*cdf0e10cSrcweir             // For the top and bottom border line of the
98*cdf0e10cSrcweir             // parallelogram, we determine the distance to all four
99*cdf0e10cSrcweir             // corner points of the bound rect (tl, tr, bl, br). When
100*cdf0e10cSrcweir             // using the unit normal form for lines (n x - c = 0), and
101*cdf0e10cSrcweir             // choosing n to point 'outwards' the parallelogram, then
102*cdf0e10cSrcweir             // all bound rect corner points having positive distance
103*cdf0e10cSrcweir             // to the line lie outside the extended gradient rect, and
104*cdf0e10cSrcweir             // thus, the corresponding border line must be moved the
105*cdf0e10cSrcweir             // maximum distance outwards.
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir             // don't use the top and bottom border line direction, and
108*cdf0e10cSrcweir             // calculate the normal from them. Instead, use the
109*cdf0e10cSrcweir             // vertical lines (lt - lb or rt - rb), as they more
110*cdf0e10cSrcweir             // faithfully represent the direction of the
111*cdf0e10cSrcweir             // to-be-generated infinite line
112*cdf0e10cSrcweir             ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom );
113*cdf0e10cSrcweir             aDirectionVertical.normalize();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             const ::basegfx::B2DVector aNormalTop( aDirectionVertical );
116*cdf0e10cSrcweir             const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             // now extend parallelogram, such that the bound rect
119*cdf0e10cSrcweir             // point are included
120*cdf0e10cSrcweir             moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget );
121*cdf0e10cSrcweir             moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget );
122*cdf0e10cSrcweir         }
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir }
125