Skip to content
Snippets Groups Projects
Commit df0f13b5 authored by Terry D. Norbraten's avatar Terry D. Norbraten
Browse files

various object creations in loops, correct Exceptions

parent a2efbb4b
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import static edu.nps.moves.dis7.utilities.stream.PduRecorder.COMMENT_MARKER; ...@@ -6,6 +6,7 @@ import static edu.nps.moves.dis7.utilities.stream.PduRecorder.COMMENT_MARKER;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException;
import java.util.Base64; import java.util.Base64;
/** /**
...@@ -125,7 +126,7 @@ public class LogCompare ...@@ -125,7 +126,7 @@ public class LogCompare
System.out.println("End of compare. There were " + (goodmatch ? "no" : "one or more ") + "errors"); System.out.println("End of compare. There were " + (goodmatch ? "no" : "one or more ") + "errors");
System.exit(goodmatch ? 0 : 1); System.exit(goodmatch ? 0 : 1);
} }
catch (Exception ex) { catch (IOException ex) {
System.err.println("Exception reading pdu logs: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage()); System.err.println("Exception reading pdu logs: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage());
System.exit(1); System.exit(1);
} }
......
/*
* 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; package edu.nps.moves.dis7.utilities.stream;
import edu.nps.moves.dis7.enumerations.Country; import edu.nps.moves.dis7.enumerations.Country;
...@@ -10,7 +5,6 @@ import edu.nps.moves.dis7.utilities.DisNetworking; ...@@ -10,7 +5,6 @@ import edu.nps.moves.dis7.utilities.DisNetworking;
import edu.nps.moves.dis7.utilities.PduFactory; import edu.nps.moves.dis7.utilities.PduFactory;
import edu.nps.moves.dis7.*; import edu.nps.moves.dis7.*;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
/** /**
......
...@@ -13,8 +13,8 @@ import java.nio.ByteBuffer; ...@@ -13,8 +13,8 @@ import java.nio.ByteBuffer;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -58,9 +58,7 @@ public class X3dCreateInterpolators { ...@@ -58,9 +58,7 @@ public class X3dCreateInterpolators {
//PDU Factory //PDU Factory
PduFactory pduFactory = new PduFactory(); PduFactory pduFactory = new PduFactory();
Pdu localPdu = null; Pdu localPdu = pduFactory.createPdu(bufferShort);
localPdu = pduFactory.createPdu(bufferShort);
// ToDO figure out how to do this! makeEntityStatePDU // ToDO figure out how to do this! makeEntityStatePDU
EntityStatePdu localEspdu = pduFactory.makeEntityStatePdu(); EntityStatePdu localEspdu = pduFactory.makeEntityStatePdu();
...@@ -72,21 +70,21 @@ public class X3dCreateInterpolators { ...@@ -72,21 +70,21 @@ public class X3dCreateInterpolators {
Logger.getLogger(X3dCreateInterpolators.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(X3dCreateInterpolators.class.getName()).log(Level.SEVERE, null, ex);
} }
double localTimeStamp = 0; double localTimeStamp;
double localX = 0; double localX;
double localY = 0; double localY;
double localZ = 0; double localZ;
double localPhi = 0; double localPhi;
double localPsi = 0; double localPsi;
double localTheta = 0; double localTheta;
//Store the first timestamp to subtract it from all others //Store the first timestamp to subtract it from all others
//Same with X,Y,Z to create a local coordiante system //Same with X,Y,Z to create a local coordiante system
if (firstTimeStamp) { if (firstTimeStamp) {
firstLocalTimeStamp = localPdu.getTimestamp(); firstLocalTimeStamp = localPdu.getTimestamp();
localTimeStamp = localPdu.getTimestamp(); // localTimeStamp = localPdu.getTimestamp();
firstLocalX = localEspdu.getEntityLocation().getX(); firstLocalX = localEspdu.getEntityLocation().getX();
firstLocalY = localEspdu.getEntityLocation().getZ(); firstLocalY = localEspdu.getEntityLocation().getZ();
firstLocalZ = -1 * localEspdu.getEntityLocation().getY(); firstLocalZ = -1 * localEspdu.getEntityLocation().getY();
...@@ -117,7 +115,7 @@ public class X3dCreateInterpolators { ...@@ -117,7 +115,7 @@ public class X3dCreateInterpolators {
//ToDo: Add support for multiple Entities //ToDo: Add support for multiple Entities
if ((localPdu.getPduType() != null) && (localPdu.getPduType() == DISPDUType.ENTITY_STATE)) { 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 { ...@@ -130,10 +128,8 @@ public class X3dCreateInterpolators {
//Remove all collinear points. //Remove all collinear points.
X3dSlidingWindowCompression slidingWindowCompression = new X3dSlidingWindowCompression(testMap); 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. //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); //returnMap.putAll(testMap);
//Writing all values from the KeyMap to a proper Position Interpolator String //Writing all values from the KeyMap to a proper Position Interpolator String
......
...@@ -13,8 +13,8 @@ import java.nio.ByteBuffer; ...@@ -13,8 +13,8 @@ import java.nio.ByteBuffer;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -52,71 +52,67 @@ public class X3dCreateLineSet { ...@@ -52,71 +52,67 @@ public class X3dCreateLineSet {
this.bufferShort = localBufferShort.clone(); this.bufferShort = localBufferShort.clone();
if (bufferShort[2] == 1) { 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; //PDU Factory
double localX = 0; PduFactory pduFactory = new PduFactory();
double localY = 0; Pdu localPdu = pduFactory.createPdu(bufferShort);
double localZ = 0;
// 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 double localTimeStamp;
// and the standard compression class can be used double localX;
//Store the first timestamp to subtract it from all others double localY;
//Same with X,Y,Z to create a local coordiante system double localZ;
if (firstTimeStamp) {
firstLocalTimeStamp = localPdu.getTimestamp(); //TimeStamps for a lineSet is not needed but copied from X3DInterpolators to use them as key for the hashmap
localTimeStamp = localPdu.getTimestamp(); // and the standard compression class can be used
firstLocalX = localEspdu.getEntityLocation().getX(); //Store the first timestamp to subtract it from all others
firstLocalY = localEspdu.getEntityLocation().getZ(); //Same with X,Y,Z to create a local coordiante system
firstLocalZ = -1*localEspdu.getEntityLocation().getY(); 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(); firstTimeStamp = false;
localX = localEspdu.getEntityLocation().getX(); }
localY = localEspdu.getEntityLocation().getZ();
localZ = -1*localEspdu.getEntityLocation().getY();
//Debug for printing X,Y,Z localTimeStamp = localPdu.getTimestamp();
//System.out.println(localX + " " + localY + " " + localZ); localX = localEspdu.getEntityLocation().getX();
localY = localEspdu.getEntityLocation().getZ();
localTimeStamp = localTimeStamp - firstLocalTimeStamp; localZ = -1 * localEspdu.getEntityLocation().getY();
localX = localX - firstLocalX;
localY = localY - firstLocalY;
localZ = localZ - firstLocalZ;
//Divide TimeStamp by 1,300,000 to get something close to a second per Unit. //Debug for printing X,Y,Z
//According to the DIS standard one tick is 3600/(2^31) seconds ~ 1.6764 µs //System.out.println(localX + " " + localY + " " + localZ);
//1,300,000 was derived from a stream that is 61 seconds long. The number was adjusted to get a timesensor with 61 seconds localTimeStamp = localTimeStamp - firstLocalTimeStamp;
//ToDo find the real conversion between TimeStampDelta and seconds localX = localX - firstLocalX;
localTimeStamp = localTimeStamp / 1300000; localY = localY - firstLocalY;
localZ = localZ - firstLocalZ;
//Only add to stream if it is an ESPDU //Divide TimeStamp by 1,300,000 to get something close to a second per Unit.
//ToDo: Add support for multiple Entities //According to the DIS standard one tick is 3600/(2^31) seconds ~ 1.6764 µs
if ((localPdu.getPduType() != null) && (localPdu.getPduType() == DISPDUType.ENTITY_STATE)) { //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() { public void makeX3dLineSet() {
...@@ -125,10 +121,8 @@ public class X3dCreateLineSet { ...@@ -125,10 +121,8 @@ public class X3dCreateLineSet {
//Remove all collinear points. //Remove all collinear points.
X3dSlidingWindowCompression regression = new X3dSlidingWindowCompression(testMap); 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. //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); //returnMap.putAll(testMap);
//Writing all values from the KeyMap to a proper Position Interpolator String //Writing all values from the KeyMap to a proper Position Interpolator String
......
...@@ -5,6 +5,7 @@ import static java.lang.Math.sqrt; ...@@ -5,6 +5,7 @@ import static java.lang.Math.sqrt;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -14,7 +15,7 @@ import java.util.TreeMap; ...@@ -14,7 +15,7 @@ import java.util.TreeMap;
*/ */
public class X3dSlidingWindowCompression { public class X3dSlidingWindowCompression {
private LinkedHashMap<Double, X3dCoordinates> localMap; private Map<Double, X3dCoordinates> localMap;
public X3dSlidingWindowCompression(LinkedHashMap<Double, X3dCoordinates> localHashMap) { public X3dSlidingWindowCompression(LinkedHashMap<Double, X3dCoordinates> localHashMap) {
...@@ -25,7 +26,7 @@ public class X3dSlidingWindowCompression { ...@@ -25,7 +26,7 @@ public class X3dSlidingWindowCompression {
} }
} }
public TreeMap<Double, X3dCoordinates> doSlidingWindow() { public Map<Double, X3dCoordinates> doSlidingWindow() {
System.out.println("DISTools.Regression.doRegression()"); System.out.println("DISTools.Regression.doRegression()");
//Check whether points could be deleted to compress the stream //Check whether points could be deleted to compress the stream
...@@ -33,35 +34,35 @@ public class X3dSlidingWindowCompression { ...@@ -33,35 +34,35 @@ public class X3dSlidingWindowCompression {
TreeMap<Double, X3dCoordinates> streamMap = new TreeMap<>(); TreeMap<Double, X3dCoordinates> streamMap = new TreeMap<>();
//Copy LinkedHashMap into TreeMap to be able to pull the first element. //Copy LinkedHashMap into TreeMap to be able to pull the first element.
streamMap.putAll(localMap); 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 //TreeMap of slidingWindows will store all of the points that are currently processed
//use .pullFirstEntry() to get rid of the points at the beginning. //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) { while (streamMap.size() > 0) {
slidingWindow.put(streamMap.firstEntry().getKey(), streamMap.get(streamMap.firstEntry().getKey())); slidingWindow.put(streamMap.firstEntry().getKey(), streamMap.get(streamMap.firstEntry().getKey()));
streamMap.pollFirstEntry(); streamMap.pollFirstEntry();
//Calculate the mean and SD //Calculate the mean and SD
Set<Double> slidingWindowKeys = slidingWindow.keySet(); slidingWindowKeys = slidingWindow.keySet();
if (slidingWindow.size() >= 3) { 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()]; Double[] k = new Double[slidingWindowKeys.size()];
slidingWindowKeys.toArray(k); slidingWindowKeys.toArray(k);
...@@ -79,7 +80,8 @@ public class X3dSlidingWindowCompression { ...@@ -79,7 +80,8 @@ public class X3dSlidingWindowCompression {
} }
//Calculate Area of Triangle //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++) { 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)); 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 { ...@@ -91,7 +93,7 @@ public class X3dSlidingWindowCompression {
//Threshold can be adjusted (areaA) //Threshold can be adjusted (areaA)
if ((areaA >= 0.1) || (timestampList.get(i) - timestampList.get(0) >= 4.0)) { 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 //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.setX(xList.get(0));
firstPoint.setY(yList.get(0)); firstPoint.setY(yList.get(0));
firstPoint.setZ(zList.get(0)); firstPoint.setZ(zList.get(0));
...@@ -99,7 +101,7 @@ public class X3dSlidingWindowCompression { ...@@ -99,7 +101,7 @@ public class X3dSlidingWindowCompression {
firstPoint.setPsi(psiList.get(0)); firstPoint.setPsi(psiList.get(0));
firstPoint.setTheta(thetaList.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(0), firstPoint);
returnMap.put(timestampList.get(i), lastPoint); returnMap.put(timestampList.get(i), lastPoint);
...@@ -121,8 +123,9 @@ public class X3dSlidingWindowCompression { ...@@ -121,8 +123,9 @@ public class X3dSlidingWindowCompression {
//System.out.println("StreamMap empty. All points left will be added. Break"); //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 //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++) { 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); returnMap.put(timestampList.get(j), leftPoints);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment