Module vecmath

Class Math2

java.lang.Object
de.grogra.vecmath.Math2

public final class Math2 extends Object
This class contains a set of mathematical functions and constants, including random and noise generators, linear algebra, and table-based trigonometric functions.
Author:
Ole Kniemeyer
  • Field Details

    • M_PI

      public static float M_PI
      The constant π as float.
    • M_PI_2

      public static float M_PI_2
      The constant π / 2 as float.
    • M_2PI

      public static float M_2PI
      The constant 2 π as float.
    • M_1_PI

      public static float M_1_PI
      The constant 1 / π as float.
    • M_1_2PI

      public static float M_1_2PI
      The constant 1 / (2 π) as float.
    • EPSILON

      public static final float EPSILON
      The very small number epsilon. 1E-09
      See Also:
    • EPS

      public static final float EPS
      The very small number epsilon. 1E-05
      See Also:
    • nbSkySegments

      public static int nbSkySegments
      Numbers of skysegments for the turtlesky
    • turtsky

      public static final Vector3d[] turtsky
      Define the turtlesky
  • Method Details

    • random

      public static char random(char index)
      This method represents a random permutation of char values. I.e., it is a bijective map on chars, the mapping being defined by a pseudo-random number generator. The mapping remains the same between different invocations of the Java Virtual Machine.
      Parameters:
      index - an index into a permutation table
      Returns:
      the permuted value
    • noise

      public static float noise(float x, float y, float z)
      This method represents a Perlin-style 3D noise function. It is implemented based on POV-Ray's Noise function.
      Parameters:
      x - x-coordinate
      y - x-coordinate
      z - x-coordinate
      Returns:
      noise value at (x, y, z)
    • dNoise

      public static void dNoise(Tuple3f out, float x, float y, float z)
      This method represents the derivative of a Perlin-style 3D noise function. It is implemented based on POV-Ray's DNoise function.
      Parameters:
      out - the computed derivate at (x, y, z) is placed in here
      x - x-coordinate
      y - x-coordinate
      z - x-coordinate
    • dTurbulence

      public static void dTurbulence(Tuple3f out, float x, float y, float z, int octaves, float lambda, float omega)
    • turbulence

      public static float turbulence(float x, float y, float z, int octaves, float lambda, float omega)
    • csin

      public static float csin(char c)
      Returns the sine of the argument. The range of char values is mapped to [0, 2 π), i.e., the angle in radians is 2 π c / 0x10000. This method uses a pre-computed table.
      Parameters:
      c - an angle
      Returns:
      the sine of the angle
    • ccos

      public static float ccos(char c)
      Returns the cosine of the argument. The range of char values is mapped to [0, 2 π), i.e., the angle in radians is 2 π c / 0x10000. This method uses a pre-computed table.
      Parameters:
      c - an angle
      Returns:
      the cosine of the angle
    • sin

      public static float sin(float x)
      Returns the sine of the argument. This method uses a pre-computed table.
      Parameters:
      x - an angle, in radians
      Returns:
      the sine of the angle
    • sin01

      public static float sin01(float x)
      Returns the sine of the argument. The range of [0, 1] for x is mapped to [0, 2 π], i.e., the angle in radians is 2 π x. This method uses a pre-computed table.
      Parameters:
      x - an angle
      Returns:
      the sine of the angle
    • nextRandom

      public static long nextRandom(long seed)
      Returns the next 48-bit pseudo-random number based on the previous pseudo-random number seed. The sequence of numbers is the same as the sequence computed by java.util.Random:
       nextRandom = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
       
      However, this method should be slightly faster because it does not have to deal with multi-threading issues.

      Pseudo-random ints of n bits should be obtained from the returned value by the formula (int) (nextRandom >>> (48 - n)).

      Parameters:
      seed - previous number of sequence
      Returns:
      next number of sequence
    • floor

      public static int floor(double x)
    • gamma

      public static float gamma(float x)
    • quadricIntersection

      public static boolean quadricIntersection(double a00, double a01, double a02, double a11, double a12, double a22, double b0, double b1, double b2, double c, Point3d p, Vector3d d, double[] lambda)
    • distance

      public static double distance(Tuple3d p, Vector3d v, Tuple3d q)
    • closestConnection

      public static double closestConnection(Tuple3d p, Vector3d v, Tuple3d q)
    • shortestConnection

      public static double shortestConnection(Tuple3d p1, Vector3d v1, Tuple3d p2, Vector3d v2, double[] lambda)
      Computes the shortest straight connection between two lines. This method computes two scalars lambda[0] and lambda[1] such that the line from p1 + lambda[0] v1 to p2 + lambda[1] v2 is the shortest connection line between these lines.
      Parameters:
      p1 - a point on the first line
      v1 - the direction of the first line
      p2 - a point on the second line
      v2 - the direction of the second line
      lambda - the computed scalars are written into this array
      Returns:
      the length of the shortest connection
    • shortestConnection

      public static double shortestConnection(Tuple3d p1, Vector3d v1, Tuple3f p2, Vector3f v2, double[] lambda)
      Computes the shortest straight connection between two lines. This method computes two scalars lambda[0] and lambda[1] such that the line from p1 + lambda[0] v1 to p2 + lambda[1] v2 is the shortest connection line between these lines.
      Parameters:
      p1 - a point on the first line
      v1 - the direction of the first line
      p2 - a point on the second line
      v2 - the direction of the second line
      lambda - the computed scalars are written into this array
      Returns:
      the length of the shortest connection
    • shortestConnection

      public static float shortestConnection(Tuple3f p1, Vector3f v1, Tuple3f p2, Vector3f v2, float[] lambda)
      Computes the shortest straight connection between two lines. This method computes two scalars lambda[0] and lambda[1] such that the line from p1 + lambda[0] v1 to p2 + lambda[1] v2 is the shortest connection line between these lines.
      Parameters:
      p1 - a point on the first line
      v1 - the direction of the first line
      p2 - a point on the second line
      v2 - the direction of the second line
      lambda - the computed scalars are written into this array
      Returns:
      the length of the shortest connection
    • mulAffine

      public static void mulAffine(Matrix3d out, Matrix3d l, Matrix3d r)
    • invertAffine

      public static void invertAffine(Matrix3f in, Matrix3f out)
    • invertAffine

      public static void invertAffine(Matrix3d in, Matrix3d out)
    • invTransformVector

      public static void invTransformVector(Matrix3f t, Tuple2f v)
    • invTransformPoint

      public static void invTransformPoint(Matrix3f t, Tuple2f p)
    • invTransformVector

      public static void invTransformVector(Matrix3d t, Tuple2d v)
    • invTransformPoint

      public static void invTransformPoint(Matrix3d t, Tuple2d p)
    • transformVector

      public static void transformVector(Matrix3d t, Tuple2d v)
    • transformPoint

      public static void transformPoint(Matrix3d t, Tuple2d p)
    • setAffineTransform

      public static void setAffineTransform(AffineTransform out, Matrix3d in)
    • setMatrix3d

      public static void setMatrix3d(Matrix3d out, AffineTransform in)
    • invertAffine

      public static void invertAffine(Matrix4f in, Matrix4f out)
    • invertAffine

      public static void invertAffine(Matrix4d in, Matrix4d out)
    • invertAffine

      public static void invertAffine(Matrix34d in, Matrix4d out)
    • makeAffine

      public static void makeAffine(Matrix3d m)
    • makeAffine

      public static void makeAffine(Matrix4d m)
    • makeAffine

      public static void makeAffine(Matrix3f m)
    • makeAffine

      public static void makeAffine(Matrix4f m)
    • mulAffine

      public static void mulAffine(Matrix4d out, Matrix4d l, Matrix4d r)
    • setAffine

      public static void setAffine(Matrix4d out, Matrix4d in)
    • invMul

      public static void invMul(Matrix3d t, Tuple3d v)
    • invTransformVector

      public static void invTransformVector(Matrix4d t, Tuple3d v)
    • invTransformVector

      public static void invTransformVector(Matrix34d t, Tuple3d v)
    • invTransformPoint

      public static void invTransformPoint(Matrix4d t, Tuple3d p)
    • invTransformPoint

      public static void invTransformPoint(Matrix34d t, Tuple3d p)
    • invTransformPointAndVector

      public static void invTransformPointAndVector(Matrix4d t, Tuple3d p, Tuple3d v)
    • transformVector

      public static void transformVector(Matrix4d t, Tuple3d v)
    • transformPoint

      public static void transformPoint(Matrix4d t, Tuple3d p)
    • transformPoint

      public static void transformPoint(Matrix4d t, Tuple3d p, Tuple3d out)
    • transformTranspose

      public static void transformTranspose(Matrix3d t, Tuple3d v)
    • transformVector

      public static void transformVector(Matrix4f t, Tuple3f v)
    • transformPoint

      public static void transformPoint(Matrix4f t, Tuple3f p)
    • lmul

      public static void lmul(Matrix3d rot, Matrix34d m)
    • getOrthogonal

      public static void getOrthogonal(Tuple3d in, Tuple3d out)
      Computes an orthogonal vector to in. The computed vector is written to out, which may be the same reference as in.
      Parameters:
      in - an input vector
      out - the computed orthogonal vector is placed in here
    • getOrthogonal

      public static void getOrthogonal(Tuple3f in, Tuple3f out)
      Computes an orthogonal vector to in. The computed vector is written to out, which may be the same reference as in.
      Parameters:
      in - an input vector
      out - the computed orthogonal vector is placed in here
    • getOrthogonalBasis

      public static void getOrthogonalBasis(Tuple3f in, Matrix3f out, boolean orthonormal)
      Computes an orthogonal basis. The first two columns of the computed basis out are unit vectors, the third column equals in if orthonormal is false, otherwise it equals the unit vector in the direction of in. Thus, out represents a coordinate transformation which transforms the local z-axis into the direction of in.
      Parameters:
      in - the direction of the local z-axis
      out - the computed matrix
      orthonormal - compute an orthonormal matrix?
    • getOrthogonalBasis

      public static void getOrthogonalBasis(Tuple3d in, Matrix3d out, boolean orthonormal)
      Computes an orthogonal basis. The first two columns of the computed basis out are unit vectors, the third column equals in if orthonormal is false, otherwise it equals the unit vector in the direction of in. Thus, out represents a coordinate transformation which transforms the local z-axis into the direction of in.
      Parameters:
      in - the direction of the local z-axis
      out - the computed matrix
      orthonormal - compute an orthonormal matrix?
    • decomposeQR

      public static void decomposeQR(Matrix3d r, Matrix3d q)
      Computes a QR decomposition of r. The non-singular matrix a to be composed is the parameter r. This method computes r and q such that a = q * r, q is an orthogonal matrix, and r an upper triangular matrix. The signs of r.m00 and r.m11 are positive. The sign of r.m22 is the sign of det(a). As a consequence, det(q) == 1, so q is a pure rotation.
      Parameters:
      r - the input for the matrix to compose and the output for r
      q - the output for q
    • isInsideConeT

      public static boolean isInsideConeT(double px, double py, double pz, double ox, double oy, double oz, double ax, double ay, double az, double tipDistance, double capDistance, double tan)
    • isInsideConeT

      public static boolean isInsideConeT(double px, double py, double pz, double ax, double ay, double az, double tipDistance, double capDistance, double tan)
      Determines if a point p lies inside a (part of a) cone. The cone's tip is located at the origin, its axis is given by a and points to the center of the cone's cap which is located at axis * capDistance. The small cone from the tip to axis * tipDistance is removed, i.e., this tests actually checks againts a frustum. The half-angle of the cone is specified by its tangent.
      Parameters:
      px - x coordinate of point p
      py - y coordinate of point p
      pz - z coordinate of point p
      ax - x coordinate of cone axis a
      ay - y coordinate of cone axis a
      az - z coordinate of cone axis a
      tipDistance - distance to the tip (in units of axis)
      capDistance - distance of the cap (in units of axis)
      tan - tangent of cone's half angle
      Returns:
      true iff p lies within or on the cone
    • intersectLineWithFrustum

      public static double intersectLineWithFrustum(Point3d point, Vector3d direction, Point3d origin, Vector3d axis, double top, double base, double tan)
    • intersectLineWithFrustum

      public static double intersectLineWithFrustum(double px, double py, double pz, double dx, double dy, double dz, double ax, double ay, double az, double top, double base, double tan)
      Determines the fraction of a line (p,d) which intersects a frustum.

      The frustum is specified by the cone from which it is obtained by cutting away the top: The cone's tip is located at the origin, its axis is given by a and points to the center of the cone's cap which is located at base * a. The half-angle of the cone is specified by its tangent. The cutting plane for obtaining the frustum is located at the position top * a.

      The line is specified by its origin p and its vector d from the origin to the end.

      Parameters:
      px - x coordinate of line origin p
      py - y coordinate of line origin p
      pz - z coordinate of line origin p
      dx - x coordinate of line vector d
      dy - y coordinate of line vector d
      dz - z coordinate of line vector d
      ax - x coordinate of cone axis a
      ay - y coordinate of cone axis a
      az - z coordinate of cone axis a
      top - relative position of frustum's top cap along axis
      base - relative position of frustum's base cap along axis
      tan - tangent of cone's half angle
      Returns:
      fraction of line which intersects the frustum
    • pow

      public static long pow(int a, int b)
    • factorial

      public static int factorial(int n)
    • binomial

      public static int binomial(int n, int k)
    • min

      public static void min(Tuple2d min, Tuple2d t)
    • max

      public static void max(Tuple2d max, Tuple2d t)
    • min

      public static void min(Tuple2f min, Tuple2f t)
    • max

      public static void max(Tuple2f max, Tuple2f t)
    • min

      public static void min(Tuple3d min, Tuple3d t)
    • max

      public static void max(Tuple3d max, Tuple3d t)
    • min

      public static void min(Tuple3f min, Tuple3f t)
    • max

      public static void max(Tuple3f max, Tuple3f t)
    • dot

      public static double dot(Tuple3d p, Tuple3d q, Tuple3d v)
      Computes the scalar product (p - q) v
      Parameters:
      p - a point
      q - point which is subtracted from p
      v - a vector
      Returns:
      scalar product (p - q) v
    • estimateScaleSquared

      public static double estimateScaleSquared(Matrix4d transformation)
    • fresnel

      public static float fresnel(Vector3f normal, Vector3f in, float iorRatio, Vector3f reflectedOut, Vector3f transmittedOut)
      Computes reflected and transmitted directions according to Fresnel's formulas.
      Parameters:
      normal - the normal unit vector of the surface
      in - the negated direction unit vector of the incoming ray
      iorRatio - the index of refraction of the surface side where the normal vector points into, divided by the index of refraction of the opposite side
      reflectedOut - the computed unit vector of the reflection direction
      transmittedOut - the computed unit vector of the transmission direction
      Returns:
      the reflection coefficient
    • dot

      public static float dot(Tuple3f a, Tuple3f b)
    • dot

      public static float dot(Tuple3f a, Tuple3d b)
    • dot

      public static double dot(Tuple3d a, Tuple3d b)
    • mul

      public static void mul(Tuple3f out, Tuple3f a, Tuple3f b)
    • mul

      public static void mul(Tuple3d out, Tuple3d a, Tuple3d b)
    • lessThan

      public static boolean lessThan(Tuple3d a, Tuple3d b)
    • lessThanOrEqual

      public static boolean lessThanOrEqual(Tuple3d a, Tuple3d b)
    • roundUpNextPowerOfTwo

      public static int roundUpNextPowerOfTwo(int v)
      Calculate the next power of two greater or equal than v. (see "Bit Twiddling Hacks")
      Parameters:
      v -
      Returns:
    • toFloatArray

      public static float[] toFloatArray(Tuple3d p)
    • toFloatArray

      public static float[] toFloatArray(Tuple4d p)
    • toFloatArray

      public static float[] toFloatArray(Tuple4f p)
    • normalize

      public static void normalize(Vector3d aVector)
      Normalizes this vector in place. If the length is 0, then the complett vector will be (0, 0, 0).
      Parameters:
      aVector - The normalized vector
    • normalize

      public static void normalize(Vector3f aVector)
      Normalizes this vector in place. If the length is 0, then the complett vector will be (0, 0, 0).
      Parameters:
      aVector - The normalized vector
    • abstpp

      public static float abstpp(Vector3d av, Vector3d ev)
      Computated the distance between two vectors
      Parameters:
      av - first vector
      ev - second vector
      Returns:
      distance between the two vectors
    • isNullVector

      public static boolean isNullVector(Vector3d a)
      Test of all parts of the vector are null
      Parameters:
      a - the vector to test
      Returns:
      true, if all parts of the vector are null
    • abstps

      public static float abstps(Vector3d pp, Vector3d sa, Vector3d se)
      Calculates the distance between the point pp and the piece of straight line delimited by sa and se. If sa == se, the distance to this point is calculated.
      Parameters:
      pp - Point
      sa - Begin of the straight line
      se - End of the straight line
      Returns:
      Distance between the point pp and the piece of straight line
    • cutCone

      public static void cutCone(Vector3d ss, float alpha, Vector3d pp, Vector3d qq, CutConeParameter ccp)
    • cutRay2

      public static void cutRay2(float a1, float a2, float r1, float r2, float u1, float u2, float v1, float v2, CutRay2Parameter crp)
    • skySegment

      public static int skySegment(Vector3d rv)
    • absthgs

      public static float absthgs(Vector3d ss, Vector3d rr, Vector3d sa, Vector3d se)
    • determinate3

      public static float determinate3(Vector3d a1, Vector3d a2, Vector3d a3)
    • parallelVector

      public static boolean parallelVector(Vector3d a1, Vector3d a2)
    • abstphg

      public static float abstphg(Vector3d pp, Vector3d ss, Vector3d rr)
    • onbco

      public static void onbco(Vector3d result, Vector3d a, Vector3d ba1, Vector3d ba2, Vector3d ba3)
    • getBeginAndEndOfShoot

      public static void getBeginAndEndOfShoot(Matrix4d m, double length, Vector3d beginOfShoot, Vector3d endOfShoot)
    • getBeginOfShoot

      public static void getBeginOfShoot(Matrix4d m, Vector3d beginOfShoot)
    • getEndOfShoot

      public static void getEndOfShoot(Matrix4d m, double length, Vector3d endOfShoot)
    • clamp

      public static int clamp(int value, int min, int max)
    • clamp

      public static long clamp(long value, long min, long max)
    • clamp

      public static float clamp(float value, float min, float max)
    • clamp

      public static double clamp(double value, double min, double max)