[PATCH 3/4] When merging non-overlapping dives, add surface events in between

Linus Torvalds torvalds at linux-foundation.org
Sun Nov 18 10:18:34 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sat, 17 Nov 2012 11:20:10 -1000
Subject: [PATCH 3/4] When merging non-overlapping dives, add surface events in between

Most of the dive computers I have access to don't do the whole surface
event thing at the beginning or the end of the dive, so when you merge
two consecutive dives, you got this odd merged dive where the diver
spent the time in between at a depth of 1.2m or so (whatever the dive
computer "I'm now under water" depth limit happens to be).

Don't do that.  Add surface events at the end of the first dive to be
merged, and the beginning of the second one, so that the time in between
dives is properly marked as being at the surface.

The logic for "time in between dives" is a bit iffy - it's "more than 60
seconds with no samples".  If somebody has dive computers with samples
more than 60 seconds apart, this will break and we may have to revisit
the logic.  But dang, that's some seriously broken sample rate.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This makes the "merge two disjoint dives" code (in the dive menu, "Merge 
dives" when you have two adjacent acceptable dives selected) actually do 
the right thing for me.

NOTE! As mentioned, this may need more heuristics later. If somebody has a 
dive computer with events more than one minute apart (crazy!) this will do 
odd things to them and add a lot of surface events when merging dives. I 
don't think that's realistic, but let's keep it in mind - we've seen some 
odd dive computer behavior before too.

 dive.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/dive.c b/dive.c
index 15038bd9eca2..ae9ecde0085a 100644
--- a/dive.c
+++ b/dive.c
@@ -606,6 +606,29 @@ static struct dive *add_sample(struct sample *sample, int time, struct dive *div
 }
 
 /*
+ * This is like add_sample(), but if the distance from the last sample
+ * is excessive, we add two surface samples in between.
+ *
+ * This is so that if you merge two non-overlapping dives, we make sure
+ * that the time in between the dives is at the surface, not some "last
+ * sample that happened to be at a depth of 1.2m".
+ */
+static struct dive *merge_one_sample(struct sample *sample, int time, struct dive *dive)
+{
+	int last = dive->samples-1;
+	if (last >= 0) {
+		static struct sample surface;
+		int last_time = dive->sample[last].time.seconds;
+		if (time > last_time + 60) {
+			dive = add_sample(&surface, last_time+20, dive);
+			dive = add_sample(&surface, time - 20, dive);
+		}
+	}
+	return add_sample(sample, time, dive);
+}
+
+
+/*
  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
  */
 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
@@ -647,7 +670,7 @@ static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive
 		/* Only samples from a? */
 		if (bt < 0) {
 add_sample_a:
-			res = add_sample(as, at, res);
+			res = merge_one_sample(as, at, res);
 			as++;
 			asamples--;
 			continue;
@@ -656,7 +679,7 @@ add_sample_a:
 		/* Only samples from b? */
 		if (at < 0) {
 add_sample_b:
-			res = add_sample(bs, bt, res);
+			res = merge_one_sample(bs, bt, res);
 			bs++;
 			bsamples--;
 			continue;
@@ -678,7 +701,7 @@ add_sample_b:
 		if (as->cylinderindex)
 			sample.cylinderindex = as->cylinderindex;
 
-		res = add_sample(&sample, at, res);
+		res = merge_one_sample(&sample, at, res);
 
 		as++;
 		bs++;
-- 
1.8.0



More information about the subsurface mailing list