1 #ifndef FILLER_HPP
2 #define FILLER_HPP
3 
4 #include "selection_boilerplate.hpp"
5 
6 namespace libnest2d { namespace selections {
7 
8 template<class RawShape>
9 class _FillerSelection: public SelectionBoilerplate<RawShape> {
10     using Base = SelectionBoilerplate<RawShape>;
11 public:
12     using typename Base::Item;
13     using Config = int; //dummy
14 
15 private:
16     using Base::packed_bins_;
17     using typename Base::ItemGroup;
18     using Container = ItemGroup;
19     Container store_;
20 
21 public:
22 
configure(const Config &)23     void configure(const Config& /*config*/) { }
24 
25     template<class TPlacer, class TIterator,
26              class TBin = typename PlacementStrategyLike<TPlacer>::BinType,
27              class PConfig = typename PlacementStrategyLike<TPlacer>::Config>
packItems(TIterator first,TIterator last,TBin && bin,PConfig && pconfig=PConfig ())28     void packItems(TIterator first,
29                    TIterator last,
30                    TBin&& bin,
31                    PConfig&& pconfig = PConfig())
32     {
33         using Placer = PlacementStrategyLike<TPlacer>;
34 
35         store_.clear();
36         auto total = last-first;
37         store_.reserve(total);
38 
39         // TODO: support preloading
40         packed_bins_.clear();
41 
42         packed_bins_.emplace_back();
43 
44         auto makeProgress = [this, &total](
45                 PlacementStrategyLike<TPlacer>& placer)
46         {
47             packed_bins_.back() = placer.getItems();
48 #ifndef NDEBUG
49             packed_bins_.back().insert(packed_bins_.back().end(),
50                                        placer.getDebugItems().begin(),
51                                        placer.getDebugItems().end());
52 #endif
53             this->progress_(--total);
54         };
55 
56         std::copy(first, last, std::back_inserter(store_));
57 
58         auto sortfunc = [](Item& i1, Item& i2) {
59             return i1.area() > i2.area();
60         };
61 
62         this->template remove_unpackable_items<Placer>(store_, bin, pconfig);
63 
64         std::sort(store_.begin(), store_.end(), sortfunc);
65 
66         Placer placer(bin);
67         placer.configure(pconfig);
68 
69         auto it = store_.begin();
70         while(it != store_.end() && !this->stopcond_()) {
71             if(!placer.pack(*it, {std::next(it), store_.end()}))  {
72                 if(packed_bins_.back().empty()) ++it;
73                 placer.clearItems();
74                 packed_bins_.emplace_back();
75             } else {
76                 makeProgress(placer);
77                 ++it;
78             }
79         }
80 
81     }
82 };
83 
84 }
85 }
86 
87 #endif //BOTTOMLEFT_HPP
88