1 | /* > diffpos.c */ |
---|
2 | /* (Originally quick and dirty) program to compare two SURVEX .pos files */ |
---|
3 | /* Copyright (C) 1994,1996,1998,1999 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | /* size of line buffer */ |
---|
25 | #define BUFLEN 256 |
---|
26 | |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <string.h> |
---|
30 | #include <math.h> |
---|
31 | |
---|
32 | #include "cmdline.h" |
---|
33 | |
---|
34 | #define sqrd(X) ((X)*(X)) |
---|
35 | |
---|
36 | /* macro to just convert argument to a string */ |
---|
37 | #define STRING(X) _STRING(X) |
---|
38 | #define _STRING(X) #X |
---|
39 | |
---|
40 | /* very small value for comparing floating point numbers with */ |
---|
41 | /* FIXME: ought to use a real epsilon value */ |
---|
42 | #define EPSILON 0.00001 |
---|
43 | |
---|
44 | /* default threshold is 1cm */ |
---|
45 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
46 | |
---|
47 | static int diff_pos(FILE *fh1, FILE *fh2, double threshold); |
---|
48 | static int read_line(FILE *fh, double *px, double *py, double *pz, char *id); |
---|
49 | |
---|
50 | static const struct option long_opts[] = { |
---|
51 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
52 | {"help", no_argument, 0, HLP_HELP}, |
---|
53 | {"version", no_argument, 0, HLP_VERSION}, |
---|
54 | {0, 0, 0, 0} |
---|
55 | }; |
---|
56 | |
---|
57 | #define short_opts "" |
---|
58 | |
---|
59 | static struct help_msg help[] = { |
---|
60 | /* <-- */ |
---|
61 | {0, 0} |
---|
62 | }; |
---|
63 | |
---|
64 | int |
---|
65 | main(int argc, char **argv) |
---|
66 | { |
---|
67 | double threshold = DFLT_MAX_THRESHOLD; |
---|
68 | char *fnm1, *fnm2; |
---|
69 | FILE *fh1, *fh2; |
---|
70 | |
---|
71 | cmdline_set_syntax_message("POS_FILE POS_FILE [THRESHOLD]", |
---|
72 | "THRESHOLD is the max. ignorable change along " |
---|
73 | "any axis in metres (default " |
---|
74 | STRING(DFLT_MAX_THRESHOLD)")"); |
---|
75 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3); |
---|
76 | while (cmdline_getopt() != EOF) { |
---|
77 | /* do nothing */ |
---|
78 | } |
---|
79 | fnm1 = argv[optind++]; |
---|
80 | fnm2 = argv[optind++]; |
---|
81 | if (argv[optind]) { |
---|
82 | optarg = argv[optind]; |
---|
83 | threshold = cmdline_double_arg(); |
---|
84 | } |
---|
85 | fh1 = fopen(fnm1, "rb"); |
---|
86 | if (!fh1) { |
---|
87 | printf("Can't open file '%s'\n", fnm1); |
---|
88 | exit(1); |
---|
89 | } |
---|
90 | fh2 = fopen(fnm2, "rb"); |
---|
91 | if (!fh2) { |
---|
92 | printf("Can't open file '%s'\n", fnm2); |
---|
93 | exit(1); |
---|
94 | } |
---|
95 | return diff_pos(fh1, fh2, threshold); |
---|
96 | } |
---|
97 | |
---|
98 | typedef enum { Eof, Haveline, Needline } state; |
---|
99 | |
---|
100 | int |
---|
101 | diff_pos(FILE *fh1, FILE *fh2, double threshold) |
---|
102 | { |
---|
103 | state pos1 = Needline, pos2 = Needline; |
---|
104 | int result = 0; |
---|
105 | |
---|
106 | while (1) { |
---|
107 | double x1, y1, z1, x2, y2, z2; |
---|
108 | char id1[BUFLEN], id2[BUFLEN]; |
---|
109 | |
---|
110 | if (pos1 == Needline) { |
---|
111 | pos1 = Haveline; |
---|
112 | if (!read_line(fh1, &x1, &y1, &z1, id1)) pos1 = Eof; |
---|
113 | } |
---|
114 | |
---|
115 | if (pos2 == Needline) { |
---|
116 | pos2 = Haveline; |
---|
117 | if (!read_line(fh2, &x2, &y2, &z2, id2)) pos2 = Eof; |
---|
118 | } |
---|
119 | |
---|
120 | if (pos1 == Eof) { |
---|
121 | if (pos2 == Eof) break; |
---|
122 | result = 1; |
---|
123 | printf("Added: %s (at end of file)\n", id2); |
---|
124 | pos2 = Needline; |
---|
125 | } else if (pos2 == Eof) { |
---|
126 | result = 1; |
---|
127 | printf("Deleted: %s (at end of file)\n", id1); |
---|
128 | pos1 = Needline; |
---|
129 | } else { |
---|
130 | int cmp = strcmp(id1, id2); |
---|
131 | if (cmp == 0) { |
---|
132 | if (fabs(x1 - x2) - threshold > EPSILON || |
---|
133 | fabs(y1 - y2) - threshold > EPSILON || |
---|
134 | fabs(z1 - z2) - threshold > EPSILON) { |
---|
135 | result = 1; |
---|
136 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
137 | x1 - x2, y1 - y2, z1 - z2, id1); |
---|
138 | } |
---|
139 | pos1 = pos2 = Needline; |
---|
140 | } else { |
---|
141 | result = 1; |
---|
142 | if (cmp < 0) { |
---|
143 | printf("Deleted: %s\n", id1); |
---|
144 | pos1 = Needline; |
---|
145 | } else { |
---|
146 | printf("Added: %s\n", id2); |
---|
147 | pos2 = Needline; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | return result; |
---|
153 | } |
---|
154 | |
---|
155 | static int |
---|
156 | read_line(FILE *fh, double *px, double *py, double *pz, char *id) |
---|
157 | { |
---|
158 | char buf[BUFLEN]; |
---|
159 | while (1) { |
---|
160 | if (!fgets(buf, BUFLEN, fh)) return 0; |
---|
161 | if (sscanf(buf, "(%lf,%lf,%lf )%s", px, py, pz, id) == 4) break; |
---|
162 | printf("Ignoring line: %s\n", buf); |
---|
163 | } |
---|
164 | return 1; |
---|
165 | } |
---|