OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::Arachne::SquareGrid Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/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.
18{
19 assert(cell_size > 0U);
20}
coord_t cell_size
The cell (square) size.
Definition SquareGrid.hpp:98

References cell_size.

Referenced by Slic3r::Arachne::SparseGrid< ElemT >::SparseGrid(), and processLineCells().

Here is the caller graph for this function:

Member Function Documentation

◆ getCellSize()

coord_t SquareGrid::getCellSize ( ) const

Get the cell size this grid was created for.

152{
153 return cell_size;
154}

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.
147{
148 return (z >= 0) - (z < 0);
149}

◆ 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.
53{
54 return static_cast<const SquareGrid*>(this)->processLineCells(line, process_cell_func);
55}
SquareGrid(const coord_t cell_size)
Constructs a grid with the specified cell size.
Definition SquareGrid.cpp:17
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:52

References SquareGrid(), and 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.
59{
60 Point start = line.first;
61 Point end = line.second;
62 if (end.x() < start.x())
63 { // make sure X increases between start and end
64 std::swap(start, end);
65 }
66
67 const GridPoint start_cell = toGridPoint(start.cast<int64_t>());
68 const GridPoint end_cell = toGridPoint(end.cast<int64_t>());
69 const int64_t y_diff = int64_t(end.y() - start.y());
70 const grid_coord_t y_dir = nonzeroSign(y_diff);
71
72 /* This line drawing algorithm iterates over the range of Y coordinates, and
73 for each Y coordinate computes the range of X coordinates crossed in one
74 unit of Y. These ranges are rounded to be inclusive, so effectively this
75 creates a "fat" line, marking more cells than a strict one-cell-wide path.*/
76 grid_coord_t x_cell_start = start_cell.x();
77 for (grid_coord_t cell_y = start_cell.y(); cell_y * y_dir <= end_cell.y() * y_dir; cell_y += y_dir)
78 { // for all Y from start to end
79 // nearest y coordinate of the cells in the next row
80 const coord_t nearest_next_y = toLowerCoord(cell_y + ((nonzeroSign(cell_y) == y_dir || cell_y == 0) ? y_dir : coord_t(0)));
81 grid_coord_t x_cell_end; // the X coord of the last cell to include from this row
82 if (y_diff == 0)
83 {
84 x_cell_end = end_cell.x();
85 }
86 else
87 {
88 const int64_t area = int64_t(end.x() - start.x()) * int64_t(nearest_next_y - start.y());
89 // corresponding_x: the x coordinate corresponding to nearest_next_y
90 int64_t corresponding_x = int64_t(start.x()) + area / y_diff;
91 x_cell_end = toGridCoord(corresponding_x + ((corresponding_x < 0) && ((area % y_diff) != 0)));
92 if (x_cell_end < start_cell.x())
93 { // process at least one cell!
94 x_cell_end = x_cell_start;
95 }
96 }
97
98 for (grid_coord_t cell_x = x_cell_start; cell_x <= x_cell_end; ++cell_x)
99 {
100 GridPoint grid_loc(cell_x, cell_y);
101 if (! process_cell_func(grid_loc))
102 {
103 return false;
104 }
105 if (grid_loc == end_cell)
106 {
107 return true;
108 }
109 }
110 // TODO: this causes at least a one cell overlap for each row, which
111 // includes extra cells when crossing precisely on the corners
112 // where positive slope where x > 0 and negative slope where x < 0
113 x_cell_start = x_cell_end;
114 }
115 assert(false && "We should have returned already before here!");
116 return false;
117}
grid_coord_t toGridCoord(const int64_t &coord) const
Compute the grid coordinate of a real space coordinate.
Definition SquareGrid.cpp:29
grid_coord_t nonzeroSign(grid_coord_t z) const
Definition SquareGrid.cpp:146
Point GridPoint
Definition SquareGrid.hpp:41
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:40
coord_t grid_coord_t
Definition SquareGrid.hpp:42
GridPoint toGridPoint(const Vec2i64 &point) const
Compute the grid coordinates of a point.
Definition SquareGrid.cpp:23
int64_t coord_t
Definition libslic3r.h:48
T * end(Slic3r::Mat< N, M, T > &mat)
Definition Point.hpp:841
Kernel::Point_3 Point
Definition MeshRepair.cpp:81
double area(const ExPolygon &poly)
Definition ExPolygon.hpp:511

◆ 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.
125{
126 const Point min_loc(query_pt.x() - radius, query_pt.y() - radius);
127 const Point max_loc(query_pt.x() + radius, query_pt.y() + radius);
128
129 GridPoint min_grid = toGridPoint(min_loc.cast<int64_t>());
130 GridPoint max_grid = toGridPoint(max_loc.cast<int64_t>());
131
132 for (coord_t grid_y = min_grid.y(); grid_y <= max_grid.y(); ++grid_y)
133 {
134 for (coord_t grid_x = min_grid.x(); grid_x <= max_grid.x(); ++grid_x)
135 {
136 GridPoint grid_pt(grid_x,grid_y);
137 if (!process_func(grid_pt))
138 {
139 return false;
140 }
141 }
142 }
143 return true;
144}

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.
30{
31 // This mapping via truncation results in the cells with
32 // GridPoint.x==0 being twice as large and similarly for
33 // GridPoint.y==0. This doesn't cause any incorrect behavior,
34 // just changes the running time slightly. The change in running
35 // time from this is probably not worth doing a proper floor
36 // operation.
37 return coord / cell_size;
38}

References cell_size.

Referenced by Slic3r::Arachne::SparseLineGrid< PolygonsPointIndex, PolygonsPointIndexSegmentLocator >::toGridCoord(), 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.
24{
25 return Point(toGridCoord(point.x()), toGridCoord(point.y()));
26}

References toGridCoord().

Referenced by Slic3r::Arachne::SparsePointGrid< ElemT, Locator >::insert(), 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.
41{
42 // This mapping via truncation results in the cells with
43 // GridPoint.x==0 being twice as large and similarly for
44 // GridPoint.y==0. This doesn't cause any incorrect behavior,
45 // just changes the running time slightly. The change in running
46 // time from this is probably not worth doing a proper floor
47 // operation.
48 return grid_coord * cell_size;
49}

References cell_size.

Member Data Documentation

◆ cell_size


The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Arachne/utils/SquareGrid.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Arachne/utils/SquareGrid.cpp