Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / range / b2drange.hxx
blob0de9dba0e783730e7da5ff2ca115885fb8b4659b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_B2DRANGE_HXX
21 #define INCLUDED_BASEGFX_RANGE_B2DRANGE_HXX
23 #include <ostream>
24 #include <vector>
26 #include <basegfx/vector/b2dvector.hxx>
27 #include <basegfx/point/b2dpoint.hxx>
28 #include <basegfx/tuple/b2dtuple.hxx>
29 #include <basegfx/range/basicrange.hxx>
30 #include <basegfx/basegfxdllapi.h>
32 namespace basegfx
34 class B2IRange;
35 class B2DHomMatrix;
37 /** A two-dimensional interval over doubles
39 This is a set of real numbers, bounded by a lower and an upper
40 pair. All inbetween values are included in the set (see also
41 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
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
46 (a,b)).
48 That means, isInside(val) will return true also for values of
49 val=a or val=b.
51 @see B1DRange
53 class SAL_WARN_UNUSED B2DRange
55 public:
56 typedef double ValueType;
57 typedef DoubleTraits TraitsType;
59 B2DRange() {}
61 /// Create degenerate interval consisting of a single point
62 explicit B2DRange(const B2DTuple& rTuple)
63 : maRangeX(rTuple.getX()),
64 maRangeY(rTuple.getY())
68 /// Create proper interval between the two given double pairs
69 B2DRange(double x1,
70 double y1,
71 double x2,
72 double y2)
73 : maRangeX(x1),
74 maRangeY(y1)
76 maRangeX.expand(x2);
77 maRangeY.expand(y2);
80 /// Create proper interval between the two given points
81 B2DRange(const B2DTuple& rTuple1,
82 const B2DTuple& rTuple2)
83 : maRangeX(rTuple1.getX()),
84 maRangeY(rTuple1.getY())
86 expand( rTuple2 );
89 BASEGFX_DLLPUBLIC explicit B2DRange(const B2IRange& rRange);
91 /** Check if the interval set is empty
93 @return false, if no value is in this set - having a
94 single point included will already return true.
96 bool isEmpty() const
98 return (
99 maRangeX.isEmpty()
100 || maRangeY.isEmpty()
104 /// reset the object to empty state again, clearing all values
105 void reset()
107 maRangeX.reset();
108 maRangeY.reset();
111 bool operator==( const B2DRange& rRange ) const
113 return (maRangeX == rRange.maRangeX
114 && maRangeY == rRange.maRangeY);
117 bool operator!=( const B2DRange& rRange ) const
119 return (maRangeX != rRange.maRangeX
120 || maRangeY != rRange.maRangeY);
123 bool equal(const B2DRange& rRange) const
125 return (maRangeX.equal(rRange.maRangeX)
126 && maRangeY.equal(rRange.maRangeY));
129 /// get lower bound of the set. returns arbitrary values for empty sets.
130 double getMinX() const
132 return maRangeX.getMinimum();
135 /// get lower bound of the set. returns arbitrary values for empty sets.
136 double getMinY() const
138 return maRangeY.getMinimum();
141 /// get upper bound of the set. returns arbitrary values for empty sets.
142 double getMaxX() const
144 return maRangeX.getMaximum();
147 /// get upper bound of the set. returns arbitrary values for empty sets.
148 double getMaxY() const
150 return maRangeY.getMaximum();
153 /// return difference between upper and lower X value. returns 0 for empty sets.
154 double getWidth() const
156 return maRangeX.getRange();
159 /// return difference between upper and lower Y value. returns 0 for empty sets.
160 double getHeight() const
162 return maRangeY.getRange();
165 /// get lower bound of the set. returns arbitrary values for empty sets.
166 B2DPoint getMinimum() const
168 return B2DPoint(
169 maRangeX.getMinimum(),
170 maRangeY.getMinimum()
174 /// get upper bound of the set. returns arbitrary values for empty sets.
175 B2DPoint getMaximum() const
177 return B2DPoint(
178 maRangeX.getMaximum(),
179 maRangeY.getMaximum()
183 /// return difference between upper and lower point. returns (0,0) for empty sets.
184 B2DVector getRange() const
186 return B2DVector(
187 maRangeX.getRange(),
188 maRangeY.getRange()
192 /// return center point of set. returns (0,0) for empty sets.
193 B2DPoint getCenter() const
195 return B2DPoint(
196 maRangeX.getCenter(),
197 maRangeY.getCenter()
201 /// return center X value of set. returns 0 for empty sets.
202 double getCenterX() const
204 return maRangeX.getCenter();
207 /// return center Y value of set. returns 0 for empty sets.
208 double getCenterY() const
210 return maRangeY.getCenter();
213 /// yields true if given point is contained in set
214 bool isInside(const B2DTuple& rTuple) const
216 return (
217 maRangeX.isInside(rTuple.getX())
218 && maRangeY.isInside(rTuple.getY())
222 /// yields true if rRange is inside, or equal to set
223 bool isInside(const B2DRange& rRange) const
225 return (
226 maRangeX.isInside(rRange.maRangeX)
227 && maRangeY.isInside(rRange.maRangeY)
231 /// yields true if rRange at least partly inside set
232 bool overlaps(const B2DRange& rRange) const
234 return (
235 maRangeX.overlaps(rRange.maRangeX)
236 && maRangeY.overlaps(rRange.maRangeY)
240 /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
241 bool overlapsMore(const B2DRange& rRange) const
243 return (
244 maRangeX.overlapsMore(rRange.maRangeX)
245 && maRangeY.overlapsMore(rRange.maRangeY)
249 /// add point to the set, expanding as necessary
250 void expand(const B2DTuple& rTuple)
252 maRangeX.expand(rTuple.getX());
253 maRangeY.expand(rTuple.getY());
256 /// add rRange to the set, expanding as necessary
257 void expand(const B2DRange& rRange)
259 maRangeX.expand(rRange.maRangeX);
260 maRangeY.expand(rRange.maRangeY);
263 /// calc set intersection
264 void intersect(const B2DRange& rRange)
266 maRangeX.intersect(rRange.maRangeX);
267 maRangeY.intersect(rRange.maRangeY);
270 /// grow set by fValue on all sides
271 void grow(double fValue)
273 maRangeX.grow(fValue);
274 maRangeY.grow(fValue);
277 BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix& rMatrix);
279 private:
280 typedef ::basegfx::BasicRange< ValueType, TraitsType > MyBasicRange;
282 MyBasicRange maRangeX;
283 MyBasicRange maRangeY;
286 /** Round double to nearest integer for 2D range
288 @return the nearest integer for this range
290 BASEGFX_DLLPUBLIC B2IRange fround(const B2DRange& rRange);
292 /** Compute the set difference of the two given ranges
294 This method calculates the symmetric difference (aka XOR)
295 between the two given ranges, and returning the resulting
296 ranges. Thus, the result will contain all areas where one, but
297 not both ranges lie.
299 @param o_rResult
300 Result vector. The up to four difference ranges are returned
301 within this vector
303 @param rFirst
304 The first range
306 @param rSecond
307 The second range
309 @return the input vector
311 BASEGFX_DLLPUBLIC ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >& o_rResult,
312 const B2DRange& rFirst,
313 const B2DRange& rSecond );
315 template< typename charT, typename traits >
316 inline std::basic_ostream<charT, traits> & operator <<(
317 std::basic_ostream<charT, traits> & stream, const B2DRange& range )
319 return stream << range.getWidth() << "x" << range.getHeight() << "@" << range.getMinimum();
322 } // end of namespace basegfx
324 #endif // INCLUDED_BASEGFX_RANGE_B2DRANGE_HXX
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */