xref: /trunk/main/basegfx/inc/basegfx/range/b2drange.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_RANGE_B2DRANGE_HXX
25 #define _BGFX_RANGE_B2DRANGE_HXX
26 
27 #include <basegfx/vector/b2dvector.hxx>
28 #include <basegfx/point/b2dpoint.hxx>
29 #include <basegfx/tuple/b2dtuple.hxx>
30 #include <basegfx/range/basicrange.hxx>
31 #include <vector>
32 #include <basegfx/basegfxdllapi.h>
33 
34 
35 namespace basegfx
36 {
37     // predeclarations
38     class B2IRange;
39     class B2DHomMatrix;
40 
41     class B2DRange
42     {
43     public:
44         typedef double          ValueType;
45         typedef DoubleTraits    TraitsType;
46 
B2DRange()47         B2DRange()
48         {
49         }
50 
B2DRange(const B2DTuple & rTuple)51         explicit B2DRange(const B2DTuple& rTuple)
52         :   maRangeX(rTuple.getX()),
53             maRangeY(rTuple.getY())
54         {
55         }
56 
B2DRange(double x1,double y1,double x2,double y2)57         B2DRange(double x1,
58                  double y1,
59                  double x2,
60                  double y2)
61         :   maRangeX(x1),
62             maRangeY(y1)
63         {
64             maRangeX.expand(x2);
65             maRangeY.expand(y2);
66         }
67 
B2DRange(const B2DTuple & rTuple1,const B2DTuple & rTuple2)68         B2DRange(const B2DTuple& rTuple1,
69                  const B2DTuple& rTuple2)
70         :   maRangeX(rTuple1.getX()),
71             maRangeY(rTuple1.getY())
72         {
73             expand( rTuple2 );
74         }
75 
B2DRange(const B2DRange & rRange)76         B2DRange(const B2DRange& rRange)
77         :   maRangeX(rRange.maRangeX),
78             maRangeY(rRange.maRangeY)
79         {
80         }
81 
82         BASEGFX_DLLPUBLIC explicit B2DRange(const B2IRange& rRange);
83 
isEmpty() const84         bool isEmpty() const
85         {
86             return (
87                 maRangeX.isEmpty()
88                 || maRangeY.isEmpty()
89                 );
90         }
91 
reset()92         void reset()
93         {
94             maRangeX.reset();
95             maRangeY.reset();
96         }
97 
operator ==(const B2DRange & rRange) const98         bool operator==( const B2DRange& rRange ) const
99         {
100             return (maRangeX == rRange.maRangeX
101                 && maRangeY == rRange.maRangeY);
102         }
103 
operator !=(const B2DRange & rRange) const104         bool operator!=( const B2DRange& rRange ) const
105         {
106             return (maRangeX != rRange.maRangeX
107                 || maRangeY != rRange.maRangeY);
108         }
109 
operator =(const B2DRange & rRange)110         B2DRange& operator=(const B2DRange& rRange)
111         {
112             maRangeX = rRange.maRangeX;
113             maRangeY = rRange.maRangeY;
114             return *this;
115         }
116 
equal(const B2DRange & rRange) const117         bool equal(const B2DRange& rRange) const
118         {
119             return (maRangeX.equal(rRange.maRangeX)
120                     && maRangeY.equal(rRange.maRangeY));
121         }
122 
getMinX() const123         double getMinX() const
124         {
125             return maRangeX.getMinimum();
126         }
127 
getMinY() const128         double getMinY() const
129         {
130             return maRangeY.getMinimum();
131         }
132 
getMaxX() const133         double getMaxX() const
134         {
135             return maRangeX.getMaximum();
136         }
137 
getMaxY() const138         double getMaxY() const
139         {
140             return maRangeY.getMaximum();
141         }
142 
getWidth() const143         double getWidth() const
144         {
145             return maRangeX.getRange();
146         }
147 
getHeight() const148         double getHeight() const
149         {
150             return maRangeY.getRange();
151         }
152 
getMinimum() const153         B2DPoint getMinimum() const
154         {
155             return B2DPoint(
156                 maRangeX.getMinimum(),
157                 maRangeY.getMinimum()
158                 );
159         }
160 
getMaximum() const161         B2DPoint getMaximum() const
162         {
163             return B2DPoint(
164                 maRangeX.getMaximum(),
165                 maRangeY.getMaximum()
166                 );
167         }
168 
getRange() const169         B2DVector getRange() const
170         {
171             return B2DVector(
172                 maRangeX.getRange(),
173                 maRangeY.getRange()
174                 );
175         }
176 
getCenter() const177         B2DPoint getCenter() const
178         {
179             return B2DPoint(
180                 maRangeX.getCenter(),
181                 maRangeY.getCenter()
182                 );
183         }
184 
getCenterX() const185         double getCenterX() const
186         {
187             return maRangeX.getCenter();
188         }
189 
getCenterY() const190         double getCenterY() const
191         {
192             return maRangeY.getCenter();
193         }
194 
isInside(const B2DTuple & rTuple) const195         bool isInside(const B2DTuple& rTuple) const
196         {
197             return (
198                 maRangeX.isInside(rTuple.getX())
199                 && maRangeY.isInside(rTuple.getY())
200                 );
201         }
202 
isInside(const B2DRange & rRange) const203         bool isInside(const B2DRange& rRange) const
204         {
205             return (
206                 maRangeX.isInside(rRange.maRangeX)
207                 && maRangeY.isInside(rRange.maRangeY)
208                 );
209         }
210 
overlaps(const B2DRange & rRange) const211         bool overlaps(const B2DRange& rRange) const
212         {
213             return (
214                 maRangeX.overlaps(rRange.maRangeX)
215                 && maRangeY.overlaps(rRange.maRangeY)
216                 );
217         }
218 
overlapsMore(const B2DRange & rRange) const219         bool overlapsMore(const B2DRange& rRange) const
220         {
221             return (
222                 maRangeX.overlapsMore(rRange.maRangeX)
223                 && maRangeY.overlapsMore(rRange.maRangeY)
224                 );
225         }
226 
expand(const B2DTuple & rTuple)227         void expand(const B2DTuple& rTuple)
228         {
229             maRangeX.expand(rTuple.getX());
230             maRangeY.expand(rTuple.getY());
231         }
232 
expand(const B2DRange & rRange)233         void expand(const B2DRange& rRange)
234         {
235             maRangeX.expand(rRange.maRangeX);
236             maRangeY.expand(rRange.maRangeY);
237         }
238 
intersect(const B2DRange & rRange)239         void intersect(const B2DRange& rRange)
240         {
241             maRangeX.intersect(rRange.maRangeX);
242             maRangeY.intersect(rRange.maRangeY);
243         }
244 
grow(double fValue)245         void grow(double fValue)
246         {
247             maRangeX.grow(fValue);
248             maRangeY.grow(fValue);
249         }
250 
251         BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix& rMatrix);
252 
253     private:
254         typedef ::basegfx::BasicRange< ValueType, TraitsType >  MyBasicRange;
255 
256         MyBasicRange        maRangeX;
257         MyBasicRange        maRangeY;
258     };
259 
260     /** Round double to nearest integer for 2D range
261 
262         @return the nearest integer for this range
263     */
264     BASEGFX_DLLPUBLIC B2IRange fround(const B2DRange& rRange);
265 
266     /** Compute the set difference of the two given ranges
267 
268         This method calculates the symmetric difference (aka XOR)
269         between the two given ranges, and returning the resulting
270         ranges. Thus, the result will contain all areas where one, but
271         not both ranges lie.
272 
273         @param o_rResult
274         Result vector. The up to four difference ranges are returned
275         within this vector
276 
277         @param rFirst
278         The first range
279 
280         @param rSecond
281         The second range
282 
283         @return the input vector
284      */
285     BASEGFX_DLLPUBLIC ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >&   o_rResult,
286                                                      const B2DRange&            rFirst,
287                                                      const B2DRange&            rSecond );
288 
289 } // end of namespace basegfx
290 
291 
292 #endif /* _BGFX_RANGE_B2DRANGE_HXX */
293