mirror of
https://github.com/FULU-Foundation/OrcaSlicer-bambulab.git
synced 2026-05-21 23:49:28 -07:00
sla::Raster interface clarified and covered with tests.
Also renamed sla::SupportTreeAlgorithm to SupportTreeBuildsteps.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "SLARaster.hpp"
|
||||
#include "libslic3r/ExPolygon.hpp"
|
||||
#include "libslic3r/MTUtils.hpp"
|
||||
#include <libnest2d/backends/clipper/clipper_polygon.hpp>
|
||||
|
||||
// For rasterizing
|
||||
@@ -32,25 +33,30 @@ inline const ClipperLib::Paths& holes(const ClipperLib::Polygon& p) { return p.H
|
||||
|
||||
namespace sla {
|
||||
|
||||
const Raster::TMirroring Raster::NoMirror = {false, false};
|
||||
const Raster::TMirroring Raster::MirrorX = {true, false};
|
||||
const Raster::TMirroring Raster::MirrorY = {false, true};
|
||||
const Raster::TMirroring Raster::MirrorXY = {true, true};
|
||||
|
||||
|
||||
using TPixelRenderer = agg::pixfmt_gray8; // agg::pixfmt_rgb24;
|
||||
using TRawRenderer = agg::renderer_base<TPixelRenderer>;
|
||||
using TPixel = TPixelRenderer::color_type;
|
||||
using TRawBuffer = agg::rendering_buffer;
|
||||
using TBuffer = std::vector<TPixelRenderer::pixel_type>;
|
||||
|
||||
using TRendererAA = agg::renderer_scanline_aa_solid<TRawRenderer>;
|
||||
|
||||
class Raster::Impl {
|
||||
public:
|
||||
using TPixelRenderer = agg::pixfmt_gray8; // agg::pixfmt_rgb24;
|
||||
using TRawRenderer = agg::renderer_base<TPixelRenderer>;
|
||||
using TPixel = TPixelRenderer::color_type;
|
||||
using TRawBuffer = agg::rendering_buffer;
|
||||
|
||||
using TBuffer = std::vector<TPixelRenderer::pixel_type>;
|
||||
|
||||
using TRendererAA = agg::renderer_scanline_aa_solid<TRawRenderer>;
|
||||
|
||||
static const TPixel ColorWhite;
|
||||
static const TPixel ColorBlack;
|
||||
|
||||
using Format = Raster::Format;
|
||||
using Format = Raster::RawData;
|
||||
|
||||
private:
|
||||
Raster::Resolution m_resolution;
|
||||
// Raster::PixelDim m_pxdim;
|
||||
Raster::PixelDim m_pxdim_scaled; // used for scaled coordinate polygons
|
||||
TBuffer m_buf;
|
||||
TRawBuffer m_rbuf;
|
||||
@@ -59,74 +65,49 @@ private:
|
||||
TRendererAA m_renderer;
|
||||
|
||||
std::function<double(double)> m_gammafn;
|
||||
std::array<bool, 2> m_mirror;
|
||||
Format m_fmt = Format::PNG;
|
||||
Trafo m_trafo;
|
||||
|
||||
inline void flipy(agg::path_storage& path) const {
|
||||
path.flip_y(0, m_resolution.height_px);
|
||||
path.flip_y(0, double(m_resolution.height_px));
|
||||
}
|
||||
|
||||
inline void flipx(agg::path_storage& path) const {
|
||||
path.flip_x(0, m_resolution.width_px);
|
||||
path.flip_x(0, double(m_resolution.width_px));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
inline Impl(const Raster::Resolution& res, const Raster::PixelDim &pd,
|
||||
const std::array<bool, 2>& mirror, double gamma = 1.0):
|
||||
m_resolution(res),
|
||||
// m_pxdim(pd),
|
||||
m_pxdim_scaled(SCALING_FACTOR / pd.w_mm, SCALING_FACTOR / pd.h_mm),
|
||||
m_buf(res.pixels()),
|
||||
m_rbuf(reinterpret_cast<TPixelRenderer::value_type*>(m_buf.data()),
|
||||
res.width_px, res.height_px,
|
||||
int(res.width_px*TPixelRenderer::num_components)),
|
||||
m_pixfmt(m_rbuf),
|
||||
m_raw_renderer(m_pixfmt),
|
||||
m_renderer(m_raw_renderer),
|
||||
m_mirror(mirror)
|
||||
inline Impl(const Raster::Resolution & res,
|
||||
const Raster::PixelDim & pd,
|
||||
const Trafo &trafo)
|
||||
: m_resolution(res)
|
||||
, m_pxdim_scaled(SCALING_FACTOR / pd.w_mm, SCALING_FACTOR / pd.h_mm)
|
||||
, m_buf(res.pixels())
|
||||
, m_rbuf(reinterpret_cast<TPixelRenderer::value_type *>(m_buf.data()),
|
||||
unsigned(res.width_px),
|
||||
unsigned(res.height_px),
|
||||
int(res.width_px * TPixelRenderer::num_components))
|
||||
, m_pixfmt(m_rbuf)
|
||||
, m_raw_renderer(m_pixfmt)
|
||||
, m_renderer(m_raw_renderer)
|
||||
, m_trafo(trafo)
|
||||
{
|
||||
m_renderer.color(ColorWhite);
|
||||
|
||||
if(gamma > 0) m_gammafn = agg::gamma_power(gamma);
|
||||
if (trafo.gamma > 0) m_gammafn = agg::gamma_power(trafo.gamma);
|
||||
else m_gammafn = agg::gamma_threshold(0.5);
|
||||
|
||||
clear();
|
||||
}
|
||||
|
||||
inline Impl(const Raster::Resolution& res,
|
||||
const Raster::PixelDim &pd,
|
||||
Format fmt,
|
||||
double gamma = 1.0):
|
||||
Impl(res, pd, {false, false}, gamma)
|
||||
{
|
||||
switch (fmt) {
|
||||
case Format::PNG: m_mirror = {false, true}; break;
|
||||
case Format::RAW: m_mirror = {false, false}; break;
|
||||
}
|
||||
m_fmt = fmt;
|
||||
}
|
||||
|
||||
template<class P> void draw(const P &poly) {
|
||||
agg::rasterizer_scanline_aa<> ras;
|
||||
agg::scanline_p8 scanlines;
|
||||
|
||||
ras.gamma(m_gammafn);
|
||||
|
||||
auto&& path = to_path(contour(poly));
|
||||
|
||||
if(m_mirror[X]) flipx(path);
|
||||
if(m_mirror[Y]) flipy(path);
|
||||
|
||||
ras.add_path(path);
|
||||
|
||||
for(auto& h : holes(poly)) {
|
||||
auto&& holepath = to_path(h);
|
||||
if(m_mirror[X]) flipx(holepath);
|
||||
if(m_mirror[Y]) flipy(holepath);
|
||||
ras.add_path(holepath);
|
||||
}
|
||||
|
||||
ras.add_path(to_path(contour(poly)));
|
||||
for(auto& h : holes(poly)) ras.add_path(to_path(h));
|
||||
|
||||
agg::render_scanlines(ras, scanlines, m_renderer);
|
||||
}
|
||||
|
||||
@@ -135,11 +116,16 @@ public:
|
||||
}
|
||||
|
||||
inline TBuffer& buffer() { return m_buf; }
|
||||
inline const TBuffer& buffer() const { return m_buf; }
|
||||
|
||||
inline Format format() const { return m_fmt; }
|
||||
|
||||
inline const Raster::Resolution resolution() { return m_resolution; }
|
||||
|
||||
inline const Raster::PixelDim pixdim()
|
||||
{
|
||||
return {SCALING_FACTOR / m_pxdim_scaled.w_mm,
|
||||
SCALING_FACTOR / m_pxdim_scaled.h_mm};
|
||||
}
|
||||
|
||||
private:
|
||||
inline double getPx(const Point& p) {
|
||||
return p(0) * m_pxdim_scaled.w_mm;
|
||||
@@ -162,49 +148,67 @@ private:
|
||||
return p.Y * m_pxdim_scaled.h_mm;
|
||||
}
|
||||
|
||||
template<class PointVec> agg::path_storage to_path(const PointVec& poly)
|
||||
template<class PointVec> agg::path_storage _to_path(const PointVec& v)
|
||||
{
|
||||
agg::path_storage path;
|
||||
|
||||
auto it = poly.begin();
|
||||
auto it = v.begin();
|
||||
path.move_to(getPx(*it), getPy(*it));
|
||||
while(++it != v.end()) path.line_to(getPx(*it), getPy(*it));
|
||||
path.line_to(getPx(v.front()), getPy(v.front()));
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
template<class PointVec> agg::path_storage _to_path_flpxy(const PointVec& v)
|
||||
{
|
||||
agg::path_storage path;
|
||||
|
||||
auto it = v.begin();
|
||||
path.move_to(getPy(*it), getPx(*it));
|
||||
while(++it != v.end()) path.line_to(getPy(*it), getPx(*it));
|
||||
path.line_to(getPy(v.front()), getPx(v.front()));
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
template<class PointVec> agg::path_storage to_path(const PointVec &v)
|
||||
{
|
||||
auto path = m_trafo.flipXY ? _to_path_flpxy(v) : _to_path(v);
|
||||
|
||||
path.translate_all_paths(m_trafo.origin_x * m_pxdim_scaled.w_mm,
|
||||
m_trafo.origin_y * m_pxdim_scaled.h_mm);
|
||||
|
||||
if(m_trafo.mirror_x) flipx(path);
|
||||
if(m_trafo.mirror_y) flipy(path);
|
||||
|
||||
while(++it != poly.end())
|
||||
path.line_to(getPx(*it), getPy(*it));
|
||||
|
||||
path.line_to(getPx(poly.front()), getPy(poly.front()));
|
||||
return path;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
const Raster::Impl::TPixel Raster::Impl::ColorWhite = Raster::Impl::TPixel(255);
|
||||
const Raster::Impl::TPixel Raster::Impl::ColorBlack = Raster::Impl::TPixel(0);
|
||||
const TPixel Raster::Impl::ColorWhite = TPixel(255);
|
||||
const TPixel Raster::Impl::ColorBlack = TPixel(0);
|
||||
|
||||
Raster::Raster() { reset(); }
|
||||
|
||||
Raster::Raster(const Raster::Resolution &r,
|
||||
const Raster::PixelDim & pd,
|
||||
const Raster::Trafo & tr)
|
||||
{
|
||||
reset(r, pd, tr);
|
||||
}
|
||||
|
||||
template<> Raster::Raster() { reset(); };
|
||||
Raster::~Raster() = default;
|
||||
|
||||
// Raster::Raster(Raster &&m) = default;
|
||||
// Raster& Raster::operator=(Raster&&) = default;
|
||||
|
||||
// FIXME: remove after migrating to higher version of windows compiler
|
||||
Raster::Raster(Raster &&m): m_impl(std::move(m.m_impl)) {}
|
||||
Raster& Raster::operator=(Raster &&m) {
|
||||
m_impl = std::move(m.m_impl); return *this;
|
||||
}
|
||||
Raster::Raster(Raster &&m) = default;
|
||||
Raster &Raster::operator=(Raster &&) = default;
|
||||
|
||||
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd,
|
||||
Format fmt, double gamma)
|
||||
const Trafo &trafo)
|
||||
{
|
||||
m_impl.reset();
|
||||
m_impl.reset(new Impl(r, pd, fmt, gamma));
|
||||
}
|
||||
|
||||
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd,
|
||||
const std::array<bool, 2>& mirror, double gamma)
|
||||
{
|
||||
m_impl.reset();
|
||||
m_impl.reset(new Impl(r, pd, mirror, gamma));
|
||||
m_impl.reset(new Impl(r, pd, trafo));
|
||||
}
|
||||
|
||||
void Raster::reset()
|
||||
@@ -214,9 +218,16 @@ void Raster::reset()
|
||||
|
||||
Raster::Resolution Raster::resolution() const
|
||||
{
|
||||
if(m_impl) return m_impl->resolution();
|
||||
if (m_impl) return m_impl->resolution();
|
||||
|
||||
return Resolution{0, 0};
|
||||
}
|
||||
|
||||
return Resolution(0, 0);
|
||||
Raster::PixelDim Raster::pixel_dimensions() const
|
||||
{
|
||||
if (m_impl) return m_impl->pixdim();
|
||||
|
||||
return PixelDim{0., 0.};
|
||||
}
|
||||
|
||||
void Raster::clear()
|
||||
@@ -227,103 +238,83 @@ void Raster::clear()
|
||||
|
||||
void Raster::draw(const ExPolygon &expoly)
|
||||
{
|
||||
assert(m_impl);
|
||||
m_impl->draw(expoly);
|
||||
}
|
||||
|
||||
void Raster::draw(const ClipperLib::Polygon &poly)
|
||||
{
|
||||
assert(m_impl);
|
||||
m_impl->draw(poly);
|
||||
}
|
||||
|
||||
void Raster::save(std::ostream& stream, Format fmt)
|
||||
uint8_t Raster::read_pixel(size_t x, size_t y) const
|
||||
{
|
||||
assert(m_impl);
|
||||
if(!stream.good()) return;
|
||||
|
||||
switch(fmt) {
|
||||
case Format::PNG: {
|
||||
auto& b = m_impl->buffer();
|
||||
size_t out_len = 0;
|
||||
void * rawdata = tdefl_write_image_to_png_file_in_memory(
|
||||
b.data(),
|
||||
int(resolution().width_px),
|
||||
int(resolution().height_px), 1, &out_len);
|
||||
|
||||
if(rawdata == nullptr) break;
|
||||
|
||||
stream.write(static_cast<const char*>(rawdata),
|
||||
std::streamsize(out_len));
|
||||
|
||||
MZ_FREE(rawdata);
|
||||
|
||||
break;
|
||||
}
|
||||
case Format::RAW: {
|
||||
stream << "P5 "
|
||||
<< m_impl->resolution().width_px << " "
|
||||
<< m_impl->resolution().height_px << " "
|
||||
<< "255 ";
|
||||
|
||||
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
|
||||
stream.write(reinterpret_cast<const char*>(m_impl->buffer().data()),
|
||||
std::streamsize(sz));
|
||||
}
|
||||
}
|
||||
assert (m_impl);
|
||||
TPixel::value_type px;
|
||||
m_impl->buffer()[y * resolution().width_px + x].get(px);
|
||||
return px;
|
||||
}
|
||||
|
||||
void Raster::save(std::ostream &stream)
|
||||
PNGImage & PNGImage::serialize(const Raster &raster)
|
||||
{
|
||||
save(stream, m_impl->format());
|
||||
size_t s = 0;
|
||||
m_buffer.clear();
|
||||
|
||||
void *rawdata = tdefl_write_image_to_png_file_in_memory(
|
||||
get_internals(raster).buffer().data(),
|
||||
int(raster.resolution().width_px),
|
||||
int(raster.resolution().height_px), 1, &s);
|
||||
|
||||
// On error, data() will return an empty vector. No other info can be
|
||||
// retrieved from miniz anyway...
|
||||
if (rawdata == nullptr) return *this;
|
||||
|
||||
auto ptr = static_cast<std::uint8_t*>(rawdata);
|
||||
|
||||
m_buffer.reserve(s);
|
||||
std::copy(ptr, ptr + s, std::back_inserter(m_buffer));
|
||||
|
||||
MZ_FREE(rawdata);
|
||||
return *this;
|
||||
}
|
||||
|
||||
RawBytes Raster::save(Format fmt)
|
||||
std::ostream &operator<<(std::ostream &stream, const Raster::RawData &bytes)
|
||||
{
|
||||
assert(m_impl);
|
||||
|
||||
std::vector<std::uint8_t> data; size_t s = 0;
|
||||
|
||||
switch(fmt) {
|
||||
case Format::PNG: {
|
||||
void *rawdata = tdefl_write_image_to_png_file_in_memory(
|
||||
m_impl->buffer().data(),
|
||||
int(resolution().width_px),
|
||||
int(resolution().height_px), 1, &s);
|
||||
|
||||
if(rawdata == nullptr) break;
|
||||
auto ptr = static_cast<std::uint8_t*>(rawdata);
|
||||
|
||||
data.reserve(s); std::copy(ptr, ptr + s, std::back_inserter(data));
|
||||
|
||||
MZ_FREE(rawdata);
|
||||
break;
|
||||
}
|
||||
case Format::RAW: {
|
||||
auto header = std::string("P5 ") +
|
||||
std::to_string(m_impl->resolution().width_px) + " " +
|
||||
std::to_string(m_impl->resolution().height_px) + " " + "255 ";
|
||||
|
||||
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
|
||||
s = sz + header.size();
|
||||
|
||||
data.reserve(s);
|
||||
|
||||
auto buff = reinterpret_cast<std::uint8_t*>(m_impl->buffer().data());
|
||||
std::copy(header.begin(), header.end(), std::back_inserter(data));
|
||||
std::copy(buff, buff+sz, std::back_inserter(data));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {std::move(data)};
|
||||
stream.write(reinterpret_cast<const char *>(bytes.data()),
|
||||
std::streamsize(bytes.size()));
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
RawBytes Raster::save()
|
||||
Raster::RawData::~RawData() = default;
|
||||
|
||||
PPMImage & PPMImage::serialize(const Raster &raster)
|
||||
{
|
||||
return save(m_impl->format());
|
||||
auto header = std::string("P5 ") +
|
||||
std::to_string(raster.resolution().width_px) + " " +
|
||||
std::to_string(raster.resolution().height_px) + " " + "255 ";
|
||||
|
||||
const auto &impl = get_internals(raster);
|
||||
auto sz = impl.buffer().size() * sizeof(TBuffer::value_type);
|
||||
size_t s = sz + header.size();
|
||||
|
||||
m_buffer.clear();
|
||||
m_buffer.reserve(s);
|
||||
|
||||
auto buff = reinterpret_cast<const std::uint8_t*>(impl.buffer().data());
|
||||
std::copy(header.begin(), header.end(), std::back_inserter(m_buffer));
|
||||
std::copy(buff, buff+sz, std::back_inserter(m_buffer));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Raster::Impl &Raster::RawData::get_internals(const Raster &raster)
|
||||
{
|
||||
return *raster.m_impl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sla
|
||||
} // namespace Slic3r
|
||||
|
||||
#endif // SLARASTER_CPP
|
||||
|
||||
Reference in New Issue
Block a user