Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
libnest2d::_Rectangle< Sh > Class Template Reference

Subclass of _Item for regular rectangle items. More...

#include <src/libnest2d/include/libnest2d/nester.hpp>

+ Inheritance diagram for libnest2d::_Rectangle< Sh >:
+ Collaboration diagram for libnest2d::_Rectangle< Sh >:

Public Types

using Unit = TCoord< Sh >
 
using ShapeType = Sh
 The type of the shape which was handed over as the template argument.
 
using Iterator = VertexConstIterator
 Iterator type for the outer vertices.
 

Public Member Functions

 _Rectangle (Unit w, Unit h)
 
Unit width () const BP2D_NOEXCEPT
 
Unit height () const BP2D_NOEXCEPT
 
bool isFixed () const noexcept
 
void markAsFixedInBin (int binid)
 
void binId (int idx)
 
int binId () const noexcept
 
void priority (int p)
 
int priority () const noexcept
 
std::string toString () const
 Convert the polygon to string representation. The format depends on the implementation of the polygon.
 
Iterator begin () const
 Iterator tho the first contour vertex in the polygon.
 
Iterator cbegin () const
 Alias to begin()
 
Iterator end () const
 Iterator to the last contour vertex.
 
Iterator cend () const
 Alias to end()
 
Vertex vertex (unsigned long idx) const
 Get a copy of an outer vertex within the carried shape.
 
void setVertex (unsigned long idx, const Vertex &v)
 Modify a vertex.
 
void setShape (Sh rsh)
 
void setOnPackedFn (std::function< void(_Item &)> onpackedfn)
 
void onPacked ()
 
double area () const
 Calculate the shape area.
 
bool isContourConvex () const
 
bool isHoleConvex (unsigned) const
 
bool areHolesConvex () const
 
size_t vertexCount () const
 The number of the outer ring vertices.
 
size_t holeCount () const
 
bool isInside (const Vertex &p) const
 isPointInside
 
bool isInside (const _Item &sh) const
 
bool isInside (const Sh &sh) const
 
bool isInside (const _Box< TPoint< Sh > > &box) const
 
bool isInside (const _Circle< TPoint< Sh > > &box) const
 
void translate (const Vertex &d) BP2D_NOEXCEPT
 
void rotate (const Radians &rads) BP2D_NOEXCEPT
 
void inflation (Coord distance) BP2D_NOEXCEPT
 
Coord inflation () const BP2D_NOEXCEPT
 
void inflate (Coord distance) BP2D_NOEXCEPT
 
Radians rotation () const BP2D_NOEXCEPT
 
void rotation (Radians rot) BP2D_NOEXCEPT
 
TPoint< Sh > translation () const BP2D_NOEXCEPT
 
void translation (const TPoint< Sh > &tr) BP2D_NOEXCEPT
 
const Sh & transformedShape () const
 
 operator Sh () const
 
const Sh & rawShape () const BP2D_NOEXCEPT
 
void resetTransformation () BP2D_NOEXCEPT
 
Box boundingBox () const
 
Vertex referenceVertex () const
 
Vertex rightmostTopVertex () const
 
Vertex leftmostBottomVertex () const
 

Static Public Member Functions

static BP2D_CONSTEXPR Orientation orientation ()
 Get the orientation of the polygon.
 
static bool intersects (const _Item &sh1, const _Item &sh2)
 
static bool touches (const _Item &sh1, const _Item &sh2)
 

Private Types

using TO = Orientation
 
enum  Convexity
 
using Coord = TCoord< TPoint< Sh > >
 
using Vertex = TPoint< Sh >
 
using Box = _Box< Vertex >
 
using VertexConstIterator = typename TContour< Sh >::const_iterator
 

Private Member Functions

const Sh & infaltedShape () const
 
void invalidateCache () const BP2D_NOEXCEPT
 

Static Private Member Functions

static bool vsort (const Vertex &v1, const Vertex &v2)
 

Private Attributes

Sh sh_
 
Vertex translation_
 
Radians rotation_
 
Coord inflation_
 
bool has_rotation_
 
bool has_translation_
 
bool has_inflation_
 
Sh tr_cache_
 
bool tr_cache_valid_
 
double area_cache_
 
bool area_cache_valid_
 
Sh inflate_cache_
 
bool inflate_cache_valid_
 
Convexity convexity_
 
VertexConstIterator rmt_
 
VertexConstIterator lmb_
 
bool rmt_valid_
 
bool lmb_valid_
 
struct libnest2d::_Item::BBCache bb_cache_
 
int binid_
 
int priority_
 
bool fixed_
 
std::function< void(_Item &)> on_packed_
 

Detailed Description

template<class Sh>
class libnest2d::_Rectangle< Sh >

Subclass of _Item for regular rectangle items.

Member Typedef Documentation

◆ Box

using libnest2d::_Item< Sh >::Box = _Box<Vertex>
privateinherited

◆ Coord

using libnest2d::_Item< Sh >::Coord = TCoord<TPoint<Sh > >
privateinherited

◆ Iterator

using libnest2d::_Item< Sh >::Iterator = VertexConstIterator
inherited

Iterator type for the outer vertices.

Only const iterators can be used. The _Item type is not intended to modify the carried shapes from the outside. The main purpose of this type is to cache the calculation results from the various operators it supports. Giving out a non const iterator would make it impossible to perform correct cache invalidation.

◆ ShapeType

using libnest2d::_Item< Sh >::ShapeType = Sh
inherited

The type of the shape which was handed over as the template argument.

◆ TO

template<class Sh >
using libnest2d::_Rectangle< Sh >::TO = Orientation
private

◆ Unit

template<class Sh >
using libnest2d::_Rectangle< Sh >::Unit = TCoord<Sh>

◆ Vertex

using libnest2d::_Item< Sh >::Vertex = TPoint<Sh >
privateinherited

◆ VertexConstIterator

using libnest2d::_Item< Sh >::VertexConstIterator = typename TContour<Sh >::const_iterator
privateinherited

Member Enumeration Documentation

◆ Convexity

enum libnest2d::_Item::Convexity : char
strongprivateinherited
56 : char {
57 UNCHECKED,
58 C_TRUE,
59 C_FALSE
60 };

Constructor & Destructor Documentation

◆ _Rectangle()

template<class Sh >
libnest2d::_Rectangle< Sh >::_Rectangle ( Unit  w,
Unit  h 
)
inline
492: _Item<Sh>{create_rect<Sh>(w, h)} {}

Member Function Documentation

◆ area()

double libnest2d::_Item< Sh >::area ( ) const
inlineinherited

Calculate the shape area.

The method returns absolute value and does not reflect polygon orientation. The result is cached, subsequent calls will have very little cost.

Returns
The shape area in floating point double precision.
234 {
235 double ret ;
236 if(area_cache_valid_) ret = area_cache_;
237 else {
238 ret = sl::area(infaltedShape());
239 area_cache_ = ret;
240 area_cache_valid_ = true;
241 }
242 return ret;
243 }
bool area_cache_valid_
Definition nester.hpp:52
const Sh & infaltedShape() const
Definition nester.hpp:437
double area_cache_
Definition nester.hpp:51

◆ areHolesConvex()

bool libnest2d::_Item< Sh >::areHolesConvex ( ) const
inlineinherited
264 {
265 return false;
266 }

◆ begin()

Iterator libnest2d::_Item< Sh >::begin ( ) const
inlineinherited

Iterator tho the first contour vertex in the polygon.

157 {
158 return sl::cbegin(sh_);
159 }

◆ binId() [1/2]

int libnest2d::_Item< Sh >::binId ( ) const
inlinenoexceptinherited
140{ return binid_; }
int binid_
Definition nester.hpp:71

◆ binId() [2/2]

void libnest2d::_Item< Sh >::binId ( int  idx)
inlineinherited
139{ binid_ = idx; }

◆ boundingBox()

Box libnest2d::_Item< Sh >::boundingBox ( ) const
inlineinherited
382 {
383 if(!bb_cache_.valid) {
384 if(!has_rotation_)
385 bb_cache_.bb = sl::boundingBox(infaltedShape());
386 else {
387 // TODO make sure this works
388 auto rotsh = infaltedShape();
389 sl::rotate(rotsh, rotation_);
390 bb_cache_.bb = sl::boundingBox(rotsh);
391 }
392 bb_cache_.valid = true;
393 }
394
395 auto &bb = bb_cache_.bb; auto &tr = translation_;
396 return {bb.minCorner() + tr, bb.maxCorner() + tr };
397 }
Vertex translation_
Definition nester.hpp:39
struct libnest2d::_Item::BBCache bb_cache_
Box bb
Definition nester.hpp:67
bool valid
Definition nester.hpp:67

◆ cbegin()

Iterator libnest2d::_Item< Sh >::cbegin ( ) const
inlineinherited

Alias to begin()

163 {
164 return sl::cbegin(sh_);
165 }

◆ cend()

Iterator libnest2d::_Item< Sh >::cend ( ) const
inlineinherited

Alias to end()

175 {
176 return sl::cend(sh_);
177 }

◆ end()

Iterator libnest2d::_Item< Sh >::end ( ) const
inlineinherited

Iterator to the last contour vertex.

169 {
170 return sl::cend(sh_);
171 }

◆ height()

template<class Sh >
Unit libnest2d::_Rectangle< Sh >::height ( ) const
inline
498 {
499 return getY(vertex(2));
500 }
Vertex vertex(unsigned long idx) const
Get a copy of an outer vertex within the carried shape.
Definition nester.hpp:189
TCoord< P > getY(const P &p)
Definition geometry_traits.hpp:427

References libnest2d::getY(), and libnest2d::_Item< Sh >::vertex().

+ Here is the call graph for this function:

◆ holeCount()

size_t libnest2d::_Item< Sh >::holeCount ( ) const
inlineinherited
273 {
274 return sl::holeCount(sh_);
275 }

◆ infaltedShape()

const Sh & libnest2d::_Item< Sh >::infaltedShape ( ) const
inlineprivateinherited
437 {
438 if(has_inflation_ ) {
439 if(inflate_cache_valid_) return inflate_cache_;
440
442 sl::offset(inflate_cache_, inflation_);
444 return inflate_cache_;
445 }
446 return sh_;
447 }
Sh sh_
Definition nester.hpp:36
bool inflate_cache_valid_
Definition nester.hpp:54
Sh inflate_cache_
Definition nester.hpp:53

◆ inflate()

void libnest2d::_Item< Sh >::inflate ( Coord  distance)
inlineinherited
322 {
323 inflation(inflation() + distance);
324 }
void inflation(Coord distance) BP2D_NOEXCEPT
Definition nester.hpp:310

◆ inflation() [1/2]

Coord libnest2d::_Item< Sh >::inflation ( ) const
inlineinherited
317 {
318 return inflation_;
319 }
Coord inflation_
Definition nester.hpp:41

◆ inflation() [2/2]

void libnest2d::_Item< Sh >::inflation ( Coord  distance)
inlineinherited
311 {
313 has_inflation_ = true;
315 }
void invalidateCache() const BP2D_NOEXCEPT
Definition nester.hpp:449
bool has_inflation_
Definition nester.hpp:46
double distance(const P &p1, const P &p2)
Definition geometry_traits.hpp:329

◆ intersects()

static bool libnest2d::_Item< Sh >::intersects ( const _Item< Sh > &  sh1,
const _Item< Sh > &  sh2 
)
inlinestaticinherited
424 {
425 return sl::intersects(sh1.transformedShape(),
426 sh2.transformedShape());
427 }

◆ invalidateCache()

void libnest2d::_Item< Sh >::invalidateCache ( ) const
inlineprivateinherited
450 {
451 tr_cache_valid_ = false;
452 lmb_valid_ = false; rmt_valid_ = false;
453 area_cache_valid_ = false;
454 inflate_cache_valid_ = false;
455 bb_cache_.valid = false;
456 convexity_ = Convexity::UNCHECKED;
457 }
bool lmb_valid_
Definition nester.hpp:65
Convexity convexity_
Definition nester.hpp:62
bool rmt_valid_
Definition nester.hpp:65
bool tr_cache_valid_
Definition nester.hpp:50

◆ isContourConvex()

bool libnest2d::_Item< Sh >::isContourConvex ( ) const
inlineinherited
245 {
246 bool ret = false;
247
248 switch(convexity_) {
249 case Convexity::UNCHECKED:
250 ret = sl::isConvex(sl::contour(transformedShape()));
251 convexity_ = ret? Convexity::C_TRUE : Convexity::C_FALSE;
252 break;
253 case Convexity::C_TRUE: ret = true; break;
254 case Convexity::C_FALSE:;
255 }
256
257 return ret;
258 }
const Sh & transformedShape() const
Definition nester.hpp:353

◆ isFixed()

bool libnest2d::_Item< Sh >::isFixed ( ) const
inlinenoexceptinherited
132{ return fixed_; }
bool fixed_
Definition nester.hpp:72

◆ isHoleConvex()

bool libnest2d::_Item< Sh >::isHoleConvex ( unsigned  ) const
inlineinherited
260 {
261 return false;
262 }

◆ isInside() [1/5]

bool libnest2d::_Item< Sh >::isInside ( const _Box< TPoint< Sh > > &  box) const
inlineinherited
504 {
505 return sl::isInside(boundingBox(), box);
506}
Box boundingBox() const
Definition nester.hpp:382

◆ isInside() [2/5]

bool libnest2d::_Item< Sh >::isInside ( const _Circle< TPoint< Sh > > &  box) const
inlineinherited
509 {
510 return sl::isInside(transformedShape(), circ);
511}

◆ isInside() [3/5]

bool libnest2d::_Item< Sh >::isInside ( const _Item< Sh > &  sh) const
inlineinherited
288 {
289 return sl::isInside(transformedShape(), sh.transformedShape());
290 }

◆ isInside() [4/5]

bool libnest2d::_Item< Sh >::isInside ( const Sh &  sh) const
inlineinherited
293 {
294 return sl::isInside(transformedShape(), sh);
295 }

◆ isInside() [5/5]

bool libnest2d::_Item< Sh >::isInside ( const Vertex p) const
inlineinherited

isPointInside

Parameters
p
Returns
283 {
284 return sl::isInside(p, transformedShape());
285 }

◆ leftmostBottomVertex()

Vertex libnest2d::_Item< Sh >::leftmostBottomVertex ( ) const
inlineinherited
412 {
413 if(!lmb_valid_ || !tr_cache_valid_) { // find min x and min y vertex
414 auto& tsh = transformedShape();
415 lmb_ = std::min_element(sl::cbegin(tsh), sl::cend(tsh), vsort);
416 lmb_valid_ = true;
417 }
418 return *lmb_;
419 }
VertexConstIterator lmb_
Definition nester.hpp:64

◆ markAsFixedInBin()

void libnest2d::_Item< Sh >::markAsFixedInBin ( int  binid)
inlineinherited
134 {
135 fixed_ = binid >= 0;
136 binid_ = binid;
137 }

◆ onPacked()

void libnest2d::_Item< Sh >::onPacked ( )
inlineinherited
221 {
222 if (on_packed_)
223 on_packed_(*this);
224 }
std::function< void(_Item &)> on_packed_
Definition nester.hpp:73

◆ operator Sh()

libnest2d::_Item< Sh >::operator Sh ( ) const
inlineinherited
367 {
368 return transformedShape();
369 }

◆ orientation()

static BP2D_CONSTEXPR Orientation libnest2d::_Item< Sh >::orientation ( )
inlinestaticinherited

Get the orientation of the polygon.

The orientation have to be specified as a specialization of the OrientationType struct which has a Value constant.

Returns
The orientation type identifier for the _Item type.
99 {
100 return OrientationType<TContour<RawShape>>::Value;
101 }

◆ priority() [1/2]

int libnest2d::_Item< Sh >::priority ( ) const
inlinenoexceptinherited
143{ return priority_; }
int priority_
Definition nester.hpp:71

◆ priority() [2/2]

void libnest2d::_Item< Sh >::priority ( int  p)
inlineinherited
142{ priority_ = p; }

◆ rawShape()

const Sh & libnest2d::_Item< Sh >::rawShape ( ) const
inlineinherited
372 {
373 return sh_;
374 }

◆ referenceVertex()

Vertex libnest2d::_Item< Sh >::referenceVertex ( ) const
inlineinherited
399 {
400 return rightmostTopVertex();
401 }
Vertex rightmostTopVertex() const
Definition nester.hpp:403

◆ resetTransformation()

void libnest2d::_Item< Sh >::resetTransformation ( )
inlineinherited
377 {
378 has_translation_ = false; has_rotation_ = false; has_inflation_ = false;
380 }
bool has_translation_
Definition nester.hpp:46
bool has_rotation_
Definition nester.hpp:46

◆ rightmostTopVertex()

Vertex libnest2d::_Item< Sh >::rightmostTopVertex ( ) const
inlineinherited
403 {
404 if(!rmt_valid_ || !tr_cache_valid_) { // find max x and max y vertex
405 auto& tsh = transformedShape();
406 rmt_ = std::max_element(sl::cbegin(tsh), sl::cend(tsh), vsort);
407 rmt_valid_ = true;
408 }
409 return *rmt_;
410 }
VertexConstIterator rmt_
Definition nester.hpp:63

◆ rotate()

void libnest2d::_Item< Sh >::rotate ( const Radians rads)
inlineinherited
306 {
307 rotation(rotation() + rads);
308 }
Radians rotation() const BP2D_NOEXCEPT
Definition nester.hpp:326

◆ rotation() [1/2]

Radians libnest2d::_Item< Sh >::rotation ( ) const
inlineinherited
327 {
328 return rotation_;
329 }
Radians rotation_
Definition nester.hpp:40

◆ rotation() [2/2]

void libnest2d::_Item< Sh >::rotation ( Radians  rot)
inlineinherited
337 {
338 if(rotation_ != rot) {
339 rotation_ = rot; has_rotation_ = true; tr_cache_valid_ = false;
340 rmt_valid_ = false; lmb_valid_ = false;
341 bb_cache_.valid = false;
342 }
343 }

◆ setOnPackedFn()

void libnest2d::_Item< Sh >::setOnPackedFn ( std::function< void(_Item< Sh > &)>  onpackedfn)
inlineinherited
216 {
217 on_packed_ = onpackedfn;
218 }

◆ setShape()

void libnest2d::_Item< Sh >::setShape ( Sh  rsh)
inlineinherited
210 {
211 sh_ = std::move(rsh);
213 }

◆ setVertex()

void libnest2d::_Item< Sh >::setVertex ( unsigned long  idx,
const Vertex v 
)
inlineinherited

Modify a vertex.

Note that this method will invalidate every cached calculation result including polygon offset and transformations.

Parameters
idxThe index of the requested vertex.
vThe new vertex data.
204 {
206 sl::vertex(sh_, idx) = v;
207 }

◆ toString()

std::string libnest2d::_Item< Sh >::toString ( ) const
inlineinherited

Convert the polygon to string representation. The format depends on the implementation of the polygon.

Returns
151 {
152 return sl::toString(sh_);
153 }

◆ touches()

static bool libnest2d::_Item< Sh >::touches ( const _Item< Sh > &  sh1,
const _Item< Sh > &  sh2 
)
inlinestaticinherited
430 {
431 return sl::touches(sh1.transformedShape(),
432 sh2.transformedShape());
433 }

◆ transformedShape()

const Sh & libnest2d::_Item< Sh >::transformedShape ( ) const
inlineinherited
354 {
355 if(tr_cache_valid_) return tr_cache_;
356
357 RawShape cpy = infaltedShape();
358 if(has_rotation_) sl::rotate(cpy, rotation_);
359 if(has_translation_) sl::translate(cpy, translation_);
360 tr_cache_ = cpy; tr_cache_valid_ = true;
361 rmt_valid_ = false; lmb_valid_ = false;
362
363 return tr_cache_;
364 }
Sh tr_cache_
Definition nester.hpp:49

◆ translate()

void libnest2d::_Item< Sh >::translate ( const Vertex d)
inlineinherited
301 {
303 }
TPoint< Sh > translation() const BP2D_NOEXCEPT
Definition nester.hpp:331

◆ translation() [1/2]

TPoint< Sh > libnest2d::_Item< Sh >::translation ( ) const
inlineinherited
332 {
333 return translation_;
334 }

◆ translation() [2/2]

void libnest2d::_Item< Sh >::translation ( const TPoint< Sh > &  tr)
inlineinherited
346 {
347 if(translation_ != tr) {
348 translation_ = tr; has_translation_ = true; tr_cache_valid_ = false;
349 //bb_cache_.valid = false;
350 }
351 }

◆ vertex()

Vertex libnest2d::_Item< Sh >::vertex ( unsigned long  idx) const
inlineinherited

Get a copy of an outer vertex within the carried shape.

Note that the vertex considered here is taken from the original shape that this item is constructed from. This means that no transformation is applied to the shape in this call.

Parameters
idxThe index of the requested vertex.
Returns
A copy of the requested vertex.
190 {
191 return sl::vertex(sh_, idx);
192 }

◆ vertexCount()

size_t libnest2d::_Item< Sh >::vertexCount ( ) const
inlineinherited

The number of the outer ring vertices.

269 {
270 return sl::contourVertexCount(sh_);
271 }

◆ vsort()

static bool libnest2d::_Item< Sh >::vsort ( const Vertex v1,
const Vertex v2 
)
inlinestaticprivateinherited
460 {
461 TCompute<Vertex> x1 = getX(v1), x2 = getX(v2);
462 TCompute<Vertex> y1 = getY(v1), y2 = getY(v2);
463 return y1 == y2 ? x1 < x2 : y1 < y2;
464 }
TCoord< P > getX(const P &p)
Definition geometry_traits.hpp:424

◆ width()

template<class Sh >
Unit libnest2d::_Rectangle< Sh >::width ( ) const
inline
494 {
495 return getX(vertex(2));
496 }

References libnest2d::getX(), and libnest2d::_Item< Sh >::vertex().

+ Here is the call graph for this function:

Member Data Documentation

◆ area_cache_

double libnest2d::_Item< Sh >::area_cache_
mutableprivateinherited

◆ area_cache_valid_

bool libnest2d::_Item< Sh >::area_cache_valid_
mutableprivateinherited

◆ bb_cache_

struct libnest2d::_Item::BBCache libnest2d::_Item< Sh >::bb_cache_
privateinherited

◆ binid_

int libnest2d::_Item< Sh >::binid_
privateinherited

◆ convexity_

Convexity libnest2d::_Item< Sh >::convexity_
mutableprivateinherited

◆ fixed_

bool libnest2d::_Item< Sh >::fixed_
privateinherited

◆ has_inflation_

bool libnest2d::_Item< Sh >::has_inflation_
privateinherited

◆ has_rotation_

bool libnest2d::_Item< Sh >::has_rotation_
privateinherited

◆ has_translation_

bool libnest2d::_Item< Sh >::has_translation_
privateinherited

◆ inflate_cache_

Sh libnest2d::_Item< Sh >::inflate_cache_
mutableprivateinherited

◆ inflate_cache_valid_

bool libnest2d::_Item< Sh >::inflate_cache_valid_
mutableprivateinherited

◆ inflation_

Coord libnest2d::_Item< Sh >::inflation_
privateinherited

◆ lmb_

VertexConstIterator libnest2d::_Item< Sh >::lmb_
mutableprivateinherited

◆ lmb_valid_

bool libnest2d::_Item< Sh >::lmb_valid_
privateinherited

◆ on_packed_

std::function<void(_Item&)> libnest2d::_Item< Sh >::on_packed_
privateinherited

◆ priority_

int libnest2d::_Item< Sh >::priority_
privateinherited

◆ rmt_

VertexConstIterator libnest2d::_Item< Sh >::rmt_
mutableprivateinherited

◆ rmt_valid_

bool libnest2d::_Item< Sh >::rmt_valid_
mutableprivateinherited

◆ rotation_

Radians libnest2d::_Item< Sh >::rotation_
privateinherited

◆ sh_

Sh libnest2d::_Item< Sh >::sh_
privateinherited

◆ tr_cache_

Sh libnest2d::_Item< Sh >::tr_cache_
mutableprivateinherited

◆ tr_cache_valid_

bool libnest2d::_Item< Sh >::tr_cache_valid_
mutableprivateinherited

◆ translation_

Vertex libnest2d::_Item< Sh >::translation_
privateinherited

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