nss: upgrade to release 3.73
[LibreOffice.git] / include / basegfx / range / b2drange.hxx
blobf6aa7f31110810799487675f18fc6c696f528f34
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 #pragma once
22 #include <ostream>
23 #include <vector>
25 #include <basegfx/vector/b2dvector.hxx>
26 #include <basegfx/point/b2dpoint.hxx>
27 #include <basegfx/tuple/b2dtuple.hxx>
28 #include <basegfx/range/basicrange.hxx>
29 #include <basegfx/basegfxdllapi.h>
31 namespace basegfx
33 class B2IRange;
34 class B2DHomMatrix;
36 /** A two-dimensional interval over doubles
38 This is a set of real numbers, bounded by a lower and an upper
39 pair. All inbetween values are included in the set (see also
40 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
42 The set is closed, i.e. the upper and the lower bound are
43 included (if you're used to the notation - we're talking about
44 [a,b] here, compared to half-open [a,b) or open intervals
45 (a,b)).
47 That means, isInside(val) will return true also for values of
48 val=a or val=b.
50 @see B1DRange
52 class SAL_WARN_UNUSED B2DRange
54 public:
55 typedef double ValueType;
56 typedef DoubleTraits TraitsType;
58 B2DRange() {}
60 /// Create degenerate interval consisting of a single point
61 explicit B2DRange(const B2DTuple& rTuple)
62 : maRangeX(rTuple.getX()),
63 maRangeY(rTuple.getY())
67 /// Create proper interval between the two given double pairs
68 B2DRange(double x1,
69 double y1,
70 double x2,
71 double y2)
72 : maRangeX(x1),
73 maRangeY(y1)
75 maRangeX.expand(x2);
76 maRangeY.expand(y2);
79 /// Create proper interval between the two given points
80 B2DRange(const B2DTuple& rTuple1,
81 const B2DTuple& rTuple2)
82 : maRangeX(rTuple1.getX()),
83 maRangeY(rTuple1.getY())
85 expand( rTuple2 );
88 BASEGFX_DLLPUBLIC explicit B2DRange(const B2IRange& rRange);
90 /** Check if the interval set is empty
92 @return false, if no value is in this set - having a
93 single point included will already return true.
95 bool isEmpty() const
97 return (
98 maRangeX.isEmpty()
99 || maRangeY.isEmpty()
103 /// reset the object to empty state again, clearing all values
104 void reset()
106 maRangeX.reset();
107 maRangeY.reset();
110 bool operator==( const B2DRange& rRange ) const
112 return (maRangeX == rRange.maRangeX
113 && maRangeY == rRange.maRangeY);
116 bool operator!=( const B2DRange& rRange ) const
118 return (maRangeX != rRange.maRangeX
119 || maRangeY != rRange.maRangeY);
122 bool equal(const B2DRange& rRange) const
124 return (maRangeX.equal(rRange.maRangeX)
125 && maRangeY.equal(rRange.maRangeY));
128 /// get lower bound of the set. returns arbitrary values for empty sets.
129 double getMinX() const
131 return maRangeX.getMinimum();
134 /// get lower bound of the set. returns arbitrary values for empty sets.
135 double getMinY() const
137 return maRangeY.getMinimum();
140 /// get upper bound of the set. returns arbitrary values for empty sets.
141 double getMaxX() const
143 return maRangeX.getMaximum();
146 /// get upper bound of the set. returns arbitrary values for empty sets.
147 double getMaxY() const
149 return maRangeY.getMaximum();
152 /// return difference between upper and lower X value. returns 0 for empty sets.
153 double getWidth() const
155 return maRangeX.getRange();
158 /// return difference between upper and lower Y value. returns 0 for empty sets.
159 double getHeight() const
161 return maRangeY.getRange();
164 /// get lower bound of the set. returns arbitrary values for empty sets.
165 B2DPoint getMinimum() const
167 return B2DPoint(
168 maRangeX.getMinimum(),
169 maRangeY.getMinimum()
173 /// get upper bound of the set. returns arbitrary values for empty sets.
174 B2DPoint getMaximum() const
176 return B2DPoint(
177 maRangeX.getMaximum(),
178 maRangeY.getMaximum()
182 /// return difference between upper and lower point. returns (0,0) for empty sets.
183 B2DVector getRange() const
185 return B2DVector(
186 maRangeX.getRange(),
187 maRangeY.getRange()
191 /// return center point of set. returns (0,0) for empty sets.
192 B2DPoint getCenter() const
194 return B2DPoint(
195 maRangeX.getCenter(),
196 maRangeY.getCenter()
200 /// return center X value of set. returns 0 for empty sets.
201 double getCenterX() const
203 return maRangeX.getCenter();
206 /// return center Y value of set. returns 0 for empty sets.
207 double getCenterY() const
209 return maRangeY.getCenter();
212 /// yields true if given point is contained in set
213 bool isInside(const B2DTuple& rTuple) const
215 return (
216 maRangeX.isInside(rTuple.getX())
217 && maRangeY.isInside(rTuple.getY())
221 /// yields true if rRange is inside, or equal to set
222 bool isInside(const B2DRange& rRange) const
224 return (
225 maRangeX.isInside(rRange.maRangeX)
226 && maRangeY.isInside(rRange.maRangeY)
230 /// yields true if rRange at least partly inside set
231 bool overlaps(const B2DRange& rRange) const
233 return (
234 maRangeX.overlaps(rRange.maRangeX)
235 && maRangeY.overlaps(rRange.maRangeY)
239 /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
240 bool overlapsMore(const B2DRange& rRange) const
242 return (
243 maRangeX.overlapsMore(rRange.maRangeX)
244 && maRangeY.overlapsMore(rRange.maRangeY)
248 /// add point to the set, expanding as necessary
249 void expand(const B2DTuple& rTuple)
251 maRangeX.expand(rTuple.getX());
252 maRangeY.expand(rTuple.getY());
255 /// add rRange to the set, expanding as necessary
256 void expand(const B2DRange& rRange)
258 maRangeX.expand(rRange.maRangeX);
259 maRangeY.expand(rRange.maRangeY);
262 /// calc set intersection
263 void intersect(const B2DRange& rRange)
265 maRangeX.intersect(rRange.maRangeX);
266 maRangeY.intersect(rRange.maRangeY);
269 /// grow set by fValue on all sides
270 void grow(double fValue)
272 maRangeX.grow(fValue);
273 maRangeY.grow(fValue);
276 /// clamp value on range
277 B2DTuple clamp(const B2DTuple& rTuple) const
279 return B2DTuple(
280 maRangeX.clamp(rTuple.getX()),
281 maRangeY.clamp(rTuple.getY()));
284 /** Transform Range by given transformation matrix. */
285 BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix& rMatrix);
287 /** Transform Range by given transformation matrix.
289 This operation transforms the Range by transforming all four possible
290 extrema points (corners) of the given range and building a new one.
291 This means that the range will grow evtl. when a shear and/or rotation
292 is part of the transformation.
294 BASEGFX_DLLPUBLIC B2DRange& operator*=( const ::basegfx::B2DHomMatrix& rMat );
296 /** Get a range filled with (0.0, 0.0, 1.0, 1.0) */
297 static const B2DRange& getUnitB2DRange();
299 private:
300 typedef ::basegfx::BasicRange< ValueType, TraitsType > MyBasicRange;
302 MyBasicRange maRangeX;
303 MyBasicRange maRangeY;
306 /** Transform B2DRange by given transformation matrix (see operator*=())
308 B2DRange operator*( const B2DHomMatrix& rMat, const B2DRange& rB2DRange );
310 /** Round double to nearest integer for 2D range
312 @return the nearest integer for this range
314 BASEGFX_DLLPUBLIC B2IRange fround(const B2DRange& rRange);
316 /** Compute the set difference of the two given ranges
318 This method calculates the symmetric difference (aka XOR)
319 between the two given ranges, and returning the resulting
320 ranges. Thus, the result will contain all areas where one, but
321 not both ranges lie.
323 @param o_rResult
324 Result vector. The up to four difference ranges are returned
325 within this vector
327 @param rFirst
328 The first range
330 @param rSecond
331 The second range
333 @return the input vector
335 BASEGFX_DLLPUBLIC ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >& o_rResult,
336 const B2DRange& rFirst,
337 const B2DRange& rSecond );
339 template< typename charT, typename traits >
340 inline std::basic_ostream<charT, traits> & operator <<(
341 std::basic_ostream<charT, traits> & stream, const B2DRange& range )
343 return stream << range.getWidth() << "x" << range.getHeight() << "@" << range.getMinimum();
346 } // end of namespace basegfx
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */