1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_TOOLS_RECTCLIPTOOLS_HXX
25 #define _BGFX_TOOLS_RECTCLIPTOOLS_HXX
26 
27 #include <sal/types.h>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace basegfx
32 {
33 	namespace tools
34 	{
35         namespace RectClipFlags
36         {
37             static const sal_uInt32 LEFT   = (sal_Int32)0x01;
38             static const sal_uInt32 RIGHT  = (sal_Int32)0x02;
39             static const sal_uInt32 TOP    = (sal_Int32)0x04;
40             static const sal_uInt32 BOTTOM = (sal_Int32)0x08;
41         }
42 
43         /** Calc clip mask for Cohen-Sutherland rectangle clip
44 
45             This function returns a clip mask used for the
46             Cohen-Sutherland rectangle clip method, where one or more
47             of the lower four bits are set, if the given point is
48             outside one or more of the four half planes defining the
49             rectangle (see RectClipFlags for possible values)
50          */
51 		template< class Point, class Rect > inline
getCohenSutherlandClipFlags(const Point & rP,const Rect & rR)52            sal_uInt32 getCohenSutherlandClipFlags( const Point& rP,
53                                                    const Rect&  rR )
54 		{
55 			// maxY | minY | maxX | minX
56 			sal_uInt32 clip  = (rP.getX() < rR.getMinX()) << 0;
57                        clip |= (rP.getX() > rR.getMaxX()) << 1;
58                        clip |= (rP.getY() < rR.getMinY()) << 2;
59                        clip |= (rP.getY() > rR.getMaxY()) << 3;
60 			return clip;
61 		}
62 
63         /** Determine number of clip planes hit by given clip mask
64 
65             This method returns the number of one bits in the four
66             least significant bits of the argument, which amounts to
67             the number of clip planes hit within the
68             getCohenSutherlandClipFlags() method.
69          */
getNumberOfClipPlanes(sal_uInt32 nFlags)70         inline sal_uInt32 getNumberOfClipPlanes( sal_uInt32 nFlags )
71         {
72             // classic bit count algo, see e.g. Reingold, Nievergelt,
73             // Deo: Combinatorial Algorithms, Theory and Practice,
74             // Prentice-Hall 1977
75             nFlags = (nFlags & 0x05) + ((nFlags >> 1) & 0x05);
76             nFlags = (nFlags & 0x03) + (nFlags >> 2); // no need for &
77                                                       // 0x03, can't
78                                                       // overflow
79             return nFlags;
80 		}
81 	}
82 }
83 
84 #endif // _BGFX_TOOLS_RECTCLIPTOOLS_HXX
85