OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::VoxelUtils Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Feature/Interlocking/VoxelUtils.hpp>

Public Types

using grid_coord_t = coord_t

Public Member Functions

 VoxelUtils (Vec3crd cell_size)
bool walkLine (Vec3crd start, Vec3crd end, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkPolygons (const ExPolygon &polys, coord_t z, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkDilatedPolygons (const ExPolygon &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkDilatedPolygons (const ExPolygons &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkAreas (const ExPolygon &polys, coord_t z, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkDilatedAreas (const ExPolygon &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
bool walkDilatedAreas (const ExPolygons &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
std::function< bool(GridPoint3)> dilate (const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
GridPoint3 toGridPoint (const Vec3crd &point) const
grid_coord_t toGridCoord (const coord_t &coord, const size_t dim) const
Vec3crd toLowerCorner (const GridPoint3 &location) const
coord_t toLowerCoord (const grid_coord_t &grid_coord, const size_t dim) const
Polygon toPolygon (const GridPoint3 p) const

Public Attributes

Vec3crd cell_size_

Private Member Functions

bool _walkAreas (const ExPolygon &polys, coord_t z, const std::function< bool(GridPoint3)> &process_cell_func) const

Detailed Description

Utility class for walking over a 3D voxel grid.

Contains the math for intersecting voxels with lines, polgons, areas, etc.

Member Typedef Documentation

◆ grid_coord_t

Constructor & Destructor Documentation

◆ VoxelUtils()

Slic3r::VoxelUtils::VoxelUtils ( Vec3crd cell_size)
inline
75 : cell_size_(cell_size)
76 {
77 }
Vec3crd cell_size_
Definition VoxelUtils.hpp:72

References cell_size_.

Member Function Documentation

◆ _walkAreas()

bool Slic3r::VoxelUtils::_walkAreas ( const ExPolygon & polys,
coord_t z,
const std::function< bool(GridPoint3)> & process_cell_func ) const
private
Warning
the polys is assumed to be translated by half the cell_size in xy already
189{
190 Points skin_points = spreadDotsArea(polys, Point(cell_size_.x(), cell_size_.y()));
191 for (Point p : skin_points)
192 {
193 bool continue_ = process_cell_func(toGridPoint(Vec3crd(p.x() + cell_size_.x() / 2, p.y() + cell_size_.y() / 2, z)));
194 if (! continue_)
195 {
196 return false;
197 }
198 }
199 return true;
200}
GridPoint3 toGridPoint(const Vec3crd &point) const
Definition VoxelUtils.hpp:178
Kernel::Point_3 Point
Definition MeshRepair.cpp:81
static Points spreadDotsArea(const ExPolygon &polygons, Point grid_size)
Definition VoxelUtils.cpp:155
Eigen::Matrix< coord_t, 3, 1, Eigen::DontAlign > Vec3crd
Definition Point.hpp:53
std::vector< Point, PointsAllocator< Point > > Points
Definition Point.hpp:73

References _walkAreas(), cell_size_, Slic3r::spreadDotsArea(), and toGridPoint().

Referenced by _walkAreas(), walkAreas(), and walkDilatedAreas().

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

◆ dilate()

std::function< bool(GridPoint3)> Slic3r::VoxelUtils::dilate ( const DilationKernel & kernel,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Dilate with a kernel.

Extends the process_cell_func, so that for each cell we process nearby cells as well.

Apply this function to a process_cell_func to create a new process_cell_func which applies the effect to nearby voxels as well.

Parameters
kernelThe offset positions relative to the input of process_cell_func
process_cell_funcFunction to perform on each voxel cell
219{
220 return [&process_cell_func, &kernel](GridPoint3 loc)
221 {
222 for (const GridPoint3& rel : kernel.relative_cells_)
223 {
224 bool continue_ = process_cell_func(loc + rel);
225 if (! continue_)
226 return false;
227 }
228 return true;
229 };
230}
Vec3crd GridPoint3
Definition VoxelUtils.hpp:20

References dilate(), and Slic3r::DilationKernel::relative_cells_.

Referenced by dilate(), walkDilatedAreas(), and walkDilatedPolygons().

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

◆ toGridCoord()

grid_coord_t Slic3r::VoxelUtils::toGridCoord ( const coord_t & coord,
const size_t dim ) const
inline
184 {
185 assert(dim < 3);
186 return coord / cell_size_[dim] - (coord < 0);
187 }

References cell_size_.

Referenced by toGridPoint().

Here is the caller graph for this function:

◆ toGridPoint()

GridPoint3 Slic3r::VoxelUtils::toGridPoint ( const Vec3crd & point) const
inline
179 {
180 return GridPoint3(toGridCoord(point.x(), 0), toGridCoord(point.y(), 1), toGridCoord(point.z(), 2));
181 }
grid_coord_t toGridCoord(const coord_t &coord, const size_t dim) const
Definition VoxelUtils.hpp:183

References toGridCoord().

Referenced by _walkAreas(), and walkLine().

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

◆ toLowerCoord()

coord_t Slic3r::VoxelUtils::toLowerCoord ( const grid_coord_t & grid_coord,
const size_t dim ) const
inline
195 {
196 assert(dim < 3);
197 return grid_coord * cell_size_[dim];
198 }

References cell_size_.

Referenced by toLowerCorner(), and walkLine().

Here is the caller graph for this function:

◆ toLowerCorner()

Vec3crd Slic3r::VoxelUtils::toLowerCorner ( const GridPoint3 & location) const
inline
190 {
191 return Vec3crd(toLowerCoord(location.x(), 0), toLowerCoord(location.y(), 1), toLowerCoord(location.z(), 2));
192 }
coord_t toLowerCoord(const grid_coord_t &grid_coord, const size_t dim) const
Definition VoxelUtils.hpp:194

References toLowerCoord().

Referenced by toPolygon().

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

◆ toPolygon()

Polygon Slic3r::VoxelUtils::toPolygon ( const GridPoint3 p) const
inline

Returns a rectangular polygon equal to the cross section of a voxel cell at coordinate p

204 {
205 Polygon ret;
206 Vec3crd c = toLowerCorner(p);
207 ret.append({c.x(), c.y()});
208 ret.append({c.x() + cell_size_.x(), c.y()});
209 ret.append({c.x() + cell_size_.x(), c.y() + cell_size_.y()});
210 ret.append({c.x(), c.y() + cell_size_.y()});
211 return ret;
212 }
Vec3crd toLowerCorner(const GridPoint3 &location) const
Definition VoxelUtils.hpp:189
Slic3r::Polygon Polygon
Definition Emboss.cpp:101

References Slic3r::MultiPoint::append(), cell_size_, and toLowerCorner().

Here is the call graph for this function:

◆ walkAreas()

bool Slic3r::VoxelUtils::walkAreas ( const ExPolygon & polys,
coord_t z,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Process all voxels inside the area of a polygons object.

Warning
The voxels along the area are not processed. Thin areas might not process any voxels at all.
Parameters
polysThe area to fill
zThe height at which the polygons occur
process_cell_funcFunction to perform on each voxel cell
Returns
Whether executing was stopped short as indicated by the cell_processing_function
145{
146 ExPolygon translated = polys;
147 const Vec3crd translation = -cell_size_ / 2; // offset half a cell so that the dots of spreadDotsArea are centered on the middle of the cell isntead of the lower corners.
148 if (translation.x() && translation.y())
149 {
150 translated.translate(Point(translation.x(), translation.y()));
151 }
152 return _walkAreas(translated, z, process_cell_func);
153}
bool _walkAreas(const ExPolygon &polys, coord_t z, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:188

References _walkAreas(), cell_size_, Slic3r::ExPolygon::translate(), and walkAreas().

Referenced by walkAreas().

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

◆ walkDilatedAreas() [1/2]

bool Slic3r::VoxelUtils::walkDilatedAreas ( const ExPolygon & polys,
coord_t z,
const DilationKernel & kernel,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Process all voxels inside the area of a polygons object. For each voxel inside the polygon we process each of the offset voxels according to the kernel.

Warning
The voxels along the area are not processed. Thin areas might not process any voxels at all.
Parameters
polysThe area to fill
zThe height at which the polygons occur
process_cell_funcFunction to perform on each voxel cell
Returns
Whether executing was stopped short as indicated by the cell_processing_function
203{
204 ExPolygon translated = polys;
205 GridPoint3 k = kernel.kernel_size_;
206 k.x() %= 2;
207 k.y() %= 2;
208 k.z() %= 2;
209 const Vec3crd translation = (Vec3crd(1, 1, 1) - k).array() * cell_size_.array() / 2 // offset half a cell when using an even kernel
210 - cell_size_.array() / 2; // offset half a cell so that the dots of spreadDotsArea are centered on the middle of the cell isntead of the lower corners.
211 if (translation.x() && translation.y())
212 {
213 translated.translate(Point(translation.x(), translation.y()));
214 }
215 return _walkAreas(translated, z + translation.z(), dilate(kernel, process_cell_func));
216}
std::function< bool(GridPoint3)> dilate(const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:218

References _walkAreas(), cell_size_, dilate(), Slic3r::DilationKernel::kernel_size_, Slic3r::ExPolygon::translate(), and walkDilatedAreas().

Referenced by walkDilatedAreas(), and walkDilatedAreas().

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

◆ walkDilatedAreas() [2/2]

bool Slic3r::VoxelUtils::walkDilatedAreas ( const ExPolygons & polys,
coord_t z,
const DilationKernel & kernel,
const std::function< bool(GridPoint3)> & process_cell_func ) const
inline
156 {
157 for (const auto & poly : polys) {
158 if (!walkDilatedAreas(poly, z, kernel, process_cell_func)) {
159 return false;
160 }
161 }
162
163 return true;
164 }
bool walkDilatedAreas(const ExPolygon &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:202

References walkDilatedAreas().

Here is the call graph for this function:

◆ walkDilatedPolygons() [1/2]

bool Slic3r::VoxelUtils::walkDilatedPolygons ( const ExPolygon & polys,
coord_t z,
const DilationKernel & kernel,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Process voxels near the line segments of a polygon. For each voxel the polygon crosses we process each of the offset voxels according to the kernel.

Warning
Voxels may be processed multiple times!
Parameters
polysThe polygons to walk
zThe height at which the polygons occur
process_cell_funcFunction to perform on each voxel cell
Returns
Whether executing was stopped short as indicated by the cell_processing_function
130{
131 ExPolygon translated = polys;
132 GridPoint3 k = kernel.kernel_size_;
133 k.x() %= 2;
134 k.y() %= 2;
135 k.z() %= 2;
136 const Vec3crd translation = (Vec3crd(1, 1, 1) - k).array() * cell_size_.array() / 2;
137 if (translation.x() && translation.y())
138 {
139 translated.translate(Point(translation.x(), translation.y()));
140 }
141 return walkPolygons(translated, z + translation.z(), dilate(kernel, process_cell_func));
142}
bool walkPolygons(const ExPolygon &polys, coord_t z, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:111

References cell_size_, dilate(), Slic3r::DilationKernel::kernel_size_, Slic3r::ExPolygon::translate(), walkDilatedPolygons(), and walkPolygons().

Referenced by walkDilatedPolygons(), and walkDilatedPolygons().

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

◆ walkDilatedPolygons() [2/2]

bool Slic3r::VoxelUtils::walkDilatedPolygons ( const ExPolygons & polys,
coord_t z,
const DilationKernel & kernel,
const std::function< bool(GridPoint3)> & process_cell_func ) const
inline
114 {
115 for (const auto & poly : polys) {
116 if (!walkDilatedPolygons(poly, z, kernel, process_cell_func)) {
117 return false;
118 }
119 }
120
121 return true;
122 }
bool walkDilatedPolygons(const ExPolygon &polys, coord_t z, const DilationKernel &kernel, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:129

References walkDilatedPolygons().

Here is the call graph for this function:

◆ walkLine()

bool Slic3r::VoxelUtils::walkLine ( Vec3crd start,
Vec3crd end,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Process voxels which a line segment crosses.

Parameters
startStart point of the line
endEnd point of the line
process_cell_funcFunction to perform on each cell the line crosses
Returns
Whether executing was stopped short as indicated by the cell_processing_function
63{
64 Vec3crd diff = end - start;
65
66 const GridPoint3 start_cell = toGridPoint(start);
67 const GridPoint3 end_cell = toGridPoint(end);
68 if (start_cell == end_cell)
69 {
70 return process_cell_func(start_cell);
71 }
72
73 Vec3crd current_cell = start_cell;
74 while (true)
75 {
76 bool continue_ = process_cell_func(current_cell);
77
78 if (! continue_)
79 {
80 return false;
81 }
82
83 int stepping_dim = -1; // dimension in which the line next exits the current cell
84 double percentage_along_line = std::numeric_limits<double>::max();
85 for (int dim = 0; dim < 3; dim++)
86 {
87 if (diff[dim] == 0)
88 {
89 continue;
90 }
91 coord_t crossing_boundary = toLowerCoord(current_cell[dim], dim) + (diff[dim] > 0) * cell_size_[dim];
92 double percentage_along_line_here = (crossing_boundary - start[dim]) / static_cast<double>(diff[dim]);
93 if (percentage_along_line_here < percentage_along_line)
94 {
95 percentage_along_line = percentage_along_line_here;
96 stepping_dim = dim;
97 }
98 }
99 assert(stepping_dim != -1);
100 if (percentage_along_line > 1.0)
101 {
102 // next cell is beyond the end
103 return true;
104 }
105 current_cell[stepping_dim] += (diff[stepping_dim] > 0) ? 1 : -1;
106 }
107 return true;
108}
int64_t coord_t
Definition libslic3r.h:48
T * end(Slic3r::Mat< N, M, T > &mat)
Definition Point.hpp:841
Slic3r::Polygons diff(const Slic3r::Polygon &subject, const Slic3r::Polygon &clip, ApplySafetyOffset do_safety_offset)
Definition ClipperUtils.cpp:731

References cell_size_, Slic3r::diff(), toGridPoint(), toLowerCoord(), and walkLine().

Referenced by walkLine(), and walkPolygons().

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

◆ walkPolygons()

bool Slic3r::VoxelUtils::walkPolygons ( const ExPolygon & polys,
coord_t z,
const std::function< bool(GridPoint3)> & process_cell_func ) const

Process voxels which the line segments of a polygon crosses.

Warning
Voxels may be processed multiple times!
Parameters
polysThe polygons to walk
zThe height at which the polygons occur
process_cell_funcFunction to perform on each voxel cell
Returns
Whether executing was stopped short as indicated by the cell_processing_function
112{
113 for (const Polygon& poly : to_polygons(polys))
114 {
115 Point last = poly.back();
116 for (Point p : poly)
117 {
118 bool continue_ = walkLine(Vec3crd(last.x(), last.y(), z), Vec3crd(p.x(), p.y(), z), process_cell_func);
119 if (! continue_)
120 {
121 return false;
122 }
123 last = p;
124 }
125 }
126 return true;
127}
bool walkLine(Vec3crd start, Vec3crd end, const std::function< bool(GridPoint3)> &process_cell_func) const
Definition VoxelUtils.cpp:62
void poly(std::vector< std::pair< Vec2d, Vec2d > > &out, std::initializer_list< Vec2d > p)
Definition DesignSketchTool.cpp:7751
Polygons to_polygons(const ExPolygon &src)
Definition ExPolygon.hpp:307

References Slic3r::to_polygons(), walkLine(), and walkPolygons().

Referenced by walkDilatedPolygons(), and walkPolygons().

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

Member Data Documentation

◆ cell_size_


The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Feature/Interlocking/VoxelUtils.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Feature/Interlocking/VoxelUtils.cpp