mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-14 02:07:54 -06:00
Working curve approximation of the concave hull with clipper offset.
This commit is contained in:
parent
fbe415f88e
commit
f297da0d01
8 changed files with 243 additions and 694 deletions
49
src/slabasebed.cpp
Normal file
49
src/slabasebed.cpp
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <libslic3r.h>
|
||||||
|
#include "TriangleMesh.hpp"
|
||||||
|
#include "SLABasePool.hpp"
|
||||||
|
#include "benchmark.h"
|
||||||
|
|
||||||
|
const std::string USAGE_STR = {
|
||||||
|
"Usage: slabasebed stlfilename.stl"
|
||||||
|
};
|
||||||
|
|
||||||
|
void confess_at(const char * /*file*/,
|
||||||
|
int /*line*/,
|
||||||
|
const char * /*func*/,
|
||||||
|
const char * /*pat*/,
|
||||||
|
...) {}
|
||||||
|
|
||||||
|
int main(const int argc, const char *argv[]) {
|
||||||
|
using namespace Slic3r;
|
||||||
|
using std::cout; using std::endl;
|
||||||
|
|
||||||
|
if(argc < 2) {
|
||||||
|
cout << USAGE_STR << endl;
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
TriangleMesh model;
|
||||||
|
Benchmark bench;
|
||||||
|
|
||||||
|
model.ReadSTLFile(argv[1]);
|
||||||
|
model.align_to_origin();
|
||||||
|
|
||||||
|
ExPolygons ground_slice;
|
||||||
|
TriangleMesh basepool;
|
||||||
|
|
||||||
|
sla::ground_layer(model, ground_slice, 0.1f);
|
||||||
|
|
||||||
|
bench.start();
|
||||||
|
sla::create_base_pool(ground_slice, basepool);
|
||||||
|
bench.stop();
|
||||||
|
|
||||||
|
cout << "Base pool creation time: " << std::setprecision(10)
|
||||||
|
<< bench.getElapsedSec() << " seconds." << endl;
|
||||||
|
|
||||||
|
basepool.write_ascii("out.stl");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -181,8 +181,6 @@ add_library(libslic3r STATIC
|
||||||
${LIBDIR}/libslic3r/TriangleMesh.hpp
|
${LIBDIR}/libslic3r/TriangleMesh.hpp
|
||||||
${LIBDIR}/libslic3r/SLABasePool.hpp
|
${LIBDIR}/libslic3r/SLABasePool.hpp
|
||||||
${LIBDIR}/libslic3r/SLABasePool.cpp
|
${LIBDIR}/libslic3r/SLABasePool.cpp
|
||||||
${LIBDIR}/libslic3r/ConcaveHull.hpp
|
|
||||||
${LIBDIR}/libslic3r/ConcaveHull.cpp
|
|
||||||
# ${LIBDIR}/libslic3r/utils.cpp
|
# ${LIBDIR}/libslic3r/utils.cpp
|
||||||
${LIBDIR}/libslic3r/Utils.hpp
|
${LIBDIR}/libslic3r/Utils.hpp
|
||||||
|
|
||||||
|
@ -751,9 +749,14 @@ if(APPLE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Create a slic3r executable
|
# Create a slic3r executable
|
||||||
add_executable(slic3r ${PROJECT_SOURCE_DIR}/src/slic3r.cpp)
|
add_executable(slic3r EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/src/slic3r.cpp)
|
||||||
target_include_directories(XS PRIVATE src src/libslic3r)
|
target_include_directories(XS PRIVATE src src/libslic3r)
|
||||||
target_link_libraries(slic3r libslic3r libslic3r_gui admesh miniz ${Boost_LIBRARIES} clipper ${EXPAT_LIBRARIES} ${GLEW_LIBRARIES} polypartition poly2tri ${TBB_LIBRARIES} ${wxWidgets_LIBRARIES})
|
target_link_libraries(slic3r libslic3r libslic3r_gui admesh miniz ${Boost_LIBRARIES} clipper ${EXPAT_LIBRARIES} ${GLEW_LIBRARIES} polypartition poly2tri ${TBB_LIBRARIES} ${wxWidgets_LIBRARIES})
|
||||||
|
|
||||||
|
#add_executable(slabasebed ${PROJECT_SOURCE_DIR}/src/slabasebed.cpp)
|
||||||
|
#target_include_directories(slabasebed PRIVATE src src/libslic3r)
|
||||||
|
#target_link_libraries(slabasebed libslic3r libslic3r_gui admesh miniz ${Boost_LIBRARIES} clipper ${EXPAT_LIBRARIES} ${GLEW_LIBRARIES} polypartition poly2tri ${TBB_LIBRARIES} ${wxWidgets_LIBRARIES})
|
||||||
|
|
||||||
if(SLIC3R_PROFILE)
|
if(SLIC3R_PROFILE)
|
||||||
target_link_libraries(Shiny)
|
target_link_libraries(Shiny)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -1,487 +0,0 @@
|
||||||
#include "ConcaveHull.hpp"
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <iostream>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <fstream>
|
|
||||||
#include <chrono>
|
|
||||||
#include <cassert>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#include <Point.hpp>
|
|
||||||
#include <Polygon.hpp>
|
|
||||||
|
|
||||||
#pragma warning(push, 0)
|
|
||||||
#include <flann\flann.hpp>
|
|
||||||
#pragma warning(pop)
|
|
||||||
|
|
||||||
namespace Slic3r { namespace concavehull {
|
|
||||||
|
|
||||||
const size_t stride = 24; // size in bytes of x, y, id
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
// Floating point comparisons
|
|
||||||
auto Equal(double a, double b) -> bool;
|
|
||||||
auto Zero(double a) -> bool;
|
|
||||||
auto LessThan(double a, double b) -> bool;
|
|
||||||
auto LessThanOrEqual(double a, double b) -> bool;
|
|
||||||
auto GreaterThan(double a, double b) -> bool;
|
|
||||||
|
|
||||||
// Algorithm-specific
|
|
||||||
auto NearestNeighboursFlann(flann::Index<flann::L2<double>> &index, const Point &p, size_t k) -> PointValueVector;
|
|
||||||
auto SortByAngle(PointValueVector &values, const Point &p, double prevAngle) -> PointVector;
|
|
||||||
auto AddPoint(PointVector &points, const Point &p) -> void;
|
|
||||||
|
|
||||||
// General maths
|
|
||||||
auto PointsEqual(const Point &a, const Point &b) -> bool;
|
|
||||||
auto Angle(const Point &a, const Point &b) -> double;
|
|
||||||
auto NormaliseAngle(double radians) -> double;
|
|
||||||
auto PointInPolygon(const Point &p, const PointVector &list) -> bool;
|
|
||||||
auto Intersects(const LineSegment &a, const LineSegment &b) -> bool;
|
|
||||||
|
|
||||||
// Point list utilities
|
|
||||||
auto FindMinYPoint(const PointVector &points) -> Point;
|
|
||||||
auto RemoveDuplicates(PointVector &points) -> void;
|
|
||||||
auto IdentifyPoints(PointVector &points) -> void;
|
|
||||||
auto RemoveHull(PointVector &points, const PointVector &hull) -> PointVector::iterator;
|
|
||||||
auto MultiplePointInPolygon(PointVector::iterator begin, PointVector::iterator end, const PointVector &hull) -> bool;
|
|
||||||
|
|
||||||
// Compare a and b for equality
|
|
||||||
auto Equal(double a, double b) -> bool
|
|
||||||
{
|
|
||||||
return fabs(a - b) <= DBL_EPSILON;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare value to zero
|
|
||||||
auto Zero(double a) -> bool
|
|
||||||
{
|
|
||||||
return fabs(a) <= DBL_EPSILON;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare for a < b
|
|
||||||
auto LessThan(double a, double b) -> bool
|
|
||||||
{
|
|
||||||
return a < (b - DBL_EPSILON);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare for a <= b
|
|
||||||
auto LessThanOrEqual(double a, double b) -> bool
|
|
||||||
{
|
|
||||||
return a <= (b + DBL_EPSILON);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare for a > b
|
|
||||||
auto GreaterThan(double a, double b) -> bool
|
|
||||||
{
|
|
||||||
return a > (b + DBL_EPSILON);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare whether two points have the same x and y
|
|
||||||
auto PointsEqual(const Point &a, const Point &b) -> bool
|
|
||||||
{
|
|
||||||
return Equal(a.x, b.x) && Equal(a.y, b.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove duplicates in a list of points
|
|
||||||
auto RemoveDuplicates(PointVector &points) -> void
|
|
||||||
{
|
|
||||||
sort(begin(points), end(points), [](const Point & a, const Point & b)
|
|
||||||
{
|
|
||||||
if (Equal(a.x, b.x))
|
|
||||||
return LessThan(a.y, b.y);
|
|
||||||
else
|
|
||||||
return LessThan(a.x, b.x);
|
|
||||||
});
|
|
||||||
|
|
||||||
auto newEnd = unique(begin(points), end(points), [](const Point & a, const Point & b)
|
|
||||||
{
|
|
||||||
return PointsEqual(a, b);
|
|
||||||
});
|
|
||||||
|
|
||||||
points.erase(newEnd, end(points));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uniquely id the points for binary searching
|
|
||||||
auto IdentifyPoints(PointVector &points) -> void
|
|
||||||
{
|
|
||||||
uint64_t id = 0;
|
|
||||||
|
|
||||||
for (auto itr = begin(points); itr != end(points); ++itr, ++id)
|
|
||||||
{
|
|
||||||
itr->id = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the point having the smallest y-value
|
|
||||||
auto FindMinYPoint(const PointVector &points) -> Point
|
|
||||||
{
|
|
||||||
assert(!points.empty());
|
|
||||||
|
|
||||||
auto itr = min_element(begin(points), end(points), [](const Point & a, const Point & b)
|
|
||||||
{
|
|
||||||
if (Equal(a.y, b.y))
|
|
||||||
return GreaterThan(a.x, b.x);
|
|
||||||
else
|
|
||||||
return LessThan(a.y, b.y);
|
|
||||||
});
|
|
||||||
|
|
||||||
return *itr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lookup by ID and remove a point from a list of points
|
|
||||||
auto RemovePoint(PointVector &list, const Point &p) -> void
|
|
||||||
{
|
|
||||||
auto itr = std::lower_bound(begin(list), end(list), p, [](const Point & a, const Point & b)
|
|
||||||
{
|
|
||||||
return a.id < b.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
assert(itr != end(list) && itr->id == p.id);
|
|
||||||
|
|
||||||
if (itr != end(list))
|
|
||||||
list.erase(itr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a point to a list of points
|
|
||||||
auto AddPoint(PointVector &points, const Point &p) -> void
|
|
||||||
{
|
|
||||||
points.push_back(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the k-nearest points in a list of points from the given point p (uses Flann library).
|
|
||||||
auto NearestNeighboursFlann(flann::Index<flann::L2<double>> &index, const Point &p, size_t k) -> PointValueVector
|
|
||||||
{
|
|
||||||
std::vector<int> vIndices(k);
|
|
||||||
std::vector<double> vDists(k);
|
|
||||||
double test[] = { p.x, p.y };
|
|
||||||
|
|
||||||
flann::Matrix<double> query(test, 1, 2);
|
|
||||||
flann::Matrix<int> mIndices(vIndices.data(), 1, static_cast<int>(vIndices.size()));
|
|
||||||
flann::Matrix<double> mDists(vDists.data(), 1, static_cast<int>(vDists.size()));
|
|
||||||
|
|
||||||
int count_ = index.knnSearch(query, mIndices, mDists, k, flann::SearchParams(128));
|
|
||||||
size_t count = static_cast<size_t>(count_);
|
|
||||||
|
|
||||||
PointValueVector result(count);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < count; ++i)
|
|
||||||
{
|
|
||||||
int id = vIndices[i];
|
|
||||||
const double *point = index.getPoint(id);
|
|
||||||
result[i].point.x = point[0];
|
|
||||||
result[i].point.y = point[1];
|
|
||||||
result[i].point.id = id;
|
|
||||||
result[i].distance = vDists[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a list of points sorted in descending order of clockwise angle
|
|
||||||
auto SortByAngle(PointValueVector &values, const Point &from, double prevAngle) -> PointVector
|
|
||||||
{
|
|
||||||
for_each(begin(values), end(values), [from, prevAngle](PointValue & to)
|
|
||||||
{
|
|
||||||
to.angle = NormaliseAngle(Angle(from, to.point) - prevAngle);
|
|
||||||
});
|
|
||||||
|
|
||||||
sort(begin(values), end(values), [](const PointValue & a, const PointValue & b)
|
|
||||||
{
|
|
||||||
return GreaterThan(a.angle, b.angle);
|
|
||||||
});
|
|
||||||
|
|
||||||
PointVector angled(values.size());
|
|
||||||
|
|
||||||
transform(begin(values), end(values), begin(angled), [](const PointValue & pv)
|
|
||||||
{
|
|
||||||
return pv.point;
|
|
||||||
});
|
|
||||||
|
|
||||||
return angled;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the angle in radians measured clockwise from +'ve x-axis
|
|
||||||
auto Angle(const Point &a, const Point &b) -> double
|
|
||||||
{
|
|
||||||
double angle = -atan2(b.y - a.y, b.x - a.x);
|
|
||||||
|
|
||||||
return NormaliseAngle(angle);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return angle in range: 0 <= angle < 2PI
|
|
||||||
auto NormaliseAngle(double radians) -> double
|
|
||||||
{
|
|
||||||
if (radians < 0.0)
|
|
||||||
return radians + M_PI + M_PI;
|
|
||||||
else
|
|
||||||
return radians;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the new logical end after removing points from dataset having ids belonging to hull
|
|
||||||
auto RemoveHull(PointVector &points, const PointVector &hull) -> PointVector::iterator
|
|
||||||
{
|
|
||||||
std::vector<uint64_t> ids(hull.size());
|
|
||||||
|
|
||||||
transform(begin(hull), end(hull), begin(ids), [](const Point & p)
|
|
||||||
{
|
|
||||||
return p.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
sort(begin(ids), end(ids));
|
|
||||||
|
|
||||||
return remove_if(begin(points), end(points), [&ids](const Point & p)
|
|
||||||
{
|
|
||||||
return binary_search(begin(ids), end(ids), p.id);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//// Uses OpenMP to determine whether a condition exists in the specified range of elements. https://msdn.microsoft.com/en-us/library/ff521445.aspx
|
|
||||||
//template <class InIt, class Predicate>
|
|
||||||
//bool omp_parallel_any_of(InIt first, InIt last, const Predicate &pr)
|
|
||||||
//{
|
|
||||||
// typedef typename std::iterator_traits<InIt>::value_type item_type;
|
|
||||||
|
|
||||||
// // A flag that indicates that the condition exists.
|
|
||||||
// bool found = false;
|
|
||||||
|
|
||||||
// #pragma omp parallel for
|
|
||||||
// for (int i = 0; i < static_cast<int>(last - first); ++i)
|
|
||||||
// {
|
|
||||||
// if (!found)
|
|
||||||
// {
|
|
||||||
// item_type &cur = *(first + i);
|
|
||||||
|
|
||||||
// // If the element satisfies the condition, set the flag to cancel the operation.
|
|
||||||
// if (pr(cur))
|
|
||||||
// {
|
|
||||||
// found = true;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return found;
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Check whether all points in a begin/end range are inside hull.
|
|
||||||
auto MultiplePointInPolygon(PointVector::iterator begin, PointVector::iterator end, const PointVector &hull) -> bool
|
|
||||||
{
|
|
||||||
auto test = [&hull](const Point & p)
|
|
||||||
{
|
|
||||||
return !PointInPolygon(p, hull);
|
|
||||||
};
|
|
||||||
|
|
||||||
bool anyOutside = true;
|
|
||||||
|
|
||||||
#if defined USE_OPENMP
|
|
||||||
|
|
||||||
anyOutside = omp_parallel_any_of(begin, end, test); // multi-threaded
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
anyOutside = std::any_of(begin, end, test); // single-threaded
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return !anyOutside;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Point-in-polygon test
|
|
||||||
auto PointInPolygon(const Point &p, const PointVector &list) -> bool
|
|
||||||
{
|
|
||||||
if (list.size() <= 2)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const double &x = p.x;
|
|
||||||
const double &y = p.y;
|
|
||||||
|
|
||||||
int inout = 0;
|
|
||||||
auto v0 = list.begin();
|
|
||||||
auto v1 = v0 + 1;
|
|
||||||
|
|
||||||
while (v1 != list.end())
|
|
||||||
{
|
|
||||||
if ((LessThanOrEqual(v0->y, y) && LessThan(y, v1->y)) || (LessThanOrEqual(v1->y, y) && LessThan(y, v0->y)))
|
|
||||||
{
|
|
||||||
if (!Zero(v1->y - v0->y))
|
|
||||||
{
|
|
||||||
double tdbl1 = (y - v0->y) / (v1->y - v0->y);
|
|
||||||
double tdbl2 = v1->x - v0->x;
|
|
||||||
|
|
||||||
if (LessThan(x, v0->x + (tdbl2 * tdbl1)))
|
|
||||||
inout++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
v0 = v1;
|
|
||||||
v1++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inout == 0)
|
|
||||||
return false;
|
|
||||||
else if (inout % 2 == 0)
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test whether two line segments intersect each other
|
|
||||||
auto Intersects(const LineSegment &a, const LineSegment &b) -> bool
|
|
||||||
{
|
|
||||||
// https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/
|
|
||||||
|
|
||||||
const double &ax1 = a.first.x;
|
|
||||||
const double &ay1 = a.first.y;
|
|
||||||
const double &ax2 = a.second.x;
|
|
||||||
const double &ay2 = a.second.y;
|
|
||||||
const double &bx1 = b.first.x;
|
|
||||||
const double &by1 = b.first.y;
|
|
||||||
const double &bx2 = b.second.x;
|
|
||||||
const double &by2 = b.second.y;
|
|
||||||
|
|
||||||
double a1 = ay2 - ay1;
|
|
||||||
double b1 = ax1 - ax2;
|
|
||||||
double c1 = a1 * ax1 + b1 * ay1;
|
|
||||||
double a2 = by2 - by1;
|
|
||||||
double b2 = bx1 - bx2;
|
|
||||||
double c2 = a2 * bx1 + b2 * by1;
|
|
||||||
double det = a1 * b2 - a2 * b1;
|
|
||||||
|
|
||||||
if (Zero(det))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
double x = (b2 * c1 - b1 * c2) / det;
|
|
||||||
double y = (a1 * c2 - a2 * c1) / det;
|
|
||||||
|
|
||||||
bool on_both = true;
|
|
||||||
on_both = on_both && LessThanOrEqual(std::min(ax1, ax2), x) && LessThanOrEqual(x, std::max(ax1, ax2));
|
|
||||||
on_both = on_both && LessThanOrEqual(std::min(ay1, ay2), y) && LessThanOrEqual(y, std::max(ay1, ay2));
|
|
||||||
on_both = on_both && LessThanOrEqual(std::min(bx1, bx2), x) && LessThanOrEqual(x, std::max(bx1, bx2));
|
|
||||||
on_both = on_both && LessThanOrEqual(std::min(by1, by2), y) && LessThanOrEqual(y, std::max(by1, by2));
|
|
||||||
return on_both;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// The main algorithm from the Moreira-Santos paper.
|
|
||||||
auto ConcaveHull(PointVector &pointList, size_t k, PointVector &hull) -> bool
|
|
||||||
{
|
|
||||||
hull.clear();
|
|
||||||
|
|
||||||
if (pointList.size() < 3)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (pointList.size() == 3)
|
|
||||||
{
|
|
||||||
hull = pointList;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// construct a randomized kd-tree index using 4 kd-trees
|
|
||||||
// 2 columns, but stride = 24 bytes in width (x, y, ignoring id)
|
|
||||||
flann::Matrix<double> matrix(&(pointList.front().x), pointList.size(), 2, stride);
|
|
||||||
flann::Index<flann::L2<double>> flannIndex(matrix, flann::KDTreeIndexParams(4));
|
|
||||||
flannIndex.buildIndex();
|
|
||||||
|
|
||||||
std::cout << "\rFinal 'k' : " << k;
|
|
||||||
|
|
||||||
// Initialise hull with the min-y point
|
|
||||||
Point firstPoint = FindMinYPoint(pointList);
|
|
||||||
AddPoint(hull, firstPoint);
|
|
||||||
|
|
||||||
// Until the hull is of size > 3 we want to ignore the first point from nearest neighbour searches
|
|
||||||
Point currentPoint = firstPoint;
|
|
||||||
flannIndex.removePoint(firstPoint.id);
|
|
||||||
|
|
||||||
double prevAngle = 0.0;
|
|
||||||
int step = 1;
|
|
||||||
|
|
||||||
// Iterate until we reach the start, or until there's no points left to process
|
|
||||||
while ((!PointsEqual(currentPoint, firstPoint) || step == 1) && hull.size() != pointList.size())
|
|
||||||
{
|
|
||||||
if (step == 4)
|
|
||||||
{
|
|
||||||
// Put back the first point into the dataset and into the flann index
|
|
||||||
firstPoint.id = pointList.size();
|
|
||||||
flann::Matrix<double> firstPointMatrix(&firstPoint.x, 1, 2, stride);
|
|
||||||
flannIndex.addPoints(firstPointMatrix);
|
|
||||||
}
|
|
||||||
|
|
||||||
PointValueVector kNearestNeighbours = NearestNeighboursFlann(flannIndex, currentPoint, k);
|
|
||||||
PointVector cPoints = SortByAngle(kNearestNeighbours, currentPoint, prevAngle);
|
|
||||||
|
|
||||||
bool its = true;
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (its && i < cPoints.size())
|
|
||||||
{
|
|
||||||
size_t lastPoint = 0;
|
|
||||||
if (PointsEqual(cPoints[i], firstPoint))
|
|
||||||
lastPoint = 1;
|
|
||||||
|
|
||||||
size_t j = 2;
|
|
||||||
its = false;
|
|
||||||
|
|
||||||
while (!its && j < hull.size() - lastPoint)
|
|
||||||
{
|
|
||||||
auto line1 = std::make_pair(hull[step - 1], cPoints[i]);
|
|
||||||
auto line2 = std::make_pair(hull[step - j - 1], hull[step - j]);
|
|
||||||
its = Intersects(line1, line2);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (its)
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (its)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
currentPoint = cPoints[i];
|
|
||||||
|
|
||||||
AddPoint(hull, currentPoint);
|
|
||||||
|
|
||||||
prevAngle = Angle(hull[step], hull[step - 1]);
|
|
||||||
|
|
||||||
flannIndex.removePoint(currentPoint.id);
|
|
||||||
|
|
||||||
step++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The original points less the points belonging to the hull need to be fully enclosed by the hull in order to return true.
|
|
||||||
PointVector dataset = pointList;
|
|
||||||
|
|
||||||
auto newEnd = RemoveHull(dataset, hull);
|
|
||||||
bool allEnclosed = MultiplePointInPolygon(begin(dataset), newEnd, hull);
|
|
||||||
|
|
||||||
return allEnclosed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iteratively call the main algorithm with an increasing k until success
|
|
||||||
auto ConcaveHull(PointVector &dataset, size_t k, bool iterate) -> PointVector
|
|
||||||
{
|
|
||||||
while (k < dataset.size())
|
|
||||||
{
|
|
||||||
PointVector hull;
|
|
||||||
if (ConcaveHull(dataset, k, hull) || !iterate)
|
|
||||||
{
|
|
||||||
return hull;
|
|
||||||
}
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return{};
|
|
||||||
}
|
|
||||||
|
|
||||||
Point::Point(const Pointf & sp): x(sp.x), y(sp.y) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,49 +0,0 @@
|
||||||
#ifndef CONCAVEHULL_HPP
|
|
||||||
#define CONCAVEHULL_HPP
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Slic3r {
|
|
||||||
|
|
||||||
class Pointf;
|
|
||||||
|
|
||||||
namespace concavehull {
|
|
||||||
|
|
||||||
using std::uint64_t;
|
|
||||||
|
|
||||||
struct Point
|
|
||||||
{
|
|
||||||
double x = 0.0;
|
|
||||||
double y = 0.0;
|
|
||||||
std::uint64_t id = 0;
|
|
||||||
|
|
||||||
Point() = default;
|
|
||||||
|
|
||||||
Point(double x, double y)
|
|
||||||
: x(x)
|
|
||||||
, y(y)
|
|
||||||
{}
|
|
||||||
|
|
||||||
explicit Point(const Slic3r::Pointf&);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PointValue
|
|
||||||
{
|
|
||||||
Point point;
|
|
||||||
double distance = 0.0;
|
|
||||||
double angle = 0.0;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern const size_t stride; // size in bytes of x, y, id
|
|
||||||
|
|
||||||
using PointVector = std::vector<Point>;
|
|
||||||
using PointValueVector = std::vector<PointValue>;
|
|
||||||
using LineSegment = std::pair<Point, Point>;
|
|
||||||
|
|
||||||
auto ConcaveHull(PointVector &dataset, size_t k, bool iterate) -> PointVector;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // CONCAVEHULL_HPP
|
|
|
@ -9,9 +9,9 @@ namespace Slic3r {
|
||||||
class ExPolygon;
|
class ExPolygon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Raster captures an antialiased monochrome canvas where vectorial
|
* @brief Raster captures an anti-aliased monochrome canvas where vectorial
|
||||||
* polygons can be rasterized. Fill color is always white and the background is
|
* polygons can be rasterized. Fill color is always white and the background is
|
||||||
* black. Countours are antialiased.
|
* black. Contours are anti-aliased.
|
||||||
*
|
*
|
||||||
* It also supports saving the raster data into a standard output stream in raw
|
* It also supports saving the raster data into a standard output stream in raw
|
||||||
* or PNG format.
|
* or PNG format.
|
||||||
|
|
|
@ -3,63 +3,32 @@
|
||||||
#include "SLABasePool.hpp"
|
#include "SLABasePool.hpp"
|
||||||
#include "ExPolygon.hpp"
|
#include "ExPolygon.hpp"
|
||||||
#include "TriangleMesh.hpp"
|
#include "TriangleMesh.hpp"
|
||||||
#include "libnest2d/clipper_backend/clipper_backend.hpp"
|
#include <numeric>
|
||||||
#include "ClipperUtils.hpp"
|
#include "ClipperUtils.hpp"
|
||||||
|
#include "boost/log/trivial.hpp"
|
||||||
|
|
||||||
#include "ConcaveHull.hpp"
|
//#include "SVG.hpp"
|
||||||
|
|
||||||
using BoostPolygon = libnest2d::PolygonImpl;
|
|
||||||
using BoostPolygons = std::vector<libnest2d::PolygonImpl>;
|
|
||||||
|
|
||||||
namespace Slic3r { namespace sla {
|
namespace Slic3r { namespace sla {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using coord_t = Point::coord_type;
|
using coord_t = Point::coord_type;
|
||||||
|
inline coord_t mm(double v) { return coord_t(v/SCALING_FACTOR); }
|
||||||
|
|
||||||
void reverse(Polygon& p) {
|
inline coord_t x(const Point& p) { return p.x; }
|
||||||
std::reverse(p.points.begin(), p.points.end());
|
inline coord_t y(const Point& p) { return p.y; }
|
||||||
}
|
|
||||||
|
|
||||||
inline BoostPolygon convert(const ExPolygon& exp) {
|
|
||||||
auto&& ctour = Slic3rMultiPoint_to_ClipperPath(exp.contour);
|
|
||||||
auto&& holes = Slic3rMultiPoints_to_ClipperPaths(exp.holes);
|
|
||||||
return {ctour, holes};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline BoostPolygons convert(const ExPolygons& exps) {
|
|
||||||
BoostPolygons ret;
|
|
||||||
ret.reserve(exps.size());
|
|
||||||
std::for_each(exps.begin(), exps.end(), [&ret](const ExPolygon p) {
|
|
||||||
ret.emplace_back(convert(p));
|
|
||||||
});
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline ExPolygon convert(const BoostPolygon& p) {
|
|
||||||
ExPolygon ret;
|
|
||||||
|
|
||||||
auto&& ctour = ClipperPath_to_Slic3rPolygon(p.Contour);
|
|
||||||
ctour.points.pop_back();
|
|
||||||
|
|
||||||
auto&& holes = ClipperPaths_to_Slic3rPolygons(p.Holes);
|
|
||||||
for(auto&& h : holes) h.points.pop_back();
|
|
||||||
|
|
||||||
ret.contour = ctour;
|
|
||||||
ret.holes = holes;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Contour3D {
|
struct Contour3D {
|
||||||
Pointf3s points;
|
Pointf3s points;
|
||||||
std::vector<Point3> indices;
|
std::vector<Point3> indices;
|
||||||
|
|
||||||
void merge(const Contour3D& ctour) {
|
void merge(const Contour3D& ctr) {
|
||||||
auto s3 = coord_t(points.size());
|
auto s3 = coord_t(points.size());
|
||||||
auto s = coord_t(indices.size());
|
auto s = coord_t(indices.size());
|
||||||
|
|
||||||
points.insert(points.end(), ctour.points.begin(), ctour.points.end());
|
points.insert(points.end(), ctr.points.begin(), ctr.points.end());
|
||||||
indices.insert(indices.end(), ctour.indices.begin(), ctour.indices.end());
|
indices.insert(indices.end(), ctr.indices.begin(), ctr.indices.end());
|
||||||
|
|
||||||
for(auto n = s; n < indices.size(); n++) {
|
for(auto n = s; n < indices.size(); n++) {
|
||||||
auto& idx = indices[n]; idx.x += s3; idx.y += s3; idx.z += s3;
|
auto& idx = indices[n]; idx.x += s3; idx.y += s3; idx.z += s3;
|
||||||
|
@ -79,7 +48,7 @@ inline Contour3D convert(const Polygons& triangles, coord_t z, bool dir) {
|
||||||
if(dir) indices.emplace_back(a, b, c);
|
if(dir) indices.emplace_back(a, b, c);
|
||||||
else indices.emplace_back(c, b, a);
|
else indices.emplace_back(c, b, a);
|
||||||
for(auto& p : tr.points) {
|
for(auto& p : tr.points) {
|
||||||
points.emplace_back(Pointf3::new_unscale(p.x, p.y, z));
|
points.emplace_back(Pointf3::new_unscale(x(p), y(p), z));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,26 +59,26 @@ inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) {
|
||||||
Polygons triangles;
|
Polygons triangles;
|
||||||
poly.triangulate_pp(&triangles);
|
poly.triangulate_pp(&triangles);
|
||||||
|
|
||||||
auto lower = convert(triangles, 0, true);
|
auto lower = convert(triangles, 0, false);
|
||||||
auto upper = convert(triangles, z_distance, false);
|
auto upper = convert(triangles, z_distance, true);
|
||||||
lower.merge(upper);
|
lower.merge(upper);
|
||||||
return lower;
|
return lower;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Contour3D inner_bed(const ExPolygon& poly, coord_t depth) {
|
inline Contour3D inner_bed(const ExPolygon& poly, coord_t depth) {
|
||||||
Polygons triangles;
|
Polygons triangles;
|
||||||
poly.triangulate_pp(&triangles);
|
poly.triangulate_p2t(&triangles);
|
||||||
|
|
||||||
auto bottom = convert(triangles, -depth, false);
|
auto bottom = convert(triangles, -depth, false);
|
||||||
auto lines = poly.lines();
|
auto lines = poly.lines();
|
||||||
|
|
||||||
// Generate outer walls
|
// Generate outer walls
|
||||||
auto fp = [](const Point& p, Point::coord_type z) {
|
auto fp = [](const Point& p, Point::coord_type z) {
|
||||||
return Pointf3::new_unscale(p.x, p.y, z);
|
return Pointf3::new_unscale(x(p), y(p), z);
|
||||||
};
|
};
|
||||||
|
|
||||||
for(auto& l : lines) {
|
for(auto& l : lines) {
|
||||||
auto s = bottom.points.size();
|
auto s = coord_t(bottom.points.size());
|
||||||
|
|
||||||
bottom.points.emplace_back(fp(l.a, -depth));
|
bottom.points.emplace_back(fp(l.a, -depth));
|
||||||
bottom.points.emplace_back(fp(l.b, -depth));
|
bottom.points.emplace_back(fp(l.b, -depth));
|
||||||
|
@ -131,94 +100,182 @@ inline TriangleMesh mesh(Contour3D&& ctour) {
|
||||||
return {std::move(ctour.points), std::move(ctour.indices)};
|
return {std::move(ctour.points), std::move(ctour.indices)};
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void offset(BoostPolygon& sh, Point::coord_type distance) {
|
inline void offset(ExPolygon& sh, coord_t distance) {
|
||||||
using ClipperLib::ClipperOffset;
|
using ClipperLib::ClipperOffset;
|
||||||
using ClipperLib::jtRound;
|
using ClipperLib::jtRound;
|
||||||
using ClipperLib::etClosedPolygon;
|
using ClipperLib::etClosedPolygon;
|
||||||
using ClipperLib::Paths;
|
using ClipperLib::Paths;
|
||||||
using namespace libnest2d;
|
using ClipperLib::Path;
|
||||||
|
|
||||||
|
auto&& ctour = Slic3rMultiPoint_to_ClipperPath(sh.contour);
|
||||||
|
auto&& holes = Slic3rMultiPoints_to_ClipperPaths(sh.holes);
|
||||||
|
|
||||||
// If the input is not at least a triangle, we can not do this algorithm
|
// If the input is not at least a triangle, we can not do this algorithm
|
||||||
if(sh.Contour.size() <= 3 ||
|
if(ctour.size() < 3 ||
|
||||||
std::any_of(sh.Holes.begin(), sh.Holes.end(),
|
std::any_of(holes.begin(), holes.end(),
|
||||||
[](const PathImpl& p) { return p.size() <= 3; })
|
[](const Path& p) { return p.size() < 3; })
|
||||||
) throw GeometryException(GeomErr::OFFSET);
|
) {
|
||||||
|
BOOST_LOG_TRIVIAL(error) << "Invalid geometry for offsetting!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ClipperOffset offs;
|
ClipperOffset offs;
|
||||||
|
offs.ArcTolerance = 0.05*mm(1);
|
||||||
Paths result;
|
Paths result;
|
||||||
offs.AddPath(sh.Contour, jtRound, etClosedPolygon);
|
offs.AddPath(ctour, jtRound, etClosedPolygon);
|
||||||
offs.AddPaths(sh.Holes, jtRound, etClosedPolygon);
|
offs.AddPaths(holes, jtRound, etClosedPolygon);
|
||||||
offs.Execute(result, static_cast<double>(distance));
|
offs.Execute(result, static_cast<double>(distance));
|
||||||
|
|
||||||
// Offsetting reverts the orientation and also removes the last vertex
|
// Offsetting reverts the orientation and also removes the last vertex
|
||||||
// so boost will not have a closed polygon.
|
// so boost will not have a closed polygon.
|
||||||
|
|
||||||
bool found_the_contour = false;
|
bool found_the_contour = false;
|
||||||
|
sh.holes.clear();
|
||||||
for(auto& r : result) {
|
for(auto& r : result) {
|
||||||
if(ClipperLib::Orientation(r)) {
|
if(ClipperLib::Orientation(r)) {
|
||||||
// We don't like if the offsetting generates more than one contour
|
// We don't like if the offsetting generates more than one contour
|
||||||
// but throwing would be an overkill. Instead, we should warn the
|
// but throwing would be an overkill. Instead, we should warn the
|
||||||
// caller about the inability to create correct geometries
|
// caller about the inability to create correct geometries
|
||||||
if(!found_the_contour) {
|
if(!found_the_contour) {
|
||||||
sh.Contour = r;
|
auto rr = ClipperPath_to_Slic3rPolygon(r);
|
||||||
ClipperLib::ReversePath(sh.Contour);
|
sh.contour.points.swap(rr.points);
|
||||||
sh.Contour.push_back(sh.Contour.front());
|
|
||||||
found_the_contour = true;
|
found_the_contour = true;
|
||||||
} else {
|
} else {
|
||||||
dout() << "Warning: offsetting result is invalid!";
|
BOOST_LOG_TRIVIAL(warning)
|
||||||
/* TODO warning */
|
<< "Warning: offsetting result is invalid!";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO If there are multiple contours we can't be sure which hole
|
// TODO If there are multiple contours we can't be sure which hole
|
||||||
// belongs to the first contour. (But in this case the situation is
|
// belongs to the first contour. (But in this case the situation is
|
||||||
// bad enough to let it go...)
|
// bad enough to let it go...)
|
||||||
sh.Holes.push_back(r);
|
sh.holes.emplace_back(ClipperPath_to_Slic3rPolygon(r));
|
||||||
ClipperLib::ReversePath(sh.Holes.back());
|
|
||||||
sh.Holes.back().push_back(sh.Holes.back().front());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline ExPolygons unify(const ExPolygons& shapes) {
|
||||||
|
ExPolygons retv;
|
||||||
|
|
||||||
inline ExPolygon concave_hull(const ExPolygons& polys) {
|
bool closed = true;
|
||||||
concavehull::PointVector pv;
|
bool valid = true;
|
||||||
size_t s = 0;
|
|
||||||
|
|
||||||
for(auto& ep : polys) s += ep.contour.points.size();
|
ClipperLib::Clipper clipper;
|
||||||
pv.reserve(s);
|
|
||||||
|
|
||||||
std::cout << polys.size() << std::endl;
|
for(auto& path : shapes) {
|
||||||
|
auto clipperpath = Slic3rMultiPoint_to_ClipperPath(path.contour);
|
||||||
|
valid &= clipper.AddPath(clipperpath, ClipperLib::ptSubject, closed);
|
||||||
|
|
||||||
// for(const ExPolygon& ep : polys) {
|
auto clipperholes = Slic3rMultiPoints_to_ClipperPaths(path.holes);
|
||||||
auto& ep = polys.front();
|
for(auto& hole : clipperholes) {
|
||||||
|
valid &= clipper.AddPath(hole, ClipperLib::ptSubject, closed);
|
||||||
for(auto& v : ep.contour.points)
|
}
|
||||||
pv.emplace_back(Pointf::new_unscale(v.x, v.y));
|
|
||||||
|
|
||||||
std::reverse(pv.begin(), pv.end());
|
|
||||||
|
|
||||||
// auto frontpoint = ep.contour.points.front();
|
|
||||||
// pv.emplace_back(Pointf::new_unscale(frontpoint));
|
|
||||||
// }
|
|
||||||
|
|
||||||
auto result = concavehull::ConcaveHull(pv, 3, true);
|
|
||||||
|
|
||||||
if(result.empty()) std::cout << "Empty concave hull!!!" << std::endl;
|
|
||||||
std::cout << "result size " << result.size() << std::endl;
|
|
||||||
|
|
||||||
ExPolygon ret;
|
|
||||||
ret.contour.points.reserve(result.size() + 1);
|
|
||||||
|
|
||||||
std::reverse(result.begin(), result.end());
|
|
||||||
|
|
||||||
for(auto& p : result) {
|
|
||||||
std::cout << p.x << " " << p.y << std::endl;
|
|
||||||
ret.contour.points.emplace_back(Point::new_scale(p.x, p.y));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reverse(ret.contour);
|
if(!valid) BOOST_LOG_TRIVIAL(warning) << "Unification of invalid shapes!";
|
||||||
|
|
||||||
// ret.contour.points.emplace_back(ret.contour.points.front());
|
ClipperLib::PolyTree result;
|
||||||
|
clipper.Execute(ClipperLib::ctUnion, result, ClipperLib::pftNonZero);
|
||||||
|
|
||||||
|
retv.reserve(static_cast<size_t>(result.Total()));
|
||||||
|
|
||||||
|
// Now we will recursively traverse the polygon tree and serialize it
|
||||||
|
// into an ExPolygon with holes. The polygon tree has the clipper-ish
|
||||||
|
// PolyTree structure which alternates its nodes as contours and holes
|
||||||
|
|
||||||
|
// A "declaration" of function for traversing leafs which are holes
|
||||||
|
std::function<void(ClipperLib::PolyNode*, ExPolygon&)> processHole;
|
||||||
|
|
||||||
|
// Process polygon which calls processHoles which than calls processPoly
|
||||||
|
// again until no leafs are left.
|
||||||
|
auto processPoly = [&retv, &processHole](ClipperLib::PolyNode *pptr) {
|
||||||
|
ExPolygon poly;
|
||||||
|
poly.contour.points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
|
||||||
|
for(auto h : pptr->Childs) { processHole(h, poly); }
|
||||||
|
retv.push_back(poly);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Body of the processHole function
|
||||||
|
processHole = [&processPoly](ClipperLib::PolyNode *pptr, ExPolygon& poly)
|
||||||
|
{
|
||||||
|
poly.holes.emplace_back();
|
||||||
|
poly.holes.back().points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
|
||||||
|
for(auto c : pptr->Childs) processPoly(c);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Wrapper for traversing.
|
||||||
|
auto traverse = [&processPoly] (ClipperLib::PolyNode *node)
|
||||||
|
{
|
||||||
|
for(auto ch : node->Childs) {
|
||||||
|
processPoly(ch);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Here is the actual traverse
|
||||||
|
traverse(&result);
|
||||||
|
|
||||||
|
return retv;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Point centroid(Points& pp) {
|
||||||
|
Polygon p;
|
||||||
|
p.points.swap(pp);
|
||||||
|
Point c = p.centroid();
|
||||||
|
pp.swap(p.points);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Point centroid(const ExPolygon& poly) {
|
||||||
|
return poly.contour.centroid();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ExPolygon concave_hull(const ExPolygons& polys) {
|
||||||
|
if(polys.empty()) return ExPolygon();
|
||||||
|
|
||||||
|
ExPolygons punion = unify(polys);
|
||||||
|
|
||||||
|
ExPolygon ret;
|
||||||
|
|
||||||
|
if(punion.size() == 1) return punion.front();
|
||||||
|
|
||||||
|
// We get the centroids of all the islands in the 2D slice
|
||||||
|
Points centroids; centroids.reserve(punion.size());
|
||||||
|
std::transform(punion.begin(), punion.end(), std::back_inserter(centroids),
|
||||||
|
[](const ExPolygon& poly) { return centroid(poly); });
|
||||||
|
|
||||||
|
// Centroid of the centroids of islands. This is where the additional
|
||||||
|
// connector sticks are routed.
|
||||||
|
Point cc = centroid(centroids);
|
||||||
|
|
||||||
|
punion.reserve(punion.size() + centroids.size());
|
||||||
|
|
||||||
|
std::transform(centroids.begin(), centroids.end(),
|
||||||
|
std::back_inserter(punion),
|
||||||
|
[cc](const Point& c) {
|
||||||
|
|
||||||
|
double dx = x(c) - x(cc), dy = y(c) - y(cc);
|
||||||
|
double l = std::sqrt(dx * dx + dy * dy);
|
||||||
|
double nx = dx / l, ny = dy / l;
|
||||||
|
|
||||||
|
ExPolygon r;
|
||||||
|
auto& ctour = r.contour.points;
|
||||||
|
|
||||||
|
ctour.reserve(3);
|
||||||
|
ctour.emplace_back(cc);
|
||||||
|
|
||||||
|
Point d(coord_t(mm(1)*nx), coord_t(mm(1)*ny));
|
||||||
|
ctour.emplace_back(c + Point( -y(d), x(d) ));
|
||||||
|
ctour.emplace_back(c + Point( y(d), -x(d) ));
|
||||||
|
offset(r, mm(1));
|
||||||
|
|
||||||
|
return r;
|
||||||
|
});
|
||||||
|
|
||||||
|
punion = unify(punion);
|
||||||
|
|
||||||
|
if(punion.size() != 1)
|
||||||
|
BOOST_LOG_TRIVIAL(error) << "Cannot generate correct SLA base pool!";
|
||||||
|
|
||||||
|
if(!punion.empty()) ret = punion.front();
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -237,81 +294,54 @@ void ground_layer(const TriangleMesh &mesh, ExPolygons &output, float h)
|
||||||
output = tmp.front();
|
output = tmp.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out)
|
void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
|
||||||
|
double min_wall_thickness_mm,
|
||||||
|
double min_wall_height_mm)
|
||||||
{
|
{
|
||||||
using libnest2d::PolygonImpl;
|
|
||||||
using boost::geometry::convex_hull;
|
|
||||||
using boost::geometry::is_valid;
|
|
||||||
|
|
||||||
static const Point::coord_type INNER_OFFSET_DIST = 2000000;
|
|
||||||
static const Point::coord_type OFFSET_DIST = 5000000;
|
|
||||||
static const Point::coord_type HEIGHT = 10000000;
|
|
||||||
|
|
||||||
// 1: Offset the ground layer
|
// 1: Offset the ground layer
|
||||||
auto concaveh = ground_layer.front(); //concave_hull(ground_layer);
|
auto concaveh = concave_hull(ground_layer);
|
||||||
|
if(concaveh.contour.points.empty()) return;
|
||||||
concaveh.holes.clear();
|
concaveh.holes.clear();
|
||||||
|
|
||||||
// BoostPolygon chull_boost;
|
BoundingBox bb(concaveh);
|
||||||
// convex_hull(convert(ground_layer), chull_boost);
|
coord_t w = bb.max.x - bb.min.x;
|
||||||
// auto concaveh = convert(chull_boost);
|
coord_t h = bb.max.y - bb.min.y;
|
||||||
|
|
||||||
// auto pool = roofs(concaveh, HEIGHT);
|
auto wall_thickness = coord_t(std::pow((w+h)*0.1, 0.8));
|
||||||
|
|
||||||
// // Generate outer walls
|
const coord_t WALL_THICKNESS = mm(min_wall_thickness_mm) + wall_thickness;
|
||||||
// auto fp = [](const Point& p, Point::coord_type z) {
|
const coord_t WALL_DISTANCE = coord_t(0.3*WALL_THICKNESS);
|
||||||
// return Pointf3::new_unscale(p.x, p.y, z);
|
const coord_t HEIGHT = mm(min_wall_height_mm);
|
||||||
// };
|
|
||||||
|
|
||||||
// auto lines = concaveh.lines();
|
auto outer_base = concaveh;
|
||||||
// std::cout << "lines: " << lines.size() << std::endl;
|
offset(outer_base, WALL_THICKNESS+WALL_DISTANCE);
|
||||||
// for(auto& l : lines) {
|
auto inner_base = outer_base;
|
||||||
// auto s = pool.points.size();
|
offset(inner_base, -WALL_THICKNESS);
|
||||||
|
inner_base.holes.clear(); outer_base.holes.clear();
|
||||||
// pool.points.emplace_back(fp(l.a, 0));
|
|
||||||
// pool.points.emplace_back(fp(l.b, 0));
|
|
||||||
// pool.points.emplace_back(fp(l.a, HEIGHT));
|
|
||||||
// pool.points.emplace_back(fp(l.b, HEIGHT));
|
|
||||||
|
|
||||||
// pool.indices.emplace_back(s, s + 3, s + 1);
|
|
||||||
// pool.indices.emplace_back(s, s + 2, s + 3);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// out = mesh(pool);
|
|
||||||
|
|
||||||
BoostPolygon chull_boost = convert(concaveh);
|
|
||||||
// convex_hull(convert(ground_layer), chull_boost);
|
|
||||||
|
|
||||||
offset(chull_boost, INNER_OFFSET_DIST);
|
|
||||||
auto chull_outer_boost = chull_boost;
|
|
||||||
offset(chull_outer_boost, OFFSET_DIST);
|
|
||||||
|
|
||||||
|
|
||||||
// Convert back to Slic3r format
|
|
||||||
ExPolygon chull_inner = convert(chull_boost);
|
|
||||||
ExPolygon chull_outer = convert(chull_outer_boost);
|
|
||||||
|
|
||||||
ExPolygon top_poly;
|
ExPolygon top_poly;
|
||||||
top_poly.contour = chull_outer.contour;
|
top_poly.contour = outer_base.contour;
|
||||||
top_poly.holes.emplace_back(chull_inner.contour);
|
top_poly.holes.emplace_back(inner_base.contour);
|
||||||
reverse(top_poly.holes.back());
|
auto& tph = top_poly.holes.back().points;
|
||||||
|
std::reverse(tph.begin(), tph.end());
|
||||||
|
|
||||||
Contour3D pool;
|
Contour3D pool;
|
||||||
|
|
||||||
Polygons top_triangles, bottom_triangles;
|
Polygons top_triangles, bottom_triangles;
|
||||||
top_poly.triangulate_pp(&top_triangles);
|
top_poly.triangulate_p2t(&top_triangles);
|
||||||
chull_outer.triangulate_pp(&bottom_triangles);
|
outer_base.triangulate_p2t(&bottom_triangles);
|
||||||
auto top_plate = convert(top_triangles, 0, false);
|
auto top_plate = convert(top_triangles, 0, false);
|
||||||
auto bottom_plate = convert(bottom_triangles, -HEIGHT, true);
|
auto bottom_plate = convert(bottom_triangles, -HEIGHT, true);
|
||||||
auto innerbed = inner_bed(chull_inner, HEIGHT/2);
|
auto innerbed = inner_bed(inner_base, HEIGHT/2);
|
||||||
|
|
||||||
// Generate outer walls
|
// Generate outer walls
|
||||||
auto fp = [](const Point& p, Point::coord_type z) {
|
auto fp = [](const Point& p, coord_t z) {
|
||||||
return Pointf3::new_unscale(p.x, p.y, z);
|
return Pointf3::new_unscale(x(p), y(p), z);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto lines = chull_outer.lines();
|
auto lines = outer_base.lines();
|
||||||
for(auto& l : lines) {
|
for(auto& l : lines) {
|
||||||
auto s = pool.points.size();
|
auto s = coord_t(pool.points.size());
|
||||||
|
|
||||||
pool.points.emplace_back(fp(l.a, -HEIGHT));
|
pool.points.emplace_back(fp(l.a, -HEIGHT));
|
||||||
pool.points.emplace_back(fp(l.b, -HEIGHT));
|
pool.points.emplace_back(fp(l.b, -HEIGHT));
|
||||||
|
|
|
@ -15,11 +15,14 @@ using ExPolygons = std::vector<ExPolygon>;
|
||||||
/// Calculate the polygon representing the slice of the lowest layer of mesh
|
/// Calculate the polygon representing the slice of the lowest layer of mesh
|
||||||
void ground_layer(const TriangleMesh& mesh,
|
void ground_layer(const TriangleMesh& mesh,
|
||||||
ExPolygons& output,
|
ExPolygons& output,
|
||||||
float height = .1f);
|
float height = 0.1f);
|
||||||
|
|
||||||
/// Calculate the pool for the mesh for SLA printing
|
/// Calculate the pool for the mesh for SLA printing
|
||||||
void create_base_pool(const ExPolygons& ground_layer,
|
void create_base_pool(const ExPolygons& ground_layer,
|
||||||
TriangleMesh& output_mesh);
|
TriangleMesh& output_mesh,
|
||||||
|
double min_wall_thickness_mm = 4,
|
||||||
|
double min_wall_height_mm = 5
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue