xref: /aoo41x/main/basegfx/source/range/b2xrange.cxx (revision 09dbbe93)
1*09dbbe93SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*09dbbe93SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*09dbbe93SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*09dbbe93SAndrew Rist  * distributed with this work for additional information
6*09dbbe93SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*09dbbe93SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*09dbbe93SAndrew Rist  * "License"); you may not use this file except in compliance
9*09dbbe93SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*09dbbe93SAndrew Rist  *
11*09dbbe93SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*09dbbe93SAndrew Rist  *
13*09dbbe93SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*09dbbe93SAndrew Rist  * software distributed under the License is distributed on an
15*09dbbe93SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*09dbbe93SAndrew Rist  * KIND, either express or implied.  See the License for the
17*09dbbe93SAndrew Rist  * specific language governing permissions and limitations
18*09dbbe93SAndrew Rist  * under the License.
19*09dbbe93SAndrew Rist  *
20*09dbbe93SAndrew Rist  *************************************************************/
21*09dbbe93SAndrew Rist 
22*09dbbe93SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
28cdf0e10cSrcweir #include <basegfx/range/b2irange.hxx>
29cdf0e10cSrcweir #include <basegfx/range/b2ibox.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace basegfx
33cdf0e10cSrcweir {
34cdf0e10cSrcweir     namespace
35cdf0e10cSrcweir     {
36cdf0e10cSrcweir         /** Generic implementation of the difference set computation
37cdf0e10cSrcweir 
38cdf0e10cSrcweir             @tpl RangeType
39cdf0e10cSrcweir             Type to operate on. Must provide ValueType and TraitsType
40cdf0e10cSrcweir             nested types.
41cdf0e10cSrcweir          */
doComputeSetDifference(::std::vector<RangeType> & o_rRanges,const RangeType & a,const RangeType & b)42cdf0e10cSrcweir 		template< class RangeType > void doComputeSetDifference(
43cdf0e10cSrcweir             ::std::vector< RangeType >&	o_rRanges,
44cdf0e10cSrcweir             const RangeType&			a,
45cdf0e10cSrcweir             const RangeType&			b )
46cdf0e10cSrcweir 		{
47cdf0e10cSrcweir             o_rRanges.clear();
48cdf0e10cSrcweir 
49cdf0e10cSrcweir             // special-casing the empty rect case (this will fail most
50cdf0e10cSrcweir             // of the times below, because of the DBL_MIN/MAX special
51cdf0e10cSrcweir             // values denoting emptyness in the rectangle.
52cdf0e10cSrcweir             if( a.isEmpty() )
53cdf0e10cSrcweir             {
54cdf0e10cSrcweir                 o_rRanges.push_back( b );
55cdf0e10cSrcweir                 return;
56cdf0e10cSrcweir             }
57cdf0e10cSrcweir             if( b.isEmpty() )
58cdf0e10cSrcweir             {
59cdf0e10cSrcweir                 o_rRanges.push_back( a );
60cdf0e10cSrcweir                 return;
61cdf0e10cSrcweir             }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 			const typename RangeType::ValueType 					ax(a.getMinX());
64cdf0e10cSrcweir 			const typename RangeType::ValueType 					ay(a.getMinY());
65cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType 	aw(a.getWidth());
66cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType 	ah(a.getHeight());
67cdf0e10cSrcweir 			const typename RangeType::ValueType 					bx(b.getMinX());
68cdf0e10cSrcweir 			const typename RangeType::ValueType 					by(b.getMinY());
69cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	bw(b.getWidth());
70cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	bh(b.getHeight());
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	h0( (by > ay) ? by - ay : 0 );
73cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	h3( (by + bh < ay + ah) ? ay + ah - by - bh : 0 );
74cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType 	w1( (bx > ax) ? bx - ax : 0 );
75cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	w2( (ax + aw > bx + bw) ? ax + aw - bx - bw : 0 );
76cdf0e10cSrcweir 			const typename RangeType::TraitsType::DifferenceType	h12( (h0 + h3 < ah) ? ah - h0 - h3 : 0 );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             // TODO(E2): Use numeric_cast instead of static_cast here,
79cdf0e10cSrcweir             // need range checks!
80cdf0e10cSrcweir 			if (h0 > 0)
81cdf0e10cSrcweir 				o_rRanges.push_back(
82cdf0e10cSrcweir                     RangeType(ax,ay,
83cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ax+aw),
84cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0)) );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 			if (w1 > 0 && h12 > 0)
87cdf0e10cSrcweir 				o_rRanges.push_back(
88cdf0e10cSrcweir                     RangeType(ax,
89cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0),
90cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ax+w1),
91cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0+h12)) );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 			if (w2 > 0 && h12 > 0)
94cdf0e10cSrcweir 				o_rRanges.push_back(
95cdf0e10cSrcweir                     RangeType(static_cast<typename RangeType::ValueType>(bx+bw),
96cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0),
97cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(bx+bw+w2),
98cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0+h12)) );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 			if (h3 > 0)
101cdf0e10cSrcweir 				o_rRanges.push_back(
102cdf0e10cSrcweir                     RangeType(ax,
103cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0+h12),
104cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ax+aw),
105cdf0e10cSrcweir                               static_cast<typename RangeType::ValueType>(ay+h0+h12+h3)) );
106cdf0e10cSrcweir 		}
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
computeSetDifference(::std::vector<B2IRange> & o_rResult,const B2IRange & rFirst,const B2IRange & rSecond)109cdf0e10cSrcweir     ::std::vector< B2IRange >& computeSetDifference( ::std::vector< B2IRange >&	o_rResult,
110cdf0e10cSrcweir                                                      const B2IRange&			rFirst,
111cdf0e10cSrcweir                                                      const B2IRange&			rSecond )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         doComputeSetDifference( o_rResult, rFirst, rSecond );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         return o_rResult;
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
computeSetDifference(::std::vector<B2DRange> & o_rResult,const B2DRange & rFirst,const B2DRange & rSecond)118cdf0e10cSrcweir     ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >&	o_rResult,
119cdf0e10cSrcweir                                                      const B2DRange&			rFirst,
120cdf0e10cSrcweir                                                      const B2DRange&			rSecond )
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         doComputeSetDifference( o_rResult, rFirst, rSecond );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         return o_rResult;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
computeSetDifference(::std::vector<B2IBox> & o_rResult,const B2IBox & rFirst,const B2IBox & rSecond)127cdf0e10cSrcweir     ::std::vector< B2IBox >& computeSetDifference( ::std::vector< B2IBox >&	o_rResult,
128cdf0e10cSrcweir                                                    const B2IBox&			rFirst,
129cdf0e10cSrcweir                                                    const B2IBox&			rSecond )
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         doComputeSetDifference( o_rResult, rFirst, rSecond );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         return o_rResult;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir } // end of namespace basegfx
137cdf0e10cSrcweir 
138cdf0e10cSrcweir // eof
139