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

151 lines
4.1 KiB
C++
Raw Normal View History

2019-05-18 22:45:24 +02:00
#ifndef SLARASTER_HPP
#define SLARASTER_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
namespace ClipperLib { struct Polygon; }
2019-03-27 18:37:50 +01:00
2019-05-18 22:45:24 +02:00
namespace Slic3r {
2018-05-16 18:51:28 +02:00
2019-03-27 18:37:50 +01:00
class ExPolygon;
2019-05-18 22:45:24 +02:00
namespace sla {
2019-03-18 18:02:50 +01:00
// Raw byte buffer paired with its size. Suitable for compressed PNG data.
class RawBytes {
2019-03-25 14:53:25 +01:00
2019-04-25 18:03:17 +02:00
std::vector<std::uint8_t> m_buffer;
public:
2019-03-25 13:45:28 +01:00
RawBytes() = default;
2019-04-25 18:48:31 +02:00
RawBytes(std::vector<std::uint8_t>&& data): m_buffer(std::move(data)) {}
2019-04-25 18:03:17 +02:00
size_t size() const { return m_buffer.size(); }
const uint8_t * data() { return m_buffer.data(); }
2019-05-18 22:45:24 +02:00
RawBytes(const RawBytes&) = delete;
RawBytes& operator=(const RawBytes&) = delete;
2019-03-25 14:53:25 +01:00
// /////////////////////////////////////////////////////////////////////////
// FIXME: the following is needed for MSVC2013 compatibility
// /////////////////////////////////////////////////////////////////////////
2019-03-25 13:45:28 +01:00
2019-05-20 11:19:43 +02:00
// RawBytes(RawBytes&&) = default;
// RawBytes& operator=(RawBytes&&) = default;
2019-03-25 13:45:28 +01:00
2019-05-20 11:19:43 +02:00
RawBytes(RawBytes&& mv) : m_buffer(std::move(mv.m_buffer)) {}
RawBytes& operator=(RawBytes&& mv) {
m_buffer = std::move(mv.m_buffer);
return *this;
}
2019-03-25 14:53:25 +01:00
// /////////////////////////////////////////////////////////////////////////
2019-03-18 18:02:50 +01:00
};
/**
* @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:
/// Supported compression types
2019-05-18 22:45:24 +02:00
enum class Format {
RAW, //!> Uncompressed pixel data
PNG //!> PNG compression
};
/// Type that represents a resolution in pixels.
2018-05-16 18:51:28 +02:00
struct Resolution {
unsigned width_px;
unsigned height_px;
2019-05-18 16:56:46 +02:00
inline Resolution(unsigned w = 0, unsigned h = 0):
width_px(w), height_px(h) {}
inline unsigned pixels() const /*noexcept*/ {
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) {}
};
/// Constructor taking the resolution and the pixel dimension.
2019-05-18 22:45:24 +02:00
template <class...Args> Raster(Args...args) {
reset(std::forward<Args>(args)...);
}
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-05-18 23:21:59 +02:00
/// The third parameter is either the X, Y mirroring or a supported format
/// for which the correct mirroring will be configured.
void reset(const Resolution&,
const PixelDim&,
const std::array<bool, 2>& mirror,
double gamma = 1.0);
void reset(const Resolution& r,
const PixelDim& pd,
Format o,
double gamma = 1.0);
2019-05-18 22:45:24 +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;
/// 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);
2018-05-16 18:51:28 +02:00
2019-05-18 23:21:59 +02:00
// Saving the raster:
// It is possible to override the format given in the constructor but
// be aware that the mirroring will not be modified.
/// Save the raster on the specified stream.
2019-05-18 23:21:59 +02:00
void save(std::ostream& stream, Format);
void save(std::ostream& stream);
2019-03-18 18:02:50 +01:00
2019-05-18 22:45:24 +02:00
/// Save into a continuous byte stream which is returned.
2019-05-18 23:21:59 +02:00
RawBytes save(Format fmt);
RawBytes save();
2018-05-16 18:51:28 +02:00
};
2019-05-20 11:19:43 +02:00
// This prevents the duplicate default constructor warning on MSVC2013
template<> Raster::Raster();
2019-05-18 22:45:24 +02:00
} // sla
} // Slic3r
#endif // SLARASTER_HPP