Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/basegfx/range/b2ibox.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#pragma once
21
22
#include <ostream>
23
24
#include <basegfx/tuple/b2ituple.hxx>
25
#include <basegfx/range/basicbox.hxx>
26
27
namespace basegfx
28
{
29
    /** A two-dimensional interval over integers
30
31
        This is most easily depicted as a set of integers, bounded by
32
        a lower and an upper value - but excluding the upper
33
        value. All inbetween values are included in the set (see also
34
        http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
35
36
        The set is half-open, i.e. the lower bound is included, the
37
        upper bound not (if you're used to the notation - we're
38
        talking about [a,b) here, compared to closed [a,b] or fully
39
        open intervals (a,b)).
40
41
        If you don't need a half-open interval, check B2IRange.
42
43
        That means, isInside(val) will return true also for values of
44
        val=a, but not for val=b.
45
46
        Alternatively, consider this a rectangle, where the rightmost
47
        pixel column and the bottommost pixel row are excluded - this
48
        is much like polygon filling. As a result, filling a given
49
        rectangle with basebmp::BitmapDevice::fillPolyPolygon(), will
50
        affect exactly the same set of pixel as isInside() would
51
        return true for.
52
53
        @see B2IRange
54
     */
55
    class B2IBox
56
    {
57
    public:
58
        typedef sal_Int32       ValueType;
59
        typedef Int32Traits     TraitsType;
60
61
0
        B2IBox() {}
62
63
        /// Create degenerate interval that's still empty
64
        explicit B2IBox(const B2ITuple& rTuple)
65
        :   maRangeX(rTuple.getX()),
66
            maRangeY(rTuple.getY())
67
0
        {
68
0
        }
69
70
        /// Create proper interval between the two given points
71
        B2IBox(sal_Int32 x1,
72
               sal_Int32 y1,
73
               sal_Int32 x2,
74
               sal_Int32 y2) :
75
            maRangeX(x1),
76
            maRangeY(y1)
77
0
        {
78
0
            maRangeX.expand(x2);
79
0
            maRangeY.expand(y2);
80
0
        }
81
82
        /// Create proper interval between the two given points
83
        B2IBox(const B2ITuple& rTuple1,
84
               const B2ITuple& rTuple2) :
85
            maRangeX(rTuple1.getX()),
86
            maRangeY(rTuple1.getY())
87
0
        {
88
0
            expand( rTuple2 );
89
0
        }
90
91
        /** Check if the interval set is empty
92
93
            @return false, if no value is in this set - having a
94
            single value included will still return false.
95
         */
96
        bool isEmpty() const
97
0
        {
98
0
            return maRangeX.isEmpty() || maRangeY.isEmpty();
99
0
        }
100
101
        bool operator==( const B2IBox& rBox ) const
102
0
        {
103
0
            return (maRangeX == rBox.maRangeX
104
0
                && maRangeY == rBox.maRangeY);
105
0
        }
106
107
        bool operator!=( const B2IBox& rBox ) const
108
0
        {
109
0
            return (maRangeX != rBox.maRangeX
110
0
                || maRangeY != rBox.maRangeY);
111
0
        }
112
113
        /// get lower bound of the set. returns arbitrary values for empty sets.
114
        sal_Int32 getMinX() const
115
0
        {
116
0
            return maRangeX.getMinimum();
117
0
        }
118
119
        /// get lower bound of the set. returns arbitrary values for empty sets.
120
        sal_Int32 getMinY() const
121
0
        {
122
0
            return maRangeY.getMinimum();
123
0
        }
124
125
        /// get upper bound of the set. returns arbitrary values for empty sets.
126
        sal_Int32 getMaxX() const
127
0
        {
128
0
            return maRangeX.getMaximum();
129
0
        }
130
131
        /// get upper bound of the set. returns arbitrary values for empty sets.
132
        sal_Int32 getMaxY() const
133
0
        {
134
0
            return maRangeY.getMaximum();
135
0
        }
136
137
        /// return difference between upper and lower X value. returns 0 for empty sets.
138
        sal_Int64 getWidth() const
139
0
        {
140
0
            return maRangeX.getRange();
141
0
        }
142
143
        /// return difference between upper and lower Y value. returns 0 for empty sets.
144
        sal_Int64 getHeight() const
145
0
        {
146
0
            return maRangeY.getRange();
147
0
        }
148
149
        /// yields true if point is contained in set
150
        bool isInside(const B2ITuple& rTuple) const
151
0
        {
152
0
            return (
153
0
                maRangeX.isInside(rTuple.getX())
154
0
                && maRangeY.isInside(rTuple.getY())
155
0
                );
156
0
        }
157
158
        /// add point to the set, expanding as necessary
159
        void expand(const B2ITuple& rTuple)
160
0
        {
161
0
            maRangeX.expand(rTuple.getX());
162
0
            maRangeY.expand(rTuple.getY());
163
0
        }
164
165
        /// calc set intersection
166
        void intersect(const B2IBox& rBox)
167
0
        {
168
0
            maRangeX.intersect(rBox.maRangeX);
169
0
            maRangeY.intersect(rBox.maRangeY);
170
0
        }
171
172
    private:
173
        BasicBox        maRangeX;
174
        BasicBox        maRangeY;
175
    };
176
177
} // end of namespace basegfx
178
179
template< typename charT, typename traits >
180
inline std::basic_ostream<charT, traits> & operator <<(
181
    std::basic_ostream<charT, traits> & stream, const basegfx::B2IBox& box )
182
{
183
    if (box.isEmpty())
184
        return stream << "EMPTY";
185
    else
186
        return stream << box.getWidth() << 'x' << box.getHeight()
187
                      << "@(" << box.getMinX() << "," << box.getMinY() << ")";
188
}
189
190
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */