1*09dbbe93SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*09dbbe93SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*09dbbe93SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*09dbbe93SAndrew Rist  * distributed with this work for additional information
6*09dbbe93SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*09dbbe93SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*09dbbe93SAndrew Rist  * "License"); you may not use this file except in compliance
9*09dbbe93SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*09dbbe93SAndrew Rist  *
11*09dbbe93SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*09dbbe93SAndrew Rist  *
13*09dbbe93SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*09dbbe93SAndrew Rist  * software distributed under the License is distributed on an
15*09dbbe93SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*09dbbe93SAndrew Rist  * KIND, either express or implied.  See the License for the
17*09dbbe93SAndrew Rist  * specific language governing permissions and limitations
18*09dbbe93SAndrew Rist  * under the License.
19*09dbbe93SAndrew Rist  *
20*09dbbe93SAndrew Rist  *************************************************************/
21*09dbbe93SAndrew Rist 
22*09dbbe93SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "basegfx/tools/tools.hxx"
28cdf0e10cSrcweir #include "basegfx/numeric/ftools.hxx"
29cdf0e10cSrcweir #include "basegfx/range/b2drange.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace basegfx
33cdf0e10cSrcweir {
34cdf0e10cSrcweir     namespace tools
35cdf0e10cSrcweir     {
36cdf0e10cSrcweir         namespace
37cdf0e10cSrcweir         {
38cdf0e10cSrcweir             // see Foley/vanDam, pp. 122 for the Liang-Barsky line
39cdf0e10cSrcweir             // clipping algorithm
liangBarskyClipT(double nDenom,double nNumerator,double & io_rTE,double & io_rTL)40cdf0e10cSrcweir             inline bool liangBarskyClipT( double  nDenom,
41cdf0e10cSrcweir                                           double  nNumerator,
42cdf0e10cSrcweir                                           double& io_rTE,
43cdf0e10cSrcweir                                           double& io_rTL )
44cdf0e10cSrcweir             {
45cdf0e10cSrcweir                 double t;
46cdf0e10cSrcweir                 if( nDenom > 0 )
47cdf0e10cSrcweir                 {
48cdf0e10cSrcweir                     t = nNumerator / nDenom;
49cdf0e10cSrcweir                     if( t > io_rTL )
50cdf0e10cSrcweir                         return false;
51cdf0e10cSrcweir                     else if( t > io_rTE )
52cdf0e10cSrcweir                         io_rTE = t;
53cdf0e10cSrcweir                 }
54cdf0e10cSrcweir                 else if( nDenom < 0 )
55cdf0e10cSrcweir                 {
56cdf0e10cSrcweir                     t = nNumerator / nDenom;
57cdf0e10cSrcweir                     if( t < io_rTE )
58cdf0e10cSrcweir                         return false;
59cdf0e10cSrcweir                     else
60cdf0e10cSrcweir                         io_rTL = t;
61cdf0e10cSrcweir                 }
62cdf0e10cSrcweir                 else if( nNumerator > 0 )
63cdf0e10cSrcweir                 {
64cdf0e10cSrcweir                     return false;
65cdf0e10cSrcweir                 }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir                 return true;
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         // see Foley/vanDam, pp. 122 for the Liang-Barsky line
72cdf0e10cSrcweir         // clipping algorithm
liangBarskyClip2D(::basegfx::B2DPoint & io_rStart,::basegfx::B2DPoint & io_rEnd,const::basegfx::B2DRange & rClipRect)73cdf0e10cSrcweir         bool liangBarskyClip2D( ::basegfx::B2DPoint& 		io_rStart,
74cdf0e10cSrcweir                                 ::basegfx::B2DPoint& 		io_rEnd,
75cdf0e10cSrcweir                                 const ::basegfx::B2DRange&	rClipRect )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             const double nDX( io_rEnd.getX() - io_rStart.getX() );
78cdf0e10cSrcweir             const double nDY( io_rEnd.getY() - io_rStart.getY() );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             if( ::basegfx::fTools::equalZero( nDX ) &&
81cdf0e10cSrcweir                 ::basegfx::fTools::equalZero( nDY ) )
82cdf0e10cSrcweir             {
83cdf0e10cSrcweir                 return rClipRect.isInside( io_rStart );
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir             else
86cdf0e10cSrcweir             {
87cdf0e10cSrcweir                 double nTE( 0.0 );
88cdf0e10cSrcweir                 double nTL( 1.0 );
89cdf0e10cSrcweir                 if( liangBarskyClipT(nDX, rClipRect.getMinX() - io_rStart.getX(),
90cdf0e10cSrcweir                                      nTE, nTL ) ) 					// inside wrt. left edge
91cdf0e10cSrcweir                 {
92cdf0e10cSrcweir                     if( liangBarskyClipT(-nDX, io_rStart.getX() - rClipRect.getMaxX(),
93cdf0e10cSrcweir                                          nTE, nTL ) ) 				// inside wrt. right edge
94cdf0e10cSrcweir                     {
95cdf0e10cSrcweir                         if( liangBarskyClipT(nDY, rClipRect.getMinY() - io_rStart.getY(),
96cdf0e10cSrcweir                                              nTE, nTL ) ) 			// inside wrt. bottom edge
97cdf0e10cSrcweir                         {
98cdf0e10cSrcweir                             if( liangBarskyClipT(-nDY, io_rStart.getY() - rClipRect.getMaxY(),
99cdf0e10cSrcweir                                                  nTE, nTL ) ) 		// inside wrt. top edge
100cdf0e10cSrcweir                             {
101cdf0e10cSrcweir                                 // compute actual intersection points,
102cdf0e10cSrcweir                                 // if nTL has changed
103cdf0e10cSrcweir                                 if( nTL < 1.0 )
104cdf0e10cSrcweir                                 {
105cdf0e10cSrcweir                                     io_rEnd.setX( io_rStart.getX() + nTL*nDX );
106cdf0e10cSrcweir                                     io_rEnd.setY( io_rStart.getY() + nTL*nDY );
107cdf0e10cSrcweir                                 }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir                                 // compute actual intersection points,
110cdf0e10cSrcweir                                 // if nTE has changed
111cdf0e10cSrcweir                                 if( nTE > 0.0 )
112cdf0e10cSrcweir                                 {
113cdf0e10cSrcweir                                     io_rStart.setX( io_rStart.getX() + nTE*nDX );
114cdf0e10cSrcweir                                     io_rStart.setY( io_rStart.getY() + nTE*nDY );
115cdf0e10cSrcweir                                 }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir                                 // line is (at least partially) visible
118cdf0e10cSrcweir                                 return true;
119cdf0e10cSrcweir                             }
120cdf0e10cSrcweir                         }
121cdf0e10cSrcweir                     }
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             return false;
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir }
129