58 template <
typename VectorType>
63 template <
typename VectorType>
66 VectorType &poly = *_poly;
67 keep.assign(poly.size(), 1);
68 const int n = poly.size();
73 for (
int i = 1; i + 1 < n; ++i) {
79 while (L >= 0 && !
keep[L])
83 while (R < n && !
keep[R])
90 NumberT dx = poly[R].x - poly[L].x;
91 NumberT dy = poly[R].y - poly[L].y;
92 NumberT vx = poly[i].x - poly[L].x;
93 NumberT vy = poly[i].y - poly[L].y;
94 NumberT len2 = dx * dx + dy * dy;
97 ? ((dx * vy - dy * vx) * (dx * vy - dy * vx) / len2)
98 : (vx * vx + vy * vy);
109 poly.erase(poly.begin() + bestIdx);
119 template <
typename VectorType>
131 int n = polyLine.
size();
153 int i1 =
pair.second;
155 const bool has_interior = (i1 - i0) > 1;
164 NumberT maxDist2 = 0;
166 for (
int i = i0 + 1; i < i1; ++i) {
182 if (maxDist2 > minDist2) {
188 for (
int i = i0 + 1; i < i1; ++i) {
197 for (
int i = 0; i < n; ++i) {
215 NumberT dx = b.
x - a.
x;
216 NumberT dy = b.
y - a.
y;
218 NumberT vx = pt.
x - a.
x;
219 NumberT vy = pt.
y - a.
y;
222 NumberT len2 = dx * dx + dy * dy;
223 if (len2 <= NumberT(0)) {
225 return vx * vx + vy * vy;
229 NumberT cross = dx * vy - dy * vx;
231 return (cross * cross) / len2;
244 template <
typename VectorType = fl::vector<Po
int>>
246 return simplify(*polyLine, polyLine);
249 template <
typename VectorType = fl::vector<Po
int>>
259 if (polyLine.
size() > 0) {
262 if (polyLine.
size() > 1) {
265 out->assign(temp.
begin(), temp.
end());
270 NumberT max = est_max_dist;
271 NumberT mid = (min + max) / 2.0f;
274 auto diff = max - min;
275 const bool done = (diff < 0.01f);
280 size_t n = out->size();
302 while (out->size() >
mCount) {
309 if (out->size() <
mCount) {
314 mid = (min + max) / 2.0f;
321 if (polyLine.
size() < 2)
324 const Point &first = polyLine[0];
325 const Point &last = polyLine[polyLine.
size() - 1];
326 NumberT dx = last.
x - first.
x;
327 NumberT dy = last.
y - first.
y;
328 return sqrt(dx * dx + dy * dy);
331 template <
typename VectorType>
333 auto *first_out = out->data();
335 auto *other_first_out = polyLine.
data();
337 const bool is_same = first_out == other_first_out;
341 auto *last_out = first_out +
mCount;
342 auto *other_last_out = other_first_out + polyLine.
size();
344 const bool is_overlapping =
345 (first_out >= other_first_out && first_out < other_last_out) ||
346 (other_first_out >= first_out && other_first_out < last_out);
348 if (!is_overlapping) {
349 out->assign(polyLine.
data(), polyLine.
data() + polyLine.
size());
356 out->assign(temp.
begin(), temp.
end());
void push_back(const T &value)
void assign(size_t new_cap, const T &value)
void simplifyInplace(fl::vector< Point > *polyline)
void simplify(const fl::Slice< Point > &polyLine, VectorType *out)
void simplifyInternal(const fl::Slice< const Point > &polyLine)
LineSimplifier & operator=(LineSimplifier &&other)=default
LineSimplifier & operator=(const LineSimplifier &other)=default
static NumberT PerpendicularDistance2(const Point &pt, const Point &a, const Point &b)
void simplifyInplaceT(VectorType *polyLine)
void setMinimumDistance(NumberT eps)
LineSimplifier(NumberT e)
void simplify(const fl::Slice< const Point > &polyLine, fl::vector< Point > *out)
void simplifyT(const fl::Slice< const Point > &polyLine, VectorType *out)
fl::vector< Point > VectorPoint
LineSimplifier(const LineSimplifier &other)=default
static void removeOneLeastError(VectorType *_poly)
fl::vector_inlined< fl::pair< int, int >, 64 > indexStack
LineSimplifier(LineSimplifier &&other)=default
fl::vec2< NumberT > Point
void simplifyInplace(VectorType *polyLine)
LineSimplifierExact()=default
void simplifyInplace(VectorType *polyLine)
LineSimplifierExact(int count)
static NumberT estimateMaxDistance(const fl::Slice< const Point > &polyLine)
void safeCopy(const fl::Slice< const Point > &polyLine, VectorType *out)
void simplify(const fl::Slice< const Point > &polyLine, VectorType *out)
void setCount(uint32_t count)
LineSimplifier< NumberT > mLineSimplifier
BitsetInlined< N > bitset
InlinedVector< T, INLINED_SIZE > vector_inlined
FixedVector< T, INLINED_SIZE > vector_fixed
Implements a simple red square effect for 2D LED grids.