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

#include <src/libnest2d/include/libnest2d/placers/nfpplacer.hpp>

+ Collaboration diagram for libnest2d::placers::EdgeCache< RawShape >:

Classes

struct  ContourCache
 

Public Types

using iterator = std::vector< double >::iterator
 
using const_iterator = std::vector< double >::const_iterator
 

Public Member Functions

 EdgeCache ()=default
 
 EdgeCache (const _Item< RawShape > &item)
 
 EdgeCache (const RawShape &sh)
 
void accuracy (double a)
 Resolution of returned corners. The stride is derived from this value.
 
Vertex coords (double distance) const
 Get a point on the circumference of a polygon.
 
Vertex coords (unsigned hidx, double distance) const
 
double circumference () const BP2D_NOEXCEPT
 
double circumference (unsigned hidx) const BP2D_NOEXCEPT
 
const std::vector< double > & corners () const BP2D_NOEXCEPT
 Get the normalized distance values for each vertex.
 
const std::vector< double > & corners (unsigned holeidx) const BP2D_NOEXCEPT
 corners for a specific hole
 
size_t holeCount () const BP2D_NOEXCEPT
 The number of holes in the abstracted polygon.
 

Private Types

using Vertex = TPoint< RawShape >
 
using Coord = TCoord< Vertex >
 
using Edge = _Segment< Vertex >
 

Private Member Functions

void createCache (const RawShape &sh)
 
size_t stride (const size_t N) const
 
void fetchCorners () const
 
void fetchHoleCorners (unsigned hidx) const
 
Vertex coords (const ContourCache &cache, double distance) const
 

Static Private Member Functions

static double length (const Edge &e)
 

Private Attributes

struct libnest2d::placers::EdgeCache::ContourCache contour_
 
std::vector< ContourCacheholes_
 
double accuracy_ = 1.0
 

Detailed Description

template<class RawShape>
class libnest2d::placers::EdgeCache< RawShape >

A class for getting a point on the circumference of the polygon (in log time)

This is a transformation of the provided polygon to be able to pinpoint locations on the circumference. The optimizer will pass a floating point value e.g. within <0,1> and we have to transform this value quickly into a coordinate on the circumference. By definition 0 should yield the first vertex and 1.0 would be the last (which should coincide with first).

We also have to make this work for the holes of the captured polygon.


Class Documentation

◆ libnest2d::placers::EdgeCache::ContourCache

struct libnest2d::placers::EdgeCache::ContourCache
template<class RawShape>
struct libnest2d::placers::EdgeCache< RawShape >::ContourCache
+ Collaboration diagram for libnest2d::placers::EdgeCache< RawShape >::ContourCache:
Class Members
vector< double > corners
vector< double > distances
vector< Edge > emap
double full_distance = 0

Member Typedef Documentation

◆ const_iterator

template<class RawShape >
using libnest2d::placers::EdgeCache< RawShape >::const_iterator = std::vector<double>::const_iterator

◆ Coord

template<class RawShape >
using libnest2d::placers::EdgeCache< RawShape >::Coord = TCoord<Vertex>
private

◆ Edge

template<class RawShape >
using libnest2d::placers::EdgeCache< RawShape >::Edge = _Segment<Vertex>
private

◆ iterator

template<class RawShape >
using libnest2d::placers::EdgeCache< RawShape >::iterator = std::vector<double>::iterator

◆ Vertex

template<class RawShape >
using libnest2d::placers::EdgeCache< RawShape >::Vertex = TPoint<RawShape>
private

Constructor & Destructor Documentation

◆ EdgeCache() [1/3]

template<class RawShape >
libnest2d::placers::EdgeCache< RawShape >::EdgeCache ( )
inlinedefault

◆ EdgeCache() [2/3]

template<class RawShape >
libnest2d::placers::EdgeCache< RawShape >::EdgeCache ( const _Item< RawShape > &  item)
inline
282 {
283 createCache(item.transformedShape());
284 }
void createCache(const RawShape &sh)
Definition nfpplacer.hpp:158

References libnest2d::placers::EdgeCache< RawShape >::createCache(), and libnest2d::_Item< RawShape >::transformedShape().

+ Here is the call graph for this function:

◆ EdgeCache() [3/3]

template<class RawShape >
libnest2d::placers::EdgeCache< RawShape >::EdgeCache ( const RawShape &  sh)
inline
287 {
288 createCache(sh);
289 }

References libnest2d::placers::EdgeCache< RawShape >::createCache().

+ Here is the call graph for this function:

Member Function Documentation

◆ accuracy()

template<class RawShape >
void libnest2d::placers::EdgeCache< RawShape >::accuracy ( double  a)
inline

Resolution of returned corners. The stride is derived from this value.

292{ accuracy_ = a; }
double accuracy_
Definition nfpplacer.hpp:151

References libnest2d::placers::EdgeCache< RawShape >::accuracy_.

◆ circumference() [1/2]

template<class RawShape >
double libnest2d::placers::EdgeCache< RawShape >::circumference ( ) const
inline

◆ circumference() [2/2]

template<class RawShape >
double libnest2d::placers::EdgeCache< RawShape >::circumference ( unsigned  hidx) const
inline
316 {
317 return holes_[hidx].full_distance;
318 }
std::vector< ContourCache > holes_
Definition nfpplacer.hpp:149

References libnest2d::placers::EdgeCache< RawShape >::holes_.

◆ coords() [1/3]

template<class RawShape >
Vertex libnest2d::placers::EdgeCache< RawShape >::coords ( const ContourCache cache,
double  distance 
) const
inlineprivate
246 {
247 assert(distance >= .0 && distance <= 1.0);
248 if (cache.distances.empty() || cache.emap.empty()) return Vertex{};
249 if (distance > 1.0) distance = std::fmod(distance, 1.0);
250
251 // distance is from 0.0 to 1.0, we scale it up to the full length of
252 // the circumference
253 double d = distance*cache.full_distance;
254
255 auto& distances = cache.distances;
256
257 // Magic: we find the right edge in log time
258 auto it = std::lower_bound(distances.begin(), distances.end(), d);
259 auto idx = it - distances.begin(); // get the index of the edge
260 auto edge = cache.emap[idx]; // extrac the edge
261
262 // Get the remaining distance on the target edge
263 auto ed = d - (idx > 0 ? *std::prev(it) : 0 );
264 auto angle = edge.angleToXaxis();
265 Vertex ret = edge.first();
266
267 // Get the point on the edge which lies in ed distance from the start
268 ret += Vertex(static_cast<Coord>(std::round(ed*std::cos(angle))),
269 static_cast<Coord>(std::round(ed*std::sin(angle))));
270
271 return ret;
272 }
TCoord< Vertex > Coord
Definition nfpplacer.hpp:139
TPoint< RawShape > Vertex
Definition nfpplacer.hpp:138
double angle(const Eigen::MatrixBase< Derived > &v1, const Eigen::MatrixBase< Derived2 > &v2)
Definition Point.hpp:112
double distance(const P &p1, const P &p2)
Definition geometry_traits.hpp:329

References libnest2d::placers::EdgeCache< RawShape >::ContourCache::distances, libnest2d::placers::EdgeCache< RawShape >::ContourCache::emap, and libnest2d::placers::EdgeCache< RawShape >::ContourCache::full_distance.

Referenced by libnest2d::placers::EdgeCache< RawShape >::coords(), and libnest2d::placers::EdgeCache< RawShape >::coords().

+ Here is the caller graph for this function:

◆ coords() [2/3]

template<class RawShape >
Vertex libnest2d::placers::EdgeCache< RawShape >::coords ( double  distance) const
inline

Get a point on the circumference of a polygon.

Parameters
distanceA relative distance from the starting point to the end. Can be from 0.0 to 1.0 where 0.0 is the starting point and 1.0 is the closing point (which should be eqvivalent with the starting point with closed polygons).
Returns
Returns the coordinates of the point lying on the polygon circumference.
303 {
304 return coords(contour_, distance);
305 }
Vertex coords(const ContourCache &cache, double distance) const
Definition nfpplacer.hpp:246

References libnest2d::placers::EdgeCache< RawShape >::contour_, and libnest2d::placers::EdgeCache< RawShape >::coords().

+ Here is the call graph for this function:

◆ coords() [3/3]

template<class RawShape >
Vertex libnest2d::placers::EdgeCache< RawShape >::coords ( unsigned  hidx,
double  distance 
) const
inline
307 {
308 assert(hidx < holes_.size());
309 return coords(holes_[hidx], distance);
310 }

References libnest2d::placers::EdgeCache< RawShape >::coords(), and libnest2d::placers::EdgeCache< RawShape >::holes_.

+ Here is the call graph for this function:

◆ corners() [1/2]

template<class RawShape >
const std::vector< double > & libnest2d::placers::EdgeCache< RawShape >::corners ( ) const
inline

Get the normalized distance values for each vertex.

321 {
322 fetchCorners();
323 return contour_.corners;
324 }
void fetchCorners() const
Definition nfpplacer.hpp:216
std::vector< double > corners
Definition nfpplacer.hpp:143

References libnest2d::placers::EdgeCache< RawShape >::contour_, libnest2d::placers::EdgeCache< RawShape >::ContourCache::corners, and libnest2d::placers::EdgeCache< RawShape >::fetchCorners().

+ Here is the call graph for this function:

◆ corners() [2/2]

template<class RawShape >
const std::vector< double > & libnest2d::placers::EdgeCache< RawShape >::corners ( unsigned  holeidx) const
inline

corners for a specific hole

328 {
329 fetchHoleCorners(holeidx);
330 return holes_[holeidx].corners;
331 }
void fetchHoleCorners(unsigned hidx) const
Definition nfpplacer.hpp:231

References libnest2d::placers::EdgeCache< RawShape >::fetchHoleCorners(), and libnest2d::placers::EdgeCache< RawShape >::holes_.

+ Here is the call graph for this function:

◆ createCache()

template<class RawShape >
void libnest2d::placers::EdgeCache< RawShape >::createCache ( const RawShape &  sh)
inlineprivate
158 {
159 { // For the contour
160 auto first = sl::cbegin(sh);
161 auto endit = sl::cend(sh);
162 auto next = first == endit ? endit : std::next(first);
163
165
166 while(next != endit) {
167 contour_.emap.emplace_back(*(first++), *(next++));
170 }
171
172 if constexpr (ClosureTypeV<RawShape> == Closure::OPEN) {
173 if (sl::contourVertexCount(sh) > 0) {
174 contour_.emap.emplace_back(sl::back(sh), sl::front(sh));
177 }
178 }
179 }
180
181 for(auto& h : shapelike::holes(sh)) { // For the holes
182 auto first = sl::cbegin(h);
183 auto endit = sl::cend(h);
184 auto next = first == endit ? endit :std::next(first);
185
186 ContourCache hc;
187 hc.distances.reserve(sl::contourVertexCount(h));
188
189 while(next != endit) {
190 hc.emap.emplace_back(*(first++), *(next++));
191 hc.full_distance += length(hc.emap.back());
192 hc.distances.emplace_back(hc.full_distance);
193 }
194
195 if constexpr (ClosureTypeV<RawShape> == Closure::OPEN) {
196 if (sl::contourVertexCount(h) > 0) {
197 hc.emap.emplace_back(sl::back(sh), sl::front(sh));
198 hc.full_distance += length(hc.emap.back());
199 hc.distances.emplace_back(hc.full_distance);
200 }
201 }
202
203 holes_.emplace_back(std::move(hc));
204 }
205 }
static double length(const Edge &e)
Definition nfpplacer.hpp:153
std::vector< double > distances
Definition nfpplacer.hpp:145
std::vector< Edge > emap
Definition nfpplacer.hpp:144
TPoint< P > back(const P &p)
Definition geometry_traits.hpp:873
size_t contourVertexCount(const S &sh)
Definition geometry_traits.hpp:1224
TPoint< P > front(const P &p)
Definition geometry_traits.hpp:872
S::const_iterator cend(const S &sh, const PathTag &)
Definition geometry_traits.hpp:634
const THolesContainer< PolygonImpl > & holes(const Slic3r::ExPolygon &sh)
Definition geometries.hpp:189
S::const_iterator cbegin(const S &sh, const PathTag &)
Definition geometry_traits.hpp:627

References libnest2d::shapelike::back(), libnest2d::shapelike::cbegin(), libnest2d::shapelike::cend(), libnest2d::placers::EdgeCache< RawShape >::contour_, libnest2d::shapelike::contourVertexCount(), libnest2d::placers::EdgeCache< RawShape >::ContourCache::distances, libnest2d::placers::EdgeCache< RawShape >::ContourCache::emap, libnest2d::shapelike::front(), libnest2d::placers::EdgeCache< RawShape >::ContourCache::full_distance, libnest2d::shapelike::holes(), libnest2d::placers::EdgeCache< RawShape >::holes_, libnest2d::placers::EdgeCache< RawShape >::length(), and libnest2d::OPEN.

Referenced by libnest2d::placers::EdgeCache< RawShape >::EdgeCache(), and libnest2d::placers::EdgeCache< RawShape >::EdgeCache().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetchCorners()

template<class RawShape >
void libnest2d::placers::EdgeCache< RawShape >::fetchCorners ( ) const
inlineprivate
216 {
217 if(!contour_.corners.empty()) return;
218
219 const auto N = contour_.distances.size();
220 const auto S = stride(N);
221
222 contour_.corners.reserve(N / S + 1);
223 contour_.corners.emplace_back(0.0);
224 auto N_1 = N-1;
225 for(size_t i = 0; i < N_1; i += S) {
226 contour_.corners.emplace_back(
228 }
229 }
size_t stride(const size_t N) const
Definition nfpplacer.hpp:207

References libnest2d::placers::EdgeCache< RawShape >::contour_, libnest2d::placers::EdgeCache< RawShape >::ContourCache::corners, libnest2d::placers::EdgeCache< RawShape >::ContourCache::distances, libnest2d::placers::EdgeCache< RawShape >::ContourCache::full_distance, and libnest2d::placers::EdgeCache< RawShape >::stride().

Referenced by libnest2d::placers::EdgeCache< RawShape >::corners().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetchHoleCorners()

template<class RawShape >
void libnest2d::placers::EdgeCache< RawShape >::fetchHoleCorners ( unsigned  hidx) const
inlineprivate
231 {
232 auto& hc = holes_[hidx];
233 if(!hc.corners.empty()) return;
234
235 const auto N = hc.distances.size();
236 auto N_1 = N-1;
237 const auto S = stride(N);
238 hc.corners.reserve(N / S + 1);
239 hc.corners.emplace_back(0.0);
240 for(size_t i = 0; i < N_1; i += S) {
241 hc.corners.emplace_back(
242 hc.distances.at(i) / hc.full_distance);
243 }
244 }

References libnest2d::placers::EdgeCache< RawShape >::holes_, and libnest2d::placers::EdgeCache< RawShape >::stride().

Referenced by libnest2d::placers::EdgeCache< RawShape >::corners().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ holeCount()

template<class RawShape >
size_t libnest2d::placers::EdgeCache< RawShape >::holeCount ( ) const
inline

The number of holes in the abstracted polygon.

334{ return holes_.size(); }

References libnest2d::placers::EdgeCache< RawShape >::holes_.

◆ length()

template<class RawShape >
static double libnest2d::placers::EdgeCache< RawShape >::length ( const Edge e)
inlinestaticprivate
154 {
155 return std::sqrt(e.template sqlength<double>());
156 }

Referenced by libnest2d::placers::EdgeCache< RawShape >::createCache().

+ Here is the caller graph for this function:

◆ stride()

template<class RawShape >
size_t libnest2d::placers::EdgeCache< RawShape >::stride ( const size_t  N) const
inlineprivate
207 {
208 using std::round;
209 using std::pow;
210
211 return static_cast<Coord>(
212 round(N/pow(N, pow(accuracy_, 1.0/3.0)))
213 );
214 }
EIGEN_DEVICE_FUNC const RoundReturnType round() const
Definition ArrayCwiseUnaryOps.h:374
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half &a, const half &b)
Definition Half.h:477

References libnest2d::placers::EdgeCache< RawShape >::accuracy_, and round().

Referenced by libnest2d::placers::EdgeCache< RawShape >::fetchCorners(), and libnest2d::placers::EdgeCache< RawShape >::fetchHoleCorners().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ accuracy_

template<class RawShape >
double libnest2d::placers::EdgeCache< RawShape >::accuracy_ = 1.0
private

◆ contour_

◆ holes_


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