1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 20:21:09 +02:00
cae-tools/solidify/project.c
Werner Almesberger 1638123771 Cleaned up z0 plane input/output. Note: need to change fx in all .sfy files !
- solidify/face.h (fx_to_angle, fy_to_angle, fx_from_angle, fy_from_angle):
  helper functions to get/set the z0 plane inclination
- solidify/povray.c (povray_face), solidify/project.c (read_face_data,
  save_face_data): use helper functions to get/set z0 plane
- solidify/project.c (read_face_data, save_face_data): don't negate fx
2010-09-27 01:19:21 -03:00

204 lines
4.0 KiB
C

/*
* project.c - Load and save solidify project descriptions
*
* Written 2010 by Werner Almesberger
* Copyright 2010 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "util.h"
#include "face.h"
#include "solid.h"
#include "project.h"
static struct project *make_project(const char *name,
const char *top, const char *bottom, double dist_mm)
{
struct project *prj;
prj = alloc_type(struct project);
prj->name = stralloc(name);
prj->comment = NULL;
prj->top = stralloc(top);
prj->bottom = stralloc(bottom);
prj->s.a = read_face(top);
prj->s.b = read_face(bottom);
if (prj->s.a->x_step != prj->s.a->x_step ||
prj->s.a->y_step != prj->s.a->y_step ||
prj->s.a->z_step != prj->s.a->z_step) {
fprintf(stderr, "both faces must have the same resolution\n");
exit(1);
}
prj->s.dist = dist_mm/prj->s.a->z_step;
return prj;
}
struct project *new_project(const char *name,
const char *top, const char *bottom, double dist_mm)
{
FILE *file;
file = fopen(name, "r");
if (file) {
fprintf(stderr, "%s: already exists\n", name);
exit(1);
}
return make_project(name, top, bottom, dist_mm);
}
static void read_face_data(FILE *file, struct face *f)
{
float v;
if (fscanf(file, "%f", &v) != 1)
return;
f->z_ref = round(v/f->z_step);
if (fscanf(file, "%f", &v) != 1)
return;
fx_from_angle(f, v);
if (fscanf(file, "%f", &v) != 1)
return;
fy_from_angle(f, v);
if (fscanf(file, "%f", &v) != 1)
return;
v = v/180*M_PI;
f->m.a[0][0] = cos(v);
f->m.a[0][1] = -sin(v);
f->m.a[1][0] = sin(v);
f->m.a[1][1] = cos(v);
if (fscanf(file, "%f", &v) != 1)
return;
f->m.b[0] = v/f->x_step;
if (fscanf(file, "%f", &v) != 1)
return;
f->m.b[1] = v/f->y_step;
}
static void read_optional(FILE *file, struct project *prj)
{
float v;
if (fscanf(file, "%f", &v) != 1)
return;
prj->s.dist = v/prj->s.a->z_step;
read_face_data(file, prj->s.a);
read_face_data(file, prj->s.b);
}
struct project *load_project(const char *name)
{
FILE *file;
char top[1000], bottom[1000]; /* @@@ enough */
char *comment;
struct project *prj;
file = fopen(name, "r");
if (!file) {
perror(name);
exit(1);
}
comment = stralloc("");
while (1) {
if (!fgets(top, sizeof(top), file)) {
fprintf(stderr, "%s: can't read name of top face\n",
name);
exit(1);
}
if (*top != '#')
break;
comment = realloc(comment, strlen(comment)+strlen(top)+1);
strcat(comment, top);
}
if (strchr(top, '\n'))
*strchr(top, '\n') = 0;
if (!fgets(bottom, sizeof(bottom), file)) {
fprintf(stderr, "%s: can't read name of bottom face\n",
bottom);
exit(1);
}
if (strchr(bottom, '\n'))
*strchr(bottom, '\n') = 0;
prj = make_project(name, top, bottom, 0);
prj->comment = comment;
read_optional(file, prj);
return prj;
}
static void save_face_data(FILE *file, const char *name, const struct face *f)
{
double a;
a = asin(-f->m.a[0][1])/M_PI*180;
if (f->m.a[0][0] < 0)
a = 180-a;
if (fprintf(file, "%g %g %g\n%g %g %g\n",
f->z_ref*f->z_step,
fx_to_angle(f), fy_to_angle(f),
a, f->m.b[0]*f->x_step, f->m.b[1]*f->y_step) < 0) {
perror(name);
exit(1);
}
}
void save_project(const struct project *prj)
{
char tmp[1000]; /* @@@ enough */
FILE *file;
sprintf(tmp, "%s~", prj->name);
file = fopen(tmp, "w");
if (!file) {
perror(tmp);
exit(1);
}
if (fprintf(file,
"%s%s\n%s\n%g\n", prj->comment ? prj->comment : "",
prj->top, prj->bottom, prj->s.dist*prj->s.a->z_step) < 0) {
perror(tmp);
exit(1);
}
save_face_data(file, tmp, prj->s.a);
save_face_data(file, tmp, prj->s.b);
if (fclose(file) < 0) {
perror(tmp);
exit(1);
}
if (rename(tmp, prj->name) < 0) {
fprintf(stderr, "rename %s to %s: %s\n", tmp, prj->name,
strerror(errno));
exit(1);
}
}