]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Separate out the UI from the program logic
[ext/subsurface.git] / dive.c
1 /* dive.c */
2 /* maintains the internal dive list structure */
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "dive.h"
7
8 struct dive *alloc_dive(void)
9 {
10         const int initial_samples = 5;
11         unsigned int size;
12         struct dive *dive;
13
14         size = dive_size(initial_samples);
15         dive = malloc(size);
16         if (!dive)
17                 exit(1);
18         memset(dive, 0, size);
19         dive->alloc_samples = initial_samples;
20         return dive;
21 }
22
23 struct sample *prepare_sample(struct dive **divep)
24 {
25         struct dive *dive = *divep;
26         if (dive) {
27                 int nr = dive->samples;
28                 int alloc_samples = dive->alloc_samples;
29                 struct sample *sample;
30                 if (nr >= alloc_samples) {
31                         unsigned int size;
32
33                         alloc_samples = (alloc_samples * 3)/2 + 10;
34                         size = dive_size(alloc_samples);
35                         dive = realloc(dive, size);
36                         if (!dive)
37                                 return NULL;
38                         dive->alloc_samples = alloc_samples;
39                         *divep = dive;
40                 }
41                 sample = dive->sample + nr;
42                 memset(sample, 0, sizeof(*sample));
43                 return sample;
44         }
45         return NULL;
46 }
47
48 void finish_sample(struct dive *dive, struct sample *sample)
49 {
50         dive->samples++;
51 }
52
53 /*
54  * So when we re-calculate maxdepth and meandepth, we will
55  * not override the old numbers if they are close to the
56  * new ones.
57  *
58  * Why? Because a dive computer may well actually track the
59  * max depth and mean depth at finer granularity than the
60  * samples it stores. So it's possible that the max and mean
61  * have been reported more correctly originally.
62  *
63  * Only if the values calculated from the samples are clearly
64  * different do we override the normal depth values.
65  *
66  * This considers 1m to be "clearly different". That's
67  * a totally random number.
68  */
69 static void update_depth(depth_t *depth, int new)
70 {
71         if (new) {
72                 int old = depth->mm;
73
74                 if (abs(old - new) > 1000)
75                         depth->mm = new;
76         }
77 }
78
79 static void update_duration(duration_t *duration, int new)
80 {
81         if (new)
82                 duration->seconds = new;
83 }
84
85 static void update_temperature(temperature_t *temperature, int new)
86 {
87         if (new) {
88                 int old = temperature->mkelvin;
89
90                 if (abs(old - new) > 1000)
91                         temperature->mkelvin = new;
92         }
93 }
94
95 static void fixup_pressure(struct dive *dive, struct sample *sample)
96 {
97         unsigned int pressure, index;
98         cylinder_t *cyl;
99
100         pressure = sample->cylinderpressure.mbar;
101         if (!pressure)
102                 return;
103         index = sample->cylinderindex;
104         if (index >= MAX_CYLINDERS)
105                 return;
106         cyl = dive->cylinder + index;
107         if (!cyl->start.mbar)
108                 cyl->start.mbar = pressure;
109         if (!cyl->end.mbar || pressure < cyl->end.mbar)
110                 cyl->end.mbar = pressure;
111 }
112
113 struct dive *fixup_dive(struct dive *dive)
114 {
115         int i;
116         double depthtime = 0;
117         int lasttime = 0;
118         int start = -1, end = -1;
119         int maxdepth = 0, mintemp = 0;
120         int lastdepth = 0;
121         int lasttemp = 0;
122
123         for (i = 0; i < dive->samples; i++) {
124                 struct sample *sample = dive->sample + i;
125                 int time = sample->time.seconds;
126                 int depth = sample->depth.mm;
127                 int temp = sample->temperature.mkelvin;
128
129                 if (lastdepth)
130                         end = time;
131
132                 if (depth) {
133                         if (start < 0)
134                                 start = lasttime;
135                         if (depth > maxdepth)
136                                 maxdepth = depth;
137                 }
138
139                 fixup_pressure(dive, sample);
140
141                 if (temp) {
142                         /*
143                          * If we have consecutive identical
144                          * temperature readings, throw away
145                          * the redundant ones.
146                          */
147                         if (lasttemp == temp)
148                                 sample->temperature.mkelvin = 0;
149                         else
150                                 lasttemp = temp;
151
152                         if (!mintemp || temp < mintemp)
153                                 mintemp = temp;
154                 }
155                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
156                 lastdepth = depth;
157                 lasttime = time;
158         }
159         if (end < 0)
160                 return dive;
161
162         update_duration(&dive->duration, end - start);
163         if (start != end)
164                 depthtime /= (end - start);
165
166         update_depth(&dive->meandepth, depthtime);
167         update_temperature(&dive->watertemp, mintemp);
168         update_depth(&dive->maxdepth, maxdepth);
169
170         return dive;
171 }
172
173 /* Don't pick a zero for MERGE_MIN() */
174 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
175 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
176
177 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
178 {
179         struct sample *p = prepare_sample(&dive);
180
181         if (!p)
182                 return NULL;
183         *p = *sample;
184         p->time.seconds = time;
185         finish_sample(dive, p);
186         return dive;
187 }
188
189 /*
190  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
191  */
192 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
193 {
194         int asamples = a->samples;
195         int bsamples = b->samples;
196         struct sample *as = a->sample;
197         struct sample *bs = b->sample;
198
199         for (;;) {
200                 int at, bt;
201                 struct sample sample;
202
203                 if (!res)
204                         return NULL;
205
206                 at = asamples ? as->time.seconds : -1;
207                 bt = bsamples ? bs->time.seconds + offset : -1;
208
209                 /* No samples? All done! */
210                 if (at < 0 && bt < 0)
211                         return fixup_dive(res);
212
213                 /* Only samples from a? */
214                 if (bt < 0) {
215 add_sample_a:
216                         res = add_sample(as, at, res);
217                         as++;
218                         asamples--;
219                         continue;
220                 }
221
222                 /* Only samples from b? */
223                 if (at < 0) {
224 add_sample_b:
225                         res = add_sample(bs, bt, res);
226                         bs++;
227                         bsamples--;
228                         continue;
229                 }
230
231                 if (at < bt)
232                         goto add_sample_a;
233                 if (at > bt)
234                         goto add_sample_b;
235
236                 /* same-time sample: add a merged sample. Take the non-zero ones */
237                 sample = *bs;
238                 if (as->depth.mm)
239                         sample.depth = as->depth;
240                 if (as->temperature.mkelvin)
241                         sample.temperature = as->temperature;
242                 if (as->cylinderpressure.mbar)
243                         sample.cylinderpressure = as->cylinderpressure;
244                 if (as->cylinderindex)
245                         sample.cylinderindex = as->cylinderindex;
246
247                 res = add_sample(&sample, at, res);
248
249                 as++;
250                 bs++;
251                 asamples--;
252                 bsamples--;
253         }
254 }
255
256 static char *merge_text(const char *a, const char *b)
257 {
258         char *res;
259
260         if (!a || !*a)
261                 return (char *)b;
262         if (!b || !*b)
263                 return (char *)a;
264         if (!strcmp(a,b))
265                 return (char *)a;
266         res = malloc(strlen(a) + strlen(b) + 9);
267         if (!res)
268                 return (char *)a;
269         sprintf(res, "(%s) or (%s)", a, b);
270         return res;
271 }
272
273 /* Pick whichever has any info (if either). Prefer 'a' */
274 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
275 {
276         if (a->size.mliter)
277                 b = a;
278         *res = *b;
279 }
280
281 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
282 {
283         if (a->o2.permille)
284                 b = a;
285         *res = *b;
286 }
287
288 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
289 {
290         merge_cylinder_type(&res->type, &a->type, &b->type);
291         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
292         MERGE_MAX(res, a, b, start.mbar);
293         MERGE_MIN(res, a, b, end.mbar);
294 }
295
296 /*
297  * This could do a lot more merging. Right now it really only
298  * merges almost exact duplicates - something that happens easily
299  * with overlapping dive downloads.
300  */
301 struct dive *try_to_merge(struct dive *a, struct dive *b)
302 {
303         int i;
304         struct dive *res;
305
306         if (a->when != b->when)
307                 return NULL;
308
309         res = alloc_dive();
310
311         res->when = a->when;
312         res->location = merge_text(a->location, b->location);
313         res->notes = merge_text(a->notes, b->notes);
314         MERGE_MAX(res, a, b, number);
315         MERGE_MAX(res, a, b, maxdepth.mm);
316         res->meandepth.mm = 0;
317         MERGE_MAX(res, a, b, duration.seconds);
318         MERGE_MAX(res, a, b, surfacetime.seconds);
319         MERGE_MAX(res, a, b, airtemp.mkelvin);
320         MERGE_MIN(res, a, b, watertemp.mkelvin);
321         for (i = 0; i < MAX_CYLINDERS; i++)
322                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
323
324         return merge_samples(res, a, b, 0);
325 }