nss: upgrade to release 3.73
[LibreOffice.git] / include / basegfx / range / b2irange.hxx
blobf24526880963c0d57e20b988436d72651202b278
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/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>
31 namespace basegfx
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
44 (a,b)).
46 That means, isInside(val) will return true also for values of
47 val=a or val=b.
49 @see B2IBox
51 class B2IRange
53 public:
54 typedef sal_Int32 ValueType;
55 typedef Int32Traits TraitsType;
57 B2IRange() {}
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,
68 sal_Int32 y1,
69 sal_Int32 x2,
70 sal_Int32 y2)
71 : maRangeX(x1),
72 maRangeY(y1)
74 maRangeX.expand(x2);
75 maRangeY.expand(y2);
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())
84 expand( rTuple2 );
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.
92 bool isEmpty() const
94 return maRangeX.isEmpty() || maRangeY.isEmpty();
97 /// reset the object to empty state again, clearing all values
98 void reset()
100 maRangeX.reset();
101 maRangeY.reset();
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
155 return B2IPoint(
156 maRangeX.getMinimum(),
157 maRangeY.getMinimum()
161 /// get upper bound of the set. returns arbitrary values for empty sets.
162 B2IPoint getMaximum() const
164 return B2IPoint(
165 maRangeX.getMaximum(),
166 maRangeY.getMaximum()
170 /// return difference between upper and lower point. returns (0,0) for empty sets.
171 B2I64Tuple getRange() const
173 return B2I64Tuple(
174 maRangeX.getRange(),
175 maRangeY.getRange()
179 /// yields true if given point is contained in set
180 bool isInside(const B2ITuple& rTuple) const
182 return (
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
211 return B2ITuple(
212 maRangeX.clamp(rTuple.getX()),
213 maRangeY.clamp(rTuple.getY()));
216 private:
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
228 not both ranges lie.
230 @param o_rResult
231 Result vector. The up to four difference ranges are returned
232 within this vector
234 @param rFirst
235 The first range
237 @param rSecond
238 The second range
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 )
250 if (range.isEmpty())
251 return stream << "EMPTY";
252 else
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: */