Gyroid infill - automatic discretization steps and refactoring

This commit is contained in:
Lukas Matena 2018-04-13 13:46:31 +02:00
parent abe6e8a783
commit 68c3749696

View File

@ -9,77 +9,125 @@
namespace Slic3r { namespace Slic3r {
static inline Polyline make_wave_vertical(
double width, double height, double x0, static inline double f(double x, double z_sin, double z_cos, bool vertical, bool flip) {
double segmentSize, double scaleFactor, if (vertical) {
double z_cos, double z_sin, bool flip) double phase_offset = (z_cos < 0 ? M_PI : 0) + M_PI;
{ double a = sin(x + phase_offset);
Polyline polyline;
polyline.points.emplace_back(Point(coord_t(clamp(0., width, x0) * scaleFactor), 0));
double phase_offset_sin = (z_cos < 0 ? M_PI : 0) + M_PI;
double phase_offset_cos = (z_cos < 0 ? M_PI : 0) + M_PI + (flip ? M_PI : 0.);
for (double y = 0.; y < height + segmentSize; y += segmentSize) {
y = std::min(y, height);
double a = sin(y + phase_offset_sin);
double b = - z_cos; double b = - z_cos;
double res = z_sin * cos(y + phase_offset_cos); double res = z_sin * cos(x + phase_offset + (flip ? M_PI : 0.));
double r = sqrt(sqr(a) + sqr(b)); double r = sqrt(sqr(a) + sqr(b));
double x = clamp(0., width, asin(a/r) + asin(res/r) + M_PI + x0); return asin(a/r) + asin(res/r) + M_PI;
polyline.points.emplace_back(convert_to<Point>(Pointf(x, y) * scaleFactor));
} }
if (flip) else {
std::reverse(polyline.points.begin(), polyline.points.end()); double phase_offset = z_sin < 0 ? M_PI : 0.;
double a = cos(x + phase_offset);
double b = - z_sin;
double res = z_cos * sin(x + phase_offset + (flip ? 0 : M_PI));
double r = sqrt(sqr(a) + sqr(b));
return (asin(a/r) + asin(res/r) + 0.5 * M_PI);
}
}
static inline Polyline make_wave(
const std::vector<Pointf>& one_period, double width, double height, double offset, double scaleFactor,
double z_cos, double z_sin, bool vertical)
{
std::vector<Pointf> points = one_period;
double period = points.back().x;
points.pop_back();
int n = points.size();
do {
points.emplace_back(Pointf(points[points.size()-n].x + period, points[points.size()-n].y));
} while (points.back().x < width);
points.back().x = width;
// and construct the final polyline to return:
Polyline polyline;
for (auto& point : points) {
point.y += offset;
point.y = clamp(0., height, double(point.y));
if (vertical)
std::swap(point.x, point.y);
polyline.points.emplace_back(convert_to<Point>(point * scaleFactor));
}
return polyline; return polyline;
} }
static inline Polyline make_wave_horizontal(
double width, double height, double y0, static std::vector<Pointf> make_one_period(double width, double scaleFactor, double z_cos, double z_sin, bool vertical, bool flip) {
double segmentSize, double scaleFactor, std::vector<Pointf> points;
double z_cos, double z_sin, bool flip) double dx = M_PI_4; // very coarse spacing to begin with
{ double limit = std::min(2*M_PI, width);
Polyline polyline; for (double x = 0.; x < limit + EPSILON; x += dx) { // so the last point is there too
polyline.points.emplace_back(Point(0, coord_t(clamp(0., height, y0) * scaleFactor))); x = std::min(x, limit);
double phase_offset_sin = (z_sin < 0 ? M_PI : 0) + (flip ? 0 : M_PI); points.emplace_back(Pointf(x,f(x, z_sin,z_cos, vertical, flip)));
double phase_offset_cos = z_sin < 0 ? M_PI : 0.;
for (double x = 0.; x < width + segmentSize; x += segmentSize) {
x = std::min(x, width);
double a = cos(x + phase_offset_cos);
double b = - z_sin;
double res = z_cos * sin(x + phase_offset_sin);
double r = sqrt(sqr(a) + sqr(b));
double y = clamp(0., height, asin(a/r) + asin(res/r) + 0.5 * M_PI + y0);
polyline.points.emplace_back(convert_to<Point>(Pointf(x, y) * scaleFactor));
} }
if (flip)
std::reverse(polyline.points.begin(), polyline.points.end()); // now we will check all internal points and in case some are too far from the line connecting its neighbours,
return polyline; // we will add one more point on each side:
const double tolerance = .1;
for (unsigned int i=1;i<points.size()-1;++i) {
auto& lp = points[i-1]; // left point
auto& tp = points[i]; // this point
auto& rp = points[i+1]; // right point
// calculate distance of the point to the line:
double dist_mm = unscale(scaleFactor * std::abs( (rp.y - lp.y)*tp.x + (lp.x - rp.x)*tp.y + (rp.x*lp.y - rp.y*lp.x) ) / std::hypot((rp.y - lp.y),(lp.x - rp.x)));
if (dist_mm > tolerance) { // if the difference from straight line is more than this
double x = 0.5f * (points[i-1].x + points[i].x);
points.emplace_back(Pointf(x, f(x, z_sin, z_cos, vertical, flip)));
x = 0.5f * (points[i+1].x + points[i].x);
points.emplace_back(Pointf(x, f(x, z_sin, z_cos, vertical, flip)));
std::sort(points.begin(), points.end()); // we added the points to the end, but need them all in order
--i; // decrement i so we also check the first newly added point
}
}
return points;
} }
static Polylines make_gyroid_waves(double gridZ, double density_adjusted, double line_spacing, double width, double height) static Polylines make_gyroid_waves(double gridZ, double density_adjusted, double line_spacing, double width, double height)
{ {
double scaleFactor = scale_(line_spacing) / density_adjusted; const double scaleFactor = scale_(line_spacing) / density_adjusted;
double segmentSize = 0.5 * density_adjusted;
//scale factor for 5% : 8 712 388 //scale factor for 5% : 8 712 388
// 1z = 10^-6 mm ? // 1z = 10^-6 mm ?
double z = gridZ / scaleFactor; const double z = gridZ / scaleFactor;
double z_sin = sin(z); const double z_sin = sin(z);
double z_cos = cos(z); const double z_cos = cos(z);
Polylines result;
if (abs(z_sin) <= abs(z_cos)) { bool vertical = (std::abs(z_sin) <= std::abs(z_cos));
// Vertical wave double lower_bound = 0.;
double x0 = M_PI * (int)((- 0.5 * M_PI) / M_PI - 1.); double upper_bound = height;
bool flip = ((int)(x0 / M_PI + 1.) & 1) != 0; bool flip = true;
for (; x0 < width - 0.5 * M_PI; x0 += M_PI, flip = ! flip) if (vertical) {
result.emplace_back(make_wave_vertical(width, height, x0, segmentSize, scaleFactor, z_cos, z_sin, flip)); flip = false;
} else { lower_bound = -M_PI;
// Horizontal wave upper_bound = width - M_PI_2;
bool flip = true; std::swap(width,height);
for (double y0 = 0.; y0 < height; y0 += M_PI, flip = !flip)
result.emplace_back(make_wave_horizontal(width, height, y0, segmentSize, scaleFactor, z_cos, z_sin, flip));
} }
std::vector<Pointf> one_period = make_one_period(width, scaleFactor, z_cos, z_sin, vertical, flip); // creates one period of the waves, so it doesn't have to be recalculated all the time
Polylines result;
for (double y0 = lower_bound; y0 < upper_bound+EPSILON; y0 += 2*M_PI) // creates odd polylines
result.emplace_back(make_wave(one_period, width, height, y0, scaleFactor, z_cos, z_sin, vertical));
flip = !flip; // even polylines are a bit shifted
one_period = make_one_period(width, scaleFactor, z_cos, z_sin, vertical, flip); // updates the one period sample
for (double y0 = lower_bound + M_PI; y0 < upper_bound+EPSILON; y0 += 2*M_PI) // creates even polylines
result.emplace_back(make_wave(one_period, width, height, y0, scaleFactor, z_cos, z_sin, vertical));
return result; return result;
} }
void FillGyroid::_fill_surface_single( void FillGyroid::_fill_surface_single(
const FillParams &params, const FillParams &params,
unsigned int thickness_layers, unsigned int thickness_layers,
@ -90,7 +138,9 @@ void FillGyroid::_fill_surface_single(
// no rotation is supported for this infill pattern (yet) // no rotation is supported for this infill pattern (yet)
BoundingBox bb = expolygon.contour.bounding_box(); BoundingBox bb = expolygon.contour.bounding_box();
// Density adjusted to have a good %of weight. // Density adjusted to have a good %of weight.
double density_adjusted = params.density * 1.75; double density_adjusted = std::max(0., params.density * 2.);
// Distance between the gyroid waves in scaled coordinates. // Distance between the gyroid waves in scaled coordinates.
coord_t distance = coord_t(scale_(this->spacing) / density_adjusted); coord_t distance = coord_t(scale_(this->spacing) / density_adjusted);