curvedraw

changeset 6:6e980fddbf3b

curve export
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Dec 2015 07:07:19 +0200
parents 2b7ae76c173f
children 5ace6f6f4973 8bf96e11ed1f
files curvedraw.vcxproj curvedraw.vcxproj.filters src/app.cc src/curvefile.cc src/curvefile.h
diffstat 5 files changed, 99 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/curvedraw.vcxproj	Fri Dec 18 03:47:10 2015 +0200
     1.2 +++ b/curvedraw.vcxproj	Fri Dec 18 07:07:19 2015 +0200
     1.3 @@ -82,12 +82,14 @@
     1.4    <ItemGroup>
     1.5      <ClCompile Include="src\app.cc" />
     1.6      <ClCompile Include="src\curve.cc" />
     1.7 +    <ClCompile Include="src\curvefile.cc" />
     1.8      <ClCompile Include="src\main.cc" />
     1.9      <ClCompile Include="src\widgets.cc" />
    1.10    </ItemGroup>
    1.11    <ItemGroup>
    1.12      <ClInclude Include="src\app.h" />
    1.13      <ClInclude Include="src\curve.h" />
    1.14 +    <ClInclude Include="src\curvefile.h" />
    1.15      <ClInclude Include="src\opengl.h" />
    1.16      <ClInclude Include="src\widgets.h" />
    1.17    </ItemGroup>
     2.1 --- a/curvedraw.vcxproj.filters	Fri Dec 18 03:47:10 2015 +0200
     2.2 +++ b/curvedraw.vcxproj.filters	Fri Dec 18 07:07:19 2015 +0200
     2.3 @@ -19,6 +19,9 @@
     2.4      <ClCompile Include="src\widgets.cc">
     2.5        <Filter>src</Filter>
     2.6      </ClCompile>
     2.7 +    <ClCompile Include="src\curvefile.cc">
     2.8 +      <Filter>src</Filter>
     2.9 +    </ClCompile>
    2.10    </ItemGroup>
    2.11    <ItemGroup>
    2.12      <ClInclude Include="src\app.h">
    2.13 @@ -33,5 +36,8 @@
    2.14      <ClInclude Include="src\widgets.h">
    2.15        <Filter>src</Filter>
    2.16      </ClInclude>
    2.17 +    <ClInclude Include="src\curvefile.h">
    2.18 +      <Filter>src</Filter>
    2.19 +    </ClInclude>
    2.20    </ItemGroup>
    2.21  </Project>
    2.22 \ No newline at end of file
     3.1 --- a/src/app.cc	Fri Dec 18 03:47:10 2015 +0200
     3.2 +++ b/src/app.cc	Fri Dec 18 07:07:19 2015 +0200
     3.3 @@ -7,6 +7,7 @@
     3.4  #include "app.h"
     3.5  #include "curve.h"
     3.6  #include "widgets.h"
     3.7 +#include "curvefile.h"
     3.8  
     3.9  enum SnapMode {
    3.10  	SNAP_NONE,
    3.11 @@ -252,6 +253,15 @@
    3.12  				post_redisplay();
    3.13  			}
    3.14  			break;
    3.15 +
    3.16 +		case 'e':
    3.17 +		case 'E':
    3.18 +			// TODO: GUI for filename at least
    3.19 +			if(!save_curves(stdout, &curves[0], (int)curves.size())) {
    3.20 +				fprintf(stderr, "failed to export curves\n");
    3.21 +			}
    3.22 +			printf("exported %d curves\n", (int)curves.size());
    3.23 +			break;
    3.24  		}
    3.25  	}
    3.26  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/curvefile.cc	Fri Dec 18 07:07:19 2015 +0200
     4.3 @@ -0,0 +1,68 @@
     4.4 +#include <stdlib.h>
     4.5 +#include "curvefile.h"
     4.6 +
     4.7 +static bool save_curve(FILE *fp, const Curve *curve);
     4.8 +
     4.9 +bool save_curves(const char *fname, const Curve * const *curves, int count)
    4.10 +{
    4.11 +	FILE *fp = fopen(fname, "wb");
    4.12 +	if(!fp) return false;
    4.13 +
    4.14 +	bool res = save_curves(fp, curves, count);
    4.15 +	fclose(fp);
    4.16 +	return res;
    4.17 +}
    4.18 +
    4.19 +bool save_curves(FILE *fp, const Curve * const *curves, int count)
    4.20 +{
    4.21 +	fprintf(stderr, "GCURVES\ncount %d\n", count);
    4.22 +
    4.23 +	for(int i=0; i<count; i++) {
    4.24 +		if(!save_curve(fp, curves[i])) {
    4.25 +			return false;
    4.26 +		}
    4.27 +	}
    4.28 +	return true;
    4.29 +}
    4.30 +
    4.31 +static const char *curve_type_str(CurveType type)
    4.32 +{
    4.33 +	switch(type) {
    4.34 +	case CURVE_LINEAR:
    4.35 +		return "polyline";
    4.36 +	case CURVE_HERMITE:
    4.37 +		return "hermite";
    4.38 +	case CURVE_BSPLINE:
    4.39 +		return "bspline";
    4.40 +	}
    4.41 +	abort();
    4.42 +}
    4.43 +
    4.44 +static bool save_curve(FILE *fp, const Curve *curve)
    4.45 +{
    4.46 +	fprintf(fp, "curve {\n");
    4.47 +	fprintf(fp, "    type %s\n", curve_type_str(curve->get_type()));
    4.48 +	fprintf(fp, "    cpcount %d\n", curve->size());
    4.49 +	for(int i=0; i<curve->size(); i++) {
    4.50 +		Vector3 cp = curve->get_homo_point(i);
    4.51 +		fprintf(fp, "    cp %g %g %g\n", cp.x, cp.y, cp.z);
    4.52 +	}
    4.53 +	fprintf(fp, "}\n");
    4.54 +	return true;
    4.55 +}
    4.56 +
    4.57 +Curve **load_curves(const char *fname, int *countret)
    4.58 +{
    4.59 +	FILE *fp = fopen(fname, "rb");
    4.60 +	if(!fp) return false;
    4.61 +
    4.62 +	Curve **res = load_curves(fp, countret);
    4.63 +	fclose(fp);
    4.64 +	return res;
    4.65 +}
    4.66 +
    4.67 +Curve **load_curves(FILE *fp, int *countret)
    4.68 +{
    4.69 +	return 0;	// TODO
    4.70 +}
    4.71 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/curvefile.h	Fri Dec 18 07:07:19 2015 +0200
     5.3 @@ -0,0 +1,13 @@
     5.4 +#ifndef CURVEFILE_H_
     5.5 +#define CURVEFILE_H_
     5.6 +
     5.7 +#include <stdio.h>
     5.8 +#include "curve.h"
     5.9 +
    5.10 +bool save_curves(const char *fname, const Curve * const *curves, int count);
    5.11 +bool save_curves(FILE *fp, const Curve * const *curves, int count);
    5.12 +
    5.13 +Curve **load_curves(const char *fname, int *countret);
    5.14 +Curve **load_curves(FILE *fp, int *countret);
    5.15 +
    5.16 +#endif	// CURVEFILE_H_
    5.17 \ No newline at end of file