Advertisement
Giorgos_Xou

MathUtils.java

Oct 30th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. /**
  2.  * MathUtils.java
  3.  *
  4.  * Copyright (c) 2013-2016, F(X)yz
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions are met:
  9.  *     * Redistributions of source code must retain the above copyright
  10.  * notice, this list of conditions and the following disclaimer.
  11.  *     * Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *     * Neither the name of F(X)yz, any associated website, nor the
  15.  * names of its contributors may be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21.  * DISCLAIMED. IN NO EVENT SHALL F(X)yz BE LIABLE FOR ANY
  22.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. package app;
  31. /**
  32.  *
  33.  * @author Dub
  34.  */
  35. public class MathUtils {
  36.     public static final double DBL_EPSILON = 2.220446049250313E-16d;
  37.     public static final double ZERO_TOLERANCE = 0.0001d;
  38.     public static final double ONE_THIRD = 1d / 3d;
  39.     public static final double TAU =  (Math.PI * 2.0);
  40.     public static final double HALF_TAU =  Math.PI;
  41.     public static final double QUARTER_TAU =   (Math.PI / 2.0);
  42.     public static final double INVERSE_TAU =   (1.0 / Math.PI);
  43.     public static final double PI =   Math.PI;
  44.     public static final double TWO_PI = 2.0d * PI;
  45.     public static final double HALF_PI = 0.5d * PI;
  46.     public static final double QUARTER_PI = 0.25d * PI;
  47.     public static final double INV_PI = 1.0d / PI;
  48.     public static final double INV_TWO_PI = 1.0d / TWO_PI;
  49.     public static final double DEG_TO_RAD = PI / 180.0d;
  50.     public static final double RAD_TO_DEG = 180.0d / PI;
  51.    
  52.     public static boolean isWithinEpsilon(double a, double b, double epsilon) {
  53.         return Math.abs(a - b) <= epsilon;
  54.     }
  55.  
  56.     public static boolean isWithinEpsilon(double a, double b) {
  57.         return isWithinEpsilon(a, b, ZERO_TOLERANCE);
  58.     }
  59.  
  60.     public static boolean isPowerOfTwo(int number) {
  61.         return (number > 0) && (number & (number - 1)) == 0;
  62.     }
  63.  
  64.     public static int nearestPowerOfTwo(int number) {
  65.         return (int) Math.pow(2, Math.ceil(Math.log(number) / Math.log(2)));
  66.     }
  67.    
  68.        
  69.     public static javafx.geometry.Point3D computeNormal(javafx.geometry.Point3D v1, javafx.geometry.Point3D v2, javafx.geometry.Point3D v3) {
  70.         javafx.geometry.Point3D a1 = v1.subtract(v2);
  71.         javafx.geometry.Point3D a2 = v3.subtract(v2);
  72.         return a2.crossProduct(a1).normalize();
  73.     }
  74.    
  75.     public static javafx.geometry.Point3D sphericalToCartesian(javafx.geometry.Point3D sphereCoords) {
  76.         double a, x, y, z;
  77.         y = sphereCoords.getX() * Math.sin(sphereCoords.getZ());
  78.         a = sphereCoords.getX() * Math.cos(sphereCoords.getZ());
  79.         x = a * Math.cos(sphereCoords.getY());
  80.         z = a * Math.sin(sphereCoords.getY());
  81.         return new javafx.geometry.Point3D(x, y, z);
  82.     }
  83.    
  84.     public static javafx.geometry.Point3D cartesianToSpherical(javafx.geometry.Point3D cartCoords) {
  85.         double x = cartCoords.getX();
  86.         double storex, storey, storez;
  87.         if (x == 0) {
  88.             x = DBL_EPSILON;
  89.         }
  90.         storex = Math.sqrt((x * x)
  91.                 + (cartCoords.getY() * cartCoords.getY())
  92.                 + (cartCoords.getZ() * cartCoords.getZ()));
  93.         storey = Math.atan(cartCoords.getZ() / x);
  94.         if (x < 0) {
  95.             storey += PI;
  96.         }
  97.         storez = Math.asin(cartCoords.getY() / storex);
  98.         return new javafx.geometry.Point3D(storex, storey, storez);
  99.     }
  100.     public static float clamp(float input, float min, float max) {
  101.         return (input < min) ? min : (input > max) ? max : input;
  102.     }
  103.     public static double clamp(double input, double min, double max) {
  104.         return (input < min) ? min : (input > max) ? max : input;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement