Files
OrcaSlicer-bambulab/src/libslic3r/SLA/Raster.hpp
T

158 lines
4.4 KiB
C++
Raw Normal View History

#ifndef SLA_RASTER_HPP
#define SLA_RASTER_HPP
2018-05-16 18:51:28 +02:00
#include <ostream>
#include <memory>
2019-03-18 18:02:50 +01:00
#include <vector>
2019-05-20 11:19:43 +02:00
#include <array>
#include <utility>
2019-03-25 13:45:28 +01:00
#include <cstdint>
2018-05-16 18:51:28 +02:00
#include <libslic3r/ExPolygon.hpp>
namespace ClipperLib { struct Polygon; }
2019-03-27 18:37:50 +01:00
2019-10-04 14:47:02 +02:00
namespace Slic3r {
2019-05-18 22:45:24 +02:00
namespace sla {
/**
* @brief Raster captures an anti-aliased monochrome canvas where vectorial
* polygons can be rasterized. Fill color is always white and the background is
* black. Contours are anti-aliased.
*
* It also supports saving the raster data into a standard output stream in raw
* or PNG format.
*/
2018-05-16 18:51:28 +02:00
class Raster {
class Impl;
std::unique_ptr<Impl> m_impl;
2018-05-16 18:51:28 +02:00
public:
2019-10-04 14:47:02 +02:00
// Raw byte buffer paired with its size. Suitable for compressed image data.
class RawData
{
protected:
std::vector<std::uint8_t> m_buffer;
const Impl& get_internals(const Raster& raster);
public:
RawData() = default;
RawData(std::vector<std::uint8_t>&& data): m_buffer(std::move(data)) {}
virtual ~RawData();
2019-10-04 14:47:02 +02:00
RawData(const RawData &) = delete;
RawData &operator=(const RawData &) = delete;
2019-10-04 14:47:02 +02:00
RawData(RawData &&) = default;
RawData &operator=(RawData &&) = default;
2019-10-04 14:47:02 +02:00
size_t size() const { return m_buffer.size(); }
const uint8_t * data() const { return m_buffer.data(); }
2019-10-04 14:47:02 +02:00
virtual RawData& serialize(const Raster &/*raster*/) { return *this; }
virtual std::string get_file_extension() const = 0;
};
/// Type that represents a resolution in pixels.
2018-05-16 18:51:28 +02:00
struct Resolution {
size_t width_px;
size_t height_px;
2019-05-18 16:56:46 +02:00
inline Resolution(size_t w = 0, size_t h = 0)
: width_px(w), height_px(h)
{}
2019-05-18 16:56:46 +02:00
inline size_t pixels() const { return width_px * height_px; }
2018-05-16 18:51:28 +02:00
};
/// Types that represents the dimension of a pixel in millimeters.
2018-05-16 18:51:28 +02:00
struct PixelDim {
double w_mm;
double h_mm;
2019-05-18 16:56:46 +02:00
inline PixelDim(double px_width_mm = 0.0, double px_height_mm = 0.0):
2018-05-16 18:51:28 +02:00
w_mm(px_width_mm), h_mm(px_height_mm) {}
};
2019-10-04 14:47:02 +02:00
enum Orientation { roLandscape, roPortrait };
2019-10-04 14:47:02 +02:00
using TMirroring = std::array<bool, 2>;
static const TMirroring NoMirror;
static const TMirroring MirrorX;
static const TMirroring MirrorY;
static const TMirroring MirrorXY;
2019-10-04 14:47:02 +02:00
struct Trafo {
bool mirror_x = false, mirror_y = false, flipXY = false;
2019-10-04 14:47:02 +02:00
coord_t origin_x = 0, origin_y = 0;
// If gamma is zero, thresholding will be performed which disables AA.
double gamma = 1.;
2019-10-04 14:47:02 +02:00
// Portrait orientation will make sure the drawed polygons are rotated
// by 90 degrees.
Trafo(Orientation o = roLandscape, const TMirroring &mirror = NoMirror)
// XY flipping implicitly does an X mirror
: mirror_x(o == roPortrait ? !mirror[0] : mirror[0])
, mirror_y(!mirror[1]) // Makes raster origin to be top left corner
, flipXY(o == roPortrait)
{}
};
2019-10-04 14:47:02 +02:00
Raster();
Raster(const Resolution &r,
const PixelDim & pd,
const Trafo & tr = {});
2019-10-04 14:47:02 +02:00
Raster(const Raster& cpy) = delete;
Raster& operator=(const Raster& cpy) = delete;
2018-05-16 18:51:28 +02:00
Raster(Raster&& m);
2019-05-18 22:45:24 +02:00
Raster& operator=(Raster&&);
~Raster();
2018-05-16 18:51:28 +02:00
/// Reallocated everything for the given resolution and pixel dimension.
2019-10-04 14:47:02 +02:00
void reset(const Resolution& r,
const PixelDim& pd,
const Trafo &tr = {});
2019-10-04 14:47:02 +02:00
/**
* Release the allocated resources. Drawing in this state ends in
* unspecified behavior.
*/
void reset();
/// Get the resolution of the raster.
Resolution resolution() const;
PixelDim pixel_dimensions() const;
/// Clear the raster with black color.
2018-05-16 18:51:28 +02:00
void clear();
/// Draw a polygon with holes.
2019-03-27 18:37:50 +01:00
void draw(const ExPolygon& poly);
void draw(const ClipperLib::Polygon& poly);
2019-10-04 14:47:02 +02:00
uint8_t read_pixel(size_t w, size_t h) const;
2019-10-04 14:47:02 +02:00
inline bool empty() const { return ! bool(m_impl); }
2019-10-04 14:47:02 +02:00
2018-05-16 18:51:28 +02:00
};
class PNGImage: public Raster::RawData {
public:
PNGImage& serialize(const Raster &raster) override;
std::string get_file_extension() const override { return "png"; }
};
2019-05-20 11:19:43 +02:00
class PPMImage: public Raster::RawData {
public:
PPMImage& serialize(const Raster &raster) override;
std::string get_file_extension() const override { return "ppm"; }
};
std::ostream& operator<<(std::ostream &stream, const Raster::RawData &bytes);
2019-05-20 11:19:43 +02:00
2019-05-18 22:45:24 +02:00
} // sla
} // Slic3r
2019-05-18 22:45:24 +02:00
#endif // SLARASTER_HPP