Created
August 26, 2016 18:27
-
-
Save NetzwergX/90da2ef4630ddeab2718ce135e27e1b1 to your computer and use it in GitHub Desktop.
Simple utility for jme3 to convert cartesian to spherical coordinates and vice-versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016 Sebastian Teumert (<http://teumert.net>) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | |
* OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package net.teumert.jme3.math; | |
import com.jme3.math.Vector3f; | |
/** | |
* <p> | |
* Utility class to convert cartesian coordinates to spherical (polar) | |
* coordinates and vice-versa. | |
* </p> | |
* | |
* <p> | |
* Can also be used to derive geographical coordinates, if needed, by using | |
* the conventions/conversions: | |
* </p> | |
* | |
* <pre> | |
* Mathematics Geography/Cartography | |
* r, radial distance R + h, radius + altitude | |
* φ,p polar angle 90 - φ, colatitude (latitude = 90 - colatitude) | |
* θ,a azimuthal angle λ, longitude | |
* radians degrees = radians * 360 / 2π | |
* </pre> | |
* | |
* <p> | |
* Formula are as described at | |
* <a href="http://mathworld.wolfram.com/SphericalCoordinates.html"> | |
* Weisstein, Eric W. "Spherical Coordinates." | |
* From MathWorld--A Wolfram Web Resource. | |
* </a> | |
* </p> | |
* | |
* <p> | |
* This class assumes the polar axis is the z-axis, and the equatorial plane is | |
* the XY plane. | |
* </p> | |
* @author Sebastian Teumert | |
* | |
*/ | |
public class CoordinateUtil { | |
/** | |
* Converts cartesian to (spherical) polar coordinates. See class | |
* description for conventions. | |
* | |
* @param x cartesian x-ordinate | |
* @param y cartesian y-ordinate | |
* @param z cartesian z-ordinate | |
* @return {@link Vector3f} with the spherical coordinates (r, a , p) | |
*/ | |
public static Vector3f cartesianToSpherical (float x, float y, float z) { | |
float r = (float) Math.sqrt(x * x + y * y + z * z); | |
float a = (float) Math.atan2(y, x); | |
float p = (float) Math.acos(z / r); | |
return new Vector3f(r, a, p); | |
} | |
/** | |
* Converts cartesian to (spherical) polar coordinates. See class | |
* description for conventions. | |
* | |
* @param v {@link Vector3f} in the cartesian (x, y ,z) form | |
* @return {@link Vector3f} in spherical (r, a , p) form | |
*/ | |
public static Vector3f cartesianToSpherical (Vector3f v) { | |
return cartesianToSpherical(v.x, v.y, v.z); | |
} | |
/** | |
* Converts polar (spherical) co-ordinates in the form (r, a, p) to | |
* cartesian ones. | |
* | |
* @param r radial distance | |
* @param a azimuth angle | |
* @param p polar angle | |
* @return {@link Vector3f} in cartesian (x, y, z) form | |
*/ | |
public static Vector3f sphericalToCartesian(float r, float a, float p) { | |
float x = (float) (r * Math.cos(a) * Math.sin(p)); | |
float y = (float) (r * Math.sin(a) * Math.sin(p)); | |
float z = (float) (r * Math.cos(p)); | |
return new Vector3f(x, y, z); | |
} | |
/** | |
* Converts polar (spherical) co-ordinates in the form (r, a, p) to | |
* cartesian ones. | |
* | |
* @param v {@link Vector3f} in spherical (r, a , p) form | |
* @return {@link Vector3f} in cartesian (x, y, z) form | |
*/ | |
public static Vector3f sphericalToCartesian(Vector3f v) { | |
return sphericalToCartesian(v.x, v.y, v.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment