Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::Arachne::SquareGrid Class Reference

#include <src/libslic3r/Arachne/utils/SquareGrid.hpp>

+ Inheritance diagram for Slic3r::Arachne::SquareGrid:

Public Types

using GridPoint = Point
 
using grid_coord_t = coord_t
 

Public Member Functions

 SquareGrid (const coord_t cell_size)
 Constructs a grid with the specified cell size.
 
coord_t getCellSize () const
 
bool processLineCells (const std::pair< Point, Point > line, const std::function< bool(GridPoint)> &process_cell_func)
 Process cells along a line indicated by line.
 
bool processLineCells (const std::pair< Point, Point > line, const std::function< bool(GridPoint)> &process_cell_func) const
 Process cells along a line indicated by line.
 
bool processNearby (const Point &query_pt, coord_t radius, const std::function< bool(const GridPoint &)> &process_func) const
 Process cells that might contain sought after points.
 
GridPoint toGridPoint (const Vec2i64 &point) const
 Compute the grid coordinates of a point.
 
grid_coord_t toGridCoord (const int64_t &coord) const
 Compute the grid coordinate of a real space coordinate.
 
coord_t toLowerCoord (const grid_coord_t &grid_coord) const
 Compute the lowest coord in a grid cell. The lowest point is the point in the grid cell closest to the origin.
 

Protected Member Functions

grid_coord_t nonzeroSign (grid_coord_t z) const
 

Protected Attributes

coord_t cell_size
 The cell (square) size.
 

Detailed Description

Helper class to calculate coordinates on a square grid, and providing some utility functions to process grids.

Doesn't contain any data, except cell size. The purpose is only to automatically generate coordinates on a grid, and automatically feed them to functions. The grid is theoretically infinite (bar integer limits).

Member Typedef Documentation

◆ grid_coord_t

◆ GridPoint

Constructor & Destructor Documentation

◆ SquareGrid()

SquareGrid::SquareGrid ( const coord_t  cell_size)

Constructs a grid with the specified cell size.

Parameters
[in]cell_sizeThe size to use for a cell (square) in the grid.
11{
12 assert(cell_size > 0U);
13}
coord_t cell_size
The cell (square) size.
Definition SquareGrid.hpp:94

References cell_size.

Member Function Documentation

◆ getCellSize()

coord_t SquareGrid::getCellSize ( ) const

Get the cell size this grid was created for.

145{
146 return cell_size;
147}

References cell_size.

◆ nonzeroSign()

SquareGrid::grid_coord_t SquareGrid::nonzeroSign ( grid_coord_t  z) const
protected

Compute the sign of a number.

The number 0 will result in a positive sign (1).

Parameters
zThe number to find the sign of.
Returns
1 if the number is positive or 0, or -1 if the number is negative.
140{
141 return (z >= 0) - (z < 0);
142}

Referenced by processLineCells().

+ Here is the caller graph for this function:

◆ processLineCells() [1/2]

bool SquareGrid::processLineCells ( const std::pair< Point, Point line,
const std::function< bool(GridPoint)> &  process_cell_func 
)

Process cells along a line indicated by line.

Parameters
lineThe line along which to process cells.
process_funcProcesses each cell. process_func(elem) is called for each cell. Processing stops if function returns false.
Returns
Whether we need to continue processing after this function.
46{
47 return static_cast<const SquareGrid*>(this)->processLineCells(line, process_cell_func);
48}
Definition SquareGrid.hpp:25
bool processLineCells(const std::pair< Point, Point > line, const std::function< bool(GridPoint)> &process_cell_func)
Process cells along a line indicated by line.
Definition SquareGrid.cpp:45

References processLineCells().

Referenced by Slic3r::Arachne::SparseLineGrid< ElemT, Locator >::insert(), and processLineCells().

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

◆ processLineCells() [2/2]

bool SquareGrid::processLineCells ( const std::pair< Point, Point line,
const std::function< bool(GridPoint)> &  process_cell_func 
) const

Process cells along a line indicated by line.

Parameters
lineThe line along which to process cells
process_funcProcesses each cell. process_func(elem) is called for each cell. Processing stops if function returns false.
Returns
Whether we need to continue processing after this function.
52{
53 Point start = line.first;
54 Point end = line.second;
55 if (end.x() < start.x())
56 { // make sure X increases between start and end
57 std::swap(start, end);
58 }
59
60 const GridPoint start_cell = toGridPoint(start.cast<int64_t>());
61 const GridPoint end_cell = toGridPoint(end.cast<int64_t>());
62 const int64_t y_diff = int64_t(end.y() - start.y());
63 const grid_coord_t y_dir = nonzeroSign(y_diff);
64
65 /* This line drawing algorithm iterates over the range of Y coordinates, and
66 for each Y coordinate computes the range of X coordinates crossed in one
67 unit of Y. These ranges are rounded to be inclusive, so effectively this
68 creates a "fat" line, marking more cells than a strict one-cell-wide path.*/
69 grid_coord_t x_cell_start = start_cell.x();
70 for (grid_coord_t cell_y = start_cell.y(); cell_y * y_dir <= end_cell.y() * y_dir; cell_y += y_dir)
71 { // for all Y from start to end
72 // nearest y coordinate of the cells in the next row
73 const coord_t nearest_next_y = toLowerCoord(cell_y + ((nonzeroSign(cell_y) == y_dir || cell_y == 0) ? y_dir : coord_t(0)));
74 grid_coord_t x_cell_end; // the X coord of the last cell to include from this row
75 if (y_diff == 0)
76 {
77 x_cell_end = end_cell.x();
78 }
79 else
80 {
81 const int64_t area = int64_t(end.x() - start.x()) * int64_t(nearest_next_y - start.y());
82 // corresponding_x: the x coordinate corresponding to nearest_next_y
83 int64_t corresponding_x = int64_t(start.x()) + area / y_diff;
84 x_cell_end = toGridCoord(corresponding_x + ((corresponding_x < 0) && ((area % y_diff) != 0)));
85 if (x_cell_end < start_cell.x())
86 { // process at least one cell!
87 x_cell_end = x_cell_start;
88 }
89 }
90
91 for (grid_coord_t cell_x = x_cell_start; cell_x <= x_cell_end; ++cell_x)
92 {
93 GridPoint grid_loc(cell_x, cell_y);
94 if (! process_cell_func(grid_loc))
95 {
96 return false;
97 }
98 if (grid_loc == end_cell)
99 {
100 return true;
101 }
102 }
103 // TODO: this causes at least a one cell overlap for each row, which
104 // includes extra cells when crossing precisely on the corners
105 // where positive slope where x > 0 and negative slope where x < 0
106 x_cell_start = x_cell_end;
107 }
108 assert(false && "We should have returned already before here!");
109 return false;
110}
grid_coord_t toGridCoord(const int64_t &coord) const
Compute the grid coordinate of a real space coordinate.
Definition SquareGrid.cpp:22
grid_coord_t nonzeroSign(grid_coord_t z) const
Definition SquareGrid.cpp:139
Point GridPoint
Definition SquareGrid.hpp:37
coord_t toLowerCoord(const grid_coord_t &grid_coord) const
Compute the lowest coord in a grid cell. The lowest point is the point in the grid cell closest to th...
Definition SquareGrid.cpp:33
coord_t grid_coord_t
Definition SquareGrid.hpp:38
GridPoint toGridPoint(const Vec2i64 &point) const
Compute the grid coordinates of a point.
Definition SquareGrid.cpp:16
int32_t coord_t
Definition libslic3r.h:39
double area(const ExPolygon &poly)
Definition ExPolygon.hpp:467
S::iterator end(S &sh, const PathTag &)
Definition geometry_traits.hpp:620
Kernel::Point_2 Point
Definition point_areas.cpp:20
__int64 int64_t
Definition unistd.h:76

References Slic3r::area(), nonzeroSign(), toGridCoord(), toGridPoint(), and toLowerCoord().

+ Here is the call graph for this function:

◆ processNearby()

bool SquareGrid::processNearby ( const Point query_pt,
coord_t  radius,
const std::function< bool(const GridPoint &)> &  process_func 
) const

Process cells that might contain sought after points.

Processes cells that might be within a square with twice radius as width, centered around query_pt. May process elements that are up to radius + cell_size from query_pt.

Parameters
query_ptThe point to search around.
radiusThe search radius.
process_funcProcesses each cell. process_func(loc) is called for each cell coord within range. Processing stops if function returns false.
Returns
Whether we need to continue processing after this function.
118{
119 const Point min_loc(query_pt.x() - radius, query_pt.y() - radius);
120 const Point max_loc(query_pt.x() + radius, query_pt.y() + radius);
121
122 GridPoint min_grid = toGridPoint(min_loc.cast<int64_t>());
123 GridPoint max_grid = toGridPoint(max_loc.cast<int64_t>());
124
125 for (coord_t grid_y = min_grid.y(); grid_y <= max_grid.y(); ++grid_y)
126 {
127 for (coord_t grid_x = min_grid.x(); grid_x <= max_grid.x(); ++grid_x)
128 {
129 GridPoint grid_pt(grid_x,grid_y);
130 if (!process_func(grid_pt))
131 {
132 return false;
133 }
134 }
135 }
136 return true;
137}

References toGridPoint().

Referenced by Slic3r::Arachne::SparseGrid< ElemT >::processNearby().

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

◆ toGridCoord()

SquareGrid::grid_coord_t SquareGrid::toGridCoord ( const int64_t coord) const

Compute the grid coordinate of a real space coordinate.

Parameters
coordThe actual location.
Returns
The grid coordinate that corresponds to coord.
23{
24 // This mapping via truncation results in the cells with
25 // GridPoint.x==0 being twice as large and similarly for
26 // GridPoint.y==0. This doesn't cause any incorrect behavior,
27 // just changes the running time slightly. The change in running
28 // time from this is probably not worth doing a proper floor
29 // operation.
30 return coord / cell_size;
31}

References cell_size.

Referenced by processLineCells(), and toGridPoint().

+ Here is the caller graph for this function:

◆ toGridPoint()

SquareGrid::GridPoint SquareGrid::toGridPoint ( const Vec2i64 point) const

Compute the grid coordinates of a point.

Parameters
pointThe actual location.
Returns
The grid coordinates that correspond to point.
17{
18 return Point(toGridCoord(point.x()), toGridCoord(point.y()));
19}

References toGridCoord().

Referenced by Slic3r::Arachne::SparsePointGrid< ElemT, Locator >::insert(), processLineCells(), and processNearby().

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

◆ toLowerCoord()

coord_t SquareGrid::toLowerCoord ( const grid_coord_t grid_coord) const

Compute the lowest coord in a grid cell. The lowest point is the point in the grid cell closest to the origin.

Parameters
grid_coordThe grid coordinate.
Returns
The print space coordinate that corresponds to grid_coord.
34{
35 // This mapping via truncation results in the cells with
36 // GridPoint.x==0 being twice as large and similarly for
37 // GridPoint.y==0. This doesn't cause any incorrect behavior,
38 // just changes the running time slightly. The change in running
39 // time from this is probably not worth doing a proper floor
40 // operation.
41 return grid_coord * cell_size;
42}

References cell_size.

Referenced by processLineCells().

+ Here is the caller graph for this function:

Member Data Documentation

◆ cell_size

coord_t Slic3r::Arachne::SquareGrid::cell_size
protected

The cell (square) size.

Referenced by SquareGrid(), getCellSize(), toGridCoord(), and toLowerCoord().


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