OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::ToolpathsObjExporter Class Reference
Collaboration diagram for Slic3r::GUI::ToolpathsObjExporter:

Classes

struct  SegmentLocalAxes
struct  Vertex
struct  CrossSection

Public Member Functions

 ToolpathsObjExporter (const libvgcode::Viewer &viewer)
void export_to (const std::string &filename)

Private Types

enum  CornerType : unsigned char { RightTurn , LeftTurn , Straight }

Private Member Functions

SegmentLocalAxes segment_local_axes (const Vec3f &v1, const Vec3f &v2)
CrossSection cross_section (const Vec3f &v, const Vec3f &right, const Vec3f &up, float width, float height)
CrossSection normal_cross_section (const Vec3f &v, const SegmentLocalAxes &axes, float width, float height)
CrossSection corner_cross_section (const Vec3f &v, const SegmentLocalAxes &axes1, const SegmentLocalAxes &axes2, float width, float height, CornerType &corner_type)
void export_segment (FILE &f, unsigned char flags, size_t v1_id, const libvgcode::PathVertex &v1, const libvgcode::PathVertex &v2, const libvgcode::PathVertex &v3)
size_t vertex_id (int id)
void export_vertex (FILE &f, const Vertex &v)
void export_material (FILE &f, size_t material_id)
void export_triangle (FILE &f, size_t v1, size_t v2, size_t v3)
void export_materials (FILE &f)
size_t color_id (size_t vertex_id)

Private Attributes

const libvgcode::Viewer & m_viewer
size_t m_vertices_count { 0 }
std::vector< libvgcode::Color > m_colors

Static Private Attributes

static const unsigned char Flag_First = 0x01
static const unsigned char Flag_Last = 0x02
static const unsigned char Flag_Internal = 0x04
static const float Cap_Rounding_Factor = 0.25f

Detailed Description


Class Documentation

◆ Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes

struct Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes
Class Members
Vec3f forward
Vec3f right
Vec3f up

◆ Slic3r::GUI::ToolpathsObjExporter::Vertex

struct Slic3r::GUI::ToolpathsObjExporter::Vertex
Class Members
Vec3f normal
Vec3f position

◆ Slic3r::GUI::ToolpathsObjExporter::CrossSection

struct Slic3r::GUI::ToolpathsObjExporter::CrossSection
Collaboration diagram for Slic3r::GUI::ToolpathsObjExporter::CrossSection:
Class Members
Vertex bottom
Vertex left
Vertex right
Vertex top

Member Enumeration Documentation

◆ CornerType

enum Slic3r::GUI::ToolpathsObjExporter::CornerType : unsigned char
private
Enumerator
RightTurn 
LeftTurn 
Straight 
2936 {
2937 RightTurn,
2938 LeftTurn,
2939 Straight
2940 };
@ LeftTurn
Definition GCodeViewer.cpp:2938
@ RightTurn
Definition GCodeViewer.cpp:2937
@ Straight
Definition GCodeViewer.cpp:2939

Constructor & Destructor Documentation

◆ ToolpathsObjExporter()

Slic3r::GUI::ToolpathsObjExporter::ToolpathsObjExporter ( const libvgcode::Viewer & viewer)
inlineexplicit
2824 : m_viewer(viewer) {
2825 }
const libvgcode::Viewer & m_viewer
Definition GCodeViewer.cpp:2879

References m_viewer.

Member Function Documentation

◆ color_id()

size_t Slic3r::GUI::ToolpathsObjExporter::color_id ( size_t vertex_id)
inlineprivate
3104 {
3105 const libvgcode::PathVertex& v = m_viewer.get_vertex_at(vertex_id);
3106 const size_t top_layer_id = m_viewer.is_top_layer_only_view_range() ? m_viewer.get_layers_view_range()[1] : 0;
3107 const bool color_top_layer_only = m_viewer.get_view_full_range()[1] != m_viewer.get_view_visible_range()[1];
3108 const libvgcode::Color color = (color_top_layer_only && v.layer_id < top_layer_id &&
3109 (!m_viewer.is_spiral_vase_mode() || vertex_id != m_viewer.get_view_enabled_range()[0])) ?
3110 libvgcode::DUMMY_COLOR : m_viewer.get_vertex_color(v);
3111 auto color_it = std::find_if(m_colors.begin(), m_colors.end(), [&color](const libvgcode::Color& m) { return m == color; });
3112 if (color_it == m_colors.end()) {
3113 m_colors.emplace_back(color);
3114 color_it = std::prev(m_colors.end());
3115 }
3116 return std::distance(m_colors.begin(), color_it);
3117 }
std::vector< libvgcode::Color > m_colors
Definition GCodeViewer.cpp:2881
size_t vertex_id(int id)
Definition GCodeViewer.cpp:3076
std::array< uint8_t, 3 > Color
Definition Types.hpp:59
static const Color DUMMY_COLOR
Definition Types.hpp:223
uint32_t layer_id
Definition PathVertex.hpp:74

References libvgcode::DUMMY_COLOR, libvgcode::PathVertex::layer_id, m_colors, m_viewer, and vertex_id().

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ corner_cross_section()

CrossSection Slic3r::GUI::ToolpathsObjExporter::corner_cross_section ( const Vec3f & v,
const SegmentLocalAxes & axes1,
const SegmentLocalAxes & axes2,
float width,
float height,
CornerType & corner_type )
inlineprivate
2943 {
2944 if (std::abs(std::abs(axes1.forward.dot(axes2.forward)) - 1.0f) < EPSILON)
2945 corner_type = CornerType::Straight;
2946 else if (axes1.up.dot(axes1.forward.cross(axes2.forward)) < 0.0f)
2947 corner_type = CornerType::RightTurn;
2948 else
2949 corner_type = CornerType::LeftTurn;
2950 const Vec3f right = (0.5f * (axes1.right + axes2.right)).normalized();
2951 return cross_section(v, right, axes1.up, width, height);
2952 }
#define EPSILON
Definition ColorUtils.cpp:64
CrossSection cross_section(const Vec3f &v, const Vec3f &right, const Vec3f &up, float width, float height)
Definition GCodeViewer.cpp:2916
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
@ right
Definition MarchingSquares.hpp:190

References cross_section(), EPSILON, Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::forward, LeftTurn, Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::right, RightTurn, Straight, and Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::up.

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cross_section()

CrossSection Slic3r::GUI::ToolpathsObjExporter::cross_section ( const Vec3f & v,
const Vec3f & right,
const Vec3f & up,
float width,
float height )
inlineprivate
2916 {
2917 CrossSection ret;
2918 const Vec3f w_shift = 0.5f * width * right;
2919 const Vec3f h_shift = 0.5f * height * up;
2920 ret.right.position = v + w_shift;
2921 ret.right.normal = right;
2922 ret.top.position = v + h_shift;
2923 ret.top.normal = up;
2924 ret.left.position = v - w_shift;
2925 ret.left.normal = -right;
2926 ret.bottom.position = v - h_shift;
2927 ret.bottom.normal = -up;
2928 return ret;
2929 }
coord_t width(const BoundingBox &box)
Definition Arrange.cpp:1184
coord_t height(const BoundingBox &box)
Definition Arrange.cpp:1185
@ up
Definition MarchingSquares.hpp:192

References Slic3r::GUI::ToolpathsObjExporter::CrossSection::bottom, Slic3r::GUI::ToolpathsObjExporter::CrossSection::left, Slic3r::GUI::ToolpathsObjExporter::Vertex::normal, Slic3r::GUI::ToolpathsObjExporter::Vertex::position, Slic3r::GUI::ToolpathsObjExporter::CrossSection::right, and Slic3r::GUI::ToolpathsObjExporter::CrossSection::top.

Referenced by corner_cross_section(), and normal_cross_section().

Here is the caller graph for this function:

◆ export_material()

void Slic3r::GUI::ToolpathsObjExporter::export_material ( FILE & f,
size_t material_id )
inlineprivate
3083 {
3084 fprintf(&f, "\nusemtl material_%zu\n", material_id + 1);
3085 }
static double f(double x, double z_sin, double z_cos, bool vertical, bool flip)
Definition FillGyroid.cpp:124

References Slic3r::f().

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_materials()

void Slic3r::GUI::ToolpathsObjExporter::export_materials ( FILE & f)
inlineprivate
3091 {
3092 static const float inv_255 = 1.0f / 255.0f;
3093 size_t materials_counter = 0;
3094 for (const auto& color : m_colors) {
3095 fprintf(&f, "\nnewmtl material_%zu\n", ++materials_counter);
3096 fprintf(&f, "Ka 1 1 1\n");
3097 fprintf(&f, "Kd %g %g %g\n", static_cast<float>(color[0]) * inv_255,
3098 static_cast<float>(color[1]) * inv_255,
3099 static_cast<float>(color[2]) * inv_255);
3100 fprintf(&f, "Ks 0 0 0\n");
3101 }
3102 }

References Slic3r::f(), and m_colors.

Referenced by export_to().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_segment()

void Slic3r::GUI::ToolpathsObjExporter::export_segment ( FILE & f,
unsigned char flags,
size_t v1_id,
const libvgcode::PathVertex & v1,
const libvgcode::PathVertex & v2,
const libvgcode::PathVertex & v3 )
inlineprivate
2954 {
2955 const Vec3f v1_pos = libvgcode::convert(v1.position);
2956 const Vec3f v2_pos = libvgcode::convert(v2.position);
2957 const Vec3f v3_pos = libvgcode::convert(v3.position);
2958 const SegmentLocalAxes v1_v2 = segment_local_axes(v1_pos, v2_pos);
2959 const SegmentLocalAxes v2_v3 = segment_local_axes(v2_pos, v3_pos);
2960
2961 // starting cap
2962 if ((flags & Flag_First) > 0) {
2963 const Vertex v0 = { v1_pos - Cap_Rounding_Factor * v1.width * v1_v2.forward, -v1_v2.forward };
2964 const CrossSection ncs = normal_cross_section(v1_pos, v1_v2, v1.width, v1.height);
2965 export_vertex(f, v0); // 0
2966 export_vertex(f, ncs.right); // 1
2967 export_vertex(f, ncs.top); // 2
2968 export_vertex(f, ncs.left); // 3
2969 export_vertex(f, ncs.bottom); // 4
2970 export_material(f, color_id(v1_id));
2975 m_vertices_count += 5;
2976 }
2977 // segment body + ending cap
2978 if ((flags & Flag_Last) > 0) {
2979 const Vertex v0 = { v2_pos + Cap_Rounding_Factor * v2.width * v1_v2.forward, v1_v2.forward };
2980 const CrossSection ncs = normal_cross_section(v2_pos, v1_v2, v2.width, v2.height);
2981 export_vertex(f, v0); // 0
2982 export_vertex(f, ncs.right); // 1
2983 export_vertex(f, ncs.top); // 2
2984 export_vertex(f, ncs.left); // 3
2985 export_vertex(f, ncs.bottom); // 4
2986 export_material(f, color_id(v1_id + 1));
2987 // segment body
2996 // ending cap
3001 m_vertices_count += 5;
3002 }
3003 else {
3004 CornerType corner_type;
3005 const CrossSection ccs = corner_cross_section(v2_pos, v1_v2, v2_v3, v2.width, v2.height, corner_type);
3006 const CrossSection ncs12 = normal_cross_section(v2_pos, v1_v2, v2.width, v2.height);
3007 const CrossSection ncs23 = normal_cross_section(v2_pos, v2_v3, v2.width, v2.height);
3008 if (corner_type == CornerType::Straight) {
3009 export_vertex(f, ncs12.right); // 0
3010 export_vertex(f, ncs12.top); // 1
3011 export_vertex(f, ncs12.left); // 2
3012 export_vertex(f, ncs12.bottom); // 3
3013 export_material(f, color_id(v1_id + 1));
3014 // segment body
3023 m_vertices_count += 4;
3024 }
3025 else if (corner_type == CornerType::RightTurn) {
3026 export_vertex(f, ncs12.left); // 0
3027 export_vertex(f, ccs.left); // 1
3028 export_vertex(f, ccs.right); // 2
3029 export_vertex(f, ncs12.top); // 3
3030 export_vertex(f, ncs23.left); // 4
3031 export_vertex(f, ncs12.bottom); // 5
3032 export_material(f, color_id(v1_id + 1));
3033 // segment body
3042 // corner
3047 m_vertices_count += 6;
3048 }
3049 else {
3050 export_vertex(f, ncs12.right); // 0
3051 export_vertex(f, ccs.right); // 1
3052 export_vertex(f, ncs23.right); // 2
3053 export_vertex(f, ncs12.top); // 3
3054 export_vertex(f, ccs.left); // 4
3055 export_vertex(f, ncs12.bottom); // 5
3056 export_material(f, color_id(v1_id + 1));
3057 // segment body
3066 // corner
3071 m_vertices_count += 6;
3072 }
3073 }
3074 }
CornerType
Definition GCodeViewer.cpp:2936
size_t m_vertices_count
Definition GCodeViewer.cpp:2880
static const unsigned char Flag_Last
Definition GCodeViewer.cpp:2883
void export_triangle(FILE &f, size_t v1, size_t v2, size_t v3)
Definition GCodeViewer.cpp:3087
SegmentLocalAxes segment_local_axes(const Vec3f &v1, const Vec3f &v2)
Definition GCodeViewer.cpp:2894
void export_vertex(FILE &f, const Vertex &v)
Definition GCodeViewer.cpp:3078
static const float Cap_Rounding_Factor
Definition GCodeViewer.cpp:2885
void export_material(FILE &f, size_t material_id)
Definition GCodeViewer.cpp:3083
static const unsigned char Flag_First
Definition GCodeViewer.cpp:2882
size_t color_id(size_t vertex_id)
Definition GCodeViewer.cpp:3104
CrossSection corner_cross_section(const Vec3f &v, const SegmentLocalAxes &axes1, const SegmentLocalAxes &axes2, float width, float height, CornerType &corner_type)
Definition GCodeViewer.cpp:2942
CrossSection normal_cross_section(const Vec3f &v, const SegmentLocalAxes &axes, float width, float height)
Definition GCodeViewer.cpp:2931
Definition GCodeViewer.cpp:2903
Vec3 convert(const Slic3r::Vec3f &v)
Definition LibVGCodeWrapper.cpp:45
Vec3 position
Definition PathVertex.hpp:24
float height
Definition PathVertex.hpp:28
float width
Definition PathVertex.hpp:32

References Slic3r::GUI::ToolpathsObjExporter::CrossSection::bottom, Cap_Rounding_Factor, color_id(), libvgcode::convert(), corner_cross_section(), export_material(), export_triangle(), export_vertex(), Slic3r::f(), Flag_First, Flag_Last, Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::forward, libvgcode::PathVertex::height, Slic3r::GUI::ToolpathsObjExporter::CrossSection::left, m_vertices_count, normal_cross_section(), libvgcode::PathVertex::position, Slic3r::GUI::ToolpathsObjExporter::CrossSection::right, RightTurn, segment_local_axes(), Straight, Slic3r::GUI::ToolpathsObjExporter::CrossSection::top, vertex_id(), and libvgcode::PathVertex::width.

Referenced by export_to().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_to()

void Slic3r::GUI::ToolpathsObjExporter::export_to ( const std::string & filename)
inline
2827 {
2828 CNumericLocalesSetter locales_setter;
2829
2830 // open geometry file
2831 FilePtr f_geo = boost::nowide::fopen(filename.c_str(), "w");
2832 if (f_geo.f == nullptr) {
2833 BOOST_LOG_TRIVIAL(error) << "ToolpathsObjExporter: Couldn't open " << filename << " for writing";
2834 return;
2835 }
2836
2837 boost::filesystem::path materials_filename(filename);
2838 materials_filename.replace_extension("mtl");
2839
2840 // write header to geometry file
2841 fprintf(f_geo.f, "# G-Code Toolpaths\n");
2842 fprintf(f_geo.f, "# Generated by %s-%s based on Slic3r\n", SLIC3R_APP_NAME, SoftFever_VERSION);
2843 fprintf(f_geo.f, "\nmtllib ./%s\n", materials_filename.filename().string().c_str());
2844
2845 // open material file
2846 FilePtr f_mat = boost::nowide::fopen(materials_filename.string().c_str(), "w");
2847 if (f_mat.f == nullptr) {
2848 BOOST_LOG_TRIVIAL(error) << "ToolpathsObjExporter: Couldn't open " << materials_filename.string() << " for writing";
2849 return;
2850 }
2851
2852 // write header to material file
2853 fprintf(f_mat.f, "# G-Code Toolpaths Materials\n");
2854 fprintf(f_mat.f, "# Generated by %s-%s based on Slic3r\n", SLIC3R_APP_NAME, SoftFever_VERSION);
2855
2856 libvgcode::Interval visible_range = m_viewer.get_view_visible_range();
2857 if (m_viewer.is_top_layer_only_view_range())
2858 visible_range[0] = m_viewer.get_view_full_range()[0];
2859 for (size_t i = visible_range[0]; i <= visible_range[1]; ++i) {
2860 const libvgcode::PathVertex& curr = m_viewer.get_vertex_at(i);
2861 const libvgcode::PathVertex& next = m_viewer.get_vertex_at(i + 1);
2862 if (!curr.is_extrusion() || !next.is_extrusion())
2863 continue;
2864
2865 const libvgcode::PathVertex& nextnext = m_viewer.get_vertex_at(i + 2);
2866 unsigned char flags = 0;
2867 if (curr.gcode_id == next.gcode_id)
2868 flags |= Flag_First;
2869 if (i + 1 == visible_range[1] || !nextnext.is_extrusion())
2870 flags |= Flag_Last;
2871 else
2872 flags |= Flag_Internal;
2873 export_segment(*f_geo.f, flags, i, curr, next, nextnext);
2874 }
2875 export_materials(*f_mat.f);
2876 }
static const unsigned char Flag_Internal
Definition GCodeViewer.cpp:2884
void export_materials(FILE &f)
Definition GCodeViewer.cpp:3091
void export_segment(FILE &f, unsigned char flags, size_t v1_id, const libvgcode::PathVertex &v1, const libvgcode::PathVertex &v2, const libvgcode::PathVertex &v3)
Definition GCodeViewer.cpp:2954
std::array< std::size_t, 2 > Interval
Definition Types.hpp:85
bool is_extrusion() const
Definition PathVertex.cpp:12
uint32_t gcode_id
Definition PathVertex.hpp:70

References export_materials(), export_segment(), Slic3r::FilePtr::f, Flag_First, Flag_Internal, Flag_Last, libvgcode::PathVertex::gcode_id, libvgcode::PathVertex::is_extrusion(), and m_viewer.

Referenced by Slic3r::GUI::GCodeViewer::export_toolpaths_to_obj().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_triangle()

void Slic3r::GUI::ToolpathsObjExporter::export_triangle ( FILE & f,
size_t v1,
size_t v2,
size_t v3 )
inlineprivate
3087 {
3088 fprintf(&f, "f %zu//%zu %zu//%zu %zu//%zu\n", v1, v1, v2, v2, v3, v3);
3089 }

References Slic3r::f().

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_vertex()

void Slic3r::GUI::ToolpathsObjExporter::export_vertex ( FILE & f,
const Vertex & v )
inlineprivate
3078 {
3079 fprintf(&f, "v %g %g %g\n", v.position.x(), v.position.y(), v.position.z());
3080 fprintf(&f, "vn %g %g %g\n", v.normal.x(), v.normal.y(), v.normal.z());
3081 }

References Slic3r::f(), Slic3r::GUI::ToolpathsObjExporter::Vertex::normal, and Slic3r::GUI::ToolpathsObjExporter::Vertex::position.

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ normal_cross_section()

CrossSection Slic3r::GUI::ToolpathsObjExporter::normal_cross_section ( const Vec3f & v,
const SegmentLocalAxes & axes,
float width,
float height )
inlineprivate
2931 {
2932 return cross_section(v, axes.right, axes.up, width, height);
2933 }

References cross_section(), Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::right, and Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::up.

Referenced by export_segment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ segment_local_axes()

SegmentLocalAxes Slic3r::GUI::ToolpathsObjExporter::segment_local_axes ( const Vec3f & v1,
const Vec3f & v2 )
inlineprivate
2894 {
2895 SegmentLocalAxes ret;
2896 ret.forward = (v2 - v1).normalized();
2897 ret.right = ret.forward.cross(Vec3f::UnitZ()).normalized();
2898 ret.up = ret.right.cross(ret.forward);
2899 return ret;
2900 }
Vec3f forward
Definition GCodeViewer.cpp:2889

References Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::forward, Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::right, and Slic3r::GUI::ToolpathsObjExporter::SegmentLocalAxes::up.

Referenced by export_segment().

Here is the caller graph for this function:

◆ vertex_id()

size_t Slic3r::GUI::ToolpathsObjExporter::vertex_id ( int id)
inlineprivate
3076{ return static_cast<size_t>(1 + static_cast<int>(m_vertices_count) + id); }

References m_vertices_count.

Referenced by color_id(), and export_segment().

Here is the caller graph for this function:

Member Data Documentation

◆ Cap_Rounding_Factor

const float Slic3r::GUI::ToolpathsObjExporter::Cap_Rounding_Factor = 0.25f
staticprivate

Referenced by export_segment().

◆ Flag_First

const unsigned char Slic3r::GUI::ToolpathsObjExporter::Flag_First = 0x01
staticprivate

Referenced by export_segment(), and export_to().

◆ Flag_Internal

const unsigned char Slic3r::GUI::ToolpathsObjExporter::Flag_Internal = 0x04
staticprivate

Referenced by export_to().

◆ Flag_Last

const unsigned char Slic3r::GUI::ToolpathsObjExporter::Flag_Last = 0x02
staticprivate

Referenced by export_segment(), and export_to().

◆ m_colors

std::vector<libvgcode::Color> Slic3r::GUI::ToolpathsObjExporter::m_colors
private

Referenced by color_id(), and export_materials().

◆ m_vertices_count

size_t Slic3r::GUI::ToolpathsObjExporter::m_vertices_count { 0 }
private
2880{ 0 };

Referenced by export_segment(), and vertex_id().

◆ m_viewer

const libvgcode::Viewer& Slic3r::GUI::ToolpathsObjExporter::m_viewer
private

The documentation for this class was generated from the following file: