commit fb3443a9494a431b849fce7b4e0f0da6be884521
parent f3b5f1586153fd7588b937c544eebad441d127f1
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Tue, 28 Feb 2023 10:58:32 +0100
Add a dump polygon/component to obj feature
Diffstat:
3 files changed, 104 insertions(+), 2 deletions(-)
diff --git a/src/scpr.h b/src/scpr.h
@@ -238,6 +238,17 @@ scpr_polygon_eq
const struct scpr_polygon* polygon2,
int* is_eq);
+SCPR_API res_T
+scpr_polygon_dump_to_obj
+ (struct scpr_polygon* polygon,
+ FILE* stream);
+
+SCPR_API res_T
+scpr_polygon_dump_component_to_obj
+ (struct scpr_polygon* polygon,
+ const size_t icomponent,
+ FILE* stream);
+
/*******************************************************************************
* Type of meshes, as manipulated by star-cpr.
* Meshes can be made of any number of triangles and are subject to a range
diff --git a/src/scpr_polygon.c b/src/scpr_polygon.c
@@ -101,7 +101,7 @@ one_path_is_eq
sz = pp->size();
FOR_EACH(i, 0, sz) {
if(matched[i]) continue;
- if(path_is_eq(&pp->at(i), p)) {
+ if(path_is_eq(&(*pp)[i], p)) {
matched[i] = 1;
return 1;
}
@@ -491,7 +491,7 @@ scpr_polygon_eq
/* Check actual coordinates */
matched = (char*)calloc(sz, sizeof(*matched));
FOR_EACH(i, 0, sz) {
- if(!one_path_is_eq(&polygon1->paths, &polygon2->paths.at(i), matched)) {
+ if(!one_path_is_eq(&polygon1->paths, &polygon2->paths[i], matched)) {
*is_eq = 0;
goto exit;
}
@@ -504,3 +504,73 @@ exit:
error:
goto exit;
}
+
+res_T
+scpr_polygon_dump_to_obj
+ (struct scpr_polygon* polygon,
+ FILE* stream)
+{
+ res_T res = RES_OK;
+ size_t i, j, n = 1;
+
+ if(!polygon || !stream) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ /* Vertice */
+ for(i = 0; i < polygon->paths.size(); i++) {
+ for(j = 0; j < polygon->paths[i].size(); j++) {
+ Clipper2Lib::Point64& pt = polygon->paths[i][j];
+ fprintf(stream, "v %zu %zu 0\n", pt.x, pt.y);
+ }
+ }
+
+ /* Lines */
+ for(i = 0; i < polygon->paths.size(); i++) {
+ size_t fst = n;
+ fprintf(stream, "l ");
+ for(j = 0; j < polygon->paths[i].size(); j++) {
+ fprintf(stream, " %zu", n++);
+ }
+ fprintf(stream, " %zu\n", fst);
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+res_T
+scpr_polygon_dump_component_to_obj
+ (struct scpr_polygon* polygon,
+ const size_t icomponent,
+ FILE* stream)
+{
+ res_T res = RES_OK;
+ size_t i;
+
+ if(!polygon || !stream || icomponent >= polygon->paths.size()) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ /* Vertice */
+ for(i = 0; i < polygon->paths[icomponent].size(); i++) {
+ Clipper2Lib::Point64& pt = polygon->paths[icomponent][i];
+ fprintf(stream, "v %zu %zu 0\n", pt.x, pt.y);
+ }
+
+ /* Line */
+ fprintf(stream, "l ");
+ for(i = 0; i < polygon->paths[icomponent].size(); i++) {
+ fprintf(stream, " %zu", ++i);
+ }
+ fprintf(stream, " 1\n");
+
+exit:
+ return res;
+error:
+ goto exit;
+}
diff --git a/src/test_scpr_polygon.c b/src/test_scpr_polygon.c
@@ -13,6 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#include <stdio.h>
#define _POSIX_C_SOURCE 200112L
#include "scpr.h"
@@ -59,6 +60,7 @@ main(int argc, char** argv)
struct scpr_device_create_args args = SCPR_DEVICE_CREATE_ARGS_DEFAULT;
int eq, in;
double low[2] = {0, 0}, up[2] = {1, 1};
+ FILE* stream = NULL;
(void)argc, (void)argv;
mem_init_proxy_allocator(&allocator, &mem_default_allocator);
@@ -206,6 +208,25 @@ main(int argc, char** argv)
BAD(scpr_offset_polygon(NULL, 0, SCPR_JOIN_MITER));
OK(scpr_offset_polygon(polygon, 0, SCPR_JOIN_MITER));
+ BAD(scpr_polygon_dump_to_obj(NULL, NULL));
+ BAD(scpr_polygon_dump_to_obj(polygon, NULL));
+ BAD(scpr_polygon_dump_to_obj(NULL, stream));
+ BAD(scpr_polygon_dump_to_obj(polygon, stream));
+ stream = tmpfile();
+ OK(scpr_polygon_dump_to_obj(polygon, stream));
+ fclose(stream);
+ stream = NULL;
+
+ BAD(scpr_polygon_dump_component_to_obj(NULL, ncomps, NULL));
+ BAD(scpr_polygon_dump_component_to_obj(polygon, ncomps, NULL));
+ BAD(scpr_polygon_dump_component_to_obj(NULL, 0, NULL));
+ BAD(scpr_polygon_dump_component_to_obj(NULL, ncomps, stream));
+ BAD(scpr_polygon_dump_component_to_obj(polygon, ncomps, stream));
+ BAD(scpr_polygon_dump_component_to_obj(polygon, 0, stream));
+ stream = tmpfile();
+ OK(scpr_polygon_dump_component_to_obj(polygon, 0, stream));
+ fclose(stream);
+
OK(scpr_device_ref_put(dev));
OK(scpr_polygon_ref_put(polygon));
OK(scpr_polygon_ref_put(copy));