Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _BGFX_RANGE_B2DRANGE_HXX
30 : : #define _BGFX_RANGE_B2DRANGE_HXX
31 : :
32 : : #include <basegfx/vector/b2dvector.hxx>
33 : : #include <basegfx/point/b2dpoint.hxx>
34 : : #include <basegfx/tuple/b2dtuple.hxx>
35 : : #include <basegfx/range/basicrange.hxx>
36 : : #include <vector>
37 : : #include <basegfx/basegfxdllapi.h>
38 : :
39 : :
40 : : namespace basegfx
41 : : {
42 : : // predeclarations
43 : : class B2IRange;
44 : : class B2DHomMatrix;
45 : :
46 : : /** A two-dimensional interval over doubles
47 : :
48 : : This is a set of real numbers, bounded by a lower and an upper
49 : : pair. All inbetween values are included in the set (see also
50 : : http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
51 : :
52 : : The set is closed, i.e. the upper and the lower bound are
53 : : included (if you're used to the notation - we're talking about
54 : : [a,b] here, compared to half-open [a,b) or open intervals
55 : : (a,b)).
56 : :
57 : : That means, isInside(val) will return true also for values of
58 : : val=a or val=b.
59 : :
60 : : @see B1DRange
61 : : */
62 : : class B2DRange
63 : : {
64 : : public:
65 : : typedef double ValueType;
66 : : typedef DoubleTraits TraitsType;
67 : :
68 : 8137410 : B2DRange() {}
69 : :
70 : : /// Create degenerate interval consisting of a single point
71 : : explicit B2DRange(const B2DTuple& rTuple)
72 : : : maRangeX(rTuple.getX()),
73 : : maRangeY(rTuple.getY())
74 : : {
75 : : }
76 : :
77 : : /// Create proper interval between the two given double pairs
78 : 1050 : B2DRange(double x1,
79 : : double y1,
80 : : double x2,
81 : : double y2)
82 : : : maRangeX(x1),
83 : 1050 : maRangeY(y1)
84 : : {
85 : 1050 : maRangeX.expand(x2);
86 : 1050 : maRangeY.expand(y2);
87 : 1050 : }
88 : :
89 : : /// Create proper interval between the two given points
90 : 125928 : B2DRange(const B2DTuple& rTuple1,
91 : : const B2DTuple& rTuple2)
92 : : : maRangeX(rTuple1.getX()),
93 : 125928 : maRangeY(rTuple1.getY())
94 : : {
95 : 125928 : expand( rTuple2 );
96 : 125928 : }
97 : :
98 : : BASEGFX_DLLPUBLIC explicit B2DRange(const B2IRange& rRange);
99 : :
100 : : /** Check if the interval set is empty
101 : :
102 : : @return false, if no value is in this set - having a
103 : : single point included will already return true.
104 : : */
105 : 258983 : bool isEmpty() const
106 : : {
107 : : return (
108 : 258983 : maRangeX.isEmpty()
109 : 258983 : || maRangeY.isEmpty()
110 [ + - - + ]: 517966 : );
111 : : }
112 : :
113 : : /// reset the object to empty state again, clearing all values
114 : 250079 : void reset()
115 : : {
116 : 250079 : maRangeX.reset();
117 : 250079 : maRangeY.reset();
118 : 250079 : }
119 : :
120 : 0 : bool operator==( const B2DRange& rRange ) const
121 : : {
122 : 0 : return (maRangeX == rRange.maRangeX
123 [ # # ][ # # ]: 0 : && maRangeY == rRange.maRangeY);
124 : : }
125 : :
126 : : bool operator!=( const B2DRange& rRange ) const
127 : : {
128 : : return (maRangeX != rRange.maRangeX
129 : : || maRangeY != rRange.maRangeY);
130 : : }
131 : :
132 : : bool equal(const B2DRange& rRange) const
133 : : {
134 : : return (maRangeX.equal(rRange.maRangeX)
135 : : && maRangeY.equal(rRange.maRangeY));
136 : : }
137 : :
138 : : /// get lower bound of the set. returns arbitrary values for empty sets.
139 : 2567097 : double getMinX() const
140 : : {
141 : 2567097 : return maRangeX.getMinimum();
142 : : }
143 : :
144 : : /// get lower bound of the set. returns arbitrary values for empty sets.
145 : 2568262 : double getMinY() const
146 : : {
147 : 2568262 : return maRangeY.getMinimum();
148 : : }
149 : :
150 : : /// get upper bound of the set. returns arbitrary values for empty sets.
151 : 2564397 : double getMaxX() const
152 : : {
153 : 2564397 : return maRangeX.getMaximum();
154 : : }
155 : :
156 : : /// get upper bound of the set. returns arbitrary values for empty sets.
157 : 2589473 : double getMaxY() const
158 : : {
159 : 2589473 : return maRangeY.getMaximum();
160 : : }
161 : :
162 : : /// return difference between upper and lower X value. returns 0 for empty sets.
163 : 1690 : double getWidth() const
164 : : {
165 : 1690 : return maRangeX.getRange();
166 : : }
167 : :
168 : : /// return difference between upper and lower Y value. returns 0 for empty sets.
169 : 2730 : double getHeight() const
170 : : {
171 : 2730 : return maRangeY.getRange();
172 : : }
173 : :
174 : : /// get lower bound of the set. returns arbitrary values for empty sets.
175 : 1060 : B2DPoint getMinimum() const
176 : : {
177 : : return B2DPoint(
178 : : maRangeX.getMinimum(),
179 : : maRangeY.getMinimum()
180 : 1060 : );
181 : : }
182 : :
183 : : /// get upper bound of the set. returns arbitrary values for empty sets.
184 : 1060 : B2DPoint getMaximum() const
185 : : {
186 : : return B2DPoint(
187 : : maRangeX.getMaximum(),
188 : : maRangeY.getMaximum()
189 : 1060 : );
190 : : }
191 : :
192 : : /// return difference between upper and lower point. returns (0,0) for empty sets.
193 : 15 : B2DVector getRange() const
194 : : {
195 : : return B2DVector(
196 : : maRangeX.getRange(),
197 : : maRangeY.getRange()
198 : 15 : );
199 : : }
200 : :
201 : : /// return center point of set. returns (0,0) for empty sets.
202 : 23941 : B2DPoint getCenter() const
203 : : {
204 : : return B2DPoint(
205 : : maRangeX.getCenter(),
206 : : maRangeY.getCenter()
207 : 23941 : );
208 : : }
209 : :
210 : : /// return center X value of set. returns 0 for empty sets.
211 : : double getCenterX() const
212 : : {
213 : : return maRangeX.getCenter();
214 : : }
215 : :
216 : : /// return center Y value of set. returns 0 for empty sets.
217 : : double getCenterY() const
218 : : {
219 : : return maRangeY.getCenter();
220 : : }
221 : :
222 : : /// yields true if given point is contained in set
223 : 120189 : bool isInside(const B2DTuple& rTuple) const
224 : : {
225 : : return (
226 : 120189 : maRangeX.isInside(rTuple.getX())
227 : 52528 : && maRangeY.isInside(rTuple.getY())
228 [ + + + + ]: 172717 : );
229 : : }
230 : :
231 : : /// yields true if rRange is inside, or equal to set
232 : 8265 : bool isInside(const B2DRange& rRange) const
233 : : {
234 : : return (
235 : 8265 : maRangeX.isInside(rRange.maRangeX)
236 : 6761 : && maRangeY.isInside(rRange.maRangeY)
237 [ + + + + ]: 15026 : );
238 : : }
239 : :
240 : : /// yields true if rRange at least partly inside set
241 : 53400 : bool overlaps(const B2DRange& rRange) const
242 : : {
243 : : return (
244 : 53400 : maRangeX.overlaps(rRange.maRangeX)
245 : 19824 : && maRangeY.overlaps(rRange.maRangeY)
246 [ + + + + ]: 73224 : );
247 : : }
248 : :
249 : : /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
250 : 31422 : bool overlapsMore(const B2DRange& rRange) const
251 : : {
252 : : return (
253 : 31422 : maRangeX.overlapsMore(rRange.maRangeX)
254 : 9800 : && maRangeY.overlapsMore(rRange.maRangeY)
255 [ + + + + ]: 41222 : );
256 : : }
257 : :
258 : : /// add point to the set, expanding as necessary
259 : 18596512 : void expand(const B2DTuple& rTuple)
260 : : {
261 : 18596512 : maRangeX.expand(rTuple.getX());
262 : 18596512 : maRangeY.expand(rTuple.getY());
263 : 18596512 : }
264 : :
265 : : /// add rRange to the set, expanding as necessary
266 : 4379777 : void expand(const B2DRange& rRange)
267 : : {
268 : 4379777 : maRangeX.expand(rRange.maRangeX);
269 : 4379777 : maRangeY.expand(rRange.maRangeY);
270 : 4379777 : }
271 : :
272 : : /// calc set intersection
273 : : void intersect(const B2DRange& rRange)
274 : : {
275 : : maRangeX.intersect(rRange.maRangeX);
276 : : maRangeY.intersect(rRange.maRangeY);
277 : : }
278 : :
279 : : /// grow set by fValue on all sides
280 : : void grow(double fValue)
281 : : {
282 : : maRangeX.grow(fValue);
283 : : maRangeY.grow(fValue);
284 : : }
285 : :
286 : : BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix& rMatrix);
287 : :
288 : : private:
289 : : typedef ::basegfx::BasicRange< ValueType, TraitsType > MyBasicRange;
290 : :
291 : : MyBasicRange maRangeX;
292 : : MyBasicRange maRangeY;
293 : : };
294 : :
295 : : /** Round double to nearest integer for 2D range
296 : :
297 : : @return the nearest integer for this range
298 : : */
299 : : BASEGFX_DLLPUBLIC B2IRange fround(const B2DRange& rRange);
300 : :
301 : : /** Compute the set difference of the two given ranges
302 : :
303 : : This method calculates the symmetric difference (aka XOR)
304 : : between the two given ranges, and returning the resulting
305 : : ranges. Thus, the result will contain all areas where one, but
306 : : not both ranges lie.
307 : :
308 : : @param o_rResult
309 : : Result vector. The up to four difference ranges are returned
310 : : within this vector
311 : :
312 : : @param rFirst
313 : : The first range
314 : :
315 : : @param rSecond
316 : : The second range
317 : :
318 : : @return the input vector
319 : : */
320 : : BASEGFX_DLLPUBLIC ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >& o_rResult,
321 : : const B2DRange& rFirst,
322 : : const B2DRange& rSecond );
323 : :
324 : : } // end of namespace basegfx
325 : :
326 : :
327 : : #endif /* _BGFX_RANGE_B2DRANGE_HXX */
328 : :
329 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|