diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/Coordinate.java b/jme3-desktop/src/main/java/jme3tools/navigation/Coordinate.java deleted file mode 100644 index 79a72f5712..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/Coordinate.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright (c) 2009-2018 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -import java.text.DecimalFormat; - -/** - * Coordinate class. Used to store a coordinate in [DD]D MM.M format. - * - * @author Benjamin Jakobus (based on JMarine by Benjamin Jakobus and Cormac Gebruers) - * @version 1.0 - * @since 1.0 - */ -public class Coordinate { - - /* the degree part of the position (+ N/E, -W/S) */ - private int deg; - - /* the decimals of a minute */ - private double minsDecMins; - - /* the coordinate as a decimal*/ - private double decCoordinate; - - /* whether this coordinate is a latitude or a longitude: : LAT==0, LONG==1 */ - private int coOrdinate; - - /* The minutes trailing decimal precision to use for positions */ - public static final int MINPRECISION = 4; - /* The degrees trailing decimal precision to use for positions */ - public static final int DEGPRECISION = 7; - - /* typeDefs for coOrdinates */ - public static final int LAT = 0; - public static final int LNG = 1; - - /* typeDefs for quadrant */ - public static final int E = 0; - public static final int S = 1; - public static final int W = 2; - public static final int N = 3; - - /** - * Constructor - * - * @param deg - * @param minsDecMins - * @param coOrdinate - * @param quad - * @throws InvalidPositionException - * @since 1.0 - */ - public Coordinate(int deg, float minsDecMins, int coOrdinate, - int quad) throws InvalidPositionException { - buildCoOrdinate(deg, minsDecMins, coOrdinate, quad); - if (verify()) { - } else { - throw new InvalidPositionException(); - } - } - - /** - * Constructor - * @param decCoordinate - * @param coOrdinate - * @throws InvalidPositionException - * @since 1.0 - */ - public Coordinate(double decCoordinate, int coOrdinate) throws InvalidPositionException { - DecimalFormat form = new DecimalFormat("#.#######"); - - this.decCoordinate = decCoordinate; - this.coOrdinate = coOrdinate; - if (verify()) { - deg = new Float(decCoordinate).intValue(); - if (deg < 0) { - minsDecMins = Double.parseDouble(form.format((Math.abs(decCoordinate) - Math.abs(deg)) * 60)); - } else { - minsDecMins = Double.parseDouble(form.format((decCoordinate - deg) * 60)); - } - } else { - throw new InvalidPositionException(); - } - } - - /** - * This constructor takes a coordinate in the ALRS formats i.e - * 38∞31.64'N for lat, and 28∞19.12'W for long - * Note: ALRS positions are occasionally written with the decimal minutes - * apostrophe in the 'wrong' place and with a non CP1252 compliant decimal character. - * This issue has to be corrected in the source database - * @param coOrdinate - * @throws InvalidPositionException - * @since 1.0 - */ - public Coordinate(String coOrdinate) throws InvalidPositionException { - //firstly split it into its component parts and dispose of the unneeded characters - String[] items = coOrdinate.split("°"); - int deg = Integer.valueOf(items[0]); - - items = items[1].split("'"); - float minsDecMins = Float.valueOf(items[0]); - char quad = items[1].charAt(0); - - switch (quad) { - case 'N': - buildCoOrdinate(deg, minsDecMins, Coordinate.LAT, Coordinate.N); - break; - case 'S': - buildCoOrdinate(deg, minsDecMins, Coordinate.LAT, Coordinate.S); - break; - case 'E': - buildCoOrdinate(deg, minsDecMins, Coordinate.LNG, Coordinate.E); - break; - case 'W': - buildCoOrdinate(deg, minsDecMins, Coordinate.LNG, Coordinate.W); - } - if (verify()) { - } else { - throw new InvalidPositionException(); - } - } - - /** - * Prints out a coordinate as a string - * @return the coordinate in decimal format - * @since 1.0 - */ - public String toStringDegMin() { - String str = ""; - String quad = ""; - StringUtil su = new StringUtil(); - switch (coOrdinate) { - case LAT: - if (decCoordinate >= 0) { - quad = "N"; - } else { - quad = "S"; - } - str = su.padNumZero(Math.abs(deg), 2); - str += "\u00b0" + su.padNumZero(Math.abs(minsDecMins), 2, MINPRECISION) + "'" + quad; - break; - case LNG: - if (decCoordinate >= 0) { - quad = "E"; - } else { - quad = "W"; - } - str = su.padNumZero(Math.abs(deg), 3); - str += "\u00b0" + su.padNumZero(Math.abs(minsDecMins), 2, MINPRECISION) + "'" + quad; - break; - } - return str; - } - - /** - * Prints out a coordinate as a string - * @return the coordinate in decimal format - * @since 1.0 - */ - public String toStringDec() { - StringUtil u = new StringUtil(); - switch (coOrdinate) { - case LAT: - return u.padNumZero(decCoordinate, 2, DEGPRECISION); - case LNG: - return u.padNumZero(decCoordinate, 3, DEGPRECISION); - } - return "error"; - } - - /** - * Returns the coordinate's decimal value - * @return float the decimal value of the coordinate - * @since 1.0 - */ - public double decVal() { - return decCoordinate; - } - - /** - * Determines whether a decimal position is valid - * @return result of validity test - * @since 1.0 - */ - private boolean verify() { - switch (coOrdinate) { - case LAT: - if (Math.abs(decCoordinate) > 90.0) { - return false; - } - break; - - case LNG: - if (Math.abs(decCoordinate) > 180) { - return false; - } - } - return true; - } - - /** - * Populate this object by parsing the arguments to the function - * Placed here to allow multiple constructors to use it - * @since 1.0 - */ - private void buildCoOrdinate(int deg, float minsDecMins, int coOrdinate, - int quad) { - NumUtil nu = new NumUtil(); - - switch (coOrdinate) { - case LAT: - switch (quad) { - case N: - this.deg = deg; - this.minsDecMins = minsDecMins; - this.coOrdinate = coOrdinate; - decCoordinate = nu.Round(this.deg + (float) this.minsDecMins / 60, Coordinate.MINPRECISION); - break; - - case S: - this.deg = -deg; - this.minsDecMins = minsDecMins; - this.coOrdinate = coOrdinate; - decCoordinate = nu.Round(this.deg - ((float) this.minsDecMins / 60), Coordinate.MINPRECISION); - } - - case LNG: - switch (quad) { - case E: - this.deg = deg; - this.minsDecMins = minsDecMins; - this.coOrdinate = coOrdinate; - decCoordinate = nu.Round(this.deg + ((float) this.minsDecMins / 60), Coordinate.MINPRECISION); - break; - - case W: - this.deg = -deg; - this.minsDecMins = minsDecMins; - this.coOrdinate = coOrdinate; - decCoordinate = nu.Round(this.deg - ((float) this.minsDecMins / 60), Coordinate.MINPRECISION); - } - } - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/GCSailing.java b/jme3-desktop/src/main/java/jme3tools/navigation/GCSailing.java deleted file mode 100644 index 5a5182ba2c..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/GCSailing.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -/** - * A utility class to package up a great circle sailing. - * - * @author Benjamin Jakobus, based on JMarine (by Cormac Gebruers and Benjamin - * Jakobus) - * - * @version 1.0 - * @since 1.0 - */ -public class GCSailing { - - private int[] courses; - private float[] distancesNM; - - public GCSailing(int[] pCourses, float[] pDistancesNM) { - courses = pCourses; - distancesNM = pDistancesNM; - } - - public int[] getCourses() { - return courses; - } - - public float[] getDistancesNM() { - return distancesNM; - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/InvalidPositionException.java b/jme3-desktop/src/main/java/jme3tools/navigation/InvalidPositionException.java deleted file mode 100644 index 77240ed406..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/InvalidPositionException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -/** - * - * @author normenhansen - */ -public class InvalidPositionException extends Exception{ - -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/MapModel2D.java b/jme3-desktop/src/main/java/jme3tools/navigation/MapModel2D.java deleted file mode 100644 index 2cd683a36d..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/MapModel2D.java +++ /dev/null @@ -1,394 +0,0 @@ -/* - * Copyright (c) 2009-2020 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -import java.awt.Point; -import java.text.DecimalFormat; - -/** - * A representation of the actual map in terms of lat/long and x,y co-ordinates. - * The Map class contains various helper methods such as methods for determining - * the pixel positions for lat/long co-ordinates and vice versa. - * - * @author Cormac Gebruers - * @author Benjamin Jakobus - * @version 1.0 - * @since 1.0 - */ -public class MapModel2D { - - /* The number of radians per degree */ - private final static double RADIANS_PER_DEGREE = 57.2957; - - /* The number of degrees per radian */ - private final static double DEGREES_PER_RADIAN = 0.0174532925; - - /* The map's width in longitude */ - public final static int DEFAULT_MAP_WIDTH_LONGITUDE = 360; - - /* The top right hand corner of the map */ - private Position centre; - - /* The x and y co-ordinates for the viewport's centre */ - private int xCentre; - private int yCentre; - - /* The width (in pixels) of the viewport holding the map */ - private int viewportWidth; - - /* The viewport height in pixels */ - private int viewportHeight; - - /* The number of minutes that one pixel represents */ - private double minutesPerPixel; - - /** - * Constructor - * @param viewportWidth the pixel width of the viewport (component) in which - * the map is displayed - * @since 1.0 - */ - public MapModel2D(int viewportWidth) { - try { - this.centre = new Position(0, 0); - } catch (InvalidPositionException e) { - e.printStackTrace(); - } - - this.viewportWidth = viewportWidth; - - // Calculate the number of minutes that one pixel represents along the longitude - calculateMinutesPerPixel(DEFAULT_MAP_WIDTH_LONGITUDE); - - // Calculate the viewport height based on its width and the number of degrees (85) - // in our map - viewportHeight = ((int) NavCalculator.computeDMPClarkeSpheroid(0, 85) / (int) minutesPerPixel) * 2; -// viewportHeight = viewportWidth; // REMOVE!!! - // Determine the map's x,y centre - xCentre = viewportWidth / 2; - yCentre = viewportHeight / 2; - } - - /** - * Returns the height of the viewport in pixels - * @return the height of the viewport in pixels - * @since 0.1 - */ - public int getViewportPixelHeight() { - return viewportHeight; - } - - /** - * Calculates the number of minutes per pixels using a given - * map width in longitude - * @param mapWidthInLongitude - * @since 1.0 - */ - public void calculateMinutesPerPixel(double mapWidthInLongitude) { - minutesPerPixel = (mapWidthInLongitude * 60) / viewportWidth; - } - - /** - * Returns the width of the viewport in pixels - * @return the width of the viewport in pixels - * @since 0.1 - */ - public int getViewportPixelWidth() { - return viewportWidth; - } - - public void setViewportWidth(int viewportWidth) { - this.viewportWidth = viewportWidth; - } - - public void setViewportHeight(int viewportHeight) { - this.viewportHeight = viewportHeight; - } - - public void setCentre(Position centre) { - this.centre = centre; - } - - /** - * Returns the number of minutes there are per pixel - * @return the number of minutes per pixel - * @since 1.0 - */ - public double getMinutesPerPixel() { - return minutesPerPixel; - } - - public double getMetersPerPixel() { - return 1853 * minutesPerPixel; - } - - public void setMinutesPerPixel(double minutesPerPixel) { - this.minutesPerPixel = minutesPerPixel; - } - - /** - * Converts a latitude/longitude position into a pixel co-ordinate - * @param position the position to convert - * @return {@code Point} a pixel co-ordinate - * @since 1.0 - */ - public Point toPixel(Position position) { - // Get the distance between position and the centre for calculating - // the position's longitude translation - double distance = NavCalculator.computeLongDiff(centre.getLongitude(), - position.getLongitude()); - - // Use the distance from the centre to calculate the pixel x co-ordinate - double distanceInPixels = (distance / minutesPerPixel); - - // Use the difference in meridional parts to calculate the pixel y co-ordinate - double dmp = NavCalculator.computeDMPClarkeSpheroid(centre.getLatitude(), - position.getLatitude()); - - int x = 0; - int y = 0; - - if (centre.getLatitude() == position.getLatitude()) { - y = yCentre; - } - if (centre.getLongitude() == position.getLongitude()) { - x = xCentre; - } - - // Distinguish between northern and southern hemisphere for latitude calculations - if (centre.getLatitude() > 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is north. Position is north of centre - y = yCentre + (int) ((dmp) / minutesPerPixel); - } else if (centre.getLatitude() > 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is north. Position is south of centre - y = yCentre - (int) ((dmp) / minutesPerPixel); - } else if (centre.getLatitude() < 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is south. Position is north of centre - y = yCentre + (int) ((dmp) / minutesPerPixel); - } else if (centre.getLatitude() < 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is south. Position is south of centre - y = yCentre - (int) ((dmp) / minutesPerPixel); - } else if (centre.getLatitude() == 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is at the equator. Position is north of the equator - y = yCentre + (int) ((dmp) / minutesPerPixel); - } else if (centre.getLatitude() == 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is at the equator. Position is south of the equator - y = yCentre - (int) ((dmp) / minutesPerPixel); - } - - // Distinguish between western and eastern hemisphere for longitude calculations - if (centre.getLongitude() < 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is west. Position is west of centre - x = xCentre - (int) distanceInPixels; - } else if (centre.getLongitude() < 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is west. Position is south of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() > 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is east. Position is west of centre - x = xCentre - (int) distanceInPixels; - } else if (centre.getLongitude() > 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is east. Position is east of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() == 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is at the equator. Position is east of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() == 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is at the equator. Position is west of centre - x = xCentre - (int) distanceInPixels; - } - - // Distinguish between northern and souterhn hemisphere for longitude calculations - return new Point(x, y); - } - - /** - * Converts a pixel position into a mercator position - * @param p {@link Point} object that you wish to convert into - * longitude / latiude - * @return the converted {@code Position} object - * @since 1.0 - */ - public Position toPosition(Point p) { - double lat, lon; - Position pos = null; - try { - Point pixelCentre = toPixel(new Position(0, 0)); - - // Get the distance between position and the centre - double xDistance = distance(xCentre, p.getX()); - double yDistance = distance(pixelCentre.getY(), p.getY()); - double lonDistanceInDegrees = (xDistance * minutesPerPixel) / 60; - double mp = (yDistance * minutesPerPixel); - // If we are zoomed in past a certain point, then use linear search. - // Otherwise use binary search - if (getMinutesPerPixel() < 0.05) { - lat = findLat(mp, getCentre().getLatitude()); - if (lat == -1000) { - System.out.println("lat: " + lat); - } - } else { - lat = findLat(mp, 0.0, 85.0); - } - lon = (p.getX() < xCentre ? centre.getLongitude() - lonDistanceInDegrees - : centre.getLongitude() + lonDistanceInDegrees); - - if (p.getY() > pixelCentre.getY()) { - lat = -1 * lat; - } - if (lat == -1000 || lon == -1000) { - return pos; - } - pos = new Position(lat, lon); - } catch (InvalidPositionException ipe) { - ipe.printStackTrace(); - } - return pos; - } - - /** - * Calculates distance between two points on the map in pixels - * @param a - * @param b - * @return distance the distance between a and b in pixels - * @since 1.0 - */ - private double distance(double a, double b) { - return Math.abs(a - b); - } - - /** - * Defines the centre of the map in pixels - * @param p Point object denoting the map's new centre - * @since 1.0 - */ - public void setCentre(Point p) { - try { - Position newCentre = toPosition(p); - if (newCentre != null) { - centre = newCentre; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Sets the map's xCentre - * @param xCentre - * @since 1.0 - */ - public void setXCentre(int xCentre) { - this.xCentre = xCentre; - } - - /** - * Sets the map's yCentre - * @param yCentre - * @since 1.0 - */ - public void setYCentre(int yCentre) { - this.yCentre = yCentre; - } - - /** - * Returns the pixel (x,y) centre of the map - * @return {@link Point} object marking the map's (x,y) centre - * @since 1.0 - */ - public Point getPixelCentre() { - return new Point(xCentre, yCentre); - } - - /** - * Returns the {@code Position} centre of the map - * @return {@code Position} object marking the map's (lat, long) centre - * @since 1.0 - */ - public Position getCentre() { - return centre; - } - - /** - * Uses binary search to find the latitude of a given MP. - * - * @param mp maridian part - * @param low - * @param high - * @return the latitude of the MP value - * @since 1.0 - */ - private double findLat(double mp, double low, double high) { - DecimalFormat form = new DecimalFormat("#.####"); - mp = Math.round(mp); - double midLat = (low + high) / 2.0; - // ctr is used to make sure that with some - // numbers which can't be represented exactly don't inifitely repeat - double guessMP = NavCalculator.computeDMPClarkeSpheroid(0, (float) midLat); - - while (low <= high) { - if (guessMP == mp) { - return midLat; - } else { - if (guessMP > mp) { - high = midLat - 0.0001; - } else { - low = midLat + 0.0001; - } - } - - midLat = Double.valueOf(form.format(((low + high) / 2.0))); - guessMP = NavCalculator.computeDMPClarkeSpheroid(0, (float) midLat); - guessMP = Math.round(guessMP); - } - return -1000; - } - - /** - * Uses linear search to find the latitude of a given MP - * @param mp the meridian part for which to find the latitude - * @param previousLat the previous latitude. Used as a upper / lower bound - * @return the latitude of the MP value - */ - private double findLat(double mp, double previousLat) { - DecimalFormat form = new DecimalFormat("#.#####"); - mp = Double.parseDouble(form.format(mp)); - double guessMP; - for (double lat = previousLat - 0.25; lat < previousLat + 1; lat += 0.00001) { - guessMP = NavCalculator.computeDMPClarkeSpheroid(0, lat); - guessMP = Double.parseDouble(form.format(guessMP)); - if (guessMP == mp || Math.abs(guessMP - mp) < 0.001) { - return lat; - } - } - return -1000; - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/MapModel3D.java b/jme3-desktop/src/main/java/jme3tools/navigation/MapModel3D.java deleted file mode 100644 index 3450da797b..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/MapModel3D.java +++ /dev/null @@ -1,416 +0,0 @@ -/* - * Copyright (c) 2009-2020 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -import com.jme3.math.Vector3f; -import java.text.DecimalFormat; - - -/** - * A representation of the actual map in terms of lat/long and x,y,z co-ordinates. - * The Map class contains various helper methods such as methods for determining - * the world unit positions for lat/long coordinates and vice versa. This map projection - * does not handle screen/pixel coordinates. - * - * @author Benjamin Jakobus (thanks to Cormac Gebruers) - * @version 1.0 - * @since 1.0 - */ -public class MapModel3D { - - /* The number of radians per degree */ - private final static double RADIANS_PER_DEGREE = 57.2957; - - /* The number of degrees per radian */ - private final static double DEGREES_PER_RADIAN = 0.0174532925; - - /* The map's width in longitude */ - public final static int DEFAULT_MAP_WIDTH_LONGITUDE = 360; - - /* The top right hand corner of the map */ - private Position centre; - - /* The x and y co-ordinates for the viewport's centre */ - private int xCentre; - private int zCentre; - - /* The width (in world units (wu)) of the viewport holding the map */ - private int worldWidth; - - /* The viewport height in pixels */ - private int worldHeight; - - /* The number of minutes that one pixel represents */ - private double minutesPerWorldUnit; - - /** - * Constructor. - * - * @param worldWidth The world unit width the map's area - * @since 1.0 - */ - public MapModel3D(int worldWidth) { - try { - this.centre = new Position(0, 0); - } catch (InvalidPositionException e) { - e.printStackTrace(); - } - - this.worldWidth = worldWidth; - - // Calculate the number of minutes that one pixel represents along the longitude - calculateMinutesPerWorldUnit(DEFAULT_MAP_WIDTH_LONGITUDE); - - // Calculate the viewport height based on its width and the number of degrees (85) - // in our map - worldHeight = ((int) NavCalculator.computeDMPClarkeSpheroid(0, 85) / (int) minutesPerWorldUnit) * 2; - - // Determine the map's x,y centre - xCentre = 0; - zCentre = 0; -// xCentre = worldWidth / 2; -// zCentre = worldHeight / 2; - } - - /** - * Returns the height of the viewport in pixels. - * - * @return The height of the viewport in pixels. - * @since 1.0 - */ - public int getWorldHeight() { - return worldHeight; - } - - /** - * Calculates the number of minutes per pixels using a given - * map width in longitude. - * - * @param mapWidthInLongitude The map's with in degrees of longitude. - * @since 1.0 - */ - public void calculateMinutesPerWorldUnit(double mapWidthInLongitude) { - // Multiply mapWidthInLongitude by 60 to convert it to minutes. - minutesPerWorldUnit = (mapWidthInLongitude * 60) / worldWidth; - } - - /** - * Returns the width of the viewport in pixels. - * - * @return The width of the viewport in pixels. - * @since 1.0 - */ - public int getWorldWidth() { - return worldWidth; - } - - /** - * Sets the world's desired width. - * - * @param viewportWidth The world's desired width in WU. - * @since 1.0 - */ - public void setWorldWidth(int viewportWidth) { - this.worldWidth = viewportWidth; - } - - /** - * Sets the world's desired height. - * - * @param viewportHeight The world's desired height in WU. - * @since 1.0 - */ - public void setWorldHeight(int viewportHeight) { - this.worldHeight = viewportHeight; - } - - /** - * Sets the map's centre. - * - * @param centre The Position denoting the map's - * desired centre. - * @since 1.0 - */ - public void setCentre(Position centre) { - this.centre = centre; - } - - /** - * Returns the number of minutes there are per WU. - * - * @return The number of minutes per WU. - * @since 1.0 - */ - public double getMinutesPerWu() { - return minutesPerWorldUnit; - } - - /** - * Returns the meters per WU. - * - * @return The meters per WU. - * @since 1.0 - */ - public double getMetersPerWu() { - return 1853 * minutesPerWorldUnit; - } - - /** - * Converts a latitude/longitude position into a WU coordinate. - * - * @param position The Position to convert. - * @return The Point a pixel coordinate. - * @since 1.0 - */ - public Vector3f toWorldUnit(Position position) { - // Get the difference between position and the centre for calculating - // the position's longitude translation - double distance = NavCalculator.computeLongDiff(centre.getLongitude(), - position.getLongitude()); - - // Use the difference from the centre to calculate the pixel x co-ordinate - double distanceInPixels = (distance / minutesPerWorldUnit); - - // Use the difference in meridional parts to calculate the pixel y co-ordinate - double dmp = NavCalculator.computeDMPClarkeSpheroid(centre.getLatitude(), - position.getLatitude()); - - int x = 0; - int z = 0; - - if (centre.getLatitude() == position.getLatitude()) { - z = zCentre; - } - if (centre.getLongitude() == position.getLongitude()) { - x = xCentre; - } - - // Distinguish between northern and southern hemisphere for latitude calculations - if (centre.getLatitude() > 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is north. Position is north of centre - z = zCentre - (int) ((dmp) / minutesPerWorldUnit); - } else if (centre.getLatitude() > 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is north. Position is south of centre - z = zCentre + (int) ((dmp) / minutesPerWorldUnit); - } else if (centre.getLatitude() < 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is south. Position is north of centre - z = zCentre - (int) ((dmp) / minutesPerWorldUnit); - } else if (centre.getLatitude() < 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is south. Position is south of centre - z = zCentre + (int) ((dmp) / minutesPerWorldUnit); - } else if (centre.getLatitude() == 0 && position.getLatitude() > centre.getLatitude()) { - // Centre is at the equator. Position is north of the equator - z = zCentre - (int) ((dmp) / minutesPerWorldUnit); - } else if (centre.getLatitude() == 0 && position.getLatitude() < centre.getLatitude()) { - // Centre is at the equator. Position is south of the equator - z = zCentre + (int) ((dmp) / minutesPerWorldUnit); - } - - // Distinguish between western and eastern hemisphere for longitude calculations - if (centre.getLongitude() < 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is west. Position is west of centre - x = xCentre - (int) distanceInPixels; - } else if (centre.getLongitude() < 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is west. Position is south of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() > 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is east. Position is west of centre - x = xCentre - (int) distanceInPixels; - } else if (centre.getLongitude() > 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is east. Position is east of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() == 0 && position.getLongitude() > centre.getLongitude()) { - // Centre is at the equator. Position is east of centre - x = xCentre + (int) distanceInPixels; - } else if (centre.getLongitude() == 0 && position.getLongitude() < centre.getLongitude()) { - // Centre is at the equator. Position is west of centre - x = xCentre - (int) distanceInPixels; - } - - // Distinguish between northern and southern hemisphere for longitude calculations - return new Vector3f(x, 0, z); - } - - /** - * Converts a world position into a Mercator position. - * - * @param posVec Vector containing the world unit - * coordinates that are to be converted into - * longitude / latitude coordinates. - * @return The resulting Position in degrees of - * latitude and longitude. - * @since 1.0 - */ - public Position toPosition(Vector3f posVec) { - double lat, lon; - Position pos = null; - try { - Vector3f worldCentre = toWorldUnit(new Position(0, 0)); - - // Get the difference between position and the centre - double xDistance = difference(xCentre, posVec.getX()); - double yDistance = difference(worldCentre.getZ(), posVec.getZ()); - double lonDistanceInDegrees = (xDistance * minutesPerWorldUnit) / 60; - double mp = (yDistance * minutesPerWorldUnit); - // If we are zoomed in past a certain point, then use linear search. - // Otherwise use binary search - if (getMinutesPerWu() < 0.05) { - lat = findLat(mp, getCentre().getLatitude()); - if (lat == -1000) { - System.out.println("lat: " + lat); - } - } else { - lat = findLat(mp, 0.0, 85.0); - } - lon = (posVec.getX() < xCentre ? centre.getLongitude() - lonDistanceInDegrees - : centre.getLongitude() + lonDistanceInDegrees); - - if (posVec.getZ() > worldCentre.getZ()) { - lat = -1 * lat; - } - if (lat == -1000 || lon == -1000) { - return pos; - } - pos = new Position(lat, lon); - } catch (InvalidPositionException ipe) { - ipe.printStackTrace(); - } - return pos; - } - - /** - * Calculates difference between two points on the map in WU. - * - * @param a - * @param b - * @return difference The difference between a and b in WU. - * @since 1.0 - */ - private double difference(double a, double b) { - return Math.abs(a - b); - } - - /** - * Defines the centre of the map in pixels. - * - * @param posVec Vector3f object denoting the map's new centre. - * @since 1.0 - */ - public void setCentre(Vector3f posVec) { - try { - Position newCentre = toPosition(posVec); - if (newCentre != null) { - centre = newCentre; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Returns the WU (x,y,z) centre of the map. - * - * @return Vector3f object marking the map's (x,y) centre. - * @since 1.0 - */ - public Vector3f getCentreWu() { - return new Vector3f(xCentre, 0, zCentre); - } - - /** - * Returns the Position centre of the map. - * - * @return Position object marking the map's (lat, long) - * centre. - * @since 1.0 - */ - public Position getCentre() { - return centre; - } - - /** - * Uses binary search to find the latitude of a given MP. - * - * @param mp Maridian part whose latitude to determine. - * @param low Minimum latitude bounds. - * @param high Maximum latitude bounds. - * @return The latitude of the MP value - * @since 1.0 - */ - private double findLat(double mp, double low, double high) { - DecimalFormat form = new DecimalFormat("#.####"); - mp = Math.round(mp); - double midLat = (low + high) / 2.0; - // ctr is used to make sure that with some - // numbers which can't be represented exactly don't inifitely repeat - double guessMP = NavCalculator.computeDMPClarkeSpheroid(0, (float) midLat); - - while (low <= high) { - if (guessMP == mp) { - return midLat; - } else { - if (guessMP > mp) { - high = midLat - 0.0001; - } else { - low = midLat + 0.0001; - } - } - - midLat = Double.valueOf(form.format(((low + high) / 2.0))); - guessMP = NavCalculator.computeDMPClarkeSpheroid(0, (float) midLat); - guessMP = Math.round(guessMP); - } - return -1000; - } - - /** - * Uses linear search to find the latitude of a given MP. - * - * @param mp The meridian part for which to find the latitude. - * @param previousLat The previous latitude. Used as a upper / lower bound. - * @return The latitude of the MP value. - * @since 1.0 - */ - private double findLat(double mp, double previousLat) { - DecimalFormat form = new DecimalFormat("#.#####"); - mp = Double.parseDouble(form.format(mp)); - double guessMP; - for (double lat = previousLat - 0.25; lat < previousLat + 1; lat += 0.00001) { - guessMP = NavCalculator.computeDMPClarkeSpheroid(0, lat); - guessMP = Double.parseDouble(form.format(guessMP)); - if (guessMP == mp || Math.abs(guessMP - mp) < 0.05) { - return lat; - } - } - return -1000; - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/NavCalculator.java b/jme3-desktop/src/main/java/jme3tools/navigation/NavCalculator.java deleted file mode 100644 index e67247ae68..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/NavCalculator.java +++ /dev/null @@ -1,617 +0,0 @@ -/* - * Copyright (c) 2009-2019 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - - - -/** - * A utlity class for performing position calculations - * - * @author Benjamin Jakobus, based on JMarine (by Cormac Gebruers and Benjamin - * Jakobus) - * @version 1.0 - * @since 1.0 - */ -public class NavCalculator { - - private double distance; - private double trueCourse; - - /* The earth's radius in meters */ - public static final int WGS84_EARTH_RADIUS = 6378137; - private String strCourse; - - /* The sailing calculation type */ - public static final int MERCATOR = 0; - public static final int GC = 1; - - /* The degree precision to use for courses */ - public static final int RL_CRS_PRECISION = 1; - - /* The distance precision to use for distances */ - public static final int RL_DIST_PRECISION = 1; - public static final int METERS_PER_MINUTE = 1852; - - /** - * Constructor - * @param P1 - * @param P2 - * @param calcType - * @since 1.0 - */ - public NavCalculator(Position P1, Position P2, int calcType) { - switch (calcType) { - case MERCATOR: - mercatorSailing(P1, P2); - break; - case GC: - greatCircleSailing(P1, P2); - break; - } - } - - /** - * Constructor - * @since 1.0 - */ - public NavCalculator() { - } - - /** - * Determines a great circle track between two positions - * @param p1 origin position - * @param p2 destination position - */ - public GCSailing greatCircleSailing(Position p1, Position p2) { - return new GCSailing(new int[0], new float[0]); - } - - /** - * Determines a Rhumb Line course and distance between two points - * @param p1 origin position - * @param p2 destination position - */ - public RLSailing rhumbLineSailing(Position p1, Position p2) { - RLSailing rl = mercatorSailing(p1, p2); - return rl; - } - - /** - * Determines the rhumb line course and distance between two positions - * @param p1 origin position - * @param p2 destination position - */ - public RLSailing mercatorSailing(Position p1, Position p2) { - - double dLat = computeDLat(p1.getLatitude(), p2.getLatitude()); - //plane sailing... - if (dLat == 0) { - RLSailing rl = planeSailing(p1, p2); - return rl; - } - - double dLong = computeDLong(p1.getLongitude(), p2.getLongitude()); - double dmp = (float) computeDMPClarkeSpheroid(p1.getLatitude(), p2.getLatitude()); - - trueCourse = (float) Math.toDegrees(Math.atan(dLong / dmp)); - double degCrs = convertCourse((float) trueCourse, p1, p2); - distance = (float) Math.abs(dLat / Math.cos(Math.toRadians(trueCourse))); - - RLSailing rl = new RLSailing(degCrs, (float) distance); - trueCourse = rl.getCourse(); - strCourse = (dLat < 0 ? "S" : "N"); - strCourse += " " + trueCourse; - strCourse += " " + (dLong < 0 ? "W" : "E"); - return rl; - - } - - /** - * Calculate a plane sailing situation - i.e. where Lats are the same - * @param p1 - * @param p2 - * @return a new instance - * @since 1.0 - */ - public RLSailing planeSailing(Position p1, Position p2) { - double dLong = computeDLong(p1.getLongitude(), p2.getLongitude()); - - double sgnDLong = 0 - (dLong / Math.abs(dLong)); - if (Math.abs(dLong) > 180 * 60) { - dLong = (360 * 60 - Math.abs(dLong)) * sgnDLong; - } - - double redist = 0; - double recourse = 0; - if (p1.getLatitude() == 0) { - redist = Math.abs(dLong); - } else { - redist = Math.abs(dLong * (float) Math.cos(p1.getLatitude() * 2 * Math.PI / 360)); - } - recourse = (float) Math.asin(0 - sgnDLong); - recourse = recourse * 360 / 2 / (float) Math.PI; - - if (recourse < 0) { - recourse = recourse + 360; - } - return new RLSailing(recourse, redist); - } - - /** - * Converts a course from cardinal XddY to ddd notation - * @param tc - * @param p1 position one - * @param p2 position two - * @return angle (in degrees) - * @since 1.0 - */ - public static double convertCourse(float tc, Position p1, Position p2) { - - double dLat = p1.getLatitude() - p2.getLatitude(); - double dLong = p1.getLongitude() - p2.getLongitude(); - //NE - if (dLong >= 0 & dLat >= 0) { - return Math.abs(tc); - } - - //SE - if (dLong >= 0 & dLat < 0) { - return 180 - Math.abs(tc); - } - - //SW - if (dLong < 0 & dLat < 0) { - return 180 + Math.abs(tc); - } - - //NW - if (dLong < 0 & dLat >= 0) { - return 360 - Math.abs(tc); - } - return -1; - } - - /** - * Getter method for the distance between two points - * @return distance - * @since 1.0 - */ - public double getDistance() { - return distance; - } - - /** - * Getter method for the true course - * @return true course - * @since 1.0 - */ - public double getTrueCourse() { - return trueCourse; - } - - /** - * Getter method for the true course - * @return true course - * @since 1.0 - */ - public String getStrCourse() { - return strCourse; - } - - /** - * Computes the difference in meridional parts for two latitudes in minutes - * (based on Clark 1880 spheroid) - * @param lat1 - * @param lat2 - * @return difference in minutes - * @since 1.0 - */ - public static double computeDMPClarkeSpheroid(double lat1, double lat2) { - double absLat1 = Math.abs(lat1); - double absLat2 = Math.abs(lat2); - - double m1 = (7915.704468 * (Math.log(Math.tan(Math.toRadians(45 - + (absLat1 / 2)))) / Math.log(10)) - - 23.268932 * Math.sin(Math.toRadians(absLat1)) - - 0.052500 * Math.pow(Math.sin(Math.toRadians(absLat1)), 3) - - 0.000213 * Math.pow(Math.sin(Math.toRadians(absLat1)), 5)); - - double m2 = (7915.704468 * (Math.log(Math.tan(Math.toRadians(45 - + (absLat2 / 2)))) / Math.log(10)) - - 23.268932 * Math.sin(Math.toRadians(absLat2)) - - 0.052500 * Math.pow(Math.sin(Math.toRadians(absLat2)), 3) - - 0.000213 * Math.pow(Math.sin(Math.toRadians(absLat2)), 5)); - if ((lat1 <= 0 && lat2 <= 0) || (lat1 > 0 && lat2 > 0)) { - return Math.abs(m1 - m2); - } else { - return m1 + m2; - } - } - - /** - * Computes the difference in meridional parts for a perfect sphere between - * two degrees of latitude - * @param lat1 - * @param lat2 - * @return difference in meridional parts between lat1 and lat2 in minutes - * @since 1.0 - */ - public static float computeDMPWGS84Spheroid(float lat1, float lat2) { - float absLat1 = Math.abs(lat1); - float absLat2 = Math.abs(lat2); - - float m1 = (float) (7915.7045 * Math.log10(Math.tan(Math.toRadians(45 + (absLat1 / 2)))) - - 23.01358 * Math.sin(absLat1 - 0.05135) * Math.pow(Math.sin(absLat1), 3)); - - float m2 = (float) (7915.7045 * Math.log10(Math.tan(Math.toRadians(45 + (absLat2 / 2)))) - - 23.01358 * Math.sin(absLat2 - 0.05135) * Math.pow(Math.sin(absLat2), 3)); - - if (lat1 <= 0 & lat2 <= 0 || lat1 > 0 & lat2 > 0) { - return Math.abs(m1 - m2); - } else { - return m1 + m2; - } - } - - /** - * Predicts the position of a target for a given time in the future - * @param time the number of seconds from now for which to predict the future - * position - * @param speed the miles per minute that the target is traveling - * @param currentLat the target's current latitude - * @param currentLong the target's current longitude - * @param course the target's current course in degrees - * @return the predicted future position - * @since 1.0 - */ - public static Position predictPosition(int time, double speed, - double currentLat, double currentLong, double course) { - Position futurePosition = null; - course = Math.toRadians(course); - double futureLong = currentLong + speed * time * Math.sin(course); - double futureLat = currentLat + speed * time * Math.cos(course); - try { - futurePosition = new Position(futureLat, futureLong); - } catch (InvalidPositionException ipe) { - ipe.printStackTrace(); - } - return futurePosition; - - } - - /** - * Computes the coordinate of position B relative to an offset given - * a distance and an angle. - * - * @param initialPos - * @param heading - * @param distance The distance, in meters, between the offset - * and point B. - * @return The position of point B that is located from - * given offset at given distance and angle. - * @since 1.0 - */ - public static Position computePosition(Position initialPos, double heading, - double distance) { - if (initialPos == null) { - return null; - } - double angle; - if (heading < 90) { - angle = heading; - } else if (heading > 90 && heading < 180) { - angle = 180 - heading; - } else if (heading > 180 && heading < 270) { - angle = heading - 180; - } else { - angle = 360 - heading; - } - - Position newPosition = null; - - // Convert meters into nautical miles - distance = distance * 0.000539956803; - angle = Math.toRadians(angle); - double initialLat = initialPos.getLatitude(); - double initialLong = initialPos.getLongitude(); - double dlat = distance * Math.cos(angle); - dlat = dlat / 60; - dlat = Math.abs(dlat); - double newLat = 0; - if ((heading > 270 && heading < 360) || (heading > 0 && heading < 90)) { - newLat = initialLat + dlat; - } else if (heading < 270 && heading > 90) { - newLat = initialLat - dlat; - } - double meanLat = (Math.abs(dlat) / 2.0) + newLat; - double dep = (Math.abs(dlat * 60)) * Math.tan(angle); - double dlong = dep * (1.0 / Math.cos(Math.toRadians(meanLat))); - dlong = dlong / 60; - dlong = Math.abs(dlong); - double newLong; - if (heading > 180 && heading < 360) { - newLong = initialLong - dlong; - } else { - newLong = initialLong + dlong; - } - - if (newLong < -180) { - double diff = Math.abs(newLong + 180); - newLong = 180 - diff; - } - - if (newLong > 180) { - double diff = Math.abs(newLong + 180); - newLong = (180 - diff) * -1; - } - - if (heading == 0 || heading == 360 || heading == 180) { - newLong = initialLong; - newLat = initialLat + dlat; - } else if (heading == 90 || heading == 270) { - newLat = initialLat; -// newLong = initialLong + dlong; THIS WAS THE ORIGINAL (IT WORKED) - newLong = initialLong - dlong; - } - try { - newPosition = new Position(newLat, - newLong); - } catch (InvalidPositionException ipe) { - ipe.printStackTrace(); - System.out.println(newLat + "," + newLong); - } - return newPosition; - } - - /** - * Computes the difference in Longitude between two positions and assigns the - * correct sign -westwards travel, + eastwards travel - * @param lng1 - * @param lng2 - * @return difference in longitude - * @since 1.0 - */ - public static double computeDLong(double lng1, double lng2) { - if (lng1 - lng2 == 0) { - return 0; - } - - // both easterly - if (lng1 >= 0 & lng2 >= 0) { - return -(lng1 - lng2) * 60; - } - //both westerly - if (lng1 < 0 & lng2 < 0) { - return -(lng1 - lng2) * 60; - } - - //opposite sides of Date line meridian - - //sum less than 180 - if (Math.abs(lng1) + Math.abs(lng2) < 180) { - if (lng1 < 0 & lng2 > 0) { - return -(Math.abs(lng1) + Math.abs(lng2)) * 60; - } else { - return Math.abs(lng1) + Math.abs(lng2) * 60; - } - } else { - //sum greater than 180 - if (lng1 < 0 & lng2 > 0) { - return -(360 - (Math.abs(lng1) + Math.abs(lng2))) * 60; - } else { - return (360 - (Math.abs(lng1) + Math.abs(lng2))) * 60; - } - } - } - - /** - * Computes the difference in Longitude between two positions and assigns the - * correct sign -westwards travel, + eastwards travel - * @param lng1 - * @param lng2 - * @return difference in longitude - * @since 1.0 - */ - public static double computeLongDiff(double lng1, double lng2) { - if (lng1 - lng2 == 0) { - return 0; - } - - // both easterly - if (lng1 >= 0 & lng2 >= 0) { - return Math.abs(-(lng1 - lng2) * 60); - } - //both westerly - if (lng1 < 0 & lng2 < 0) { - return Math.abs(-(lng1 - lng2) * 60); - } - - if (lng1 == 0) { - return Math.abs(lng2 * 60); - } - - if (lng2 == 0) { - return Math.abs(lng1 * 60); - } - - return (Math.abs(lng1) + Math.abs(lng2)) * 60; - } - - /** - * Compute the difference in latitude between two positions - * @param lat1 - * @param lat2 - * @return difference in latitude - * @since 1.0 - */ - public static double computeDLat(double lat1, double lat2) { - //same side of equator - - //plane sailing - if (lat1 - lat2 == 0) { - return 0; - } - - //both northerly - if (lat1 >= 0 & lat2 >= 0) { - return -(lat1 - lat2) * 60; - } - //both southerly - if (lat1 < 0 & lat2 < 0) { - return -(lat1 - lat2) * 60; - } - - //opposite sides of equator - if (lat1 >= 0) { - //heading south - return -(Math.abs(lat1) + Math.abs(lat2)); - } else { - //heading north - return (Math.abs(lat1) + Math.abs(lat2)); - } - } - - public static class Quadrant { - - private static final Quadrant FIRST = new Quadrant(1, 1); - private static final Quadrant SECOND = new Quadrant(-1, 1); - private static final Quadrant THIRD = new Quadrant(-1, -1); - private static final Quadrant FOURTH = new Quadrant(1, -1); - private final int lonMultiplier; - private final int latMultiplier; - - public Quadrant(final int xMultiplier, final int yMultiplier) { - this.lonMultiplier = xMultiplier; - this.latMultiplier = yMultiplier; - } - - static Quadrant getQuadrant(double degrees, boolean invert) { - if (invert) { - if (degrees >= 0 && degrees <= 90) { - return FOURTH; - } else if (degrees > 90 && degrees <= 180) { - return THIRD; - } else if (degrees > 180 && degrees <= 270) { - return SECOND; - } - return FIRST; - } else { - if (degrees >= 0 && degrees <= 90) { - return FIRST; - } else if (degrees > 90 && degrees <= 180) { - return SECOND; - } else if (degrees > 180 && degrees <= 270) { - return THIRD; - } - return FOURTH; - } - } - } - - /** - * Converts meters to degrees. - * - * @param meters The meters that you want to convert into degrees. - * @return The degree equivalent of the given meters. - * @since 1.0 - */ - public static double toDegrees(double meters) { - return (meters / METERS_PER_MINUTE) / 60; - } - - /** - * Computes the bearing between two points. - * - * @param p1 - * @param p2 - * @return bearing (in degrees) - * @since 1.0 - */ - public static int computeBearing(Position p1, Position p2) { - int bearing; - double dLon = computeDLong(p1.getLongitude(), p2.getLongitude()); - double y = Math.sin(dLon) * Math.cos(p2.getLatitude()); - double x = Math.cos(p1.getLatitude()) * Math.sin(p2.getLatitude()) - - Math.sin(p1.getLatitude()) * Math.cos(p2.getLatitude()) * Math.cos(dLon); - bearing = (int) Math.toDegrees(Math.atan2(y, x)); - return bearing; - } - - /** - * Computes the angle between two points. - * - * @param p1 - * @param p2 - * @return angle (in degrees) - */ - public static int computeAngle(Position p1, Position p2) { - // cos (adj / hyp) - double adj = Math.abs(p1.getLongitude() - p2.getLongitude()); - double opp = Math.abs(p1.getLatitude() - p2.getLatitude()); - return (int) Math.toDegrees(Math.atan(opp / adj)); - -// int angle = (int)Math.atan2(p2.getLatitude() - p1.getLatitude(), -// p2.getLongitude() - p1.getLongitude()); - //Actually it's ATan2(dy , dx) where dy = y2 - y1 and dx = x2 - x1, or ATan(dy / dx) - } - - public static int computeHeading(Position p1, Position p2) { - int angle = computeAngle(p1, p2); - // NE - if (p2.getLongitude() >= p1.getLongitude() && p2.getLatitude() >= p1.getLatitude()) { - return angle; - } else if (p2.getLongitude() >= p1.getLongitude() && p2.getLatitude() <= p1.getLatitude()) { - // SE - return 90 + angle; - } else if (p2.getLongitude() <= p1.getLongitude() && p2.getLatitude() <= p1.getLatitude()) { - // SW - return 270 - angle; - } else { - // NW - return 270 + angle; - } - } - - public static void main(String[] args) { - try { - int pos = NavCalculator.computeHeading(new Position(0, 0), new Position(10, -10)); -// System.out.println(pos.getLatitude() + "," + pos.getLongitude()); - System.out.println(pos); - } catch (Exception e) { - } - - - - - - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/NumUtil.java b/jme3-desktop/src/main/java/jme3tools/navigation/NumUtil.java deleted file mode 100644 index 40f5ce0c35..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/NumUtil.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2009-2020 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -/** - * Provides various helper methods for number conversions (such as degree to radian - * conversion, decimal degree to radians etc) - * @author Benjamin Jakobus, based on JMarine (by Cormac Gebruers and Benjamin - * Jakobus) - * @version 1.0 - * @since 1.0 - */ -public class NumUtil { - - /** - * Rounds a number - * @param Rval number to be rounded - * @param Rpl number of decimal places - * @return rounded number - * @since 0.1 - */ - public float Round(float Rval, int Rpl) { - float p = (float) Math.pow(10, Rpl); - Rval = Rval * p; - float tmp = Math.round(Rval); - return tmp / p; - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/Position.java b/jme3-desktop/src/main/java/jme3tools/navigation/Position.java deleted file mode 100644 index 2de517facc..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/Position.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -/** - * This class represents the position of an entity in the world. - * - * @author Benjamin Jakobus (based on JMarine by Cormac Gebruers and Benjamin Jakobus) - * @version 1.0 - * @since 1.0 - */ -public class Position { - - /* the latitude (+ N/E) */ - private Coordinate lat; - - /* the longitude (-W/S) */ - private Coordinate lng; - - /* An optional time to associate with this position - for historical tracking */ - private String utcTimeStamp; - - /* Degree position */ - private double degree; - - /** - * A new position expressed in decimal format - * @param dblLat - * @param dblLng - * @since 1.0 - */ - public Position(double dblLat, double dblLng) throws InvalidPositionException { - lat = new Coordinate(dblLat, Coordinate.LAT); - lng = new Coordinate(dblLng, Coordinate.LNG); - } - - /** - * A new position expressed in decimal format and degrees - * @param dblLat - * @param dblLng - * @param degree - * @since 1.0 - */ -// public Position(double dblLat, double dblLng, double degree) throws InvalidPositionException { -// lat = new Coordinate(dblLat, Coordinate.LAT); -// lng = new Coordinate(dblLng, Coordinate.LNG); -// this.degree = degree; -// } - /** - * A new position expressed in DegMin format - * @param latDeg - * @param latMin - * @param lngDeg - * @param lngMin - * @since 1.0 - */ - public Position(int latDeg, float latMin, int latQuad, int lngDeg, - float lngMin, int lngQuad) throws InvalidPositionException { - lat = new Coordinate(latDeg, latMin, Coordinate.LAT, latQuad); - lng = new Coordinate(lngDeg, lngMin, Coordinate.LNG, lngQuad); - } - - /** - * A new position expressed in ALRS format - * @param lat - * @param lng - * @since 1.0 - */ - public Position(String lat, String lng) throws InvalidPositionException { - this.lat = new Coordinate(lat); - this.lng = new Coordinate(lng); - } - - /** - * A new position expressed in NMEA GPS message format: - * 4807.038,N,01131.000,E - * @param latNMEAGPS - * @param latQuad - * @param lngNMEAGPS - * @param lngQuad - * @param utcTimeStamp - * @since 12.0 - */ - public Position(String latNMEAGPS, String latQuad, String lngNMEAGPS, String lngQuad, String utcTimeStamp) { - int quad; - - //LAT - if (latQuad.compareTo("N") == 0) { - quad = Coordinate.N; - } else { - quad = Coordinate.S; - } - try { - this.lat = new Coordinate(Integer.valueOf(latNMEAGPS.substring(0, 2)), Float.valueOf(latNMEAGPS.substring(2)), Coordinate.LAT, quad); - } catch (InvalidPositionException e) { - e.printStackTrace(); - } - - //LNG - if (lngQuad.compareTo("E") == 0) { - quad = Coordinate.E; - } else { - quad = Coordinate.W; - } - try { - this.lng = new Coordinate(Integer.valueOf(lngNMEAGPS.substring(0, 3)), Float.valueOf(lngNMEAGPS.substring(3)), Coordinate.LNG, quad); - } catch (InvalidPositionException e) { - e.printStackTrace(); - } - - //TIMESTAMP - this.associateUTCTime(utcTimeStamp); - } - - /** - * Add a reference time for this position - useful for historical tracking - * @param data - * @since 1.0 - */ - public void associateUTCTime(String data) { - utcTimeStamp = data; - } - - /** - * Returns the UTC time stamp - * @return str the UTC timestamp - * @since 1.0 - */ - public String utcTimeStamp() { - return utcTimeStamp; - } - - /** - * Prints out position using decimal format - * @return the position in decimal format - */ - public String toStringDec() { - return lat.toStringDec() + " " + lng.toStringDec(); - } - - /** - * Return the position latitude in decimal format - * @return the latitude in decimal format - * @since 1.0 - */ - public double getLatitude() { - return lat.decVal(); - } - - /** - * Returns the degree of the entity - * @return degree - * @since 1.0 - */ -// public double getDegree() { -// return degree; -// } - /** - * Return the position longitude in decimal format - * @return the longitude in decimal format - * @since 1.0 - */ - public double getLongitude() { - return lng.decVal(); - } - - /** - * Prints out position using DegMin format - * @return the position in DegMin Format - * @since 1.0 - */ - public String toStringDegMin() { - String output = ""; - output += lat.toStringDegMin(); - output += " " + lng.toStringDegMin(); - return output; - } - - /** - * Prints out the position latitude - * @return the latitude as a string for display purposes - * @since 1.0 - */ - public String toStringDegMinLat() { - return lat.toStringDegMin(); - } - - /** - * Prints out the position longitude - * @return the longitude as a string for display purposes - * @since 1.0 - */ - public String toStringDegMinLng() { - return lng.toStringDegMin(); - } - - /** - * Prints out the position latitude - * @return the latitude as a string for display purposes - * @since 1.0 - */ - public String toStringDecLat() { - return lat.toStringDec(); - } - - /** - * Prints out the position longitude - * @return the longitude as a string for display purposes - * @since 1.0 - */ - public String toStringDecLng() { - return lng.toStringDec(); - } - - //TEST HARNESS - DO NOT DELETE! - public static void main(String[] argsc) { - - //NMEA GPS Position format: - Position p = new Position("4807.038", "N", "01131.000", "W", "123519"); - System.out.println(p.toStringDegMinLat()); - System.out.println(p.getLatitude()); - System.out.println(p.getLongitude()); - System.out.println(p.toStringDegMinLng()); - System.out.println(p.utcTimeStamp()); - - }//main -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/RLSailing.java b/jme3-desktop/src/main/java/jme3tools/navigation/RLSailing.java deleted file mode 100644 index c9d70b8e53..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/RLSailing.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -/** - * A utility class to package up a rhumb line sailing - * - * @author Benjamin Jakobus, based on JMarine (by Cormac Gebruers and Benjamin - * Jakobus) - * @version 1.0 - * @since 1.0 - */ -public class RLSailing { - - private double course; - private double distNM; - - public RLSailing(double pCourse, double pDistNM) { - course = pCourse; - distNM = pDistNM; - } - - public double getCourse() { - return course; - } - - public double getDistNM() { - return distNM; - } -} diff --git a/jme3-desktop/src/main/java/jme3tools/navigation/StringUtil.java b/jme3-desktop/src/main/java/jme3tools/navigation/StringUtil.java deleted file mode 100644 index 873e6087bb..0000000000 --- a/jme3-desktop/src/main/java/jme3tools/navigation/StringUtil.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package jme3tools.navigation; - -import java.util.regex.Pattern; - -/** - * A collection of String utilities. - * - * @author Benjamin Jakobus - * @version 1.0 - */ -public class StringUtil { - - /** - * Splits a newline (\n) delimited string into an array of strings - * - * @param str the string to split up - * @param delimiter the delimiter to use in splitting - * @return an array of String objects equivalent to str - */ - public String[] splitDelimitedStr(String str, String delimiter) { - Pattern pttn = Pattern.compile(delimiter); - return pttn.split(str); - } - - /** - * Right aligns a long number with spaces for printing - * - * @param num the number to be aligned - * @param totalLen the total length of the padded string - * @return the padded number - */ - public String padNum(long num, int totalLen) { - String numStr = Long.toString(num); - int len = totalLen - numStr.length(); - String pads = ""; - for (int i = 0; i < len; i++) { - pads += " "; - } - return pads + numStr; - } - - /** - * Right aligns a long number with zeros for printing - * - * @param num the number to be aligned - * @param totalLen the total length of the padded string - * @return the padded number - */ - public String padNumZero(long num, int totalLen) { - String numStr = Long.toString(num); - int len = totalLen - numStr.length(); - String pads = ""; - for (int i = 0; i < len; i++) { - pads += "0"; - } - return pads + numStr; - } - - /** - * Right aligns an integer number with spaces for printing - * - * @param num the number to be aligned - * @param totalLen the total length of the padded string - * @return the padded number - */ - public String padNum(int num, int totalLen) { - String numStr = Integer.toString(num); - int len = totalLen - numStr.length(); - String pads = ""; - for (int i = 0; i < len; i++) { - pads += " "; - } - return pads + numStr; - } - - /** - * Right aligns an integer number with zeros for printing - * - * @param num the number to be aligned - * @param totalLen the total length of the padded string - * @return the padded number - */ - public String padNumZero(int num, int totalLen) { - String numStr = Integer.toString(num); - int len = totalLen - numStr.length(); - String pads = ""; - for (int i = 0; i < len; i++) { - pads += "0"; - } - return pads + numStr; - } - - /** - * Right aligns a double number with spaces for printing - * - * @param num the number to be aligned - * @param wholeLen the total length of the padded string - * @return the padded number - */ - public String padNum(double num, int wholeLen, int decimalPlaces) { - String numStr = Double.toString(num); - int dpLoc = numStr.indexOf("."); - - int len = wholeLen - dpLoc; - String pads = ""; - for (int i = 0; i < len; i++) { - pads += " "; - } - - numStr = pads + numStr; - - dpLoc = numStr.indexOf("."); - - if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) { - return numStr; - } - return numStr.substring(0, dpLoc + 1 + decimalPlaces); - } - - /** - * Right aligns a double number with zeros for printing - * - * @param num the number to be aligned - * @param wholeLen the total length of the padded string - * @return the padded number - */ - public String padNumZero(double num, int wholeLen, int decimalPlaces) { - String numStr = Double.toString(num); - int dpLoc = numStr.indexOf("."); - - int len = wholeLen - dpLoc; - String pads = ""; - for (int i = 0; i < len; i++) { - pads += "0"; - } - - numStr = pads + numStr; - - dpLoc = numStr.indexOf("."); - - if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) { - return numStr; - } - return numStr.substring(0, dpLoc + 1 + decimalPlaces); - } - - /** - * Right aligns a float number with spaces for printing - * - * @param num the number to be aligned - * @param wholeLen the total length of the padded string - * @return the padded number - */ - public String padNum(float num, int wholeLen, int decimalPlaces) { - String numStr = Float.toString(num); - int dpLoc = numStr.indexOf("."); - - int len = wholeLen - dpLoc; - String pads = ""; - for (int i = 0; i < len; i++) { - pads += " "; - } - - numStr = pads + numStr; - - dpLoc = numStr.indexOf("."); - - if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) { - return numStr; - } - return numStr.substring(0, dpLoc + 1 + decimalPlaces); - } - - /** - * Right aligns a float number with zeros for printing - * - * @param num the number to be aligned - * @param wholeLen the total length of the padded string - * @return the padded number - */ - public String padNumZero(float num, int wholeLen, int decimalPlaces) { - String numStr = Float.toString(num); - int dpLoc = numStr.indexOf("."); - - int len = wholeLen - dpLoc; - String pads = ""; - - if (numStr.charAt(0) == '-') { - len += 1; - for (int i = 0; i < len; i++) { - pads += "0"; - } - pads = "-" + pads; - numStr = pads + numStr.substring(1); - } else { - for (int i = 0; i < len; i++) { - pads += "0"; - } - numStr = pads + numStr; - } - - dpLoc = numStr.indexOf("."); - int length = numStr.substring(dpLoc).length(); - while (length < decimalPlaces) { - numStr += "0"; - } - return numStr; - - } - - /** - * Right aligns a {@link String} with zeros for printing - * - * @param input the String to be aligned - * @param wholeLen the total length of the padded string - * @return the padded number - */ - public String padStringRight(String input, int wholeLen) { - for (int i = input.length(); i < wholeLen; i++) { - input += " "; - } - return input; - } - - /** - * @param arr a boolean array to be represented as a string - * @return the array as a string - */ - public String boolArrToStr(boolean[] arr) { - String output = ""; - for (int i = 0; i < arr.length; i++) { - if (arr[i]) { - output += "1"; - } else { - output += "0"; - } - } - return output; - } - - /** - * Formats a double nicely for printing: THIS DOES NOT ROUND!!!! - * @param num the double to be turned into a pretty string - * @return the pretty string - */ - public String prettyNum(double num) { - String numStr = (new Double(num)).toString(); - - while (numStr.length() < 4) { - numStr += "0"; - } - - numStr = numStr.substring(0, numStr.indexOf(".") + 3); - return numStr; - } -}