mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-24 07:03:59 -06:00
[refactor] Move remaining utility functions into sla_test_utils
This commit is contained in:
parent
4bc4e347fb
commit
7f476f38b9
3 changed files with 175 additions and 167 deletions
|
@ -292,3 +292,103 @@ void check_validity(const TriangleMesh &input_mesh, int flags)
|
|||
REQUIRE(mesh.is_manifold());
|
||||
}
|
||||
}
|
||||
|
||||
void check_raster_transformations(sla::Raster::Orientation o, sla::Raster::TMirroring mirroring)
|
||||
{
|
||||
double disp_w = 120., disp_h = 68.;
|
||||
sla::Raster::Resolution res{2560, 1440};
|
||||
sla::Raster::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
|
||||
|
||||
auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
|
||||
sla::Raster::Trafo trafo{o, mirroring};
|
||||
trafo.origin_x = bb.center().x();
|
||||
trafo.origin_y = bb.center().y();
|
||||
|
||||
sla::Raster raster{res, pixdim, trafo};
|
||||
|
||||
// create box of size 32x32 pixels (not 1x1 to avoid antialiasing errors)
|
||||
coord_t pw = 32 * coord_t(std::ceil(scaled<double>(pixdim.w_mm)));
|
||||
coord_t ph = 32 * coord_t(std::ceil(scaled<double>(pixdim.h_mm)));
|
||||
ExPolygon box;
|
||||
box.contour.points = {{-pw, -ph}, {pw, -ph}, {pw, ph}, {-pw, ph}};
|
||||
|
||||
double tr_x = scaled<double>(20.), tr_y = tr_x;
|
||||
|
||||
box.translate(tr_x, tr_y);
|
||||
ExPolygon expected_box = box;
|
||||
|
||||
// Now calculate the position of the translated box according to output
|
||||
// trafo.
|
||||
if (o == sla::Raster::Orientation::roPortrait) expected_box.rotate(PI / 2.);
|
||||
|
||||
if (mirroring[X])
|
||||
for (auto &p : expected_box.contour.points) p.x() = -p.x();
|
||||
|
||||
if (mirroring[Y])
|
||||
for (auto &p : expected_box.contour.points) p.y() = -p.y();
|
||||
|
||||
raster.draw(box);
|
||||
|
||||
Point expected_coords = expected_box.contour.bounding_box().center();
|
||||
double rx = unscaled(expected_coords.x() + bb.center().x()) / pixdim.w_mm;
|
||||
double ry = unscaled(expected_coords.y() + bb.center().y()) / pixdim.h_mm;
|
||||
auto w = size_t(std::floor(rx));
|
||||
auto h = res.height_px - size_t(std::floor(ry));
|
||||
|
||||
REQUIRE((w < res.width_px && h < res.height_px));
|
||||
|
||||
auto px = raster.read_pixel(w, h);
|
||||
|
||||
if (px != FullWhite) {
|
||||
sla::PNGImage img;
|
||||
std::fstream outf("out.png", std::ios::out);
|
||||
|
||||
outf << img.serialize(raster);
|
||||
}
|
||||
|
||||
REQUIRE(px == FullWhite);
|
||||
}
|
||||
|
||||
ExPolygon square_with_hole(double v)
|
||||
{
|
||||
ExPolygon poly;
|
||||
coord_t V = scaled(v / 2.);
|
||||
|
||||
poly.contour.points = {{-V, -V}, {V, -V}, {V, V}, {-V, V}};
|
||||
poly.holes.emplace_back();
|
||||
V = V / 2;
|
||||
poly.holes.front().points = {{-V, V}, {V, V}, {V, -V}, {-V, -V}};
|
||||
return poly;
|
||||
}
|
||||
|
||||
double raster_white_area(const sla::Raster &raster)
|
||||
{
|
||||
if (raster.empty()) return std::nan("");
|
||||
|
||||
auto res = raster.resolution();
|
||||
double a = 0;
|
||||
|
||||
for (size_t x = 0; x < res.width_px; ++x)
|
||||
for (size_t y = 0; y < res.height_px; ++y) {
|
||||
auto px = raster.read_pixel(x, y);
|
||||
a += pixel_area(px, raster.pixel_dimensions());
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
double predict_error(const ExPolygon &p, const sla::Raster::PixelDim &pd)
|
||||
{
|
||||
auto lines = p.lines();
|
||||
double pix_err = pixel_area(FullWhite, pd) / 2.;
|
||||
|
||||
// Worst case is when a line is parallel to the shorter axis of one pixel,
|
||||
// when the line will be composed of the max number of pixels
|
||||
double pix_l = std::min(pd.h_mm, pd.w_mm);
|
||||
|
||||
double error = 0.;
|
||||
for (auto &l : lines)
|
||||
error += (unscaled(l.length()) / pix_l) * pix_err;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue