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_B2IRANGE_HXX
25 #define _BGFX_RANGE_B2IRANGE_HXX
26 
27 #include <basegfx/point/b2ipoint.hxx>
28 #include <basegfx/point/b2dpoint.hxx>
29 #include <basegfx/tuple/b2ituple.hxx>
30 #include <basegfx/tuple/b2i64tuple.hxx>
31 #include <basegfx/range/basicrange.hxx>
32 #include <vector>
33 #include <basegfx/basegfxdllapi.h>
34 
35 
36 namespace basegfx
37 {
38 	class BASEGFX_DLLPUBLIC B2IRange
39 	{
40 	public:
41         typedef sal_Int32 		ValueType;
42         typedef Int32Traits 	TraitsType;
43 
B2IRange()44 		B2IRange()
45 		{
46 		}
47 
B2IRange(const B2ITuple & rTuple)48 		explicit B2IRange(const B2ITuple& rTuple)
49 		:	maRangeX(rTuple.getX()),
50 			maRangeY(rTuple.getY())
51 		{
52 		}
53 
B2IRange(sal_Int32 x1,sal_Int32 y1,sal_Int32 x2,sal_Int32 y2)54 		B2IRange(sal_Int32 x1,
55                  sal_Int32 y1,
56                  sal_Int32 x2,
57                  sal_Int32 y2)
58 		:	maRangeX(x1),
59 			maRangeY(y1)
60 		{
61 			maRangeX.expand(x2);
62 			maRangeY.expand(y2);
63 		}
64 
B2IRange(const B2ITuple & rTuple1,const B2ITuple & rTuple2)65 		B2IRange(const B2ITuple& rTuple1,
66                  const B2ITuple& rTuple2)
67 		:	maRangeX(rTuple1.getX()),
68 			maRangeY(rTuple1.getY())
69 		{
70             expand( rTuple2 );
71 		}
72 
B2IRange(const B2IRange & rRange)73 		B2IRange(const B2IRange& rRange)
74 		:	maRangeX(rRange.maRangeX),
75 			maRangeY(rRange.maRangeY)
76 		{
77 		}
78 
isEmpty() const79 		bool isEmpty() const
80 		{
81 			return maRangeX.isEmpty() || maRangeY.isEmpty();
82 		}
83 
reset()84 		void reset()
85 		{
86 			maRangeX.reset();
87 			maRangeY.reset();
88 		}
89 
operator ==(const B2IRange & rRange) const90 		bool operator==( const B2IRange& rRange ) const
91 		{
92 			return (maRangeX == rRange.maRangeX
93 				&& maRangeY == rRange.maRangeY);
94 		}
95 
operator !=(const B2IRange & rRange) const96 		bool operator!=( const B2IRange& rRange ) const
97 		{
98 			return (maRangeX != rRange.maRangeX
99 				|| maRangeY != rRange.maRangeY);
100 		}
101 
operator =(const B2IRange & rRange)102 		B2IRange& operator=(const B2IRange& rRange)
103 		{
104 			maRangeX = rRange.maRangeX;
105 			maRangeY = rRange.maRangeY;
106 			return *this;
107 		}
108 
getMinX() const109         sal_Int32 getMinX() const
110         {
111             return maRangeX.getMinimum();
112         }
113 
getMinY() const114         sal_Int32 getMinY() const
115         {
116             return maRangeY.getMinimum();
117         }
118 
getMaxX() const119         sal_Int32 getMaxX() const
120         {
121             return maRangeX.getMaximum();
122         }
123 
getMaxY() const124         sal_Int32 getMaxY() const
125         {
126             return maRangeY.getMaximum();
127         }
128 
getWidth() const129         sal_Int64 getWidth() const
130         {
131             return maRangeX.getRange();
132         }
133 
getHeight() const134         sal_Int64 getHeight() const
135         {
136             return maRangeY.getRange();
137         }
138 
getMinimum() const139 		B2IPoint getMinimum() const
140 		{
141 			return B2IPoint(
142 				maRangeX.getMinimum(),
143 				maRangeY.getMinimum()
144 				);
145 		}
146 
getMaximum() const147 		B2IPoint getMaximum() const
148 		{
149 			return B2IPoint(
150 				maRangeX.getMaximum(),
151 				maRangeY.getMaximum()
152 				);
153 		}
154 
getRange() const155 		B2I64Tuple getRange() const
156 		{
157 			return B2I64Tuple(
158 				maRangeX.getRange(),
159 				maRangeY.getRange()
160 				);
161 		}
162 
getCenter() const163 		B2DPoint getCenter() const
164 		{
165 			return B2DPoint(
166 				maRangeX.getCenter(),
167 				maRangeY.getCenter()
168 				);
169 		}
170 
isInside(const B2ITuple & rTuple) const171 		bool isInside(const B2ITuple& rTuple) const
172 		{
173 			return (
174 				maRangeX.isInside(rTuple.getX())
175 				&& maRangeY.isInside(rTuple.getY())
176 				);
177 		}
178 
isInside(const B2IRange & rRange) const179 		bool isInside(const B2IRange& rRange) const
180 		{
181 			return (
182 				maRangeX.isInside(rRange.maRangeX)
183 				&& maRangeY.isInside(rRange.maRangeY)
184 				);
185 		}
186 
overlaps(const B2IRange & rRange) const187 		bool overlaps(const B2IRange& rRange) const
188 		{
189 			return (
190 				maRangeX.overlaps(rRange.maRangeX)
191 				&& maRangeY.overlaps(rRange.maRangeY)
192 				);
193 		}
194 
expand(const B2ITuple & rTuple)195 		void expand(const B2ITuple& rTuple)
196 		{
197 			maRangeX.expand(rTuple.getX());
198 			maRangeY.expand(rTuple.getY());
199 		}
200 
expand(const B2IRange & rRange)201 		void expand(const B2IRange& rRange)
202 		{
203 			maRangeX.expand(rRange.maRangeX);
204 			maRangeY.expand(rRange.maRangeY);
205 		}
206 
intersect(const B2IRange & rRange)207 		void intersect(const B2IRange& rRange)
208 		{
209 			maRangeX.intersect(rRange.maRangeX);
210 			maRangeY.intersect(rRange.maRangeY);
211 		}
212 
grow(sal_Int32 nValue)213 		void grow(sal_Int32 nValue)
214 		{
215 			maRangeX.grow(nValue);
216 			maRangeY.grow(nValue);
217 		}
218 
219     private:
220         typedef ::basegfx::BasicRange< ValueType, TraitsType >	MyBasicRange;
221 
222 		MyBasicRange		maRangeX;
223 		MyBasicRange		maRangeY;
224 	};
225 
226     /** Compute the set difference of the two given ranges
227 
228     	This method calculates the symmetric difference (aka XOR)
229     	between the two given ranges, and returning the resulting
230     	ranges. Thus, the result will contain all areas where one, but
231     	not both ranges lie.
232 
233     	@param o_rResult
234         Result vector. The up to four difference ranges are returned
235         within this vector
236 
237         @param rFirst
238         The first range
239 
240         @param rSecond
241         The second range
242 
243         @return the input vector
244      */
245     BASEGFX_DLLPUBLIC ::std::vector< B2IRange >& computeSetDifference( ::std::vector< B2IRange >&	o_rResult,
246                                                      const B2IRange&			rFirst,
247                                                      const B2IRange&			rSecond );
248 
249 } // end of namespace basegfx
250 
251 #endif /* _BGFX_RANGE_B2IRANGE_HXX */
252