176 {
180
181
182 auto isBig = [bin_area](
double a) {
184 };
185
186
187 auto ibb = item.boundingBox();
188
189
191
192
193
195 if(spatindex.empty()) bigbb = fullbb;
196 else {
197 auto boostbb = spatindex.bounds();
198 boost::geometry::convert(boostbb, bigbb);
199 }
200
201
202 double score = 0;
203
204
205 double density = 0;
206
207
208 enum e_cases {
209
210
211 BIG_ITEM,
212
213
214 LAST_BIG_ITEM,
215
216
217 SMALL_ITEM
218 } compute_case;
219
220 bool bigitems = isBig(item.area()) || spatindex.empty();
221 if(bigitems && !
m_remaining.empty()) compute_case = BIG_ITEM;
222 else if (bigitems &&
m_remaining.empty()) compute_case = LAST_BIG_ITEM;
223 else compute_case = SMALL_ITEM;
224
225 switch (compute_case) {
226 case BIG_ITEM: {
227 const Point& minc = ibb.minCorner();
228 const Point& maxc = ibb.maxCorner();
229
230
233
234
235
236 std::array<double, 5> dists;
237 auto cc = fullbb.center();
243
244
245 double dist =
norm(*(std::min_element(dists.begin(), dists.end())));
248
249
250
251
252
253
254
255 auto alignment_score = 1.0;
256
257 auto query = bgi::intersects(ibb);
258 auto& index = isBig(item.area()) ? spatindex : smalls_spatindex;
259
260
261 std::vector<SpatElement> result;
262 result.reserve(index.size());
263
264 index.query(query, std::back_inserter(result));
265
266
267 for(auto& e : result) {
268 auto idx = e.second;
270 auto parea = p.area();
271 if(std::abs(1.0 - parea/item.area()) < 1e-6) {
273 auto bbarea = bb.area();
274 auto ascore = 1.0 - (item.area() + parea)/bbarea;
275
276 if(ascore < alignment_score) alignment_score = ascore;
277 }
278 }
279
280 density = std::sqrt(
norm(fullbb.width()) *
norm(fullbb.height()));
282
283
284
285
286 if (result.empty())
287 score = 0.50 *
dist + 0.50 * density;
288 else
289
290 score = 0.50 *
dist + (1.0 - R) * 0.20 * density +
291 0.30 * alignment_score;
292
293 break;
294 }
295 case LAST_BIG_ITEM: {
297 break;
298 }
299 case SMALL_ITEM: {
300
301
302
303
305 break;
306 }
307 }
308
309 return std::make_tuple(score, fullbb);
310 }
size_t m_item_count
Definition Arrange.cpp:162
ArithmeticOnly< T, double > norm(T val)
Definition Arrange.cpp:164
P center() const BP2D_NOEXCEPT
Definition geometry_traits.hpp:499
T dist(const boost::polygon::point_data< T > &p1, const boost::polygon::point_data< T > &p2)
Definition Geometry.cpp:280
bgi::rtree< SpatElement, bgi::rstar< 16, 4 > > SpatIndex
Definition Arrange.cpp:76
double distance(const P &p1, const P &p2)
Definition geometry_traits.hpp:329
TCoord< P > getX(const P &p)
Definition geometry_traits.hpp:424
_Box< PointImpl > Box
Definition libnest2d.hpp:31
TCoord< P > getY(const P &p)
Definition geometry_traits.hpp:427
Kernel::Point_2 Point
Definition point_areas.cpp:20