1st installment of export of gcode toolpaths to obj file

This commit is contained in:
Enrico Turri 2019-08-20 09:01:09 +02:00
parent 4fbee3216b
commit 1f6aab312b
9 changed files with 160 additions and 1 deletions

View file

@ -853,6 +853,108 @@ std::string GLVolumeCollection::log_memory_info() const
return " (GLVolumeCollection RAM: " + format_memsize_MB(this->cpu_memory_used()) + " GPU: " + format_memsize_MB(this->gpu_memory_used()) + " Both: " + format_memsize_MB(this->gpu_memory_used()) + ")";
}
void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const
{
if (filename == nullptr)
return;
FILE* fp = boost::nowide::fopen(filename, "w");
if (fp == nullptr) {
BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << filename << " for writing";
return;
}
fprintf(fp, "# G-Code Toolpaths\n");
fprintf(fp, "# Generated by %s based on Slic3r\n\n", SLIC3R_BUILD_ID);
unsigned int vertices_count = 0;
unsigned int volume_count = 0;
for (const GLVolume* volume : this->volumes)
{
if (!volume->is_extrusion_path)
continue;
std::vector<float> vertices_and_normals_interleaved;
std::vector<int> triangle_indices;
std::vector<int> quad_indices;
if (!volume->indexed_vertex_array.vertices_and_normals_interleaved.empty())
vertices_and_normals_interleaved = volume->indexed_vertex_array.vertices_and_normals_interleaved;
else if ((volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id != 0) && (volume->indexed_vertex_array.vertices_and_normals_interleaved_size != 0))
{
vertices_and_normals_interleaved = std::vector<float>(volume->indexed_vertex_array.vertices_and_normals_interleaved_size, 0.0f);
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id));
glsafe(::glGetBufferSubData(GL_ARRAY_BUFFER, 0, vertices_and_normals_interleaved.size() * sizeof(float), vertices_and_normals_interleaved.data()));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
}
else
continue;
if (!volume->indexed_vertex_array.triangle_indices.empty())
triangle_indices = volume->indexed_vertex_array.triangle_indices;
else if ((volume->indexed_vertex_array.triangle_indices_VBO_id != 0) && (volume->indexed_vertex_array.triangle_indices_size != 0))
{
triangle_indices = std::vector<int>(volume->indexed_vertex_array.triangle_indices_size, 0);
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.triangle_indices_VBO_id));
glsafe(::glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, triangle_indices.size() * sizeof(int), triangle_indices.data()));
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
if (!volume->indexed_vertex_array.quad_indices.empty())
quad_indices = volume->indexed_vertex_array.quad_indices;
if ((volume->indexed_vertex_array.quad_indices_VBO_id != 0) && (volume->indexed_vertex_array.quad_indices_size != 0))
{
quad_indices = std::vector<int>(volume->indexed_vertex_array.quad_indices_size, 0);
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.quad_indices_VBO_id));
glsafe(::glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, quad_indices.size() * sizeof(int), quad_indices.data()));
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
if (triangle_indices.empty() && quad_indices.empty())
continue;
fprintf(fp, "\n# vertices volume %d\n", volume_count);
for (unsigned int i = 0; i < vertices_and_normals_interleaved.size(); i += 6)
{
fprintf(fp, "v %f %f %f\n", vertices_and_normals_interleaved[i + 3], vertices_and_normals_interleaved[i + 4], vertices_and_normals_interleaved[i + 5]);
}
fprintf(fp, "\n# normals volume %d\n", volume_count);
for (unsigned int i = 0; i < vertices_and_normals_interleaved.size(); i += 6)
{
fprintf(fp, "vn %f %f %f\n", vertices_and_normals_interleaved[i + 0], vertices_and_normals_interleaved[i + 1], vertices_and_normals_interleaved[i + 2]);
}
fprintf(fp, "\n# triangular facets volume %d\n", volume_count);
for (unsigned int i = 0; i < triangle_indices.size(); i += 3)
{
int id_v1 = vertices_count + 1 + triangle_indices[i + 0];
int id_v2 = vertices_count + 1 + triangle_indices[i + 1];
int id_v3 = vertices_count + 1 + triangle_indices[i + 2];
fprintf(fp, "f %d//%d %d//%d %d//%d\n", id_v1, id_v1, id_v2, id_v2, id_v3, id_v3);
}
fprintf(fp, "\n# quadrangular facets volume %d\n", volume_count);
for (unsigned int i = 0; i < quad_indices.size(); i += 4)
{
int id_v1 = vertices_count + 1 + quad_indices[i + 0];
int id_v2 = vertices_count + 1 + quad_indices[i + 1];
int id_v3 = vertices_count + 1 + quad_indices[i + 2];
int id_v4 = vertices_count + 1 + quad_indices[i + 3];
fprintf(fp, "f %d//%d %d//%d %d//%d %d//%d\n", id_v1, id_v1, id_v2, id_v2, id_v3, id_v3, id_v4, id_v4);
}
++volume_count;
vertices_count += vertices_and_normals_interleaved.size() / 6;
}
fclose(fp);
}
// caller is responsible for supplying NO lines with zero length
static void thick_lines_to_indexed_vertex_array(
const Lines &lines,