OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::NearestPoints< K > Class Template Reference
Collaboration diagram for Slic3r::NearestPoints< K >:

Public Member Functions

 NearestPoints (const std::vector< Vec2d > &points)
template<typename Filter>
std::array< size_t, K > query (const Vec2d &q, Filter filter) const

Static Public Attributes

static constexpr size_t npos = size_t(-1)

Private Member Functions

size_t cell (const Vec2d &p) const

Private Attributes

const std::vector< Vec2d > & m_points
Vec2d m_origin
double m_cell
int m_cols
int m_rows
std::vector< size_t > m_first
std::vector< size_t > m_index

Detailed Description

template<size_t K>
class Slic3r::NearestPoints< K >

Constructor & Destructor Documentation

◆ NearestPoints()

template<size_t K>
Slic3r::NearestPoints< K >::NearestPoints ( const std::vector< Vec2d > & points)
inlineexplicit
1105 : m_points(points)
1106 {
1108 for (const Vec2d &p : points)
1109 bbox.merge(p);
1110 const Vec2d size = bbox.size();
1111 const double count = double(std::max<size_t>(points.size(), 1));
1112 m_cell = std::max(std::sqrt(2. * size.x() * size.y() / count), std::max(size.x(), size.y()) / count);
1113 if (m_cell <= 0.)
1114 m_cell = 1.;
1115 m_origin = bbox.min;
1116 m_cols = int(size.x() / m_cell) + 1;
1117 m_rows = int(size.y() / m_cell) + 1;
1118 m_first.assign(size_t(m_cols) * m_rows + 1, 0);
1119 for (const Vec2d &p : points)
1120 ++ m_first[cell(p) + 1];
1121 for (size_t c = 1; c < m_first.size(); ++ c)
1122 m_first[c] += m_first[c - 1];
1123 m_index.resize(points.size());
1124 std::vector<size_t> next(m_first.begin(), m_first.end() - 1);
1125 for (size_t i = 0; i < points.size(); ++ i)
1126 m_index[next[cell(points[i])] ++] = i;
1127 }
Definition ShortestPath.cpp:1101
Vec2d m_origin
Definition ShortestPath.cpp:1185
std::vector< size_t > m_first
Definition ShortestPath.cpp:1189
std::vector< size_t > m_index
Definition ShortestPath.cpp:1190
int m_cols
Definition ShortestPath.cpp:1187
double m_cell
Definition ShortestPath.cpp:1186
size_t cell(const Vec2d &p) const
Definition ShortestPath.cpp:1177
int m_rows
Definition ShortestPath.cpp:1188
const std::vector< Vec2d > & m_points
Definition ShortestPath.cpp:1184

References cell(), count, m_cell, m_cols, m_first, m_index, m_origin, m_points, m_rows, Slic3r::BoundingBoxBase< PointType, APointsType >::merge(), Slic3r::BoundingBoxBase< PointType, APointsType >::min, and Slic3r::BoundingBoxBase< PointType, APointsType >::size().

Here is the call graph for this function:

Member Function Documentation

◆ cell()

template<size_t K>
size_t Slic3r::NearestPoints< K >::cell ( const Vec2d & p) const
inlineprivate
1178 {
1179 const int x = std::clamp(int((p.x() - m_origin.x()) / m_cell), 0, m_cols - 1);
1180 const int y = std::clamp(int((p.y() - m_origin.y()) / m_cell), 0, m_rows - 1);
1181 return size_t(y) * m_cols + x;
1182 }

References m_cell, m_cols, m_origin, and m_rows.

Referenced by NearestPoints().

Here is the caller graph for this function:

◆ query()

template<size_t K>
template<typename Filter>
std::array< size_t, K > Slic3r::NearestPoints< K >::query ( const Vec2d & q,
Filter filter ) const
inline
1132 {
1134 size_t found = 0;
1135 const int cx = int(std::clamp(std::floor((q.x() - m_origin.x()) / m_cell), 0., double(m_cols - 1)));
1136 const int cy = int(std::clamp(std::floor((q.y() - m_origin.y()) / m_cell), 0., double(m_rows - 1)));
1137 auto visit = [&](int x, int y) {
1138 if (x < 0 || y < 0 || x >= m_cols || y >= m_rows)
1139 return;
1140 const size_t c = size_t(y) * m_cols + x;
1141 for (size_t k = m_first[c]; k < m_first[c + 1]; ++ k) {
1142 const size_t i = m_index[k];
1143 if (! filter(i))
1144 continue;
1146 if (found == K && ! (candidate < best[K - 1]))
1147 continue;
1148 auto it = std::upper_bound(best.begin(), best.begin() + found, candidate);
1149 std::move_backward(it, best.begin() + std::min(found, K - 1), best.begin() + std::min(found + 1, K));
1150 *it = candidate;
1151 found = std::min(found + 1, K);
1152 }
1153 };
1154 // Points beyond ring r are at least r cells away.
1155 for (int r = 0; r <= std::max(m_cols, m_rows); ++ r) {
1156 if (r == 0)
1157 visit(cx, cy);
1158 for (int d = -r; d <= r && r > 0; ++ d) {
1159 visit(cx + d, cy - r);
1160 visit(cx + d, cy + r);
1161 if (d != -r && d != r) {
1162 visit(cx - r, cy + d);
1163 visit(cx + r, cy + d);
1164 }
1165 }
1166 if (found == K && best[K - 1].first <= sqr(r * m_cell))
1167 break;
1168 }
1170 out.fill(npos);
1171 for (size_t k = 0; k < found; ++ k)
1172 out[k] = best[k].second;
1173 return out;
1174 }
static constexpr size_t npos
Definition ShortestPath.cpp:1103
constexpr T sqr(T x)
Definition libslic3r.h:301

References m_cell, m_cols, m_first, m_index, m_origin, m_points, m_rows, npos, and Slic3r::sqr().

Referenced by Slic3r::chain_nearest_next(), and Slic3r::improve_chain_by_local_search().

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

Member Data Documentation

◆ m_cell

template<size_t K>
double Slic3r::NearestPoints< K >::m_cell
private

Referenced by NearestPoints(), cell(), and query().

◆ m_cols

template<size_t K>
int Slic3r::NearestPoints< K >::m_cols
private

Referenced by NearestPoints(), cell(), and query().

◆ m_first

template<size_t K>
std::vector<size_t> Slic3r::NearestPoints< K >::m_first
private

Referenced by NearestPoints(), and query().

◆ m_index

template<size_t K>
std::vector<size_t> Slic3r::NearestPoints< K >::m_index
private

Referenced by NearestPoints(), and query().

◆ m_origin

template<size_t K>
Vec2d Slic3r::NearestPoints< K >::m_origin
private

Referenced by NearestPoints(), cell(), and query().

◆ m_points

template<size_t K>
const std::vector<Vec2d>& Slic3r::NearestPoints< K >::m_points
private

Referenced by NearestPoints(), and query().

◆ m_rows

template<size_t K>
int Slic3r::NearestPoints< K >::m_rows
private

Referenced by NearestPoints(), cell(), and query().

◆ npos

template<size_t K>
size_t Slic3r::NearestPoints< K >::npos = size_t(-1)
staticconstexpr

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