SLA backend refactored, except Hollowing

This commit is contained in:
tamasmeszaros
2021-05-21 14:08:05 +02:00
parent 1c35dfe591
commit 1009f78862
22 changed files with 687 additions and 404 deletions

View File

@@ -2,22 +2,22 @@
namespace Slic3r { namespace sla {
Contour3D sphere(double rho, Portion portion, double fa) {
indexed_triangle_set sphere(double rho, Portion portion, double fa) {
Contour3D ret;
indexed_triangle_set ret;
// prohibit close to zero radius
if(rho <= 1e-6 && rho >= -1e-6) return ret;
auto& vertices = ret.points;
auto& facets = ret.faces3;
auto& vertices = ret.vertices;
auto& facets = ret.indices;
// Algorithm:
// Add points one-by-one to the sphere grid and form facets using relative
// coordinates. Sphere is composed effectively of a mesh of stacked circles.
// adjust via rounding to get an even multiple for any provided angle.
double angle = (2*PI / floor(2*PI / fa));
double angle = (2 * PI / floor(2*PI / fa) );
// Ring to be scaled to generate the steps of the sphere
std::vector<double> ring;
@@ -32,8 +32,9 @@ Contour3D sphere(double rho, Portion portion, double fa) {
// special case: first ring connects to 0,0,0
// insert and form facets.
if(sbegin == 0)
vertices.emplace_back(Vec3d(0.0, 0.0, -rho + increment*sbegin*2.0*rho));
if (sbegin == 0)
vertices.emplace_back(
Vec3f(0.f, 0.f, float(-rho + increment * sbegin * 2. * rho)));
auto id = coord_t(vertices.size());
for (size_t i = 0; i < ring.size(); i++) {
@@ -42,7 +43,7 @@ Contour3D sphere(double rho, Portion portion, double fa) {
// radius of the circle for this step.
const double r = std::sqrt(std::abs(rho*rho - z*z));
Vec2d b = Eigen::Rotation2Dd(ring[i]) * Eigen::Vector2d(0, r);
vertices.emplace_back(Vec3d(b(0), b(1), z));
vertices.emplace_back(Vec3d(b(0), b(1), z).cast<float>());
if (sbegin == 0)
(i == 0) ? facets.emplace_back(coord_t(ring.size()), 0, 1) :
@@ -53,12 +54,12 @@ Contour3D sphere(double rho, Portion portion, double fa) {
// General case: insert and form facets for each step,
// joining it to the ring below it.
for (size_t s = sbegin + 2; s < send - 1; s++) {
const double z = -rho + increment*double(s*2.0*rho);
const double z = -rho + increment * double(s * 2. * rho);
const double r = std::sqrt(std::abs(rho*rho - z*z));
for (size_t i = 0; i < ring.size(); i++) {
Vec2d b = Eigen::Rotation2Dd(ring[i]) * Eigen::Vector2d(0, r);
vertices.emplace_back(Vec3d(b(0), b(1), z));
vertices.emplace_back(Vec3d(b(0), b(1), z).cast<float>());
auto id_ringsize = coord_t(id - int(ring.size()));
if (i == 0) {
// wrap around
@@ -75,7 +76,7 @@ Contour3D sphere(double rho, Portion portion, double fa) {
// special case: last ring connects to 0,0,rho*2.0
// only form facets.
if(send >= size_t(2*PI / angle)) {
vertices.emplace_back(Vec3d(0.0, 0.0, -rho + increment*send*2.0*rho));
vertices.emplace_back(0.f, 0.f, float(-rho + increment*send*2.0*rho));
for (size_t i = 0; i < ring.size(); i++) {
auto id_ringsize = coord_t(id - int(ring.size()));
if (i == 0) {
@@ -92,15 +93,15 @@ Contour3D sphere(double rho, Portion portion, double fa) {
return ret;
}
Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d &sp)
indexed_triangle_set cylinder(double r, double h, size_t ssteps, const Vec3d &sp)
{
assert(ssteps > 0);
Contour3D ret;
indexed_triangle_set ret;
auto steps = int(ssteps);
auto& points = ret.points;
auto& indices = ret.faces3;
auto& points = ret.vertices;
auto& indices = ret.indices;
points.reserve(2*ssteps);
double a = 2*PI/steps;
@@ -110,17 +111,17 @@ Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d &sp)
// Upper circle points
for(int i = 0; i < steps; ++i) {
double phi = i*a;
double ex = endp(X) + r*std::cos(phi);
double ey = endp(Y) + r*std::sin(phi);
points.emplace_back(ex, ey, endp(Z));
auto ex = float(endp(X) + r*std::cos(phi));
auto ey = float(endp(Y) + r*std::sin(phi));
points.emplace_back(ex, ey, float(endp(Z)));
}
// Lower circle points
for(int i = 0; i < steps; ++i) {
double phi = i*a;
double x = jp(X) + r*std::cos(phi);
double y = jp(Y) + r*std::sin(phi);
points.emplace_back(x, y, jp(Z));
auto x = float(jp(X) + r*std::cos(phi));
auto y = float(jp(Y) + r*std::sin(phi));
points.emplace_back(x, y, float(jp(Z)));
}
// Now create long triangles connecting upper and lower circles
@@ -139,13 +140,13 @@ Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d &sp)
// According to the slicing algorithms, we need to aid them with generating
// a watertight body. So we create a triangle fan for the upper and lower
// ending of the cylinder to close the geometry.
points.emplace_back(jp); int ci = int(points.size() - 1);
points.emplace_back(jp.cast<float>()); int ci = int(points.size() - 1);
for(int i = 0; i < steps - 1; ++i)
indices.emplace_back(i + offs + 1, i + offs, ci);
indices.emplace_back(offs, steps + offs - 1, ci);
points.emplace_back(endp); ci = int(points.size() - 1);
points.emplace_back(endp.cast<float>()); ci = int(points.size() - 1);
for(int i = 0; i < steps - 1; ++i)
indices.emplace_back(ci, i, i + 1);
@@ -154,14 +155,17 @@ Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d &sp)
return ret;
}
Contour3D pinhead(double r_pin, double r_back, double length, size_t steps)
indexed_triangle_set pinhead(double r_pin,
double r_back,
double length,
size_t steps)
{
assert(steps > 0);
assert(length >= 0.);
assert(r_back > 0.);
assert(r_pin > 0.);
Contour3D mesh;
indexed_triangle_set mesh;
// We create two spheres which will be connected with a robe that fits
// both circles perfectly.
@@ -187,66 +191,66 @@ Contour3D pinhead(double r_pin, double r_back, double length, size_t steps)
auto &&s1 = sphere(r_back, make_portion(0, PI / 2 + phi), detail);
auto &&s2 = sphere(r_pin, make_portion(PI / 2 + phi, PI), detail);
for (auto &p : s2.points) p.z() += h;
for (auto &p : s2.vertices) p.z() += h;
mesh.merge(s1);
mesh.merge(s2);
its_merge(mesh, s1);
its_merge(mesh, s2);
for (size_t idx1 = s1.points.size() - steps, idx2 = s1.points.size();
idx1 < s1.points.size() - 1; idx1++, idx2++) {
for (size_t idx1 = s1.vertices.size() - steps, idx2 = s1.vertices.size();
idx1 < s1.vertices.size() - 1; idx1++, idx2++) {
coord_t i1s1 = coord_t(idx1), i1s2 = coord_t(idx2);
coord_t i2s1 = i1s1 + 1, i2s2 = i1s2 + 1;
mesh.faces3.emplace_back(i1s1, i2s1, i2s2);
mesh.faces3.emplace_back(i1s1, i2s2, i1s2);
mesh.indices.emplace_back(i1s1, i2s1, i2s2);
mesh.indices.emplace_back(i1s1, i2s2, i1s2);
}
auto i1s1 = coord_t(s1.points.size()) - coord_t(steps);
auto i2s1 = coord_t(s1.points.size()) - 1;
auto i1s2 = coord_t(s1.points.size());
auto i2s2 = coord_t(s1.points.size()) + coord_t(steps) - 1;
auto i1s1 = coord_t(s1.vertices.size()) - coord_t(steps);
auto i2s1 = coord_t(s1.vertices.size()) - 1;
auto i1s2 = coord_t(s1.vertices.size());
auto i2s2 = coord_t(s1.vertices.size()) + coord_t(steps) - 1;
mesh.faces3.emplace_back(i2s2, i2s1, i1s1);
mesh.faces3.emplace_back(i1s2, i2s2, i1s1);
mesh.indices.emplace_back(i2s2, i2s1, i1s1);
mesh.indices.emplace_back(i1s2, i2s2, i1s1);
return mesh;
}
Contour3D halfcone(double baseheight,
double r_bottom,
double r_top,
const Vec3d &pos,
size_t steps)
indexed_triangle_set halfcone(double baseheight,
double r_bottom,
double r_top,
const Vec3d &pos,
size_t steps)
{
assert(steps > 0);
if (baseheight <= 0 || steps <= 0) return {};
Contour3D base;
indexed_triangle_set base;
double a = 2 * PI / steps;
auto last = int(steps - 1);
Vec3d ep{pos.x(), pos.y(), pos.z() + baseheight};
for (size_t i = 0; i < steps; ++i) {
double phi = i * a;
double x = pos.x() + r_top * std::cos(phi);
double y = pos.y() + r_top * std::sin(phi);
base.points.emplace_back(x, y, ep.z());
auto x = float(pos.x() + r_top * std::cos(phi));
auto y = float(pos.y() + r_top * std::sin(phi));
base.vertices.emplace_back(x, y, float(ep.z()));
}
for (size_t i = 0; i < steps; ++i) {
double phi = i * a;
double x = pos.x() + r_bottom * std::cos(phi);
double y = pos.y() + r_bottom * std::sin(phi);
base.points.emplace_back(x, y, pos.z());
auto x = float(pos.x() + r_bottom * std::cos(phi));
auto y = float(pos.y() + r_bottom * std::sin(phi));
base.vertices.emplace_back(x, y, float(pos.z()));
}
base.points.emplace_back(pos);
base.points.emplace_back(ep);
base.vertices.emplace_back(pos.cast<float>());
base.vertices.emplace_back(ep.cast<float>());
auto &indices = base.faces3;
auto hcenter = int(base.points.size() - 1);
auto lcenter = int(base.points.size() - 2);
auto &indices = base.indices;
auto hcenter = int(base.vertices.size() - 1);
auto lcenter = int(base.vertices.size() - 2);
auto offs = int(steps);
for (int i = 0; i < last; ++i) {
indices.emplace_back(i, i + offs, offs + i + 1);