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 .
20 #ifndef INCLUDED_BASEGFX_TOOLS_RECTCLIPTOOLS_HXX
21 #define INCLUDED_BASEGFX_TOOLS_RECTCLIPTOOLS_HXX
23 #include <sal/types.h>
24 #include <basegfx/range/b2ibox.hxx>
32 namespace RectClipFlags
34 static const sal_uInt32 LEFT
= (sal_Int32
)0x01;
35 static const sal_uInt32 RIGHT
= (sal_Int32
)0x02;
36 static const sal_uInt32 TOP
= (sal_Int32
)0x04;
37 static const sal_uInt32 BOTTOM
= (sal_Int32
)0x08;
40 /** Calc clip mask for Cohen-Sutherland rectangle clip
42 This function returns a clip mask used for the
43 Cohen-Sutherland rectangle clip method, where one or more
44 of the lower four bits are set, if the given point is
45 outside one or more of the four half planes defining the
46 rectangle (see RectClipFlags for possible values)
48 template< class Point
, class Rect
> inline
49 sal_uInt32
getCohenSutherlandClipFlags( const Point
& rP
,
52 // maxY | minY | maxX | minX
53 sal_uInt32 clip
= (rP
.getX() < rR
.getMinX()) << 0;
54 clip
|= (rP
.getX() > rR
.getMaxX()) << 1;
55 clip
|= (rP
.getY() < rR
.getMinY()) << 2;
56 clip
|= (rP
.getY() > rR
.getMaxY()) << 3;
60 /// Cohen-Sutherland mask calculation - overload for boxes.
61 template< class Point
> inline
62 sal_uInt32
getCohenSutherlandClipFlags( const Point
& rP
,
65 // maxY | minY | maxX | minX
66 sal_uInt32 clip
= (rP
.getX() < rB
.getMinX()) << 0;
67 clip
|= (rP
.getX() >= rB
.getMaxX()) << 1;
68 clip
|= (rP
.getY() < rB
.getMinY()) << 2;
69 clip
|= (rP
.getY() >= rB
.getMaxY()) << 3;
73 /** Determine number of clip planes hit by given clip mask
75 This method returns the number of one bits in the four
76 least significant bits of the argument, which amounts to
77 the number of clip planes hit within the
78 getCohenSutherlandClipFlags() method.
80 inline sal_uInt32
getNumberOfClipPlanes( sal_uInt32 nFlags
)
82 // classic bit count algo, see e.g. Reingold, Nievergelt,
83 // Deo: Combinatorial Algorithms, Theory and Practice,
85 nFlags
= (nFlags
& 0x05) + ((nFlags
>> 1) & 0x05);
86 nFlags
= (nFlags
& 0x03) + (nFlags
>> 2); // no need for &
94 #endif // INCLUDED_BASEGFX_TOOLS_RECTCLIPTOOLS_HXX
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */