WIP: IO: C++ STL exporter #105598

Closed
Eyad Ahmed wants to merge 8 commits from (deleted):io-cpp-stl-exporter into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
1 changed files with 16 additions and 5 deletions
Showing only changes of commit 2f0f20e9e5 - Show all commits

View File

@ -11,12 +11,23 @@
#include <cstdio>
#include <stdexcept>
#include "BLI_assert.h"
#include "BLI_utility_mixins.hh"
#include "stl_export_writer.hh"
namespace blender::io::stl {
#pragma pack(push, 1)
struct STLBinaryTriangle {
float normal[3]{};

Use the float3 type here instead

Use the `float3` type here instead
float vertices[3][3]{};
uint16_t attribute_byte_count{};

The defaults with {} shouldn't be necessary here

The defaults with `{}` shouldn't be necessary here
};
#pragma pack(pop)
BLI_STATIC_ASSERT_ALIGN(STLBinaryTriangle,
sizeof(float[3]) + sizeof(float[3][3]) + sizeof(uint16_t));
class BinaryFileWriter : public FileWriter, NonCopyable {
private:
FILE *file_ = nullptr;
@ -45,12 +56,12 @@ BinaryFileWriter::BinaryFileWriter(const char *filepath)
void BinaryFileWriter::write_triangle(const Triangle *t)
{
bool success = (fwrite(t->normal, sizeof(float[3]), 1, file_) == 1);
success = success && (fwrite(t->vertices, sizeof(float[3][3]), 1, file_) == 1);
STLBinaryTriangle packed_triangle{};
memcpy(packed_triangle.normal, t->normal, sizeof(float[3]));
memcpy(packed_triangle.vertices, t->vertices, sizeof(float[3][3]));
packed_triangle.attribute_byte_count = 0;
uint16_t attribute_byte_count = 0;
success = success && (fwrite(&attribute_byte_count, sizeof(uint16_t), 1, file_) == 1);
if (success) {
if (fwrite(&packed_triangle, sizeof(STLBinaryTriangle), 1, file_) == 1) {
tris_num_++;
}
}