summaryrefslogtreecommitdiff
path: root/meme/meme.c
blob: 2a8d11bb6b56a3e4bc6968ba84d1b253d2ed4692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * meme.c - Measmesh command-line processing and setup
 *
 * Written 2014-2015 by Werner Almesberger
 * Copyright 2014-2015 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 <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

#include "time.h"
#include "mesh.h"
#include "stl.h"
#include "overlay.h"
#include "slice.h"
#include "gui.h"


#define	N_OVERLAYS	3


static bool count_edge(const struct vertex *a, const struct vertex *b,
    void *user)
{
	unsigned *count = user;

	(*count)++;
	return 0;
}


static unsigned count_edges(void)
{
	unsigned count = 0;

	edge_foreach(count_edge, &count);
	return count;
}


static void slice_out(double x, double y, double z, bool last)
{
	printf("%f %f %f\n%s", x, y, z, last ? "\n" : "");
}


static void overlay_xform(float x, float y, float *res_x, float *res_y)
{
	*res_x = round(x * 1000);
	*res_y = round(y * 1000);
}


static void usage(const char *name)
{
	fprintf(stderr,
"usage: %s [-l logfile] [-o overlay.gp ...] [file.stl]\n"
"       %s -s plane.gp [-n] [file.stl]\n",
	    name, name);
	exit(1);
}


int main(int argc, char **argv)
{
	int c;
	const char *logfile = NULL;
	const char *slice = NULL;
	bool needle = 0;
	const struct overlay *overlays[N_OVERLAYS];
	unsigned n_overlays = 0;
	struct time t_load;

	while ((c = getopt(argc, argv, "l:no:s:")) != EOF)
		switch (c) {
		case 'l':
			logfile = optarg;
			break;
		case 'n':
			needle = 1;
			break;
		case 'o':
			if (n_overlays == N_OVERLAYS) {
				fprintf(stderr, "too many overlays\n");
				exit(1);
			}
			overlays[n_overlays] = overlay_load(optarg,
			    overlay_xform);
			n_overlays++;
			break;
		case 's':
			slice = optarg;
			break;
		default:
			usage(*argv);
		}

	switch (argc - optind) {
	case 0:
		time_start(&t_load);
		stl_load_file(stdin);
		time_stop(&t_load);
		break;
	case 1:
		time_start(&t_load);
		stl_load(argv[optind]);
		time_stop(&t_load);
		break;
	default:
		usage(*argv);
	}

	fprintf(stderr, "STL loaded in %.3f s\n", time_ms(&t_load)/1000.0);
	fprintf(stderr, "%u edges\n", count_edges());

	if (slice) {
		slice_load(slice);
		slice_run(slice_out, needle);
		exit(1);
	}

	gui(logfile, overlays, n_overlays);

	return 0;
}