Skip to content

Commit

Permalink
Merge branch 'develop' into 5121_eclipse_ease_python
Browse files Browse the repository at this point in the history
  • Loading branch information
IanMayo committed Mar 11, 2024
2 parents 73ba640 + e41d5b8 commit 52aed32
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
import MWC.GUI.Layers;
import MWC.GUI.PlainChart;
import MWC.GUI.Plottable;
import MWC.GUI.ToolParent;
import MWC.GUI.Canvas.MetafileCanvas;
import MWC.GUI.Tools.Chart.HitTester;
import MWC.GUI.Tools.Chart.RightClickEdit;
Expand Down Expand Up @@ -919,11 +920,15 @@ protected void paintBackground(final CanvasType dest) {
// we start plotting the images
proj.toScreen(proj.getDataArea().getCentre());

final BufferedImage img = GeoToolsPainter.drawAwtImage(width, height, gp,
dest.getBackgroundColor());
if (img != null) {
final ImageData swtImage = awtToSwt(img, width + 1, height + 1);
_swtImage = new Image(Display.getCurrent(), swtImage);
try {
final BufferedImage img = GeoToolsPainter.drawAwtImage(width, height, gp,
dest.getBackgroundColor());
if (img != null) {
final ImageData swtImage = awtToSwt(img, width + 1, height + 1);
_swtImage = new Image(Display.getCurrent(), swtImage);
}
} catch (Exception ee) {
CorePlugin.logError(ToolParent.ERROR, "Error creating image", ee);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,115 @@ public IStatus undo(final IProgressMonitor monitor, final IAdaptable info) throw
}
}


private static class SpatialSplitTracksOperation extends CMAPOperation {

/**
* the parent to update on completion
*/
private final Layers _layers;
private final List<TrackWrapper> _tracks;
private final double _factor;
private final HashMap<TrackWrapper, List<TrackSegment>> _trackChanges;

public SpatialSplitTracksOperation(final String title, final Layers theLayers, final List<TrackWrapper> tracks,
final double factor) {
super(title);
_layers = theLayers;
_tracks = tracks;
_factor = factor;
_trackChanges = new HashMap<TrackWrapper, List<TrackSegment>>();
}

@Override
public boolean canRedo() {
return true;
}

@Override
public boolean canUndo() {
return true;
}

@Override
public IStatus execute(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
boolean modified = false;
_trackChanges.clear();

// loop through the tracks
for (final TrackWrapper track : _tracks) {
final List<TrackSegment> newSegments = TrackWrapper_Support.splitTrackAtSpatialJumps(track, _factor);
modified = modified || !newSegments.isEmpty();
_trackChanges.put(track, newSegments);
}

// did anything get changed
if (modified) {
fireModified();
}
return Status.OK_STATUS;
}

private void fireModified() {
_layers.fireExtended();
}

@Override
public IStatus undo(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
int numChanges = 0;

// ok, merge the segments
for (final TrackWrapper track : _trackChanges.keySet()) {
final List<TrackSegment> splits = _trackChanges.get(track);

final TrackSegment target = splits.get(0);

final SegmentList existingSegments = track.getSegments();

int ctr = 0;
for (final TrackSegment segment : splits) {
if (segment != target) {
// check this is an existing segment for this track
// if we've performed several split/merge operations
// the list may now be out of sync
if (existingSegments.contains(segment)) {
// remove the segment
track.removeElement(segment);

final Enumeration<Editable> fixes = segment.elements();
while (fixes.hasMoreElements()) {
final FixWrapper fix = (FixWrapper) fixes.nextElement();
target.addFix(fix);
}
ctr++;
}
}
}

numChanges += ctr;
}

final boolean modified = numChanges > 0;

// did anything get changed
if (modified) {
fireModified();
}
return Status.OK_STATUS;
}
}
public static class TestSplittingTracks extends TestCase {
private static FixWrapper getFix(final long dtg, final double course, final double speed) {
final Fix theFix = new Fix(new HiResDate(dtg), new WorldLocation(2, 2, 2), course,
Conversions.Kts2Yps(speed));
final FixWrapper res = new FixWrapper(theFix);
return res;
}
private static FixWrapper getFix2(final long dtg, final double lat, final double lng) {
final Fix theFix = new Fix(new HiResDate(dtg), new WorldLocation(lat, lng, 0), 0d,0d);
final FixWrapper res = new FixWrapper(theFix);
return res;
}

private static TrackWrapper getOne() {
final TrackWrapper tOne = new TrackWrapper();
Expand Down Expand Up @@ -196,6 +298,24 @@ private static TrackWrapper getTwo() {
return tTwo;
}

private static TrackWrapper getThree() {
final TrackWrapper tThree = new TrackWrapper();
tThree.setName("t-3");
tThree.addFix(getFix2(1000, 22, 33));
tThree.addFix(getFix2(2000, 22, 34));
tThree.addFix(getFix2(2100, 22, 35));
tThree.addFix(getFix2(4000, 22, 36));
tThree.addFix(getFix2(5000, 22, 30));
tThree.addFix(getFix2(8000, 22, 31));
tThree.addFix(getFix2(9000, 22, 32));
tThree.addFix(getFix2(10000, 22, 38));
tThree.addFix(getFix2(11100, 22, 39));
tThree.addFix(getFix2(12000, 22, 38));
tThree.addFix(getFix2(13000, 22, 37));
tThree.addFix(getFix2(14000, 22, 36));
return tThree;
}

public void testSplitOperation() throws ExecutionException {

final TrackWrapper tOne = getOne();
Expand Down Expand Up @@ -234,6 +354,24 @@ public void testSplitOperation() throws ExecutionException {
assertEquals("correct positions", 14, tOne.numFixes());
assertEquals("correct positions", 12, tTwo.numFixes());
}


public void testSpatialSplitOperation1() throws ExecutionException {

final TrackWrapper tThree = getThree();
final List<TrackWrapper> tracks = new ArrayList<TrackWrapper>();
tracks.add(tThree);
List<TrackSegment> segments = TrackWrapper_Support.splitTrackAtSpatialJumps(tThree, 3d);
assertEquals("three legs", 3, segments.size());
}
public void testSpatialSplitOperation2() throws ExecutionException {

final TrackWrapper tTwo = getTwo();
final List<TrackWrapper> tracks = new ArrayList<TrackWrapper>();
tracks.add(tTwo);
List<TrackSegment> segments = TrackWrapper_Support.splitTrackAtSpatialJumps(tTwo, 3d);
assertEquals("three legs", 0, segments.size());
}
}

@Override
Expand Down Expand Up @@ -268,11 +406,12 @@ public void generate(final IMenuManager parent, final Layers theLayers, final La

final String msg = tracks.size() > 1 ? "tracks" : "track";

final String fullMsg = "Split " + msg + " into segments on gaps over...";
final String fullMsg1 = "Split " + msg + " into segments on gaps over...";

final MenuManager listing = new MenuManager(fullMsg);
final MenuManager listing1 = new MenuManager(fullMsg1);

final HashMap<Long, String> choices = new HashMap<Long, String>();
choices.put(10 * 1000L, "10 Seconds");
choices.put(60 * 1000L, "1 Minute");
choices.put(60 * 60 * 1000L, "1 Hour");
choices.put(24 * 60 * 60 * 1000L, "1 Day");
Expand All @@ -291,9 +430,30 @@ public void run() {
CorePlugin.run(theAction);
}
};
listing.add(doMerge);
listing1.add(doMerge);
}
parent.add(listing1);

// now the spatial distance factor
final String fullMsg2 = "Split " + msg + " into segments where distance increases by factor of ...";

final MenuManager listing2 = new MenuManager(fullMsg2);

final long factors[] = {2, 3, 5, 10, 20};
for (final Long factor : factors) {

// create this operation
final Action doMerge = new Action("" + factor) {
@Override
public void run() {
final IUndoableOperation theAction = new SpatialSplitTracksOperation(
"Split tracks where distance increases by factor of " + factor, theLayers, tracks, factor);
CorePlugin.run(theAction);
}
};
listing2.add(doMerge);
}
parent.add(listing);
parent.add(listing2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package Debrief.ReaderWriter.FlatFile;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -26,12 +27,16 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;

import Debrief.Wrappers.FixWrapper;
import Debrief.Wrappers.TrackWrapper;
import Debrief.Wrappers.Track.TrackSegment;
import Debrief.Wrappers.Track.TrackWrapper_Support;
import MWC.Algorithms.Conversions;
import MWC.GUI.Editable;
import MWC.GUI.ErrorLogger;
import MWC.GUI.Layer;
import MWC.GUI.Layers;
Expand Down Expand Up @@ -199,15 +204,17 @@ public ImportNmeaRadarFileAction(final List<RadarEntry> track, final WorldLocati

@Override
public void execute() {
final List<TrackWrapper> newTracks = new ArrayList<TrackWrapper>();
for (RadarEntry entry: _entries) {
// get the parent track
final Layer layer = _layers.findLayer("" + entry.trackId);
final TrackWrapper track;
if(layer == null) {
track = new TrackWrapper();
track.setName(""+ entry.trackId);
track.setColor(DebriefColors.GREEN);
track.setColor(DebriefColors.RandomColorProvider.getRandomColor(entry.trackId));
_layers.addThisLayer(track);
newTracks.add(track);
} else {
track = (TrackWrapper) layer;
}
Expand All @@ -218,6 +225,24 @@ public void execute() {
// add to the track
track.addFix(fix);
}

// split the tracks
for (final TrackWrapper track: newTracks) {
final List<TrackSegment> legs = TrackWrapper_Support.splitTrackAtSpatialJumps(track, 10d);
// shade each leg differnetly
if (legs.size() > 1) {
for (TrackSegment leg: legs) {
final Color col = DebriefColors.RandomColorProvider.getRandomColor((int)(Math.random() * 100));
final Enumeration<Editable> ele = leg.elements();
while(ele.hasMoreElements()) {
FixWrapper fix = (FixWrapper) ele.nextElement();
fix.setColor(col);
}
}
}
}

// update layers
_layers.fireExtended();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import MWC.GUI.Layer;
import MWC.GUI.Plottables;
import MWC.GUI.SupportsPropertyListeners;
import MWC.GenericData.WorldLocation;

public class TrackWrapper_Support {

Expand Down Expand Up @@ -462,4 +463,59 @@ public static List<TrackSegment> splitTrackAtJumps(final TrackWrapper track, fin
return newSegments;
}


public static List<TrackSegment> splitTrackAtSpatialJumps(final TrackWrapper track, final double factor) {
final Enumeration<Editable> segs = track.getSegments().elements();
final List<FixWrapper> jumps = new ArrayList<FixWrapper>();
final List<TrackSegment> newSegments = new ArrayList<TrackSegment>();

// find the jumps
while (segs.hasMoreElements()) {
final TrackSegment segment = (TrackSegment) segs.nextElement();
final Enumeration<Editable> posits = segment.elements();
WorldLocation lastLoc = null;
double lastDistDegs = Double.MIN_NORMAL;
while (posits.hasMoreElements()) {
final FixWrapper next = (FixWrapper) posits.nextElement();
final WorldLocation loc = next.getLocation();
if (lastLoc != null) {
// get this distance
final double dist = loc.subtract(lastLoc).getRange();

// do we have a previous distance?
if (lastDistDegs != Double.MIN_NORMAL && lastDistDegs != 0d) {
final double ratio = dist / lastDistDegs;
if (Math.abs(ratio) > factor) {
jumps.add(next);
lastLoc = null;
lastDistDegs = Double.MIN_NORMAL;

}
}

lastDistDegs = dist;
}
lastLoc = loc;
}
}

// now split on the jumps
for (final FixWrapper jump : jumps) {
// what's the old segment for this fix
final TrackSegment oldSegment = jump.getSegment();
final Vector<TrackSegment> newSegs = track.splitTrack(jump, true);

// the old segment has been replaced by two new ones, so delete it
newSegments.remove(oldSegment);

// now put in the two new segments
for (final TrackSegment seg : newSegs) {
if (!newSegments.contains(seg)) {
newSegments.add(seg);
}
}
}

return newSegments;
}
}

0 comments on commit 52aed32

Please sign in to comment.