diff --git a/src/edu/nps/moves/dis7/utilities/stream/LogCompare.java b/src/edu/nps/moves/dis7/utilities/stream/LogCompare.java index bd191e9019833b73be115015fa3461a49c239884..bad4bc4dbbff21913a23fb44e40a953f6d3dd550 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/LogCompare.java +++ b/src/edu/nps/moves/dis7/utilities/stream/LogCompare.java @@ -6,6 +6,7 @@ import static edu.nps.moves.dis7.utilities.stream.PduRecorder.COMMENT_MARKER; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.Base64; /** @@ -125,7 +126,7 @@ public class LogCompare System.out.println("End of compare. There were " + (goodmatch ? "no" : "one or more ") + "errors"); System.exit(goodmatch ? 0 : 1); } - catch (Exception ex) { + catch (IOException ex) { System.err.println("Exception reading pdu logs: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage()); System.exit(1); } diff --git a/src/edu/nps/moves/dis7/utilities/stream/ValidationPdusMakerV1.java b/src/edu/nps/moves/dis7/utilities/stream/ValidationPdusMakerV1.java index b6070e3815868adc48a5c91007315892d5834abc..80a6babfef07d79f8d161f3589ca39db776d5160 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/ValidationPdusMakerV1.java +++ b/src/edu/nps/moves/dis7/utilities/stream/ValidationPdusMakerV1.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package edu.nps.moves.dis7.utilities.stream; import edu.nps.moves.dis7.enumerations.Country; @@ -10,7 +5,6 @@ import edu.nps.moves.dis7.utilities.DisNetworking; import edu.nps.moves.dis7.utilities.PduFactory; import edu.nps.moves.dis7.*; import java.io.File; -import java.io.IOException; import java.nio.file.Path; /** diff --git a/src/edu/nps/moves/dis7/utilities/stream/X3dCreateInterpolators.java b/src/edu/nps/moves/dis7/utilities/stream/X3dCreateInterpolators.java index f4cff4359c71f811e26f3274c0a7b458a3051d15..b4e82ce51b81ccf9edb16785ff8740d86c46f210 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/X3dCreateInterpolators.java +++ b/src/edu/nps/moves/dis7/utilities/stream/X3dCreateInterpolators.java @@ -13,8 +13,8 @@ import java.nio.ByteBuffer; import java.text.NumberFormat; import java.util.LinkedHashMap; import java.util.Locale; +import java.util.Map; import java.util.Set; -import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -58,9 +58,7 @@ public class X3dCreateInterpolators { //PDU Factory PduFactory pduFactory = new PduFactory(); - Pdu localPdu = null; - - localPdu = pduFactory.createPdu(bufferShort); + Pdu localPdu = pduFactory.createPdu(bufferShort); // ToDO figure out how to do this! makeEntityStatePDU EntityStatePdu localEspdu = pduFactory.makeEntityStatePdu(); @@ -72,21 +70,21 @@ public class X3dCreateInterpolators { Logger.getLogger(X3dCreateInterpolators.class.getName()).log(Level.SEVERE, null, ex); } - double localTimeStamp = 0; - double localX = 0; - double localY = 0; - double localZ = 0; + double localTimeStamp; + double localX; + double localY; + double localZ; - double localPhi = 0; - double localPsi = 0; - double localTheta = 0; + double localPhi; + double localPsi; + double localTheta; //Store the first timestamp to subtract it from all others //Same with X,Y,Z to create a local coordiante system if (firstTimeStamp) { firstLocalTimeStamp = localPdu.getTimestamp(); - localTimeStamp = localPdu.getTimestamp(); +// localTimeStamp = localPdu.getTimestamp(); firstLocalX = localEspdu.getEntityLocation().getX(); firstLocalY = localEspdu.getEntityLocation().getZ(); firstLocalZ = -1 * localEspdu.getEntityLocation().getY(); @@ -117,7 +115,7 @@ public class X3dCreateInterpolators { //ToDo: Add support for multiple Entities if ((localPdu.getPduType() != null) && (localPdu.getPduType() == DISPDUType.ENTITY_STATE)) { - testMap.put((double) localTimeStamp, new X3dCoordinates(localX, localY, localZ, localPhi, localPsi, localTheta)); + testMap.put(localTimeStamp, new X3dCoordinates(localX, localY, localZ, localPhi, localPsi, localTheta)); } } @@ -130,10 +128,8 @@ public class X3dCreateInterpolators { //Remove all collinear points. X3dSlidingWindowCompression slidingWindowCompression = new X3dSlidingWindowCompression(testMap); - TreeMap<Double, X3dCoordinates> returnMap = new TreeMap<>(); - //To turn of the compression just comment the next line out and the very next in. - returnMap = slidingWindowCompression.doSlidingWindow(); + Map<Double, X3dCoordinates> returnMap = slidingWindowCompression.doSlidingWindow(); //returnMap.putAll(testMap); //Writing all values from the KeyMap to a proper Position Interpolator String diff --git a/src/edu/nps/moves/dis7/utilities/stream/X3dCreateLineSet.java b/src/edu/nps/moves/dis7/utilities/stream/X3dCreateLineSet.java index 344b9beadac5dffdd5c0580d067a0141751f8e2c..151153ac2623f143a935ba83bdeb982c247261af 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/X3dCreateLineSet.java +++ b/src/edu/nps/moves/dis7/utilities/stream/X3dCreateLineSet.java @@ -13,8 +13,8 @@ import java.nio.ByteBuffer; import java.text.NumberFormat; import java.util.LinkedHashMap; import java.util.Locale; +import java.util.Map; import java.util.Set; -import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -52,71 +52,67 @@ public class X3dCreateLineSet { this.bufferShort = localBufferShort.clone(); if (bufferShort[2] == 1) { - - //PDU Factory - PduFactory pduFactory = new PduFactory(); - Pdu localPdu = null; - - localPdu = pduFactory.createPdu(bufferShort); - - // ToDO figure out how to do this! makeEntityStatePDU - EntityStatePdu localEspdu = pduFactory.makeEntityStatePdu(); - //Put all the data we need into the localEspdu - ByteBuffer espduBuffer = ByteBuffer.wrap(bufferShort); - try { - localEspdu.unmarshal(espduBuffer); - } catch (Exception ex) { - Logger.getLogger(X3dCreateLineSet.class.getName()).log(Level.SEVERE, null, ex); - } - double localTimeStamp = 0; - double localX = 0; - double localY = 0; - double localZ = 0; + //PDU Factory + PduFactory pduFactory = new PduFactory(); + Pdu localPdu = pduFactory.createPdu(bufferShort); + + // ToDO figure out how to do this! makeEntityStatePDU + EntityStatePdu localEspdu = pduFactory.makeEntityStatePdu(); + //Put all the data we need into the localEspdu + ByteBuffer espduBuffer = ByteBuffer.wrap(bufferShort); + try { + localEspdu.unmarshal(espduBuffer); + } catch (Exception ex) { + Logger.getLogger(X3dCreateLineSet.class.getName()).log(Level.SEVERE, null, ex); + } - //TimeStamps for a lineSet is not needed but copied from X3DInterpolators to use them as key for the hashmap - // and the standard compression class can be used - //Store the first timestamp to subtract it from all others - //Same with X,Y,Z to create a local coordiante system - if (firstTimeStamp) { + double localTimeStamp; + double localX; + double localY; + double localZ; - firstLocalTimeStamp = localPdu.getTimestamp(); - localTimeStamp = localPdu.getTimestamp(); - firstLocalX = localEspdu.getEntityLocation().getX(); - firstLocalY = localEspdu.getEntityLocation().getZ(); - firstLocalZ = -1*localEspdu.getEntityLocation().getY(); + //TimeStamps for a lineSet is not needed but copied from X3DInterpolators to use them as key for the hashmap + // and the standard compression class can be used + //Store the first timestamp to subtract it from all others + //Same with X,Y,Z to create a local coordiante system + if (firstTimeStamp) { - firstTimeStamp = false; - } + firstLocalTimeStamp = localPdu.getTimestamp(); +// localTimeStamp = localPdu.getTimestamp(); + firstLocalX = localEspdu.getEntityLocation().getX(); + firstLocalY = localEspdu.getEntityLocation().getZ(); + firstLocalZ = -1 * localEspdu.getEntityLocation().getY(); - localTimeStamp = localPdu.getTimestamp(); - localX = localEspdu.getEntityLocation().getX(); - localY = localEspdu.getEntityLocation().getZ(); - localZ = -1*localEspdu.getEntityLocation().getY(); + firstTimeStamp = false; + } - //Debug for printing X,Y,Z - //System.out.println(localX + " " + localY + " " + localZ); - - localTimeStamp = localTimeStamp - firstLocalTimeStamp; - localX = localX - firstLocalX; - localY = localY - firstLocalY; - localZ = localZ - firstLocalZ; + localTimeStamp = localPdu.getTimestamp(); + localX = localEspdu.getEntityLocation().getX(); + localY = localEspdu.getEntityLocation().getZ(); + localZ = -1 * localEspdu.getEntityLocation().getY(); - //Divide TimeStamp by 1,300,000 to get something close to a second per Unit. - //According to the DIS standard one tick is 3600/(2^31) seconds ~ 1.6764 µs - //1,300,000 was derived from a stream that is 61 seconds long. The number was adjusted to get a timesensor with 61 seconds - //ToDo find the real conversion between TimeStampDelta and seconds - localTimeStamp = localTimeStamp / 1300000; + //Debug for printing X,Y,Z + //System.out.println(localX + " " + localY + " " + localZ); + localTimeStamp = localTimeStamp - firstLocalTimeStamp; + localX = localX - firstLocalX; + localY = localY - firstLocalY; + localZ = localZ - firstLocalZ; - //Only add to stream if it is an ESPDU - //ToDo: Add support for multiple Entities - if ((localPdu.getPduType() != null) && (localPdu.getPduType() == DISPDUType.ENTITY_STATE)) { + //Divide TimeStamp by 1,300,000 to get something close to a second per Unit. + //According to the DIS standard one tick is 3600/(2^31) seconds ~ 1.6764 µs + //1,300,000 was derived from a stream that is 61 seconds long. The number was adjusted to get a timesensor with 61 seconds + //ToDo find the real conversion between TimeStampDelta and seconds + localTimeStamp = localTimeStamp / 1300000; - testMap.put((double) localTimeStamp, new X3dCoordinates(localX, localY, localZ, 0.0, 0.0, 0.0)); + //Only add to stream if it is an ESPDU + //ToDo: Add support for multiple Entities + if ((localPdu.getPduType() != null) && (localPdu.getPduType() == DISPDUType.ENTITY_STATE)) { - } - } + testMap.put(localTimeStamp, new X3dCoordinates(localX, localY, localZ, 0.0, 0.0, 0.0)); + } + } } public void makeX3dLineSet() { @@ -125,10 +121,8 @@ public class X3dCreateLineSet { //Remove all collinear points. X3dSlidingWindowCompression regression = new X3dSlidingWindowCompression(testMap); - TreeMap<Double, X3dCoordinates> returnMap = new TreeMap<>(); - //To turn of the compression just comment the next line out and the very next in. - returnMap = regression.doSlidingWindow(); + Map<Double, X3dCoordinates> returnMap = regression.doSlidingWindow(); //returnMap.putAll(testMap); //Writing all values from the KeyMap to a proper Position Interpolator String diff --git a/src/edu/nps/moves/dis7/utilities/stream/X3dSlidingWindowCompression.java b/src/edu/nps/moves/dis7/utilities/stream/X3dSlidingWindowCompression.java index c966aaffb12a55e43ea1b692dfa6f788e7a85350..ec7b6a93969e99c119db3ebae919916ba3c1cfdf 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/X3dSlidingWindowCompression.java +++ b/src/edu/nps/moves/dis7/utilities/stream/X3dSlidingWindowCompression.java @@ -5,6 +5,7 @@ import static java.lang.Math.sqrt; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.TreeMap; @@ -14,7 +15,7 @@ import java.util.TreeMap; */ public class X3dSlidingWindowCompression { - private LinkedHashMap<Double, X3dCoordinates> localMap; + private Map<Double, X3dCoordinates> localMap; public X3dSlidingWindowCompression(LinkedHashMap<Double, X3dCoordinates> localHashMap) { @@ -25,7 +26,7 @@ public class X3dSlidingWindowCompression { } } - public TreeMap<Double, X3dCoordinates> doSlidingWindow() { + public Map<Double, X3dCoordinates> doSlidingWindow() { System.out.println("DISTools.Regression.doRegression()"); //Check whether points could be deleted to compress the stream @@ -33,35 +34,35 @@ public class X3dSlidingWindowCompression { TreeMap<Double, X3dCoordinates> streamMap = new TreeMap<>(); //Copy LinkedHashMap into TreeMap to be able to pull the first element. streamMap.putAll(localMap); - TreeMap<Double, X3dCoordinates> returnMap = new TreeMap<>(); + Map<Double, X3dCoordinates> returnMap = new TreeMap<>(); //TreeMap of slidingWindows will store all of the points that are currently processed //use .pullFirstEntry() to get rid of the points at the beginning. - TreeMap<Double, X3dCoordinates> slidingWindow = new TreeMap<>(); - + Map<Double, X3dCoordinates> slidingWindow = new TreeMap<>(); + + Set<Double> slidingWindowKeys; + /** List of Times in Seconds */ + List<Double> timestampList = new ArrayList<>(); + /** List of x coordinates in meters*/ + List<Double> xList = new ArrayList<>(); + /** List of y coordinates in meters*/ + List<Double> yList = new ArrayList<>(); + /** List of z coordinates in meters*/ + List<Double> zList = new ArrayList<>(); + /** List of angle phi in radians*/ + List<Double> phiList = new ArrayList<>(); + /** List of angle psi in radians*/ + List<Double> psiList = new ArrayList<>(); + /** List of angle theta in radians*/ + List<Double> thetaList = new ArrayList<>(); while (streamMap.size() > 0) { slidingWindow.put(streamMap.firstEntry().getKey(), streamMap.get(streamMap.firstEntry().getKey())); streamMap.pollFirstEntry(); //Calculate the mean and SD - Set<Double> slidingWindowKeys = slidingWindow.keySet(); + slidingWindowKeys = slidingWindow.keySet(); if (slidingWindow.size() >= 3) { - /** List of Times in Seconds */ - List<Double> timestampList = new ArrayList<>(); - /** List of x coordinates in meters*/ - List<Double> xList = new ArrayList<>(); - /** List of y coordinates in meters*/ - List<Double> yList = new ArrayList<>(); - /** List of z coordinates in meters*/ - List<Double> zList = new ArrayList<>(); - /** List of angle phi in radians*/ - List<Double> phiList = new ArrayList<>(); - /** List of angle psi in radians*/ - List<Double> psiList = new ArrayList<>(); - /** List of angle theta in radians*/ - List<Double> thetaList = new ArrayList<>(); - Double[] k = new Double[slidingWindowKeys.size()]; slidingWindowKeys.toArray(k); @@ -79,7 +80,8 @@ public class X3dSlidingWindowCompression { } //Calculate Area of Triangle - //Credit: http://www.ambrsoft.com/TrigoCalc/Line3D/LineColinear.htm + //Credit: http://www.ambrsoft.com/TrigoCalc/Line3D/LineColinear.htm; + X3dCoordinates firstPoint, lastPoint; for (int i = 0; i < slidingWindow.size(); i++) { double a = sqrt(pow(xList.get(1) - xList.get(0), 2) + pow(yList.get(1) - yList.get(0), 2) + pow(zList.get(1) - zList.get(0), 2)); @@ -91,7 +93,7 @@ public class X3dSlidingWindowCompression { //Threshold can be adjusted (areaA) if ((areaA >= 0.1) || (timestampList.get(i) - timestampList.get(0) >= 4.0)) { //grab the first and the last point from the sliding window and push it to the returnMap - X3dCoordinates firstPoint = new X3dCoordinates(); + firstPoint = new X3dCoordinates(); firstPoint.setX(xList.get(0)); firstPoint.setY(yList.get(0)); firstPoint.setZ(zList.get(0)); @@ -99,7 +101,7 @@ public class X3dSlidingWindowCompression { firstPoint.setPsi(psiList.get(0)); firstPoint.setTheta(thetaList.get(0)); - X3dCoordinates lastPoint = new X3dCoordinates(xList.get(i), yList.get(i), zList.get(i), phiList.get(i), psiList.get(i), thetaList.get(i)); + lastPoint = new X3dCoordinates(xList.get(i), yList.get(i), zList.get(i), phiList.get(i), psiList.get(i), thetaList.get(i)); returnMap.put(timestampList.get(0), firstPoint); returnMap.put(timestampList.get(i), lastPoint); @@ -121,8 +123,9 @@ public class X3dSlidingWindowCompression { //System.out.println("StreamMap empty. All points left will be added. Break"); //grab the first and the last point from the siding window and push it to the returnMap + X3dCoordinates leftPoints; for (int j = 0; j < slidingWindow.size(); j++) { - X3dCoordinates leftPoints = new X3dCoordinates(xList.get(j), yList.get(j), zList.get(j), phiList.get(j), psiList.get(j), thetaList.get(j)); + leftPoints = new X3dCoordinates(xList.get(j), yList.get(j), zList.get(j), phiList.get(j), psiList.get(j), thetaList.get(j)); returnMap.put(timestampList.get(j), leftPoints); }