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 .
25 #include <basegfx/point/b2ipoint.hxx>
26 #include <basegfx/tuple/b2ituple.hxx>
27 #include <basegfx/tuple/b2i64tuple.hxx>
28 #include <basegfx/range/basicrange.hxx>
29 #include <basegfx/basegfxdllapi.h>
33 /** A two-dimensional interval over integers
35 This is a set of real numbers, bounded by a lower and an upper
36 pair. All inbetween values are included in the set (see also
37 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
39 Probably you rather want B2IBox for integers.
41 The set is closed, i.e. the upper and the lower bound are
42 included (if you're used to the notation - we're talking about
43 [a,b] here, compared to half-open [a,b) or open intervals
46 That means, isInside(val) will return true also for values of
54 typedef sal_Int32 ValueType
;
55 typedef Int32Traits TraitsType
;
59 /// Create degenerate interval consisting of a single point
60 explicit B2IRange(const B2ITuple
& rTuple
)
61 : maRangeX(rTuple
.getX()),
62 maRangeY(rTuple
.getY())
66 /// Create proper interval between the two given integer pairs
67 B2IRange(sal_Int32 x1
,
78 /// Create proper interval between the two given points
79 B2IRange(const B2ITuple
& rTuple1
,
80 const B2ITuple
& rTuple2
)
81 : maRangeX(rTuple1
.getX()),
82 maRangeY(rTuple1
.getY())
87 /** Check if the interval set is empty
89 @return false, if no value is in this set - having a
90 single point included will already return true.
94 return maRangeX
.isEmpty() || maRangeY
.isEmpty();
97 /// reset the object to empty state again, clearing all values
104 bool operator==( const B2IRange
& rRange
) const
106 return (maRangeX
== rRange
.maRangeX
107 && maRangeY
== rRange
.maRangeY
);
110 bool operator!=( const B2IRange
& rRange
) const
112 return (maRangeX
!= rRange
.maRangeX
113 || maRangeY
!= rRange
.maRangeY
);
116 /// get lower bound of the set. returns arbitrary values for empty sets.
117 sal_Int32
getMinX() const
119 return maRangeX
.getMinimum();
122 /// get lower bound of the set. returns arbitrary values for empty sets.
123 sal_Int32
getMinY() const
125 return maRangeY
.getMinimum();
128 /// get upper bound of the set. returns arbitrary values for empty sets.
129 sal_Int32
getMaxX() const
131 return maRangeX
.getMaximum();
134 /// get upper bound of the set. returns arbitrary values for empty sets.
135 sal_Int32
getMaxY() const
137 return maRangeY
.getMaximum();
140 /// return difference between upper and lower X value. returns 0 for empty sets.
141 sal_Int64
getWidth() const
143 return maRangeX
.getRange();
146 /// return difference between upper and lower Y value. returns 0 for empty sets.
147 sal_Int64
getHeight() const
149 return maRangeY
.getRange();
152 /// get lower bound of the set. returns arbitrary values for empty sets.
153 B2IPoint
getMinimum() const
156 maRangeX
.getMinimum(),
157 maRangeY
.getMinimum()
161 /// get upper bound of the set. returns arbitrary values for empty sets.
162 B2IPoint
getMaximum() const
165 maRangeX
.getMaximum(),
166 maRangeY
.getMaximum()
170 /// return difference between upper and lower point. returns (0,0) for empty sets.
171 B2I64Tuple
getRange() const
179 /// yields true if given point is contained in set
180 bool isInside(const B2ITuple
& rTuple
) const
183 maRangeX
.isInside(rTuple
.getX())
184 && maRangeY
.isInside(rTuple
.getY())
188 /// add point to the set, expanding as necessary
189 void expand(const B2ITuple
& rTuple
)
191 maRangeX
.expand(rTuple
.getX());
192 maRangeY
.expand(rTuple
.getY());
195 /// add rRange to the set, expanding as necessary
196 void expand(const B2IRange
& rRange
)
198 maRangeX
.expand(rRange
.maRangeX
);
199 maRangeY
.expand(rRange
.maRangeY
);
202 /// calc set intersection
203 void intersect(const B2IRange
& rRange
)
205 maRangeX
.intersect(rRange
.maRangeX
);
206 maRangeY
.intersect(rRange
.maRangeY
);
209 B2ITuple
clamp(const B2ITuple
& rTuple
) const
212 maRangeX
.clamp(rTuple
.getX()),
213 maRangeY
.clamp(rTuple
.getY()));
217 typedef ::basegfx::BasicRange
< ValueType
, TraitsType
> MyBasicRange
;
219 MyBasicRange maRangeX
;
220 MyBasicRange maRangeY
;
223 /** Compute the set difference of the two given ranges
225 This method calculates the symmetric difference (aka XOR)
226 between the two given ranges, and returning the resulting
227 ranges. Thus, the result will contain all areas where one, but
231 Result vector. The up to four difference ranges are returned
240 @return the input vector
242 BASEGFX_DLLPUBLIC ::std::vector
< B2IRange
>& computeSetDifference( ::std::vector
< B2IRange
>& o_rResult
,
243 const B2IRange
& rFirst
,
244 const B2IRange
& rSecond
);
246 template< typename charT
, typename traits
>
247 inline std::basic_ostream
<charT
, traits
> & operator <<(
248 std::basic_ostream
<charT
, traits
> & stream
, const B2IRange
& range
)
251 return stream
<< "EMPTY";
253 return stream
<< range
.getWidth() << 'x' << range
.getHeight()
254 << "@(" << range
.getMinX() << "," << range
.getMinY() << ")";
257 } // end of namespace basegfx
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */