Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
libnest2d::selections::_FirstFitSelection< RawShape > Class Template Reference

#include <src/libnest2d/include/libnest2d/selections/firstfit.hpp>

+ Inheritance diagram for libnest2d::selections::_FirstFitSelection< RawShape >:
+ Collaboration diagram for libnest2d::selections::_FirstFitSelection< RawShape >:

Public Types

using Config = int
 
using Item = _Item< RawShape >
 
using ShapeType = RawShape
 
using PackGroup = _PackGroup< RawShape >
 

Public Member Functions

void configure (const Config &)
 
template<class TPlacer , class TIterator , class TBin = typename PlacementStrategyLike<TPlacer>::BinType, class PConfig = typename PlacementStrategyLike<TPlacer>::Config>
void packItems (TIterator first, TIterator last, TBin &&bin, PConfig &&pconfig=PConfig())
 
const PackGroupgetResult () const
 
int lastPackedBinId () const
 
void progressIndicator (ProgressFunction fn)
 
void stopCondition (StopCondition cond)
 
void clear ()
 

Protected Member Functions

template<class Placer , class Container , class Bin , class PCfg >
void remove_unpackable_items (Container &c, const Bin &bin, const PCfg &pcfg)
 

Protected Attributes

ProgressFunction progress_ = [](unsigned){}
 
StopCondition stopcond_ = [](){ return false; }
 
int last_packed_bin_id_ = -1
 

Private Types

using Base = SelectionBoilerplate< RawShape >
 
using Container = ItemGroup
 
using ItemGroup = _ItemGroup< RawShape >
 

Private Attributes

Container store_
 
PackGroup packed_bins_
 

Detailed Description

template<class RawShape>
class libnest2d::selections::_FirstFitSelection< RawShape >

Member Typedef Documentation

◆ Base

template<class RawShape >
using libnest2d::selections::_FirstFitSelection< RawShape >::Base = SelectionBoilerplate<RawShape>
private

◆ Config

template<class RawShape >
using libnest2d::selections::_FirstFitSelection< RawShape >::Config = int

◆ Container

template<class RawShape >
using libnest2d::selections::_FirstFitSelection< RawShape >::Container = ItemGroup
private

◆ Item

template<class RawShape >
using libnest2d::selections::SelectionBoilerplate< RawShape >::Item = _Item<RawShape>

◆ ItemGroup

template<class RawShape >
using libnest2d::selections::SelectionBoilerplate< RawShape >::ItemGroup = _ItemGroup<RawShape>
private

◆ PackGroup

template<class RawShape >
using libnest2d::selections::SelectionBoilerplate< RawShape >::PackGroup = _PackGroup<RawShape>
inherited

◆ ShapeType

template<class RawShape >
using libnest2d::selections::SelectionBoilerplate< RawShape >::ShapeType = RawShape
inherited

Member Function Documentation

◆ clear()

template<class RawShape >
void libnest2d::selections::SelectionBoilerplate< RawShape >::clear ( )
inlineinherited
27{ packed_bins_.clear(); }
PackGroup packed_bins_
Definition selection_boilerplate.hpp:56

References libnest2d::selections::SelectionBoilerplate< RawShape >::packed_bins_.

◆ configure()

template<class RawShape >
void libnest2d::selections::_FirstFitSelection< RawShape >::configure ( const Config )
inline
24{ }

◆ getResult()

template<class RawShape >
const PackGroup & libnest2d::selections::SelectionBoilerplate< RawShape >::getResult ( ) const
inlineinherited

◆ lastPackedBinId()

template<class RawShape >
int libnest2d::selections::SelectionBoilerplate< RawShape >::lastPackedBinId ( ) const
inlineinherited

◆ packItems()

template<class RawShape >
template<class TPlacer , class TIterator , class TBin = typename PlacementStrategyLike<TPlacer>::BinType, class PConfig = typename PlacementStrategyLike<TPlacer>::Config>
void libnest2d::selections::_FirstFitSelection< RawShape >::packItems ( TIterator  first,
TIterator  last,
TBin &&  bin,
PConfig &&  pconfig = PConfig() 
)
inline
33 {
34
35 using Placer = PlacementStrategyLike<TPlacer>;
36
37 store_.clear();
38 store_.reserve(last-first);
39
40 std::vector<Placer> placers;
41 placers.reserve(last-first);
42
43 std::for_each(first, last, [this](Item& itm) {
44 if(itm.isFixed()) {
45 if (itm.binId() < 0) itm.binId(0);
46 auto binidx = size_t(itm.binId());
47
48 while(packed_bins_.size() <= binidx)
49 packed_bins_.emplace_back();
50
51 packed_bins_[binidx].emplace_back(itm);
52 } else {
53 store_.emplace_back(itm);
54 }
55 });
56
57 // If the packed_items array is not empty we have to create as many
58 // placers as there are elements in packed bins and preload each item
59 // into the appropriate placer
60 for(ItemGroup& ig : packed_bins_) {
61 placers.emplace_back(bin);
62 placers.back().configure(pconfig);
63 placers.back().preload(ig);
64 }
65
66 auto sortfunc = [](Item& i1, Item& i2) {
67 int p1 = i1.priority(), p2 = i2.priority();
68 return p1 == p2 ? i1.area() > i2.area() : p1 > p2;
69 };
70
71 std::sort(store_.begin(), store_.end(), sortfunc);
72
73 auto total = last-first;
74 auto makeProgress = [this, &total](Placer& placer, size_t bin_idx) {
75 packed_bins_[bin_idx] = placer.getItems();
76 this->last_packed_bin_id_ = int(bin_idx);
77 this->progress_(static_cast<unsigned>(--total));
78 };
79
80 auto& cancelled = this->stopcond_;
81
82 this->template remove_unpackable_items<Placer>(store_, bin, pconfig);
83
84 auto it = store_.begin();
85
86 while(it != store_.end() && !cancelled()) {
87 bool was_packed = false;
88 size_t j = 0;
89 while(!was_packed && !cancelled()) {
90 for(; j < placers.size() && !was_packed && !cancelled(); j++) {
91 if((was_packed = placers[j].pack(*it, rem(it, store_) ))) {
92 it->get().binId(int(j));
93 makeProgress(placers[j], j);
94 }
95 }
96
97 if(!was_packed) {
98 placers.emplace_back(bin);
99 placers.back().configure(pconfig);
100 packed_bins_.emplace_back();
101 j = placers.size() - 1;
102 }
103 }
104 ++it;
105 }
106 }
_ItemGroup< RawShape > ItemGroup
Definition selection_boilerplate.hpp:14
_Item< RawShape > Item
Definition selection_boilerplate.hpp:13
PackGroup packed_bins_
Definition selection_boilerplate.hpp:56
Container store_
Definition firstfit.hpp:20
StopCondition stopcond_
Definition selection_boilerplate.hpp:58
ProgressFunction progress_
Definition selection_boilerplate.hpp:57
ConstItemRange< typename Container::const_iterator > rem(typename Container::const_iterator it, const Container &cont)
Definition nester.hpp:534

References libnest2d::_Item< RawShape >::area(), libnest2d::_Item< RawShape >::isFixed(), libnest2d::selections::SelectionBoilerplate< RawShape >::last_packed_bin_id_, libnest2d::selections::_FirstFitSelection< RawShape >::packed_bins_, libnest2d::_Item< RawShape >::priority(), libnest2d::selections::SelectionBoilerplate< RawShape >::progress_, libnest2d::rem(), libnest2d::selections::SelectionBoilerplate< RawShape >::stopcond_, and libnest2d::selections::_FirstFitSelection< RawShape >::store_.

+ Here is the call graph for this function:

◆ progressIndicator()

template<class RawShape >
void libnest2d::selections::SelectionBoilerplate< RawShape >::progressIndicator ( ProgressFunction  fn)
inlineinherited

◆ remove_unpackable_items()

template<class RawShape >
template<class Placer , class Container , class Bin , class PCfg >
void libnest2d::selections::SelectionBoilerplate< RawShape >::remove_unpackable_items ( Container &  c,
const Bin &  bin,
const PCfg &  pcfg 
)
inlineprotectedinherited
33 {
34 // Safety test: try to pack each item into an empty bin. If it fails
35 // then it should be removed from the list
36 auto it = c.begin();
37 while (it != c.end() && !stopcond_()) {
38
39 // WARNING: The copy of itm needs to be created before Placer.
40 // Placer is working with references and its destructor still
41 // manipulates the item this is why the order of stack creation
42 // matters here.
43 const Item& itm = *it;
44 Item cpy{itm};
45
46 Placer p{bin};
47 p.configure(pcfg);
48 if (itm.area() <= 0 || !p.pack(cpy)) {
49 static_cast<Item&>(*it).binId(BIN_ID_UNSET);
50 it = c.erase(it);
51 }
52 else it++;
53 }
54 }
_Item< RawShape > Item
Definition selection_boilerplate.hpp:13
static const constexpr int BIN_ID_UNSET
Definition nester.hpp:15

References libnest2d::_Item< RawShape >::area(), libnest2d::BIN_ID_UNSET, libnest2d::_Item< RawShape >::binId(), and libnest2d::selections::SelectionBoilerplate< RawShape >::stopcond_.

+ Here is the call graph for this function:

◆ stopCondition()

template<class RawShape >
void libnest2d::selections::SelectionBoilerplate< RawShape >::stopCondition ( StopCondition  cond)
inlineinherited

Member Data Documentation

◆ last_packed_bin_id_

◆ packed_bins_

template<class RawShape >
PackGroup libnest2d::selections::SelectionBoilerplate< RawShape >::packed_bins_
private

◆ progress_

◆ stopcond_

◆ store_


The documentation for this class was generated from the following file: