test_sstl_desc.c (5004B)
1 /* Copyright (C) 2015, 2016, 2019, 2021, 2023, 2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "sstl.h" 17 18 #include <rsys/float3.h> 19 #include <rsys/mem_allocator.h> 20 21 static const float vertices[4/*#vertices*/*3/*#coords*/] = { 22 0.0f, 0.0f, 0.0f, 23 0.1f, 0.0f, 0.0f, 24 0.0f, 0.0f, 0.1f, 25 0.0f, 0.1f, 0.0f, 26 }; 27 static const size_t nvertices = sizeof(vertices)/(sizeof(float)*3); 28 29 static const unsigned indices[4/*#triangles*/*3/*#ids*/] = { 30 0, 1, 2, 31 0, 3, 1, 32 0, 2, 3, 33 1, 3, 2 34 }; 35 static const size_t ntriangles = sizeof(indices)/(sizeof(unsigned)*3); 36 37 /******************************************************************************* 38 * Helper functions 39 ******************************************************************************/ 40 static FILE* 41 write_tetrahedron(struct sstl* sstl, const enum sstl_type type) 42 { 43 struct sstl_facet facet = SSTL_FACET_NULL; 44 struct sstl_writer_create_args args = SSTL_WRITER_CREATE_ARGS_DEFAULT; 45 struct sstl_writer* writer = NULL; 46 FILE* fp = NULL; 47 size_t i = 0; 48 49 CHK(sstl != NULL); 50 51 CHK((fp = tmpfile()) != NULL); 52 args.filename = "Tetrahedron.stl"; 53 args.stream = fp; 54 args.type = type; 55 args.solid_name = "cube corner"; 56 args.verbose = 3; 57 CHK(sstl_writer_create(&args, &writer) == RES_OK); 58 59 FOR_EACH(i, 0, ntriangles) { 60 f3_set(facet.vertices[0], vertices+(indices[i*3/*#ids*/+0])*3/*#coords*/); 61 f3_set(facet.vertices[1], vertices+(indices[i*3/*#ids*/+1])*3/*#coords*/); 62 f3_set(facet.vertices[2], vertices+(indices[i*3/*#ids*/+2])*3/*#coords*/); 63 CHK(sstl_write_facet(writer, &facet) == RES_OK); 64 } 65 66 CHK(sstl_writer_ref_put(writer) == RES_OK); 67 rewind(fp); 68 return fp; 69 } 70 71 static void 72 check_tetrahedron(struct sstl* sstl, FILE* fp) 73 { 74 struct sstl_desc desc = SSTL_DESC_NULL; 75 struct sstl_facet facet = SSTL_FACET_NULL; 76 float coords[3] = {0,0,0}; 77 unsigned ids[3] = {0,0,0}; 78 size_t i = 0; 79 80 CHK(sstl != NULL); 81 CHK(fp != NULL); 82 83 CHK(sstl_load_stream(sstl, fp, "Tetrahedron") == RES_OK); 84 85 CHK(sstl_get_desc(NULL, &desc) == RES_BAD_ARG); 86 CHK(sstl_get_desc(sstl, NULL) == RES_BAD_ARG); 87 CHK(sstl_get_desc(sstl, &desc) == RES_OK); 88 89 CHK(desc.vertices_count == nvertices); 90 CHK(desc.triangles_count == ntriangles); 91 92 CHK(sstl_desc_get_vertex_coords(NULL, 0, coords) == RES_BAD_ARG); 93 CHK(sstl_desc_get_vertex_coords(&desc, nvertices, coords) == RES_BAD_ARG); 94 CHK(sstl_desc_get_vertex_coords(&desc, 0, NULL) == RES_BAD_ARG); 95 FOR_EACH(i, 0, desc.vertices_count) { 96 CHK(sstl_desc_get_vertex_coords(&desc, i, coords) == RES_OK); 97 CHK(f3_eq(coords, vertices + i*3)); 98 } 99 100 CHK(sstl_desc_get_triangle_ids(NULL, 0, ids) == RES_BAD_ARG); 101 CHK(sstl_desc_get_triangle_ids(&desc, ntriangles, ids) == RES_BAD_ARG); 102 CHK(sstl_desc_get_triangle_ids(&desc, 0, NULL) == RES_BAD_ARG); 103 FOR_EACH(i, 0, desc.triangles_count) { 104 CHK(sstl_desc_get_triangle_ids(&desc, i, ids) == RES_OK); 105 CHK(ids[0] == indices[i*3+0]); 106 CHK(ids[1] == indices[i*3+1]); 107 CHK(ids[2] == indices[i*3+2]); 108 } 109 110 CHK(sstl_desc_get_facet(NULL, 0, &facet) == RES_BAD_ARG); 111 CHK(sstl_desc_get_facet(&desc, ntriangles, &facet) == RES_BAD_ARG); 112 CHK(sstl_desc_get_facet(&desc, 0, NULL) == RES_BAD_ARG); 113 FOR_EACH(i, 0, desc.triangles_count) { 114 CHK(sstl_desc_get_facet(&desc, i, &facet) == RES_OK); 115 CHK(sstl_desc_get_triangle_ids(&desc, i, ids) == RES_OK); 116 117 CHK(sstl_desc_get_vertex_coords(&desc, ids[0], coords) == RES_OK); 118 CHK(f3_eq(coords, facet.vertices[0])); 119 CHK(sstl_desc_get_vertex_coords(&desc, ids[1], coords) == RES_OK); 120 CHK(f3_eq(coords, facet.vertices[1])); 121 CHK(sstl_desc_get_vertex_coords(&desc, ids[2], coords) == RES_OK); 122 CHK(f3_eq(coords, facet.vertices[2])); 123 } 124 } 125 126 /******************************************************************************* 127 * The test 128 ******************************************************************************/ 129 int 130 main(int argc, char** argv) 131 { 132 struct sstl* sstl = NULL; 133 FILE* fp = NULL; 134 (void)argc, (void)argv; 135 136 CHK(sstl_create(NULL, NULL, 3, &sstl) == RES_OK); 137 138 CHK((fp = write_tetrahedron(sstl, SSTL_ASCII)) != NULL); 139 check_tetrahedron(sstl, fp); 140 CHK(fclose(fp) == 0); 141 142 CHK((fp = write_tetrahedron(sstl, SSTL_BINARY)) != NULL); 143 check_tetrahedron(sstl, fp); 144 CHK(fclose(fp) == 0); 145 146 CHK(sstl_ref_put(sstl) == RES_OK); 147 CHK(mem_allocated_size() == 0); 148 return 0; 149 }