Convert text to mesh

This commit is contained in:
Filip Sykala 2021-08-30 22:57:14 +02:00
parent e33c665876
commit 026ad24b64
6 changed files with 546 additions and 100 deletions

View file

@ -2,7 +2,9 @@
#define slic3r_Emboss_hpp_
#include <vector>
#include <set>
#include <optional>
#include <memory>
#include <admesh/stl.h> // indexed_triangle_set
#include "Polygon.hpp"
@ -38,6 +40,8 @@ public:
// change size of font
float scale = 1.;
// enum class Align: center/left/right
};
/// <summary>
@ -53,20 +57,80 @@ public:
/// <param name="font">Define fonts</param>
/// <param name="letter">Character to convert</param>
/// <returns>inner polygon ccw(outer cw)</returns>
static Polygons letter2polygons(const Font &font, char letter);
static Polygons letter2polygons(const Font &font, char letter);
/// <summary>
/// Convert text into polygons
/// </summary>
/// <param name="font">Define fonts</param>
/// <param name="letter">Character to convert</param>
/// <returns>inner polygon ccw(outer cw)</returns>
static Polygons text2polygons(const Font &font, const std::string &text);
/// <summary>
/// Project 2d point into space
/// Could be plane, sphere, cylindric, ...
/// </summary>
class IProject
{
public:
virtual ~IProject() = default;
/// <summary>
/// convert 2d point to 3d point
/// </summary>
/// <param name="p">2d coordinate</param>
/// <returns>
/// first - front spatial point
/// second - back spatial point
/// </returns>
virtual std::pair<Vec3f, Vec3f> project(const Point &p) const = 0;
};
/// <summary>
/// Create triangle model for text
/// </summary>
/// <param name="text"></param>
/// <param name="font"></param>
/// <param name="z_size"></param>
/// <returns></returns>
static indexed_triangle_set create_model(const std::string &text,
const Font & font,
float z_size);
/// <param name="shape2d">text or image</param>
/// <param name="projection">Define transformation from 2d to 3d(orientation, position, scale, ...)</param>
/// <returns>Projected shape into space</returns>
static indexed_triangle_set polygons2model(const Polygons &shape2d, const IProject& projection);
/// <summary>
/// Connect points by triangulation to create filled surface by triangle indices
/// </summary>
/// <param name="points">Points to connect</param>
/// <param name="edges">Constraint for edges, pair is from point(first) to point(second)</param>
/// <returns>Triangles</returns>
static std::vector<Vec3i> triangulate(const Points &points, const std::set<std::pair<uint32_t, uint32_t>> &edges);
static std::vector<Vec3i> triangulate(const Polygon &polygon);
static std::vector<Vec3i> triangulate(const Polygons &polygons);
class ProjectZ : public IProject
{
public:
ProjectZ(float depth) : m_depth(depth) {}
// Inherited via IProject
virtual std::pair<Vec3f, Vec3f> project(const Point &p) const override;
float m_depth;
};
class ProjectScale : public IProject
{
std::unique_ptr<IProject> core;
public:
ProjectScale(std::unique_ptr<IProject> core, float scale)
: m_scale(scale)
, core(std::move(core))
{}
// Inherited via IProject
virtual std::pair<Vec3f, Vec3f> project(const Point &p) const override
{
auto res = core->project(p);
return std::make_pair(res.first * m_scale, res.second * m_scale);
}
float m_scale;
};
};
} // namespace Slic3r