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_RANGE_B2IRANGE_HXX
21 #define INCLUDED_BASEGFX_RANGE_B2IRANGE_HXX
26 #include <basegfx/point/b2ipoint.hxx>
27 #include <basegfx/point/b2dpoint.hxx>
28 #include <basegfx/tuple/b2ituple.hxx>
29 #include <basegfx/tuple/b2i64tuple.hxx>
30 #include <basegfx/range/basicrange.hxx>
31 #include <basegfx/basegfxdllapi.h>
35 /** A two-dimensional interval over integers
37 This is a set of real numbers, bounded by a lower and an upper
38 pair. All inbetween values are included in the set (see also
39 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
41 Probably you rather want B2IBox for integers.
43 The set is closed, i.e. the upper and the lower bound are
44 included (if you're used to the notation - we're talking about
45 [a,b] here, compared to half-open [a,b) or open intervals
48 That means, isInside(val) will return true also for values of
56 typedef sal_Int32 ValueType
;
57 typedef Int32Traits TraitsType
;
61 /// Create degenerate interval consisting of a single point
62 explicit B2IRange(const B2ITuple
& rTuple
)
63 : maRangeX(rTuple
.getX()),
64 maRangeY(rTuple
.getY())
68 /// Create proper interval between the two given integer pairs
69 B2IRange(sal_Int32 x1
,
80 /// Create proper interval between the two given points
81 B2IRange(const B2ITuple
& rTuple1
,
82 const B2ITuple
& rTuple2
)
83 : maRangeX(rTuple1
.getX()),
84 maRangeY(rTuple1
.getY())
89 /** Check if the interval set is empty
91 @return false, if no value is in this set - having a
92 single point included will already return true.
96 return maRangeX
.isEmpty() || maRangeY
.isEmpty();
99 /// reset the object to empty state again, clearing all values
106 bool operator==( const B2IRange
& rRange
) const
108 return (maRangeX
== rRange
.maRangeX
109 && maRangeY
== rRange
.maRangeY
);
112 bool operator!=( const B2IRange
& rRange
) const
114 return (maRangeX
!= rRange
.maRangeX
115 || maRangeY
!= rRange
.maRangeY
);
118 /// get lower bound of the set. returns arbitrary values for empty sets.
119 sal_Int32
getMinX() const
121 return maRangeX
.getMinimum();
124 /// get lower bound of the set. returns arbitrary values for empty sets.
125 sal_Int32
getMinY() const
127 return maRangeY
.getMinimum();
130 /// get upper bound of the set. returns arbitrary values for empty sets.
131 sal_Int32
getMaxX() const
133 return maRangeX
.getMaximum();
136 /// get upper bound of the set. returns arbitrary values for empty sets.
137 sal_Int32
getMaxY() const
139 return maRangeY
.getMaximum();
142 /// return difference between upper and lower X value. returns 0 for empty sets.
143 sal_Int64
getWidth() const
145 return maRangeX
.getRange();
148 /// return difference between upper and lower Y value. returns 0 for empty sets.
149 sal_Int64
getHeight() const
151 return maRangeY
.getRange();
154 /// get lower bound of the set. returns arbitrary values for empty sets.
155 B2IPoint
getMinimum() const
158 maRangeX
.getMinimum(),
159 maRangeY
.getMinimum()
163 /// get upper bound of the set. returns arbitrary values for empty sets.
164 B2IPoint
getMaximum() const
167 maRangeX
.getMaximum(),
168 maRangeY
.getMaximum()
172 /// return difference between upper and lower point. returns (0,0) for empty sets.
173 B2I64Tuple
getRange() const
181 /// yields true if given point is contained in set
182 bool isInside(const B2ITuple
& rTuple
) const
185 maRangeX
.isInside(rTuple
.getX())
186 && maRangeY
.isInside(rTuple
.getY())
190 /// add point to the set, expanding as necessary
191 void expand(const B2ITuple
& rTuple
)
193 maRangeX
.expand(rTuple
.getX());
194 maRangeY
.expand(rTuple
.getY());
197 /// add rRange to the set, expanding as necessary
198 void expand(const B2IRange
& rRange
)
200 maRangeX
.expand(rRange
.maRangeX
);
201 maRangeY
.expand(rRange
.maRangeY
);
204 /// calc set intersection
205 void intersect(const B2IRange
& rRange
)
207 maRangeX
.intersect(rRange
.maRangeX
);
208 maRangeY
.intersect(rRange
.maRangeY
);
212 typedef ::basegfx::BasicRange
< ValueType
, TraitsType
> MyBasicRange
;
214 MyBasicRange maRangeX
;
215 MyBasicRange maRangeY
;
218 /** Compute the set difference of the two given ranges
220 This method calculates the symmetric difference (aka XOR)
221 between the two given ranges, and returning the resulting
222 ranges. Thus, the result will contain all areas where one, but
226 Result vector. The up to four difference ranges are returned
235 @return the input vector
237 BASEGFX_DLLPUBLIC ::std::vector
< B2IRange
>& computeSetDifference( ::std::vector
< B2IRange
>& o_rResult
,
238 const B2IRange
& rFirst
,
239 const B2IRange
& rSecond
);
241 template< typename charT
, typename traits
>
242 inline std::basic_ostream
<charT
, traits
> & operator <<(
243 std::basic_ostream
<charT
, traits
> & stream
, const B2IRange
& range
)
246 return stream
<< "EMPTY";
248 return stream
<< range
.getWidth() << 'x' << range
.getHeight()
249 << "@(" << range
.getMinX() << "," << range
.getMinY() << ")";
252 } // end of namespace basegfx
254 #endif // INCLUDED_BASEGFX_RANGE_B2IRANGE_HXX
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */