1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_BASEGFX_RANGE_B2DCONNECTEDRANGES_HXX
21 #define INCLUDED_BASEGFX_RANGE_B2DCONNECTEDRANGES_HXX
23 #include <osl/diagnose.h>
24 #include <basegfx/range/b2drange.hxx>
32 /** Calculate connected ranges from input ranges.
34 This template constructs a list of connected ranges from the
35 given input ranges. That is, the output will contain a set of
36 ranges, itself containing a number of input ranges, which will
37 be mutually non-intersecting.
45 | | | -------| --------
53 Here, the outer rectangles represent the output
54 ranges. Contained are the input rectangles that comprise these
58 User data to be stored along with the range, to later identify
59 which range went into which connected component. Must be
60 assignable, default- and copy-constructible.
62 template< typename UserData
> class B2DConnectedRanges
65 /// Type of the basic entity (rect + user data)
66 typedef ::std::pair
< B2DRange
, UserData
> ComponentType
;
67 typedef ::std::list
< ComponentType
> ComponentListType
;
69 /// List of (intersecting) components, plus overall bounds
70 struct ConnectedComponents
72 ComponentListType maComponentList
;
73 B2DRange maTotalBounds
;
76 typedef ::std::list
< ConnectedComponents
> ConnectedComponentsType
;
79 /// Create the range calculator
80 B2DConnectedRanges() :
81 maDisjunctAggregatesList(),
86 /** Add an additional range.
88 This method integrates a new range into the connected
89 ranges lists. The method has a worst-case time complexity
90 of O(n^2), with n denoting the number of already added
91 ranges (typically, for well-behaved input, it is O(n)
94 void addRange( const B2DRange
& rRange
,
95 const UserData
& rUserData
)
97 // check whether fast path is possible: if new range is
98 // outside accumulated total range, can add it as a
99 // separate component right away.
100 const bool bNotOutsideEverything(
101 maTotalBounds
.overlaps( rRange
) );
103 // update own global bounds range
104 maTotalBounds
.expand( rRange
);
106 // assemble anything intersecting with rRange into
107 // this new connected component
108 ConnectedComponents aNewConnectedComponent
;
110 // as at least rRange will be a member of
111 // aNewConnectedComponent (will be added below), can
112 // preset the overall bounds here.
113 aNewConnectedComponent
.maTotalBounds
= rRange
;
116 // STAGE 1: Search for intersecting maDisjunctAggregatesList entries
119 // if rRange is empty, it will intersect with no
120 // maDisjunctAggregatesList member. Thus, we can safe us
122 // if rRange is outside all other rectangle, skip here,
124 if( bNotOutsideEverything
&&
127 typename
ConnectedComponentsType::iterator aCurrAggregate
;
128 typename
ConnectedComponentsType::iterator aLastAggregate
;
130 // flag, determining whether we touched one or more of
131 // the maDisjunctAggregatesList entries. _If_ we did,
132 // we have to repeat the intersection process, because
133 // these changes might have generated new
135 bool bSomeAggregatesChanged
;
137 // loop, until bSomeAggregatesChanged stays false
140 // only continue loop if 'intersects' branch below was hit
141 bSomeAggregatesChanged
= false;
143 // iterate over all current members of maDisjunctAggregatesList
144 for( aCurrAggregate
=maDisjunctAggregatesList
.begin(),
145 aLastAggregate
=maDisjunctAggregatesList
.end();
146 aCurrAggregate
!= aLastAggregate
; )
148 // first check if current component's bounds
149 // are empty. This ensures that distinct empty
150 // components are not merged into one
151 // aggregate. As a matter of fact, they have
152 // no position and size.
154 if( !aCurrAggregate
->maTotalBounds
.isEmpty() &&
155 aCurrAggregate
->maTotalBounds
.overlaps(
156 aNewConnectedComponent
.maTotalBounds
) )
158 // union the intersecting
159 // maDisjunctAggregatesList element into
160 // aNewConnectedComponent
162 // calc union bounding box
163 aNewConnectedComponent
.maTotalBounds
.expand( aCurrAggregate
->maTotalBounds
);
165 // extract all aCurrAggregate components
166 // to aNewConnectedComponent
167 aNewConnectedComponent
.maComponentList
.splice(
168 aNewConnectedComponent
.maComponentList
.end(),
169 aCurrAggregate
->maComponentList
);
171 // remove and delete aCurrAggregate entry
172 // from list (we've gutted it's content
173 // above). list::erase() will update our
174 // iterator with the predecessor here.
175 aCurrAggregate
= maDisjunctAggregatesList
.erase( aCurrAggregate
);
177 // at least one aggregate changed, need to rescan everything
178 bSomeAggregatesChanged
= true;
186 while( bSomeAggregatesChanged
);
190 // STAGE 2: Add newly generated connected component list element
193 // add new component to the end of the component list
194 aNewConnectedComponent
.maComponentList
.push_back(
195 ComponentType( rRange
, rUserData
) );
197 // do some consistency checks (aka post conditions)
198 OSL_ENSURE( !aNewConnectedComponent
.maComponentList
.empty(),
199 "B2DConnectedRanges::addRange(): empty aggregate list" );
200 OSL_ENSURE( !aNewConnectedComponent
.maTotalBounds
.isEmpty() ||
201 aNewConnectedComponent
.maComponentList
.size() == 1,
202 "B2DConnectedRanges::addRange(): empty ranges must be solitary");
204 // add aNewConnectedComponent as a new entry to
205 // maDisjunctAggregatesList
206 maDisjunctAggregatesList
.push_back( aNewConnectedComponent
);
209 /** Apply a functor to each of the disjunct component
213 Functor to apply. Must provide an operator( const ConnectedComponents& ).
215 @return a copy of the functor, as applied to all aggregates.
217 template< typename UnaryFunctor
> UnaryFunctor
forEachAggregate( UnaryFunctor aFunctor
) const
219 return ::std::for_each( maDisjunctAggregatesList
.begin(),
220 maDisjunctAggregatesList
.end(),
225 B2DConnectedRanges(const B2DConnectedRanges
&) = delete;
226 B2DConnectedRanges
& operator=( const B2DConnectedRanges
& ) = delete;
228 /** Current list of disjunct sets of connected components
230 Each entry corresponds to one of the top-level rectangles
231 in the drawing above.
233 ConnectedComponentsType maDisjunctAggregatesList
;
235 /** Global bound rect over all added ranges.
237 B2DRange maTotalBounds
;
241 #endif // INCLUDED_BASEGFX_RANGE_B2DCONNECTEDRANGES_HXX
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */