Simplifying pad wall triangulation. Removing iterators.

This commit is contained in:
tamasmeszaros 2019-02-14 14:49:29 +01:00
parent 40e6980db1
commit 9bd2f0cf53

View file

@ -66,47 +66,44 @@ Contour3D walls(const Polygon& lower, const Polygon& upper,
// height and the offset difference. // height and the offset difference.
// Offset in the index array for the ceiling // Offset in the index array for the ceiling
const auto offs = long(upper.points.size()); const auto offs = upper.points.size();
// Shorthand for the vertex arrays // Shorthand for the vertex arrays
auto& upoints = upper.points, &lpoints = lower.points; auto& upoints = upper.points, &lpoints = lower.points;
auto& rpts = ret.points; auto& rfaces = ret.indices;
// If the Z levels are flipped, or the offset difference is negative, we // If the Z levels are flipped, or the offset difference is negative, we
// will interpret that as the triangles normals should be inverted. // will interpret that as the triangles normals should be inverted.
bool inverted = upper_z_mm < lower_z_mm || offset_difference_mm < 0; bool inverted = upper_z_mm < lower_z_mm || offset_difference_mm < 0;
// Copy the points into the mesh, convert them from 2D to 3D // Copy the points into the mesh, convert them from 2D to 3D
ret.points.reserve(upoints.size() + lpoints.size()); rpts.reserve(upoints.size() + lpoints.size());
for(auto& p : upoints) rfaces.reserve(2*upoints.size() + 2*lpoints.size());
ret.points.emplace_back(unscale(p.x(), p.y(), mm(upper_z_mm))); auto s_uz = mm(upper_z_mm), s_lz = mm(lower_z_mm);
for(auto& p : lpoints) for(auto& p : upoints) rpts.emplace_back(unscale(p.x(), p.y(), s_uz));
ret.points.emplace_back(unscale(p.x(), p.y(), mm(lower_z_mm))); for(auto& p : lpoints) rpts.emplace_back(unscale(p.x(), p.y(), s_lz));
// Create cyclic iterators for the vertices in both polygons. // Create pointing indices into vertex arrays. u-upper, l-lower
auto uit = upoints.begin(); size_t uidx = 0, lidx = offs, unextidx = 1, lnextidx = offs + 1;
auto lit = lpoints.begin();
// Simple squared distance calculation.
auto distfn = [](const Vec3d& p1, const Vec3d& p2) {
auto p = p1 - p2; return p.transpose() * p;
};
// We need to find the closest point on lower polygon to the first point on // We need to find the closest point on lower polygon to the first point on
// the upper polygon. These will be our starting points. // the upper polygon. These will be our starting points.
double distmin = std::numeric_limits<double>::max(); double distmin = std::numeric_limits<double>::max();
for(auto lt = lpoints.begin(); lt != lpoints.end(); ++lt) { for(size_t l = lidx; l < rpts.size(); ++l) {
thr(); thr();
Vec2d p = (*lt - *uit).cast<double>(); double d = distfn(rpts[l], rpts[uidx]);
double d = p.transpose() * p; if(d < distmin) { lidx = l; distmin = d; }
if(d < distmin) { lit = lt; distmin = d; }
} }
// Iterators to the polygon vertices which are always ahead of uit and lit // Iterators to the polygon vertices which are always ahead of uit and lit
// in cyclic mode. // in cyclic mode.
auto unextit = std::next(uit); lnextidx = lidx + 1;
auto lnextit = std::next(lit); if(lnextidx == rpts.size()) lnextidx = offs;
if(lnextit == lower.points.end()) lnextit = lower.points.begin();
// Get the integer vertex indices from the iterators.
auto uidx = uit - upper.points.begin();
auto unextidx = unextit - upper.points.begin();
auto lidx = offs + lit - lower.points.begin();
auto lnextidx = offs + lnextit - lower.points.begin();
// This will be the flip switch to toggle between upper and lower triangle // This will be the flip switch to toggle between upper and lower triangle
// creation mode // creation mode
@ -122,11 +119,6 @@ Contour3D walls(const Polygon& lower, const Polygon& upper,
// previous. // previous.
double current_fit = 0, prev_fit = 0; double current_fit = 0, prev_fit = 0;
// Simple squared distance calculation.
auto distfn = [](const Vec3d& p1, const Vec3d& p2) {
auto p = p1 - p2; return p.transpose() * p;
};
// Every triangle of the wall has two edges connecting the upper plate with // Every triangle of the wall has two edges connecting the upper plate with
// the lower plate. From the length of these two edges and the zdiff we // the lower plate. From the length of these two edges and the zdiff we
// can calculate the momentary squared offset distance at a particular // can calculate the momentary squared offset distance at a particular
@ -137,44 +129,35 @@ Contour3D walls(const Polygon& lower, const Polygon& upper,
// Mark the current vertex iterator positions. If the iterators return to // Mark the current vertex iterator positions. If the iterators return to
// the same position, the loop can be terminated. // the same position, the loop can be terminated.
auto uend = uit; auto lend = lit; size_t uendidx = uidx, lendidx = lidx;
do { do { thr(); // check throw if canceled
thr();
prev_fit = current_fit; prev_fit = current_fit;
// Get the actual 2D vertices from the upper and lower polygon.
Vec2d ip = unscale(uit->x(), uit->y());
Vec2d inextp = unscale(unextit->x(), unextit->y());
Vec2d op = unscale(lit->x(), lit->y());
Vec2d onextp = unscale(lnextit->x(), lnextit->y());
switch(proceed) { // proceed depending on the current state switch(proceed) { // proceed depending on the current state
case Proceed::UPPER: case Proceed::UPPER:
if(!ustarted || uit != uend) { // if there are vertices remaining if(!ustarted || uidx != uendidx) { // there are vertices remaining
// Get the 3D vertices in order // Get the 3D vertices in order
Vec3d p1(ip.x(), ip.y(), upper_z_mm); const Vec3d& p_up1 = rpts[size_t(uidx)];
Vec3d p2(op.x(), op.y(), lower_z_mm); const Vec3d& p_low = rpts[size_t(lidx)];
Vec3d p3(inextp.x(), inextp.y(), upper_z_mm); const Vec3d& p_up2 = rpts[size_t(unextidx)];
// Calculate fitness: the average of the two connecting edges // Calculate fitness: the average of the two connecting edges
double a = offsdiff2 - (distfn(p1, p2) - zdiff2); double a = offsdiff2 - (distfn(p_up1, p_low) - zdiff2);
double b = offsdiff2 - (distfn(p3, p2) - zdiff2); double b = offsdiff2 - (distfn(p_up2, p_low) - zdiff2);
current_fit = (std::abs(a) + std::abs(b)) / 2; current_fit = (std::abs(a) + std::abs(b)) / 2;
if(current_fit > prev_fit) { // fit is worse than previously if(current_fit > prev_fit) { // fit is worse than previously
proceed = Proceed::LOWER; proceed = Proceed::LOWER;
} else { // good to go, create the triangle } else { // good to go, create the triangle
inverted? ret.indices.emplace_back(unextidx, lidx, uidx) : inverted? rfaces.emplace_back(unextidx, lidx, uidx) :
ret.indices.emplace_back(uidx, lidx, unextidx) ; rfaces.emplace_back(uidx, lidx, unextidx) ;
// Increment the iterators, rotate if necessary // Increment the iterators, rotate if necessary
++uit; ++unextit; ++uidx; ++unextidx;
if(unextit == upoints.end()) unextit = upoints.begin(); if(unextidx == offs) unextidx = 0;
if(uit == upoints.end()) uit = upoints.begin(); if(uidx == offs) uidx = 0;
unextidx = unextit - upoints.begin();
uidx = uit - upoints.begin();
ustarted = true; // mark the movement of the iterators ustarted = true; // mark the movement of the iterators
// so that the comparison to uend can be made correctly // so that the comparison to uend can be made correctly
@ -184,26 +167,24 @@ Contour3D walls(const Polygon& lower, const Polygon& upper,
break; break;
case Proceed::LOWER: case Proceed::LOWER:
// Mode with lower segment, upper vertex. Same structure: // Mode with lower segment, upper vertex. Same structure:
if(!lstarted || lit != lend) { if(!lstarted || lidx != lendidx) {
Vec3d p1(op.x(), op.y(), lower_z_mm); const Vec3d& p_low1 = rpts[size_t(lidx)];
Vec3d p2(onextp.x(), onextp.y(), lower_z_mm); const Vec3d& p_low2 = rpts[size_t(lnextidx)];
Vec3d p3(ip.x(), ip.y(), upper_z_mm); const Vec3d& p_up = rpts[size_t(uidx)];
double a = offsdiff2 - (distfn(p3, p1) - zdiff2); double a = offsdiff2 - (distfn(p_up, p_low1) - zdiff2);
double b = offsdiff2 - (distfn(p3, p2) - zdiff2); double b = offsdiff2 - (distfn(p_up, p_low2) - zdiff2);
current_fit = (std::abs(a) + std::abs(b)) / 2; current_fit = (std::abs(a) + std::abs(b)) / 2;
if(current_fit > prev_fit) { if(current_fit > prev_fit) {
proceed = Proceed::UPPER; proceed = Proceed::UPPER;
} else { } else {
inverted? ret.indices.emplace_back(uidx, lnextidx, lidx) : inverted? rfaces.emplace_back(uidx, lnextidx, lidx) :
ret.indices.emplace_back(lidx, lnextidx, uidx); rfaces.emplace_back(lidx, lnextidx, uidx);
++lit; ++lnextit; ++lidx; ++lnextidx;
if(lnextit == lpoints.end()) lnextit = lpoints.begin(); if(lnextidx == rpts.size()) lnextidx = offs;
if(lit == lpoints.end()) lit = lpoints.begin(); if(lidx == rpts.size()) lidx = offs;
lnextidx = offs + lnextit - lpoints.begin();
lidx = offs + lit - lpoints.begin();
lstarted = true; lstarted = true;
} }
@ -211,7 +192,7 @@ Contour3D walls(const Polygon& lower, const Polygon& upper,
break; break;
} // end of switch } // end of switch
} while(!ustarted || !lstarted || uit != uend || lit != lend); } while(!ustarted || !lstarted || uidx != uendidx || lidx != lendidx);
return ret; return ret;
} }
@ -356,7 +337,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
double degrees, double degrees,
double ceilheight_mm, double ceilheight_mm,
bool dir, bool dir,
ThrowOnCancel throw_on_cancel, ThrowOnCancel thr,
ExPolygon& last_offset, double& last_height) ExPolygon& last_offset, double& last_height)
{ {
auto ob = base_plate; auto ob = base_plate;
@ -375,7 +356,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
int(radius_mm*std::cos(degrees * PI / 180 - PI/2) / stepx) : steps; int(radius_mm*std::cos(degrees * PI / 180 - PI/2) / stepx) : steps;
for(int i = 1; i <= tos; ++i) { for(int i = 1; i <= tos; ++i) {
throw_on_cancel(); thr();
ob = base_plate; ob = base_plate;
@ -389,7 +370,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
Contour3D pwalls; Contour3D pwalls;
double prev_x = xx - (i - 1) * stepx; double prev_x = xx - (i - 1) * stepx;
pwalls = walls(ob.contour, ob_prev.contour, wh, wh_prev, s*prev_x, throw_on_cancel); pwalls = walls(ob.contour, ob_prev.contour, wh, wh_prev, s*prev_x, thr);
curvedwalls.merge(pwalls); curvedwalls.merge(pwalls);
ob_prev = ob; ob_prev = ob;
@ -401,7 +382,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
int tos = int(tox / stepx); int tos = int(tox / stepx);
for(int i = 1; i <= tos; ++i) { for(int i = 1; i <= tos; ++i) {
throw_on_cancel(); thr();
ob = base_plate; ob = base_plate;
double r2 = radius_mm * radius_mm; double r2 = radius_mm * radius_mm;
@ -413,7 +394,8 @@ Contour3D round_edges(const ExPolygon& base_plate,
Contour3D pwalls; Contour3D pwalls;
double prev_x = xx - radius_mm + (i - 1)*stepx; double prev_x = xx - radius_mm + (i - 1)*stepx;
pwalls = walls(ob_prev.contour, ob.contour, wh_prev, wh, s*prev_x, throw_on_cancel); pwalls =
walls(ob_prev.contour, ob.contour, wh_prev, wh, s*prev_x, thr);
curvedwalls.merge(pwalls); curvedwalls.merge(pwalls);
ob_prev = ob; ob_prev = ob;