mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 20:21:12 -06:00 
			
		
		
		
	Unify AutoArranger subclasses
This commit is contained in:
		
							parent
							
								
									ba82cbe007
								
							
						
					
					
						commit
						914bf63228
					
				
					 2 changed files with 301 additions and 268 deletions
				
			
		|  | @ -774,14 +774,14 @@ public: | |||
|     using BinType = typename TPlacer::BinType; | ||||
|     using PlacementConfig = typename TPlacer::Config; | ||||
|     using SelectionConfig = typename TSel::Config; | ||||
|     using Unit = TCoord<TPoint<typename Item::ShapeType>>; | ||||
|     using Coord = TCoord<TPoint<typename Item::ShapeType>>; | ||||
|     using PackGroup = _PackGroup<typename Item::ShapeType>; | ||||
|     using ResultType = PackGroup; | ||||
| 
 | ||||
| private: | ||||
|     BinType bin_; | ||||
|     PlacementConfig pconfig_; | ||||
|     Unit min_obj_distance_; | ||||
|     Coord min_obj_distance_; | ||||
| 
 | ||||
|     using SItem =  typename SelectionStrategy::Item; | ||||
|     using TPItem = remove_cvref_t<Item>; | ||||
|  | @ -802,7 +802,7 @@ public: | |||
|              class PConf = PlacementConfig, | ||||
|              class SConf = SelectionConfig> | ||||
|     Nester( TBinType&& bin, | ||||
|               Unit min_obj_distance = 0, | ||||
|               Coord min_obj_distance = 0, | ||||
|               const PConf& pconfig = PConf(), | ||||
|               const SConf& sconfig = SConf()): | ||||
|         bin_(std::forward<TBinType>(bin)), | ||||
|  | @ -895,7 +895,7 @@ private: | |||
|     template<class TIter> inline void __execute(TIter from, TIter to) | ||||
|     { | ||||
|         if(min_obj_distance_ > 0) std::for_each(from, to, [this](Item& item) { | ||||
|             item.addOffset(static_cast<Unit>(std::ceil(min_obj_distance_/2.0))); | ||||
|             item.addOffset(static_cast<Coord>(std::ceil(min_obj_distance_/2.0))); | ||||
|         }); | ||||
| 
 | ||||
|         selector_.template packItems<PlacementStrategy>( | ||||
|  |  | |||
|  | @ -171,24 +171,76 @@ Box boundingBox(const Box& pilebb, const Box& ibb ) { | |||
|     return Box(minc, maxc); | ||||
| } | ||||
| 
 | ||||
| // This is "the" object function which is evaluated many times for each vertex
 | ||||
| // (decimated with the accuracy parameter) of each object. Therefore it is
 | ||||
| // upmost crucial for this function to be as efficient as it possibly can be but
 | ||||
| // at the same time, it has to provide reasonable results.
 | ||||
| // Fill in the placer algorithm configuration with values carefully chosen for
 | ||||
| // Slic3r.
 | ||||
| template<class PConf> | ||||
| void fillConfig(PConf& pcfg) { | ||||
| 
 | ||||
|     // Align the arranged pile into the center of the bin
 | ||||
|     pcfg.alignment = PConf::Alignment::CENTER; | ||||
| 
 | ||||
|     // Start placing the items from the center of the print bed
 | ||||
|     pcfg.starting_point = PConf::Alignment::CENTER; | ||||
| 
 | ||||
|     // TODO cannot use rotations until multiple objects of same geometry can
 | ||||
|     // handle different rotations
 | ||||
|     // arranger.useMinimumBoundigBoxRotation();
 | ||||
|     pcfg.rotations = { 0.0 }; | ||||
| 
 | ||||
|     // The accuracy of optimization.
 | ||||
|     // Goes from 0.0 to 1.0 and scales performance as well
 | ||||
|     pcfg.accuracy = 0.65f; | ||||
| 
 | ||||
|     pcfg.parallel = true; | ||||
| } | ||||
| 
 | ||||
| // Type trait for an arranger class for different bin types (box, circle,
 | ||||
| // polygon, etc...)
 | ||||
| //template<class TBin>
 | ||||
| //class AutoArranger {};
 | ||||
| 
 | ||||
| template<class Bin> clppr::IntPoint center(const Bin& bin) { return bin.center(); } | ||||
| template<>          clppr::IntPoint center(const clppr::Polygon &bin) { return sl::boundingBox(bin).center(); } | ||||
| 
 | ||||
| // A class encapsulating the libnest2d Nester class and extending it with other
 | ||||
| // management and spatial index structures for acceleration.
 | ||||
| template<class TBin> | ||||
| class AutoArranger { | ||||
| public: | ||||
|     // Useful type shortcuts...
 | ||||
|     using Placer = typename placers::_NofitPolyPlacer<clppr::Polygon, TBin>; | ||||
|     using Selector = selections::_FirstFitSelection<clppr::Polygon>; | ||||
|     using Packer   = Nester<Placer, Selector>; | ||||
|     using PConfig  = typename Packer::PlacementConfig; | ||||
|     using Distance = TCoord<PointImpl>; | ||||
| 
 | ||||
| protected: | ||||
|     Packer    m_pck; | ||||
|     PConfig   m_pconf; // Placement configuration
 | ||||
|     TBin      m_bin; | ||||
|     double    m_bin_area;   // caching
 | ||||
|     PointImpl m_bincenter;  // caching
 | ||||
|     SpatIndex m_rtree; // spatial index for the normal (bigger) objects
 | ||||
|     SpatIndex m_smallsrtree;    // spatial index for only the smaller items
 | ||||
|     double    m_norm;           // A coefficient to scale distances
 | ||||
|     MultiPolygon m_merged_pile; // The already merged pile (vector of items)
 | ||||
|     Box          m_pilebb;      // The bounding box of the merged pile.
 | ||||
|     ItemGroup m_remaining; // Remaining items (m_items at the beginning)
 | ||||
|     ItemGroup m_items;     // The items to be packed
 | ||||
| 
 | ||||
|     // This is "the" object function which is evaluated many times for each
 | ||||
|     // vertex (decimated with the accuracy parameter) of each object.
 | ||||
|     // Therefore it is upmost crucial for this function to be as efficient
 | ||||
|     // as it possibly can be but at the same time, it has to provide
 | ||||
|     // reasonable results.
 | ||||
|     std::tuple<double /*score*/, Box /*farthest point from bin center*/> | ||||
| objfunc(const PointImpl& bincenter, | ||||
|         const MultiPolygon& merged_pile, | ||||
|         const Box& pilebb, | ||||
|         const ItemGroup& items, | ||||
|         const Item &item, | ||||
|         double bin_area, | ||||
|         double norm,            // A norming factor for physical dimensions
 | ||||
|         // a spatial index to quickly get neighbors of the candidate item
 | ||||
|         const SpatIndex& spatindex, | ||||
|         const SpatIndex& smalls_spatindex, | ||||
|         const ItemGroup& remaining | ||||
|         ) | ||||
|     objfunc(const Item &item ) | ||||
|     { | ||||
|         const double bin_area = m_bin_area; | ||||
|         const SpatIndex& spatindex = m_rtree; | ||||
|         const SpatIndex& smalls_spatindex = m_smallsrtree; | ||||
|         const ItemGroup& remaining = m_remaining; | ||||
|          | ||||
|         // We will treat big items (compared to the print bed) differently
 | ||||
|         auto isBig = [bin_area](double a) { | ||||
|             return a/bin_area > BIG_ITEM_TRESHOLD ; | ||||
|  | @ -198,7 +250,7 @@ objfunc(const PointImpl& bincenter, | |||
|         auto ibb = sl::boundingBox(item.transformedShape()); | ||||
|          | ||||
|         // Calculate the full bounding box of the pile with the candidate item
 | ||||
|     auto fullbb = boundingBox(pilebb, ibb); | ||||
|         auto fullbb = boundingBox(m_pilebb, ibb); | ||||
|          | ||||
|         // The bounding box of the big items (they will accumulate in the center
 | ||||
|         // of the pile
 | ||||
|  | @ -233,8 +285,8 @@ objfunc(const PointImpl& bincenter, | |||
|             dists[4] = pl::distance(bottom_right, cc); | ||||
|              | ||||
|             // The smalles distance from the arranged pile center:
 | ||||
|         auto dist = *(std::min_element(dists.begin(), dists.end())) / norm; | ||||
|         auto bindist = pl::distance(ibb.center(), bincenter) / norm; | ||||
|             double dist = *(std::min_element(dists.begin(), dists.end())) / m_norm; | ||||
|             double bindist = pl::distance(ibb.center(), m_bincenter) / m_norm; | ||||
|             dist = 0.8*dist + 0.2*bindist; | ||||
|              | ||||
|             // Density is the pack density: how big is the arranged pile
 | ||||
|  | @ -242,27 +294,28 @@ objfunc(const PointImpl& bincenter, | |||
|              | ||||
|             if(remaining.empty()) { | ||||
|                  | ||||
|             auto mp = merged_pile; | ||||
|                 auto mp = m_merged_pile; | ||||
|                 mp.emplace_back(item.transformedShape()); | ||||
|                 auto chull = sl::convexHull(mp); | ||||
|                  | ||||
|                 placers::EdgeCache<clppr::Polygon> ec(chull); | ||||
|                  | ||||
|             double circ = ec.circumference() / norm; | ||||
|             double bcirc = 2.0*(fullbb.width() + fullbb.height()) / norm; | ||||
|                 double circ = ec.circumference() / m_norm; | ||||
|                 double bcirc = 2.0*(fullbb.width() + fullbb.height()) / m_norm; | ||||
|                 score = 0.5*circ + 0.5*bcirc; | ||||
|                  | ||||
|             } else { | ||||
|                 // Prepare a variable for the alignment score.
 | ||||
|             // This will indicate: how well is the candidate item aligned with
 | ||||
|             // its neighbors. We will check the alignment with all neighbors and
 | ||||
|             // return the score for the best alignment. So it is enough for the
 | ||||
|             // candidate to be aligned with only one item.
 | ||||
|                 // This will indicate: how well is the candidate item
 | ||||
|                 // aligned with its neighbors. We will check the alignment
 | ||||
|                 // with all neighbors and return the score for the best
 | ||||
|                 // alignment. So it is enough for the candidate to be
 | ||||
|                 // aligned with only one item.
 | ||||
|                 auto alignment_score = 1.0; | ||||
|                  | ||||
|             density = std::sqrt((fullbb.width() / norm )* | ||||
|                                 (fullbb.height() / norm)); | ||||
|                 auto querybb = item.boundingBox(); | ||||
|                 density = std::sqrt((fullbb.width() / m_norm )* | ||||
|                                     (fullbb.height() / m_norm)); | ||||
|                  | ||||
|                 // Query the spatial index for the neighbors
 | ||||
|                 std::vector<SpatElement> result; | ||||
|  | @ -275,9 +328,10 @@ objfunc(const PointImpl& bincenter, | |||
|                                            std::back_inserter(result)); | ||||
|                 } | ||||
|                  | ||||
|             for(auto& e : result) { // now get the score for the best alignment
 | ||||
|                 // now get the score for the best alignment
 | ||||
|                 for(auto& e : result) {  | ||||
|                     auto idx = e.second; | ||||
|                 Item& p = items[idx]; | ||||
|                     Item& p = m_items[idx]; | ||||
|                     auto parea = p.area(); | ||||
|                     if(std::abs(1.0 - parea/item.area()) < 1e-6) { | ||||
|                         auto bb = boundingBox(p.boundingBox(), ibb); | ||||
|  | @ -288,9 +342,9 @@ objfunc(const PointImpl& bincenter, | |||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|             // The final mix of the score is the balance between the distance
 | ||||
|             // from the full pile center, the pack density and the
 | ||||
|             // alignment with the neighbors
 | ||||
|                 // The final mix of the score is the balance between the
 | ||||
|                 // distance from the full pile center, the pack density and
 | ||||
|                 // the alignment with the neighbors
 | ||||
|                 if (result.empty()) | ||||
|                     score = 0.5 * dist + 0.5 * density; | ||||
|                 else | ||||
|  | @ -301,70 +355,24 @@ objfunc(const PointImpl& bincenter, | |||
|             // already processed bigger items.
 | ||||
|             // No need to play around with the anchor points, the center will be
 | ||||
|             // just fine for small items
 | ||||
|         score = pl::distance(ibb.center(), bigbb.center()) / norm; | ||||
|             score = pl::distance(ibb.center(), bigbb.center()) / m_norm; | ||||
|         } | ||||
|          | ||||
|         return std::make_tuple(score, fullbb); | ||||
|     } | ||||
|      | ||||
| // Fill in the placer algorithm configuration with values carefully chosen for
 | ||||
| // Slic3r.
 | ||||
| template<class PConf> | ||||
| void fillConfig(PConf& pcfg) { | ||||
| 
 | ||||
|     // Align the arranged pile into the center of the bin
 | ||||
|     pcfg.alignment = PConf::Alignment::CENTER; | ||||
| 
 | ||||
|     // Start placing the items from the center of the print bed
 | ||||
|     pcfg.starting_point = PConf::Alignment::CENTER; | ||||
| 
 | ||||
|     // TODO cannot use rotations until multiple objects of same geometry can
 | ||||
|     // handle different rotations
 | ||||
|     // arranger.useMinimumBoundigBoxRotation();
 | ||||
|     pcfg.rotations = { 0.0 }; | ||||
| 
 | ||||
|     // The accuracy of optimization.
 | ||||
|     // Goes from 0.0 to 1.0 and scales performance as well
 | ||||
|     pcfg.accuracy = 0.65f; | ||||
| 
 | ||||
|     pcfg.parallel = true; | ||||
| } | ||||
| 
 | ||||
| // Type trait for an arranger class for different bin types (box, circle,
 | ||||
| // polygon, etc...)
 | ||||
| template<class TBin> | ||||
| class AutoArranger {}; | ||||
| 
 | ||||
| // A class encapsulating the libnest2d Nester class and extending it with other
 | ||||
| // management and spatial index structures for acceleration.
 | ||||
| template<class TBin> | ||||
| class _ArrBase { | ||||
| public: | ||||
|     // Useful type shortcuts...
 | ||||
|     using Placer = typename placers::_NofitPolyPlacer<clppr::Polygon, TBin>; | ||||
|     using Selector = selections::_FirstFitSelection<clppr::Polygon>; | ||||
|     using Packer   = Nester<Placer, Selector>; | ||||
|     using PConfig  = typename Packer::PlacementConfig; | ||||
|     using Distance = TCoord<PointImpl>; | ||||
| 
 | ||||
| protected: | ||||
|     Packer    m_pck; | ||||
|     PConfig   m_pconf; // Placement configuration
 | ||||
|     double    m_bin_area; | ||||
|     SpatIndex m_rtree; // spatial index for the normal (bigger) objects
 | ||||
|     SpatIndex m_smallsrtree;    // spatial index for only the smaller items
 | ||||
|     double    m_norm;           // A coefficient to scale distances
 | ||||
|     MultiPolygon m_merged_pile; // The already merged pile (vector of items)
 | ||||
|     Box          m_pilebb;      // The bounding box of the merged pile.
 | ||||
|     ItemGroup m_remaining; // Remaining items (m_items at the beginning)
 | ||||
|     ItemGroup m_items;     // The items to be packed
 | ||||
|     std::function<double(const Item&)> get_objfn(); | ||||
|      | ||||
| public: | ||||
|     _ArrBase(const TBin& bin, Distance dist, | ||||
|     AutoArranger(const TBin &                  bin, | ||||
|                  Distance                      dist, | ||||
|                  std::function<void(unsigned)> progressind, | ||||
|              std::function<bool(void)> stopcond): | ||||
|        m_pck(bin, dist), m_bin_area(sl::area(bin)), | ||||
|        m_norm(std::sqrt(m_bin_area)) | ||||
|                  std::function<bool(void)>     stopcond) | ||||
|         : m_pck(bin, dist) | ||||
|         , m_bin(bin) | ||||
|         , m_bin_area(sl::area(bin)) | ||||
|         , m_bincenter(center(bin)) | ||||
|         , m_norm(std::sqrt(m_bin_area)) | ||||
|     { | ||||
|         fillConfig(m_pconf); | ||||
| 
 | ||||
|  | @ -396,8 +404,12 @@ public: | |||
|             } | ||||
|         }; | ||||
|          | ||||
|         m_pconf.object_function = get_objfn(); | ||||
|          | ||||
|         if (progressind) m_pck.progressIndicator(progressind); | ||||
|         if (stopcond) m_pck.stopCondition(stopcond); | ||||
|          | ||||
|         m_pck.configure(m_pconf); | ||||
|     } | ||||
|      | ||||
|     template<class...Args> inline PackGroup operator()(Args&&...args) { | ||||
|  | @ -405,15 +417,16 @@ public: | |||
|         return m_pck.execute(std::forward<Args>(args)...); | ||||
|     } | ||||
|      | ||||
|     inline void preload(const PackGroup& pg) { | ||||
|     inline void preload(std::vector<Item>& fixeditems) { | ||||
|         m_pconf.alignment = PConfig::Alignment::DONT_ALIGN; | ||||
|         m_pconf.object_function = nullptr; // drop the special objectfunction
 | ||||
|         m_pck.preload(pg); | ||||
| //        m_pconf.object_function = nullptr; // drop the special objectfunction
 | ||||
| //        m_pck.preload(pg);
 | ||||
| 
 | ||||
|         // Build the rtree for queries to work
 | ||||
|         for(const ItemGroup& grp : pg) | ||||
|         for(unsigned idx = 0; idx < grp.size(); ++idx) { | ||||
|             Item& itm = grp[idx]; | ||||
|          | ||||
|         for(unsigned idx = 0; idx < fixeditems.size(); ++idx) { | ||||
|             Item& itm = fixeditems[idx]; | ||||
|             itm.markAsFixed(); | ||||
|             m_rtree.insert({itm.boundingBox(), idx}); | ||||
|         } | ||||
| 
 | ||||
|  | @ -429,72 +442,27 @@ public: | |||
|     } | ||||
| }; | ||||
| 
 | ||||
| // Arranger specialization for a Box shaped bin.
 | ||||
| template<> class AutoArranger<Box>: public _ArrBase<Box> { | ||||
| public: | ||||
| 
 | ||||
|     AutoArranger(const Box& bin, Distance dist, | ||||
|                  std::function<void(unsigned)> progressind = [](unsigned){}, | ||||
|                  std::function<bool(void)> stopcond = [](){return false;}): | ||||
|         _ArrBase<Box>(bin, dist, progressind, stopcond) | ||||
| template<> std::function<double(const Item&)> AutoArranger<Box>::get_objfn() | ||||
| { | ||||
| 
 | ||||
|         // Here we set up the actual object function that calls the common
 | ||||
|         // object function for all bin shapes than does an additional inside
 | ||||
|         // check for the arranged pile.
 | ||||
|         m_pconf.object_function = [this, bin] (const Item &item) { | ||||
| 
 | ||||
|             auto result = objfunc(bin.center(), | ||||
|                                   m_merged_pile, | ||||
|                                   m_pilebb, | ||||
|                                   m_items, | ||||
|                                   item, | ||||
|                                   m_bin_area, | ||||
|                                   m_norm, | ||||
|                                   m_rtree, | ||||
|                                   m_smallsrtree, | ||||
|                                   m_remaining); | ||||
|     return [this](const Item &itm) { | ||||
|         auto result = objfunc(itm); | ||||
|          | ||||
|         double score = std::get<0>(result); | ||||
|         auto& fullbb = std::get<1>(result); | ||||
| 
 | ||||
|             double miss = Placer::overfit(fullbb, bin); | ||||
|         double miss = Placer::overfit(fullbb, m_bin); | ||||
|         miss = miss > 0? miss : 0; | ||||
|         score += miss*miss; | ||||
| 
 | ||||
|         return score;     | ||||
|     }; | ||||
| 
 | ||||
|         m_pck.configure(m_pconf); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| inline Circle to_lnCircle(const CircleBed& circ) { | ||||
|     return Circle({circ.center()(0), circ.center()(1)}, circ.radius()); | ||||
| } | ||||
| 
 | ||||
| // Arranger specialization for circle shaped bin.
 | ||||
| template<> class AutoArranger<Circle>: public _ArrBase<Circle> { | ||||
| public: | ||||
| template<> std::function<double(const Item&)> AutoArranger<Circle>::get_objfn() | ||||
| { | ||||
|     return [this](const Item &item) { | ||||
|          | ||||
|     AutoArranger(const Circle& bin, Distance dist, | ||||
|                  std::function<void(unsigned)> progressind = [](unsigned){}, | ||||
|                  std::function<bool(void)> stopcond = [](){return false;}): | ||||
|         _ArrBase<Circle>(bin, dist, progressind, stopcond) { | ||||
| 
 | ||||
|         // As with the box, only the inside check is different.
 | ||||
|         m_pconf.object_function = [this, &bin] (const Item &item) { | ||||
| 
 | ||||
|             auto result = objfunc(bin.center(), | ||||
|                                   m_merged_pile, | ||||
|                                   m_pilebb, | ||||
|                                   m_items, | ||||
|                                   item, | ||||
|                                   m_bin_area, | ||||
|                                   m_norm, | ||||
|                                   m_rtree, | ||||
|                                   m_smallsrtree, | ||||
|                                   m_remaining); | ||||
|         auto result = objfunc(item); | ||||
| 
 | ||||
|         double score = std::get<0>(result); | ||||
| 
 | ||||
|  | @ -506,48 +474,112 @@ public: | |||
|             auto mp = m_merged_pile; | ||||
|             mp.push_back(item.transformedShape()); | ||||
|             auto chull = sl::convexHull(mp); | ||||
|                 double miss = Placer::overfit(chull, bin); | ||||
|             double miss = Placer::overfit(chull, m_bin); | ||||
|             if(miss < 0) miss = 0; | ||||
|             score += miss*miss; | ||||
|         } | ||||
| 
 | ||||
|         return score; | ||||
|     }; | ||||
| 
 | ||||
|         m_pck.configure(m_pconf); | ||||
| } | ||||
| }; | ||||
| 
 | ||||
| template<> std::function<double(const Item&)> AutoArranger<clppr::Polygon>::get_objfn() | ||||
| { | ||||
|     return [this] (const Item &item) { return std::get<0>(objfunc(item)); }; | ||||
| } | ||||
| 
 | ||||
| // Arranger specialization for a Box shaped bin.
 | ||||
| //template<> class AutoArranger<Box>: public _ArrBase<Box> {
 | ||||
| //public:
 | ||||
| 
 | ||||
| //    AutoArranger(const Box& bin, Distance dist,
 | ||||
| //                 std::function<void(unsigned)> progressind = [](unsigned){},
 | ||||
| //                 std::function<bool(void)> stopcond = [](){return false;}):
 | ||||
| //        _ArrBase<Box>(bin, dist, progressind, stopcond)
 | ||||
| //    {
 | ||||
| 
 | ||||
| //        // Here we set up the actual object function that calls the common
 | ||||
| //        // object function for all bin shapes than does an additional inside
 | ||||
| //        // check for the arranged pile.
 | ||||
| //        m_pconf.object_function = [this, bin](const Item &item) {
 | ||||
|              | ||||
| //            auto result = objfunc(bin.center(), item);
 | ||||
| 
 | ||||
| //            double score = std::get<0>(result);
 | ||||
| //            auto& fullbb = std::get<1>(result);
 | ||||
| 
 | ||||
| //            double miss = Placer::overfit(fullbb, bin);
 | ||||
| //            miss = miss > 0? miss : 0;
 | ||||
| //            score += miss*miss;
 | ||||
| 
 | ||||
| //            return score;
 | ||||
| //        };
 | ||||
| 
 | ||||
| //        m_pck.configure(m_pconf);
 | ||||
| //    }
 | ||||
| //};
 | ||||
| 
 | ||||
| inline Circle to_lnCircle(const CircleBed& circ) { | ||||
|     return Circle({circ.center()(0), circ.center()(1)}, circ.radius()); | ||||
| } | ||||
| 
 | ||||
| //// Arranger specialization for circle shaped bin.
 | ||||
| //template<> class AutoArranger<Circle>: public _ArrBase<Circle> {
 | ||||
| //public:
 | ||||
| 
 | ||||
| //    AutoArranger(const Circle& bin, Distance dist,
 | ||||
| //                 std::function<void(unsigned)> progressind = [](unsigned){},
 | ||||
| //                 std::function<bool(void)> stopcond = [](){return false;}):
 | ||||
| //        _ArrBase<Circle>(bin, dist, progressind, stopcond) {
 | ||||
| 
 | ||||
| //        // As with the box, only the inside check is different.
 | ||||
| //        m_pconf.object_function = [this, &bin](const Item &item) {
 | ||||
|              | ||||
| //            auto result = objfunc(bin.center(), item);
 | ||||
| 
 | ||||
| //            double score = std::get<0>(result);
 | ||||
| 
 | ||||
| //            auto isBig = [this](const Item& itm) {
 | ||||
| //                return itm.area()/m_bin_area > BIG_ITEM_TRESHOLD ;
 | ||||
| //            };
 | ||||
| 
 | ||||
| //            if(isBig(item)) {
 | ||||
| //                auto mp = m_merged_pile;
 | ||||
| //                mp.push_back(item.transformedShape());
 | ||||
| //                auto chull = sl::convexHull(mp);
 | ||||
| //                double miss = Placer::overfit(chull, bin);
 | ||||
| //                if(miss < 0) miss = 0;
 | ||||
| //                score += miss*miss;
 | ||||
| //            }
 | ||||
| 
 | ||||
| //            return score;
 | ||||
| //        };
 | ||||
| 
 | ||||
| //        m_pck.configure(m_pconf);
 | ||||
| //    }
 | ||||
| //};
 | ||||
| 
 | ||||
| // Arranger specialization for a generalized polygon.
 | ||||
| // Warning: this is unfinished business. It may or may not work.
 | ||||
| template<> class AutoArranger<PolygonImpl>: public _ArrBase<PolygonImpl> { | ||||
| public: | ||||
|     AutoArranger(const PolygonImpl& bin, Distance dist, | ||||
|                  std::function<void(unsigned)> progressind = [](unsigned){}, | ||||
|                  std::function<bool(void)> stopcond = [](){return false;}): | ||||
|         _ArrBase<PolygonImpl>(bin, dist, progressind, stopcond) | ||||
|     { | ||||
|         m_pconf.object_function = [this, &bin] (const Item &item) { | ||||
| //template<> class AutoArranger<PolygonImpl>: public _ArrBase<PolygonImpl> {
 | ||||
| //public:
 | ||||
| //    AutoArranger(const PolygonImpl& bin, Distance dist,
 | ||||
| //                 std::function<void(unsigned)> progressind = [](unsigned){},
 | ||||
| //                 std::function<bool(void)> stopcond = [](){return false;}):
 | ||||
| //        _ArrBase<PolygonImpl>(bin, dist, progressind, stopcond)
 | ||||
| //    {
 | ||||
| //        m_pconf.object_function = [this, &bin] (const Item &item) {
 | ||||
| 
 | ||||
|             auto binbb = sl::boundingBox(bin); | ||||
|             auto result = objfunc(binbb.center(), | ||||
|                                   m_merged_pile, | ||||
|                                   m_pilebb, | ||||
|                                   m_items, | ||||
|                                   item, | ||||
|                                   m_bin_area, | ||||
|                                   m_norm, | ||||
|                                   m_rtree, | ||||
|                                   m_smallsrtree, | ||||
|                                   m_remaining); | ||||
|             double score = std::get<0>(result); | ||||
| //            auto binbb = sl::boundingBox(bin);
 | ||||
| //            auto result = objfunc(binbb.center(), item);
 | ||||
| //            double score = std::get<0>(result);
 | ||||
| 
 | ||||
|             return score; | ||||
|         }; | ||||
| //            return score;
 | ||||
| //        };
 | ||||
| 
 | ||||
|         m_pck.configure(m_pconf); | ||||
|     } | ||||
| }; | ||||
| //        m_pck.configure(m_pconf);
 | ||||
| //    }
 | ||||
| //};
 | ||||
| 
 | ||||
| // Get the type of bed geometry from a simple vector of points.
 | ||||
| BedShapeHint bedShape(const Polyline &bed) { | ||||
|  | @ -628,9 +660,9 @@ BedShapeHint bedShape(const Polyline &bed) { | |||
|     return ret; | ||||
| } | ||||
| 
 | ||||
| template<class BinT> | ||||
| template<class BinT> // Arrange for arbitrary bin type
 | ||||
| PackGroup _arrange(std::vector<Item> &           shapes, | ||||
|                    const PackGroup &             preshapes, | ||||
|                    std::vector<Item> &           excludes, | ||||
|                    const BinT &                  bin, | ||||
|                    coord_t                       minobjd, | ||||
|                    std::function<void(unsigned)> prind, | ||||
|  | @ -638,9 +670,13 @@ PackGroup _arrange(std::vector<Item> &           shapes, | |||
| {    | ||||
|     AutoArranger<BinT> arranger{bin, minobjd, prind, stopfn}; | ||||
|      | ||||
|     for(auto it = excludes.begin(); it != excludes.end(); ++it) | ||||
|         if (!sl::isInside(it->transformedShape(), bin)) | ||||
|             it = excludes.erase(it); | ||||
|      | ||||
|     // If there is something on the plate
 | ||||
|     if(!preshapes.empty() && !preshapes.front().empty()) {  | ||||
|         arranger.preload(preshapes); | ||||
|     if(!excludes.empty()) {  | ||||
| //        arranger.preload(preshapes);
 | ||||
|         auto binbb = sl::boundingBox(bin); | ||||
|          | ||||
|         // Try to put the first item to the center, as the arranger will not
 | ||||
|  | @ -652,7 +688,8 @@ PackGroup _arrange(std::vector<Item> &           shapes, | |||
|             itm.translate(d); | ||||
| 
 | ||||
|             if (!arranger.is_colliding(itm)) { | ||||
|                 arranger.preload({{itm}}); | ||||
|                 itm.markAsFixed(); | ||||
| //                arranger.preload({{itm}});
 | ||||
|                  | ||||
|                 // Write the transformation data into the item. The callback
 | ||||
|                 // was set on the instantiation of Item and calls the
 | ||||
|  | @ -674,8 +711,8 @@ inline SLIC3R_CONSTEXPR coord_t stride_padding(coord_t w) | |||
|     return w + w / 5; | ||||
| } | ||||
| 
 | ||||
| //// The final client function to arrange the Model. A progress indicator and
 | ||||
| //// a stop predicate can be also be passed to control the process.
 | ||||
| // The final client function for arrangement. A progress indicator and
 | ||||
| // a stop predicate can be also be passed to control the process.
 | ||||
| bool arrange(ArrangeablePtrs &                arrangables, | ||||
|              const ArrangeablePtrs &          excludes, | ||||
|              coord_t                       min_obj_distance, | ||||
|  | @ -686,12 +723,10 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|     bool ret = true; | ||||
|     namespace clppr = ClipperLib; | ||||
|      | ||||
|     std::vector<Item> items, excluded_items; | ||||
|     std::vector<Item> items, fixeditems; | ||||
|     items.reserve(arrangables.size()); | ||||
|     coord_t binwidth = 0; | ||||
| 
 | ||||
|     PackGroup preshapes{ {} }; // pack group with one initial bin for preloading
 | ||||
| 
 | ||||
|     auto process_arrangeable = | ||||
|         [](const Arrangeable *                         arrangeable, | ||||
|            std::vector<Item> &                         outp, | ||||
|  | @ -733,9 +768,7 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|     } | ||||
|      | ||||
|     for (const Arrangeable * fixed: excludes) | ||||
|         process_arrangeable(fixed, excluded_items, nullptr); | ||||
|      | ||||
|     for(Item& excl : excluded_items) preshapes.front().emplace_back(excl); | ||||
|         process_arrangeable(fixed, fixeditems, nullptr); | ||||
|      | ||||
|     // Integer ceiling the min distance from the bed perimeters
 | ||||
|     coord_t md = min_obj_distance - SCALED_EPSILON; | ||||
|  | @ -751,7 +784,7 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|         Box binbb{{bbb.min(X), bbb.min(Y)}, {bbb.max(X), bbb.max(Y)}}; | ||||
|         binwidth = coord_t(binbb.width()); | ||||
|          | ||||
|         _arrange(items, preshapes, binbb, min_obj_distance, progressind, cfn); | ||||
|         _arrange(items, fixeditems, binbb, min_obj_distance, progressind, cfn); | ||||
|         break; | ||||
|     } | ||||
|     case BedShapeType::CIRCLE: { | ||||
|  | @ -759,7 +792,7 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|         auto cc = to_lnCircle(c); | ||||
|         binwidth = scaled(c.radius()); | ||||
|          | ||||
|         _arrange(items, preshapes, cc, min_obj_distance, progressind, cfn); | ||||
|         _arrange(items, fixeditems, cc, min_obj_distance, progressind, cfn); | ||||
|         break; | ||||
|     } | ||||
|     case BedShapeType::IRREGULAR: { | ||||
|  | @ -768,7 +801,7 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|         BoundingBox polybb(bedhint.shape.polygon); | ||||
|         binwidth = (polybb.max(X) - polybb.min(X)); | ||||
|          | ||||
|         _arrange(items, preshapes, irrbed, min_obj_distance, progressind, cfn); | ||||
|         _arrange(items, fixeditems, irrbed, min_obj_distance, progressind, cfn); | ||||
|         break; | ||||
|     } | ||||
|     case BedShapeType::INFINITE: { | ||||
|  | @ -776,12 +809,12 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|         //Box infbb{{nobin.center.x(), nobin.center.y()}};
 | ||||
|         Box infbb; | ||||
|          | ||||
|         _arrange(items, preshapes, infbb, min_obj_distance, progressind, cfn); | ||||
|         _arrange(items, fixeditems, infbb, min_obj_distance, progressind, cfn); | ||||
|         break; | ||||
|     } | ||||
|     case BedShapeType::UNKNOWN: { | ||||
|         // We know nothing about the bed, let it be infinite and zero centered 
 | ||||
|         _arrange(items, preshapes, Box{}, min_obj_distance, progressind, cfn); | ||||
|         _arrange(items, fixeditems, Box{}, min_obj_distance, progressind, cfn); | ||||
|         break; | ||||
|     } | ||||
|     }; | ||||
|  | @ -791,7 +824,7 @@ bool arrange(ArrangeablePtrs &                arrangables, | |||
|     return ret; | ||||
| } | ||||
| 
 | ||||
| /// Arrange, without the fixed items (excludes)
 | ||||
| // Arrange, without the fixed items (excludes)
 | ||||
| bool arrange(ArrangeablePtrs &                inp, | ||||
|              coord_t                       min_d, | ||||
|              const BedShapeHint &          bedhint, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 tamasmeszaros
						tamasmeszaros