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_B2IBOX_HXX
25 #define _BGFX_RANGE_B2IBOX_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/basicbox.hxx>
32 #include <vector>
33 
34 
35 namespace basegfx
36 {
37 	class B2IBox
38 	{
39 	public:
40         typedef sal_Int32 		ValueType;
41         typedef Int32Traits 	TraitsType;
42 
B2IBox()43 		B2IBox()
44 		{
45 		}
46 
B2IBox(const B2ITuple & rTuple)47 		explicit B2IBox(const B2ITuple& rTuple)
48 		:	maRangeX(rTuple.getX()),
49 			maRangeY(rTuple.getY())
50 		{
51 		}
52 
B2IBox(sal_Int32 x1,sal_Int32 y1,sal_Int32 x2,sal_Int32 y2)53 		B2IBox(sal_Int32 x1,
54                sal_Int32 y1,
55                sal_Int32 x2,
56                sal_Int32 y2) :
57             maRangeX(x1),
58             maRangeY(y1)
59 		{
60 			maRangeX.expand(x2);
61 			maRangeY.expand(y2);
62 		}
63 
B2IBox(const B2ITuple & rTuple1,const B2ITuple & rTuple2)64 		B2IBox(const B2ITuple& rTuple1,
65                const B2ITuple& rTuple2) :
66 			maRangeX(rTuple1.getX()),
67 			maRangeY(rTuple1.getY())
68 		{
69             expand( rTuple2 );
70 		}
71 
B2IBox(const B2IBox & rBox)72 		B2IBox(const B2IBox& rBox) :
73 			maRangeX(rBox.maRangeX),
74 			maRangeY(rBox.maRangeY)
75 		{
76 		}
77 
isEmpty() const78 		bool isEmpty() const
79 		{
80 			return maRangeX.isEmpty() || maRangeY.isEmpty();
81 		}
82 
reset()83 		void reset()
84 		{
85 			maRangeX.reset();
86 			maRangeY.reset();
87 		}
88 
operator ==(const B2IBox & rBox) const89 		bool operator==( const B2IBox& rBox ) const
90 		{
91 			return (maRangeX == rBox.maRangeX
92 				&& maRangeY == rBox.maRangeY);
93 		}
94 
operator !=(const B2IBox & rBox) const95 		bool operator!=( const B2IBox& rBox ) const
96 		{
97 			return (maRangeX != rBox.maRangeX
98 				|| maRangeY != rBox.maRangeY);
99 		}
100 
operator =(const B2IBox & rBox)101 		void operator=(const B2IBox& rBox)
102 		{
103 			maRangeX = rBox.maRangeX;
104 			maRangeY = rBox.maRangeY;
105 		}
106 
getMinX() const107         sal_Int32 getMinX() const
108         {
109             return maRangeX.getMinimum();
110         }
111 
getMinY() const112         sal_Int32 getMinY() const
113         {
114             return maRangeY.getMinimum();
115         }
116 
getMaxX() const117         sal_Int32 getMaxX() const
118         {
119             return maRangeX.getMaximum();
120         }
121 
getMaxY() const122         sal_Int32 getMaxY() const
123         {
124             return maRangeY.getMaximum();
125         }
126 
getWidth() const127         sal_Int64 getWidth() const
128         {
129             return maRangeX.getRange();
130         }
131 
getHeight() const132         sal_Int64 getHeight() const
133         {
134             return maRangeY.getRange();
135         }
136 
getMinimum() const137 		B2IPoint getMinimum() const
138 		{
139 			return B2IPoint(
140 				maRangeX.getMinimum(),
141 				maRangeY.getMinimum()
142 				);
143 		}
144 
getMaximum() const145 		B2IPoint getMaximum() const
146 		{
147 			return B2IPoint(
148 				maRangeX.getMaximum(),
149 				maRangeY.getMaximum()
150 				);
151 		}
152 
getRange() const153 		B2I64Tuple getRange() const
154 		{
155 			return B2I64Tuple(
156 				maRangeX.getRange(),
157 				maRangeY.getRange()
158 				);
159 		}
160 
getCenter() const161 		B2DPoint getCenter() const
162 		{
163 			return B2DPoint(
164 				maRangeX.getCenter(),
165 				maRangeY.getCenter()
166 				);
167 		}
168 
isInside(const B2ITuple & rTuple) const169 		bool isInside(const B2ITuple& rTuple) const
170 		{
171 			return (
172 				maRangeX.isInside(rTuple.getX())
173 				&& maRangeY.isInside(rTuple.getY())
174 				);
175 		}
176 
isInside(const B2IBox & rBox) const177 		bool isInside(const B2IBox& rBox) const
178 		{
179 			return (
180 				maRangeX.isInside(rBox.maRangeX)
181 				&& maRangeY.isInside(rBox.maRangeY)
182 				);
183 		}
184 
overlaps(const B2IBox & rBox) const185 		bool overlaps(const B2IBox& rBox) const
186 		{
187 			return (
188 				maRangeX.overlaps(rBox.maRangeX)
189 				&& maRangeY.overlaps(rBox.maRangeY)
190 				);
191 		}
192 
expand(const B2ITuple & rTuple)193 		void expand(const B2ITuple& rTuple)
194 		{
195 			maRangeX.expand(rTuple.getX());
196 			maRangeY.expand(rTuple.getY());
197 		}
198 
expand(const B2IBox & rBox)199 		void expand(const B2IBox& rBox)
200 		{
201 			maRangeX.expand(rBox.maRangeX);
202 			maRangeY.expand(rBox.maRangeY);
203 		}
204 
intersect(const B2IBox & rBox)205 		void intersect(const B2IBox& rBox)
206 		{
207 			maRangeX.intersect(rBox.maRangeX);
208 			maRangeY.intersect(rBox.maRangeY);
209 		}
210 
grow(sal_Int32 nValue)211 		void grow(sal_Int32 nValue)
212 		{
213 			maRangeX.grow(nValue);
214 			maRangeY.grow(nValue);
215 		}
216 
217     private:
218 		BasicBox		maRangeX;
219 		BasicBox		maRangeY;
220 	};
221 
222     /** Compute the set difference of the two given boxes
223 
224     	This method calculates the symmetric difference (aka XOR)
225     	between the two given boxes, and returning the resulting
226     	boxes. Thus, the result will contain all areas where one, but
227     	not both boxes lie.
228 
229     	@param o_rResult
230         Result vector. The up to four difference boxes are returned
231         within this vector
232 
233         @param rFirst
234         The first box
235 
236         @param rSecond
237         The second box
238 
239         @return the input vector
240      */
241     ::std::vector< B2IBox >& computeSetDifference( ::std::vector< B2IBox >&	o_rResult,
242                                                    const B2IBox&			rFirst,
243                                                    const B2IBox&			rSecond );
244 
245 } // end of namespace basegfx
246 
247 #endif /* _BGFX_RANGE_B2IBOX_HXX */
248