star-stl

Load STereo Lithography (StL) file format
git clone git://git.meso-star.fr/star-stl.git
Log | Files | Refs | README | LICENSE

commit 4286f29f9bae6b6eee49a62cc6bc1c54fc429921
parent 0a4991d8742fa0137104e2b42b9a0503bfd1d817
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 23 Apr 2025 12:29:31 +0200

Test writing an ASCII StL to an unseekable stream

Diffstat:
Msrc/test_sstl_write_ascii.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+), 0 deletions(-)

diff --git a/src/test_sstl_write_ascii.c b/src/test_sstl_write_ascii.c @@ -13,12 +13,17 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _POSIX_C_SOURCE 200112L /* fork */ + #include "sstl.h" #include <rsys/float3.h> #include <rsys/logger.h> #include <rsys/mem_allocator.h> +#include <string.h> +#include <unistd.h> /* fork, pipe */ + /******************************************************************************* * Helper functions ******************************************************************************/ @@ -197,6 +202,69 @@ check_tetrahedron(struct sstl* sstl) CHK(f3_eq_eps(desc.normals + 3*3, v, 1.e-6f) == 1); } +static void +check_no_seekable_file(struct sstl* sstl) +{ + int fd[2] = {0,0}; + const char* filename = "Pipe"; + pid_t pid = 0; + + CHK(pipe(fd) == 0); + CHK((pid = fork()) != -1); + + if(pid == 0) { /* Child process */ + struct sstl_writer_create_args args = SSTL_WRITER_CREATE_ARGS_DEFAULT; + struct sstl_facet facet = SSTL_FACET_NULL; + struct sstl_writer* writer = NULL; + FILE* fp = NULL; + + CHK(close(fd[0]) == 0); /* Close the unused input stream */ + CHK(sstl_ref_put(sstl) == RES_OK); /* Release the unused sstl */ + + CHK((fp = fdopen(fd[1], "w")) != NULL); + + args.solid_name = "Triangle"; + args.filename = filename; + args.stream = fp; + CHK(sstl_writer_create(&args, &writer) == RES_OK); + + f3(facet.normal, 0.f,-1.f, 0.f); + f3(facet.vertices[0], 1.f, 0.f, 0.f); + f3(facet.vertices[1], 0.f, 0.f, 1.f); + f3(facet.vertices[2], 0.f, 0.f, 0.f); + CHK(sstl_write_facet(writer, &facet) == RES_OK); + + CHK(sstl_writer_ref_put(writer) == RES_OK); + CHK(fclose(fp) == 0); + exit(0); + + } else { /* Parent process */ + struct sstl_desc desc = SSTL_DESC_NULL; + float v[3]; + FILE* fp = NULL; + + CHK(close(fd[1]) == 0); /* Close the unused output stream */ + + CHK(fp = fdopen(fd[0], "r")); + CHK(sstl_load_stream_ascii(sstl, fp, filename) == RES_OK); + CHK(fclose(fp) == 0); + + CHK(sstl_get_desc(sstl, &desc) == RES_OK); + CHK(desc.type == SSTL_ASCII); + CHK(!strcmp(desc.filename, filename)); + CHK(!strcmp(desc.solid_name, "Triangle")); + CHK(desc.vertices_count == 3); + CHK(desc.triangles_count == 1); + CHK(desc.indices[0] == 0); + CHK(desc.indices[1] == 1); + CHK(desc.indices[2] == 2); + CHK(f3_eq(desc.vertices + 0*3, f3(v, 1.f, 0.f, 0.f)) == 1); + CHK(f3_eq(desc.vertices + 1*3, f3(v, 0.f, 0.f, 1.f)) == 1); + CHK(f3_eq(desc.vertices + 2*3, f3(v, 0.f, 0.f, 0.f)) == 1); + CHK(f3_eq(desc.normals, f3(v, 0.f, -1.f, 0.f)) == 1); + } +} + /******************************************************************************* * The test ******************************************************************************/ @@ -211,6 +279,7 @@ main(int argc, char** argv) check_api(); check_1_triangle(sstl); check_tetrahedron(sstl); + check_no_seekable_file(sstl); CHK(sstl_ref_put(sstl) == RES_OK); CHK(mem_allocated_size() == 0);